diff --git a/micropython/modules/pico_wireless/pico_wireless.c b/micropython/modules/pico_wireless/pico_wireless.c index c9cbaa66..ef5c6482 100644 --- a/micropython/modules/pico_wireless/pico_wireless.c +++ b/micropython/modules/pico_wireless/pico_wireless.c @@ -77,6 +77,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_send_data_obj, 2, picowireless_se STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_check_data_sent_obj, 1, picowireless_check_data_sent); STATIC MP_DEFINE_CONST_FUN_OBJ_0(picowireless_get_socket_obj, picowireless_get_socket); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_set_led_obj, 3, picowireless_set_led); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(picowireless_is_pressed_obj, picowireless_is_pressed); + /***** Globals Table *****/ STATIC const mp_map_elem_t picowireless_globals_table[] = { @@ -145,7 +148,10 @@ STATIC const mp_map_elem_t picowireless_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_send_data), MP_ROM_PTR(&picowireless_send_data_obj) }, { MP_ROM_QSTR(MP_QSTR_check_data_sent), MP_ROM_PTR(&picowireless_check_data_sent_obj) }, { MP_ROM_QSTR(MP_QSTR_get_socket), MP_ROM_PTR(&picowireless_get_socket_obj) }, - //TODO Add functions for LED, button and SD card detect + + { MP_ROM_QSTR(MP_QSTR_set_led), MP_ROM_PTR(&picowireless_set_led_obj) }, + { MP_ROM_QSTR(MP_QSTR_is_pressed), MP_ROM_PTR(&picowireless_is_pressed_obj) }, + //TODO Add function for SD card detect }; STATIC MP_DEFINE_CONST_DICT(mp_module_picowireless_globals, picowireless_globals_table); diff --git a/micropython/modules/pico_wireless/pico_wireless.cpp b/micropython/modules/pico_wireless/pico_wireless.cpp index 1c3ef978..434ea702 100644 --- a/micropython/modules/pico_wireless/pico_wireless.cpp +++ b/micropython/modules/pico_wireless/pico_wireless.cpp @@ -1014,4 +1014,47 @@ mp_obj_t picowireless_get_socket() { return mp_const_none; } + +mp_obj_t picowireless_set_led(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + if(wireless != nullptr) { + enum { ARG_r, ARG_g, ARG_b }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_r, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_g, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_b, MP_ARG_REQUIRED | MP_ARG_INT }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + int r = args[ARG_r].u_int; + int g = args[ARG_g].u_int; + int b = args[ARG_b].u_int; + + if(r < 0 || r > 255) + mp_raise_ValueError("r out of range. Expected 0 to 255"); + else if(g < 0 || g > 255) + mp_raise_ValueError("g out of range. Expected 0 to 255"); + else if(b < 0 || b > 255) + mp_raise_ValueError("b out of range. Expected 0 to 255"); + else + wireless->set_led(r, g, b); + } + else + mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG); + + return mp_const_none; +} + +mp_obj_t picowireless_is_pressed() { + bool buttonPressed = false; + + if(wireless != nullptr) { + buttonPressed = wireless->is_pressed(PicoWireless::A); + } + else + mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG); + + return buttonPressed ? mp_const_true : mp_const_false; +} } \ No newline at end of file diff --git a/micropython/modules/pico_wireless/pico_wireless.h b/micropython/modules/pico_wireless/pico_wireless.h index 2cde2b72..5f7e0e49 100644 --- a/micropython/modules/pico_wireless/pico_wireless.h +++ b/micropython/modules/pico_wireless/pico_wireless.h @@ -73,4 +73,7 @@ extern mp_obj_t picowireless_send_udp_data(size_t n_args, const mp_obj_t *pos_ar extern mp_obj_t picowireless_send_data(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t picowireless_check_data_sent(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); -extern mp_obj_t picowireless_get_socket(); \ No newline at end of file +extern mp_obj_t picowireless_get_socket(); + +extern mp_obj_t picowireless_set_led(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); +extern mp_obj_t picowireless_is_pressed(); \ No newline at end of file