UWP C++/CX: Current system time & Stopwatch - c++

So, I am creating a C++ UWP application that when opened, displays current date and time. In addition, I need to also find a way so that I can display the day of the week as well. For the day of the week, I am using this code:
#include <ctime>
#include <time.h>
#include <chrono>
//under Mainpage::Mainpage()
time_t timeObj = time(nullptr);
tm aTime;
localtime_s(&aTime, &timeObj);
String ^ weekofdayout;
switch (aTime.tm_wday)
{
case 1:
weekofdayout = "Monday";
break;
case 2:
weekofdayout = "Tuesday";
break;
case 3:
weekofdayout = "Wednesday";
break;
case 4:
weekofdayout = "Thursday";
break;
case 5:
weekofdayout = "Friday";
break;
default:
weekofdayout = "(NULL)";
break;
}
timetext->Text += weekofdayout;
However, whenever I run it, the textbox returns: (NULL) the default case. I don't know whats causing it.
I have taken the system time before successfully in windows forms c++ with this code:
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
DateTime datetime = DateTime::Now;
this->label3->Text = datetime.ToString();}
I was unable to port this over to UWP C++ because I think it uses the system namespace. Any ideas on how I would be able to recreate the same effect in c++?
For the stopwatch, I haven't found any real documentation online regarding UWP C++ stopwatch but I did find this: https://iot-developer.net/windows-iot/uwp-programming-in-c/timer-and-timing/stopwatch It isn't for C++ UWP but it is for C# UWP. I tried to make my own timer with a global function but it just freezes the program until the code is done running:
int seconds = 0;
for (int i = 0; ; i++)
{
seconds++;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (stopwatch == false)
{
break;
}
}
return seconds;
I'm pretty stumped, usually, I would find the answer online but due to the low documentation of UWP C++, I have no idea where to find the answer.

Related

MBed program gets stuck

I have written some code with the help of Mbed framework, which is either supposed to take user input and then display sensor values or display the value after 15mins. When I try to execute this code, it is getting stuck at line 21 (display.printf("Inside loop\n");).
I am not able to understand why is it so and what is the fix for this problem so that the switch block gets executed. How to I solve this? FYI, although not important, the microcontroller I am using is STM32 bluepill (STM32F103C8T6).
#include "mbed.h"
#include "Sensor_input.h"
#include "Ticker.h"
#include "Dht11.h"
//#include "USBSerial.h"
Serial display(PA_2, PA_3, 9600);
char* a;
Dht11 DhtSensor(PA_4);
Ticker t;
Sensor_input Soil(PB_7, PB_6, 8);
float *SensorData;
void getSensorData();
int main ( void ){
uint8_t choice = 0;
display.printf("Enter 1 or 2:\n1.Greenhouse stats\n2.Return Control to System");
choice = display.putc(display.getc());
while(1){
display.printf("Inside loop\n");
wait_ms(15000);
switch(choice)
{
case 1:
display.printf("Inside case 1");
a = Soil.readTemp();
display.printf("Temperature: %f\n",DhtSensor.getCelsius());
display.printf("Humidity: %f\n",DhtSensor.getHumidity());
display.printf("Soil water content: %c\n ",*a);
break;
case 2:
/*<GreenHouse object>*/
/*Might have to proceed with timer*/
display.printf("Inside case 2");
t.attach(&getSensorData,4500);
display.printf("Temperature: %f\n",a[0]);
display.printf("Humidity: %f\n",a[1]);
display.printf("Soil water content: %c\n ",a[2]);
break;
default:
break;
}
}
}
void getSensorData(){
static float a[3];
a[0]=DhtSensor.getCelsius();
a[1]=DhtSensor.getHumidity();
a[2]=(int)Soil.readTemp();
}
Your switch statement is probably being executed, but always in the 'default' case. You can test this out by putting a print statement in the default.
When you request a char from the display, it will return the input as an ASCII-character. This means, if you enter '1' on the display, it will give you (as the ASCII table says) 0x31 (decimal 49) and not the value of 1. So you have to change your case to "case '1':" or "case 0x31:" and the equivalent for the second case.

