raspberrypi / pico-examples

BSD 3-Clause "New" or "Revised" License
2.83k stars 820 forks source link

No key press detected for pending in [iperf server] example #473

Closed crispsssy closed 7 months ago

crispsssy commented 7 months ago

Hi,

I tried to run pico w iperf server example, in the example there are some pending functions say "press 'd' to disconnect", but when I press keys on keyboard, only PICO_ERROR_TIMEOUT is returned by getchar_timeout_us(0).

Here is an simple code I copied from iperf example:

char key = '!';

void key_pressed_worker_func(async_context_t* context, async_when_pending_worker_t* worker){
        printf("%d 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

The printf() in key_pressed_func() is never been called and the printf() in key_press_worker_func() gives "-1 is pressed" (-1 means timeout from getchar_timeout_us(0)). here is the output from minimum:

Welcome to minicom 2.9

OPTIONS: 
Compiled on Feb  7 2024, 07:41:55.
Port /dev/tty.usbmodem21201, 21:19:35

Press Meta-Z for help on special keys

-1 is pressed
-1 is pressed
-1 is pressed
-1 is pressed
-1 is pressed
-1 is pressed
-1 is pressed

what is wrong?

peterharperuk commented 7 months ago

Duplicate https://github.com/raspberrypi/pico-examples/issues/474