From 9b256c80476306f4ac15a7764bea76db611725b7 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Fri, 26 Jun 2020 19:07:22 +0200
Subject: [PATCH] feat(ble): Basic HID support for pycardium

---
 preload/apps/hid.py          | 21 +++++++++++++++++++++
 pycardium/meson.build        |  1 +
 pycardium/modules/hid.c      | 27 +++++++++++++++++++++++++++
 pycardium/modules/qstrdefs.h |  2 ++
 pycardium/mpconfigport.h     |  1 +
 5 files changed, 52 insertions(+)
 create mode 100644 preload/apps/hid.py
 create mode 100644 pycardium/modules/hid.c

diff --git a/preload/apps/hid.py b/preload/apps/hid.py
new file mode 100644
index 000000000..4f594633c
--- /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 2bfb09d31..758c565bf 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 000000000..ba134f6ff
--- /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 f842f5344..550d4591a 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 647b159f2..8623e1e81 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)
-- 
GitLab