From 684ac29e255241c3ccb8581ca4d2dcccc993556b Mon Sep 17 00:00:00 2001
From: Adrian <gitlab@ardy.io>
Date: Wed, 17 Jul 2019 19:35:12 +0000
Subject: [PATCH] feat(modules): Add timed vibrate function

---
 epicardium/epicardium.h      |  8 ++++++++
 epicardium/modules/vibra.c   | 18 ++++++++++++++++++
 pycardium/modules/qstrdefs.h |  3 ++-
 pycardium/modules/vibra.c    |  9 +++++++++
 4 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h
index 21ce07ec..7886cc5f 100644
--- a/epicardium/epicardium.h
+++ b/epicardium/epicardium.h
@@ -11,6 +11,7 @@
 #define API_UART_READ          0x2
 #define API_LEDS_SET           0x3
 #define API_VIBRA_SET          0x4
+#define API_VIBRA_VIBRATE      0x5
 /* clang-format on */
 
 /**
@@ -66,4 +67,11 @@ API(API_LEDS_SET, void epic_leds_set(int led, uint8_t r, uint8_t g, uint8_t b));
  */
 API(API_VIBRA_SET, void epic_vibra_set(int status));
 
+/**
+ * Turn vibration motor on for a given time
+ *
+ * :param millis: number of milliseconds to run the vibration motor.
+ */
+API(API_VIBRA_VIBRATE, void epic_vibra_vibrate(int millis));
+
 #endif /* _EPICARDIUM_H */
diff --git a/epicardium/modules/vibra.c b/epicardium/modules/vibra.c
index 5fbc3bb7..dd7c3131 100644
--- a/epicardium/modules/vibra.c
+++ b/epicardium/modules/vibra.c
@@ -1,9 +1,13 @@
 #include "gpio.h"
+#include "FreeRTOS.h"
+#include "timers.h"
 
 static const gpio_cfg_t motor_pin = {
 	PORT_0, PIN_8, GPIO_FUNC_OUT, GPIO_PAD_NONE
 };
 
+static TimerHandle_t vibra_timer;
+
 void epic_vibra_set(int status)
 {
 	if (status) {
@@ -12,3 +16,17 @@ void epic_vibra_set(int status)
 		GPIO_OutClr(&motor_pin);
 	}
 }
+
+void vTimerCallback()
+{
+	epic_vibra_set(0);
+}
+
+void epic_vibra_vibrate(int millis)
+{
+	int ticks = millis * (configTICK_RATE_HZ / 1000);
+	epic_vibra_set(1);
+	vibra_timer =
+		xTimerCreate("vibratimer", ticks, pdFALSE, 0, vTimerCallback);
+	xTimerStart(vibra_timer, 0);
+}
diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h
index 37d5082b..2a6dcbe6 100644
--- a/pycardium/modules/qstrdefs.h
+++ b/pycardium/modules/qstrdefs.h
@@ -23,4 +23,5 @@ Q(ticks_add)
 Q(ticks_diff)
 
 /* vibra */
-Q(vibra)
\ No newline at end of file
+Q(vibra)
+Q(vibrate)
diff --git a/pycardium/modules/vibra.c b/pycardium/modules/vibra.c
index aca3fba6..50bd0721 100644
--- a/pycardium/modules/vibra.c
+++ b/pycardium/modules/vibra.c
@@ -16,9 +16,18 @@ STATIC mp_obj_t mp_vibra_set(mp_obj_t state_obj)
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_1(vibra_set_obj, mp_vibra_set);
 
+STATIC mp_obj_t mp_vibra_vibrate(mp_obj_t a_obj)
+{
+    int millis = mp_obj_get_int(a_obj);
+    epic_vibra_vibrate(millis);
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(vibra_vibrate_obj, mp_vibra_vibrate);
+
 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) },
+    { MP_ROM_QSTR(MP_QSTR_vibrate), MP_ROM_PTR(&vibra_vibrate_obj) }
 };
 STATIC MP_DEFINE_CONST_DICT(vibra_module_globals, vibra_module_globals_table);
 
-- 
GitLab