How to write piano program in C/C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to write a C program for piano in which the node of piano is controlled by mouse movement. But as in linux #include<dos.h> and #include<conio.h> is not exist, so i am getting error . Is there any alternative library present in linux for #include<conio.h> and #include<dos.h> ?
I have tried the code following.
#include <dos.h>
#include <graphics.h>
union REGS in, out;
void detect_mouse ()
{
in.x.ax = 0;
int86 (0X33,&in,&out); //invoke interrupt
if (out.x.ax == 0)
printf ("\nMouse Failed To Initialize");
else
printf ("\nMouse was Succesfully Initialized");
}
void showmouse_graphics ()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
in.x.ax = 1;
int86 (0X33,&in,&out);
getch ();
closegraph ();
}
void detect ()
{
int button;
while (!kbhit () )
{
in.x.ax = 3;
int86 (0X33,&in,&out);
button=out.x.bx&7
switch(button)
{
case 1:
print(“left button pressed\n”);
break;
case 2:
print(“right button pressed\n”);
break;
case 4:
print(“middle button pressed\n”);
break;
case 3:
print(“left and right button pressed\n”);
break;
case 5:
print(“left and middle button pressed\n”);
break;
case 6:
print(“right and middle button pressed\n”);
break;
case 7:
print(“all the three buttons pressed\n”);
break;
default:
print(“No button pressed\n”);
}
delay (200); // Otherwise due to quick computer response 100s of words will get print
}
}
void hide_mouse ()
{
in.x.ax = 2;
int86 (0X33,&in,&out);
}
int main ()
{
detect_mouse ();
showmouse_graphics ();
detect ();
hide_mouse ();
return 0;
}
For #include<conio.h> you can use #include <curses.h> which will give you almost all the functionalities. Getchar
For #include<dos.h> it's not usable in any other operating system than DOS and there isn't really something similar with all the functionalities in linux. But you can use usleep(microseconds) in linux when you include #include <unistd.h>.
Or you can use sleep_for in c++:
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
sleep_for(nanoseconds(20));
For Generating Sound, it seems that this post tries to do something similar/could help with your problem:
Generate Sound Frequency using GCC

Why this code doesn't work when "cout"s are commented?

I'm writing a server for an online game based on IOCP, and the core codes handling game message is something like below:
CMessage ret;
int now_roomnum = recv_msg->para1;
int now_playernum = recv_msg->para2;
/*if(true)
{
cout<<"Received Game Message: "<<endl;
cout<<"type2 = "<<recv_msg->type2;
cout<<" player_num = "<<now_playernum<<" msg= "<<recv_msg->msg<<endl;
cout<<endl;
}*/
if(recv_msg->type2 == MSG_GAME_OPERATION)
{
ret.type1 = MSG_GAME;
ret.type2 = MSG_GAME_OPERATION;
while(game_host[now_roomnum].Ready(now_playernum) == true)
{
;
}
//cout<<"Entered from "<<now_playernum<<endl;
game_host[now_roomnum].SetMessage(now_playernum, recv_msg->msg);
game_host[now_roomnum].SetReady(now_playernum, true);
game_host[now_roomnum].SetUsed(now_playernum, false);
while(true)
{
bool tmp = game_host[now_roomnum].AllReady();
if(tmp == true)
break;
}
//cout<<"AllReady from"<<now_playernum<<endl;
string all_msg = game_host[now_roomnum].GetAllMessage();
game_host[now_roomnum].SetUsed(now_playernum, true);
while(!game_host[now_roomnum].AllUsed())
{
;
}
//cout<<"AllUsed from "<<now_playernum<<endl;
EnterCriticalSection(&cs);
game_host[now_roomnum].ClearReady();
LeaveCriticalSection(&cs);
strcpy_s(ret.msg, all_msg.c_str());
//cout<<"Return msg "<<now_playernum<<": "<<ret.msg<<endl;
}
return ret;
Now, the problem is: on a PC, when all cout are commented like above, the game freezes at once; but when I cancel the comments, the server works well.
What's more, when I run the server on my laptop, everything goes fine, no matter whether I comment the cout or not. The main difference between my laptop and PC is that my laptop's OS is Windows 8.1, while the PC is Windows 7.
I'm totally confused. It will be of great help if someone can tell me what to do. Thank you!
Looks like a multithreading issue.
By the way I see you use a Critical section around ClearReady but not when testing for AllReady. That call should be wrapped as well (or, better, write a LockedAllReady that makes use of the lock).
//cout<<"Return msg "<<now_playernum<<": "<<ret.msg<<endl;
What you mean by ret.msg? if msg is method you must do ret.msg(); , is it a field?
If you have this good then like they say above probably a timing problem, try to do cout without ret.msg and see what will happen, and then you know from where the problem is.

