I apologise: I had misread your code and I thought that you were calling getchar_timeout_us() in more than one place, but you are not.But in the example (the link I pasted at the last reply) it use the same stdio_set_chars_available_callback() in the main loop, and getchar_timeout_us(0) in the key press function. And in my understanding I only need to press'd' only once to let pico w disable the wifi connection. If this is not the case, how can it say "press 'd' to disconnect"? (This sentence is in the main loop of the example, the pico w iperf server example)
However, the miniterm log you posted can't be from running exactly the code you showed, as we never see output from this printf:
Code:
key = getchar_timeout_us(0); printf("key is pressed");
My best guess is that chars available callback (key_pressed_func()) is being called more than once - the first time gets the character, the second time gets the timeout, and key_pressed_worker_func() doesn't get called until after all of that has happened (which is the entire point of these async context workers - that they delay work until later).
The whole mechanism of passing queued characters in a global variable is going to be unreliable (what if you did type two characters quickly in miniterm? The USB serial might package them up into a single USB packet so that they arrive simultaneously on the Pico.
I suspect it might work better if you changed your code as follows:
Code:
void key_pressed_func(void* param){ int temp = getchar_timeout_us(0); if (temp == PICO_ERROR_TIMEOUT) return key = temp; printf("key is pressed");// if(key == 'd' || key == 'D'){ async_context_set_work_pending((async_context_t*)param, &key_pressed_worker);// }}
Statistics: Posted by arg001 — Sat Feb 24, 2024 10:48 am