diff --git a/preload/apps/hid.py b/preload/apps/hid.py new file mode 100644 index 0000000000000000000000000000000000000000..4f594633c057a29c51a00dc834516c9a6c756794 --- /dev/null +++ b/preload/apps/hid.py @@ -0,0 +1,21 @@ +import hid +import buttons + +BUTTON_VOLUME_UP = 1 +BUTTON_VOLUME_DOWN = 2 +BUTTON_PLAY = 8 + +b_old = buttons.read() +while True: + b_new = buttons.read() + if not b_old == b_new: + print(b_new) + b_old = b_new + if b_new == buttons.TOP_RIGHT: + hid.set_button(BUTTON_PLAY) + elif b_new == buttons.BOTTOM_RIGHT: + hid.set_button(BUTTON_VOLUME_UP) + elif b_new == buttons.BOTTOM_LEFT: + hid.set_button(BUTTON_VOLUME_DOWN) + else: + hid.set_button(0) diff --git a/pycardium/meson.build b/pycardium/meson.build index 2bfb09d31b1c3578ed0da629cf3f47c1131bee35..758c565bf4887f0f22479e0c7f5ed2f3d7bd40e7 100644 --- a/pycardium/meson.build +++ b/pycardium/meson.build @@ -7,6 +7,7 @@ modsrc = files( 'modules/fat_file.c', 'modules/fat_reader_import.c', 'modules/gpio.c', + 'modules/hid.c', 'modules/interrupt.c', 'modules/light_sensor.c', 'modules/max30001-sys.c', diff --git a/pycardium/modules/hid.c b/pycardium/modules/hid.c new file mode 100644 index 0000000000000000000000000000000000000000..ba134f6ff19c504504d9277474add8f94a8a634e --- /dev/null +++ b/pycardium/modules/hid.c @@ -0,0 +1,27 @@ +#include "epicardium.h" + +#include "py/builtin.h" +#include "py/obj.h" +#include "py/runtime.h" + +static mp_obj_t mp_hid_set_button(mp_obj_t button_id) +{ + int id = mp_obj_get_int(button_id); + epic_hid_remote_report_event(id); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(hid_set_button_obj, mp_hid_set_button); + +static const mp_rom_map_elem_t hid_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_hid) }, + { MP_ROM_QSTR(MP_QSTR_set_button), MP_ROM_PTR(&hid_set_button_obj) }, +}; +static MP_DEFINE_CONST_DICT(hid_module_globals, hid_module_globals_table); + +const mp_obj_module_t hid_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&hid_module_globals, +}; + +/* clang-format off */ +MP_REGISTER_MODULE(MP_QSTR_hid, hid_module, MODULE_HID_ENABLED); diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index f842f5344616ab139c988be27c73f7f893e88310..550d4591aeb12a5c24b64ca94f74e36526d8a995 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -211,6 +211,8 @@ Q(EVENT_HANDLE_NUMERIC_COMPARISON) Q(EVENT_PAIRING_COMPLETE) Q(EVENT_PAIRING_FAILED) Q(EVENT_SCAN_REPORT) +Q(hid) +Q(set_button) /* SpO2 */ Q(spo2_algo) diff --git a/pycardium/mpconfigport.h b/pycardium/mpconfigport.h index 647b159f2886d1497f31858430c99d53e8a1c751..8623e1e811420685365023ae3496de6efd9ed0c5 100644 --- a/pycardium/mpconfigport.h +++ b/pycardium/mpconfigport.h @@ -61,6 +61,7 @@ int mp_hal_csprng_read_int(void); #define MODULE_BUTTONS_ENABLED (1) #define MODULE_DISPLAY_ENABLED (1) #define MODULE_GPIO_ENABLED (1) +#define MODULE_HID_ENABLED (1) #define MODULE_INTERRUPT_ENABLED (1) #define MODULE_LEDS_ENABLED (1) #define MODULE_LIGHT_SENSOR_ENABLED (1)