Serial Port Driver-Got stuck in SerialISR () routine

I am writing windows device driver first time for multiport serial pci card.May be this one is stupid question but i got stuck.Need someone's help.Let me first explain what I have done yet.I have bus driver which creates number of child (2/4/8/16) depends on card. Now I have to write function driver for it.The bus driver creates interrupt which I have to use.Now in my driver I got bus interface,interrupt created in bus driver and related information.I am using Serial Port Driver Sample for reference.Now in SerialISR () routine my driver got stuck in while loop.
ServicedAnInterrupt = TRUE;
do {
InterruptIdReg &= (~SERIAL_IIR_FIFOS_ENABLED);
switch (InterruptIdReg) {
case SERIAL_IIR_RLS: {
READ_LINE_STATUS(Extension, Extension->Controller);
break;
}
case SERIAL_IIR_RDA:
case SERIAL_IIR_CTI: {
READ_RECEIVE_BUFFER(Extension, Extension->Controller);
break;
}
case SERIAL_IIR_THR: {
//
// Alread clear from reading the iir.
//
// We want to keep close track of whether
// the holding register is empty.
//
Extension->HoldingEmpty = TRUE;
break;
}
case SERIAL_IIR_MS: {
READ_MODEM_STATUS(Extension, Extension->Controller);
break;
}
default: {
ASSERT(FALSE);
break;
}
}
} while (!((InterruptIdReg =
READ_INTERRUPT_ID_REG(Extension, Extension->Controller))
& SERIAL_IIR_NO_INTERRUPT_PENDING));
Every time it goes into case SERIAL_IIR_MS. This process continue for some time about 1 min & then test pc will crash.What can be the reason behind that & what should I do to overcome this problem?I have gone through msdn library information. Also read issues regarding this on OSR website.

How to check if CD drive is open or closed in linux?

I'm making an application that requires the knowledge of whether a CD drive is open or closed.
eject opens the CD drive, and checks how long it takes to open (a shorter amount of time says it's open, and a longer, well...), but I cannot use this technique, because the application actually opens the drive (and I do not want to re-open the drive if it's closed, neither do I want to close the drive if it is open).
How would I do this on linux? I saw that it is possible to do this under Windows (might be wrong though), but I haven't seen a way of doing this on linux.
If it's not possible using linux API calls, is it possible to implement a low-level function that could do this?
To make the example code work, you should do it this way:
#include <sys/ioctl.h>
#include <linux/cdrom.h>
int result=ioctl(fd, CDROM_DRIVE_STATUS, CDSL_NONE);
switch(result) {
case CDS_NO_INFO: ... break;
case CDS_NO_DISC: ... break;
case CDS_TRAY_OPEN: ... break;
case CDS_DRIVE_NOT_READY: ... break;
case CDS_DISC_OK: ... break;
default: /* error */
}
i.e. the result is returned as ioctl() function result, not into slot argument.
You can get tray state by using the CDROM_DRIVE_STATUS ioctl. All ioctls for CD-drives can be found in /usr/include/linux/cdrom.h
#define CDROM_DRIVE_STATUS 0x5326 /* Get tray position, etc. */
Taken from here
int slot;
ioctl(fd, CDROM_DRIVE_STATUS, slot);
switch(slot) {
case CDS_NO_INFO: ... break;
case CDS_NO_DISC: ... break;
case CDS_TRAY_OPEN: ... break;
case CDS_DRIVE_NOT_READY: ... break;
case CDS_DISC_OK: ... break;
default: /* error */
}