Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4535

SDK • getchar_timeout_ms() can not get the correct character

$
0
0
Hi,

I'm using c++ SDK to learn Raspi pico, and I was trying to use

Code:

getchar_timeout_ms()
to receive keyboard input through minicom by USB connection. But seems it receive the wrong input...

Here is part of my program:

Code:

char key = '!';void key_pressed_worker_func(async_context_t* context, async_when_pending_worker_t* worker){        printf("%c is pressed\n", key);        cyw43_arch_disable_sta_mode();}static async_when_pending_worker_t key_pressed_worker = {        .do_work = key_pressed_worker_func};void key_pressed_func(void* param){        key = getchar_timeout_us(0);        printf("key is pressed");//        if(key == 'd' || key == 'D'){                async_context_set_work_pending((async_context_t*)param, &key_pressed_worker);//        }}int main(){        stdio_init_all();        if(cyw43_arch_init()){                printf("failed to initialise\n");                return 1;        }        async_context_add_when_pending_worker(cyw43_arch_async_context(), &key_pressed_worker);        stdio_set_chars_available_callback(key_pressed_func, cyw43_arch_async_context());//something else after this line
First, I found the printf() in key_press_func() never work, I don't know why.
Second, whatever the key I pressed, the printf() in key_pressed_worker_func() is just "? is pressed". I don't understand why all key press is converted to a single character '?'. If I commend out the line

Code:

key = getchar_timeout_us(0);
then the character becomes the initialized one '!' (this is normal though).

Here is the output in minicom:

Code:

Welcome to minicom 2.9OPTIONS: Compiled on Feb  7 2024, 07:41:55.Port /dev/tty.usbmodem21201, 21:19:35Press Meta-Z for help on special keys? is pressed? is pressed? is pressed? is pressed? is pressed? is pressed? is pressed```
Any help please?

Statistics: Posted by crisps — Fri Feb 23, 2024 12:38 pm — Replies 4 — Views 81



Viewing all articles
Browse latest Browse all 4535

Trending Articles