Report
4 years, 4 months ago.

Wait for a key stroke with a timeout

Hi all. I would like to implement a test menu, accessible via a key stroke, right at the start of my program. It should be done in a way that if no key is pushed the program doesn't wait too much and starts its usual operations. If one particular key is pressed the menu should appear. Can you please advise how to do this? Thanks. Antonio

Comment on this question

2 Answers

4 years, 4 months ago.

I presume you are connected to mbed using the serial from PC in which case you can do a simple loop using a timer and readable. Something along the lines of:

Serial pc(USBTX, USBRX);
Timer t;
char k = '\0';
pc.baud(115200);
t.reset();
t.start();
while((t.read() < 1.0) && (k == '\0')) {
    if(pc.readable())   k = pc.getc();
}

if(k == 'M')   show_menu();

[code not checked]

Thanks a lot, Chris. I've taken your code as it is. I'm progressing fine now.

posted by Antonio Tosi 15 May 2015
4 years, 4 months ago.

hi i do not know if i know you need clearly.my way is: 1. key interrupt to serve key input, if key down , remember it 2. check if remembered any key done