From 5e9bbd9adf2c5d569e865e08ec2e7141e626badf Mon Sep 17 00:00:00 2001
From: Gerd <gerd@disroot.org>
Date: Mon, 8 Jul 2019 22:12:31 +0000
Subject: [PATCH] feat(modules): Add vibra module

---
 epicardium/epicardium.h        |  4 ++++
 epicardium/modules/meson.build |  1 +
 epicardium/modules/vibra.c     | 14 ++++++++++++++
 pycardium/meson.build          |  1 +
 pycardium/modules/qstrdefs.h   |  3 +++
 pycardium/modules/vibra.c      | 32 ++++++++++++++++++++++++++++++++
 pycardium/mpconfigport.h       |  1 +
 7 files changed, 56 insertions(+)
 create mode 100644 epicardium/modules/vibra.c
 create mode 100644 pycardium/modules/vibra.c

diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h
index e00f7615..2fc55d57 100644
--- a/epicardium/epicardium.h
+++ b/epicardium/epicardium.h
@@ -53,4 +53,8 @@ API(API_UART_READ, char epic_uart_read_chr(void));
  */
 API(API_LEDS_SET, void epic_leds_set(int led, uint8_t r, uint8_t g, uint8_t b));
 
+// turn vibration motor on or off
+#define API_VIBRA_SET  0x4
+API(API_VIBRA_SET, void epic_vibra_set(int status));
+
 #endif /* _EPICARDIUM_H */
diff --git a/epicardium/modules/meson.build b/epicardium/modules/meson.build
index d198fc5e..03fce22c 100644
--- a/epicardium/modules/meson.build
+++ b/epicardium/modules/meson.build
@@ -1,4 +1,5 @@
 module_sources = files(
   'leds.c',
   'serial.c',
+  'vibra.c'
 )
diff --git a/epicardium/modules/vibra.c b/epicardium/modules/vibra.c
new file mode 100644
index 00000000..5fbc3bb7
--- /dev/null
+++ b/epicardium/modules/vibra.c
@@ -0,0 +1,14 @@
+#include "gpio.h"
+
+static const gpio_cfg_t motor_pin = {
+	PORT_0, PIN_8, GPIO_FUNC_OUT, GPIO_PAD_NONE
+};
+
+void epic_vibra_set(int status)
+{
+	if (status) {
+		GPIO_OutSet(&motor_pin);
+	} else {
+		GPIO_OutClr(&motor_pin);
+	}
+}
diff --git a/pycardium/meson.build b/pycardium/meson.build
index 833a9e0e..7e76d925 100644
--- a/pycardium/meson.build
+++ b/pycardium/meson.build
@@ -3,6 +3,7 @@ name = 'pycardium'
 modsrc = files(
   'modules/utime.c',
   'modules/leds.c',
+  'modules/vibra.c',
 )
 
 #################################
diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h
index 943196af..37d5082b 100644
--- a/pycardium/modules/qstrdefs.h
+++ b/pycardium/modules/qstrdefs.h
@@ -21,3 +21,6 @@ Q(ticks_us)
 Q(ticks_cpu)
 Q(ticks_add)
 Q(ticks_diff)
+
+/* vibra */
+Q(vibra)
\ No newline at end of file
diff --git a/pycardium/modules/vibra.c b/pycardium/modules/vibra.c
new file mode 100644
index 00000000..aca3fba6
--- /dev/null
+++ b/pycardium/modules/vibra.c
@@ -0,0 +1,32 @@
+#include "py/obj.h"
+#include "py/runtime.h"
+#include "py/builtin.h"
+#include "epicardium.h"
+
+STATIC mp_obj_t mp_vibra_set(mp_obj_t state_obj)
+{
+	if (state_obj == mp_const_true) {
+		epic_vibra_set(1);
+	} else if (state_obj == mp_const_false) {
+		epic_vibra_set(0);
+	} else {
+		mp_raise_TypeError("expected bool");
+	}
+	return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(vibra_set_obj, mp_vibra_set);
+
+STATIC const mp_rom_map_elem_t vibra_module_globals_table[] = {
+	{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_vibra) },
+	{ MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&vibra_set_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(vibra_module_globals, vibra_module_globals_table);
+
+// Define module object.
+const mp_obj_module_t vibra_module = {
+	.base    = { &mp_type_module },
+	.globals = (mp_obj_dict_t *)&vibra_module_globals,
+};
+
+// Register the module to make it available in Python
+MP_REGISTER_MODULE(MP_QSTR_vibra, vibra_module, MODULE_VIBRA_ENABLED);
diff --git a/pycardium/mpconfigport.h b/pycardium/mpconfigport.h
index 9628e3cc..15d4e440 100644
--- a/pycardium/mpconfigport.h
+++ b/pycardium/mpconfigport.h
@@ -25,6 +25,7 @@
 /* Modules */
 #define MODULE_UTIME_ENABLED                (1)
 #define MODULE_LEDS_ENABLED                 (1)
+#define MODULE_VIBRA_ENABLED                (1)
 
 /*
  * This port is intended to be 32-bit, but unfortunately, int32_t for
-- 
GitLab