From b6a2a86ad9c608e7c14f8a127d8c26cf05b68f76 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Sat, 20 Jul 2019 21:04:09 +0200
Subject: [PATCH] fix(interrupt): add disable function, add error code to api

---
 epicardium/api/interrupt-sender.c | 37 +++++++++++++++++++------------
 epicardium/api/interrupt-sender.h |  2 +-
 epicardium/epicardium.h           |  4 ++--
 pycardium/modules/interrupt.c     | 20 ++++++++++++++++-
 pycardium/modules/qstrdefs.h      |  1 +
 5 files changed, 46 insertions(+), 18 deletions(-)

diff --git a/epicardium/api/interrupt-sender.c b/epicardium/api/interrupt-sender.c
index 03556cd05..804b0a8bb 100644
--- a/epicardium/api/interrupt-sender.c
+++ b/epicardium/api/interrupt-sender.c
@@ -4,16 +4,19 @@
 
 static bool enabled[API_INT_MAX + 1];
 
-void api_interrupt_trigger(api_int_id_t id)
+int api_interrupt_trigger(api_int_id_t id)
 {
-	if (id <= API_INT_MAX) {
-		if (enabled[id]) {
-			while (API_CALL_MEM->int_id)
-				;
-			API_CALL_MEM->int_id = id;
-			TMR_TO_Start(MXC_TMR5, 1, 0);
-		}
+	if (id > API_INT_MAX) {
+		return EINVAL;
 	}
+
+	if (enabled[id]) {
+		while (API_CALL_MEM->int_id)
+			;
+		API_CALL_MEM->int_id = id;
+		TMR_TO_Start(MXC_TMR5, 1, 0);
+	}
+	return 0;
 }
 
 void api_interrupt_init(void)
@@ -26,16 +29,22 @@ void api_interrupt_init(void)
 	}
 }
 
-void epic_interrupt_enable(api_int_id_t int_id)
+int epic_interrupt_enable(api_int_id_t int_id)
 {
-	if (int_id <= API_INT_MAX) {
-		enabled[int_id] = true;
+	if (int_id > API_INT_MAX) {
+		return EINVAL;
 	}
+
+	enabled[int_id] = true;
+	return 0;
 }
 
-void epic_interrupt_disable(api_int_id_t int_id)
+int epic_interrupt_disable(api_int_id_t int_id)
 {
-	if (int_id <= API_INT_MAX) {
-		enabled[int_id] = false;
+	if (int_id > API_INT_MAX) {
+		return EINVAL;
 	}
+
+	enabled[int_id] = false;
+	return 0;
 }
diff --git a/epicardium/api/interrupt-sender.h b/epicardium/api/interrupt-sender.h
index fe2290878..4f690167c 100644
--- a/epicardium/api/interrupt-sender.h
+++ b/epicardium/api/interrupt-sender.h
@@ -1,4 +1,4 @@
 #pragma once
 #include "api/common.h"
 void api_interrupt_init(void);
-void api_interrupt_trigger(api_int_id_t id);
+int api_interrupt_trigger(api_int_id_t id);
diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h
index 7e8f6d3ce..9add57508 100644
--- a/epicardium/epicardium.h
+++ b/epicardium/epicardium.h
@@ -156,13 +156,13 @@ typedef uint32_t api_int_id_t;
  *
  * :param int_id: The interrupt to be enabled
  */
-API(API_INTERRUPT_ENABLE, void epic_interrupt_enable(api_int_id_t int_id));
+API(API_INTERRUPT_ENABLE, int epic_interrupt_enable(api_int_id_t int_id));
 
 /**
  * Disable/mask an API interrupt
  *
  * :param int_id: The interrupt to be disabled
  */
-API(API_INTERRUPT_DISABLE, void epic_interrupt_disable(api_int_id_t int_id));
+API(API_INTERRUPT_DISABLE, int epic_interrupt_disable(api_int_id_t int_id));
 
 #endif /* _EPICARDIUM_H */
diff --git a/pycardium/modules/interrupt.c b/pycardium/modules/interrupt.c
index 1fbea1e2a..b45680c72 100644
--- a/pycardium/modules/interrupt.c
+++ b/pycardium/modules/interrupt.c
@@ -44,17 +44,33 @@ STATIC mp_obj_t mp_interrupt_enable_callback(mp_obj_t id_in)
 	api_int_id_t id = mp_obj_get_int(id_in);
 
 	// TODO: throw error if id is out of range or epic_interrupt_enable failed
-	epic_interrupt_enable(id);
+	if (epic_interrupt_enable(id) < 0) {
+	}
 
 	return mp_const_none;
 }
 
+STATIC mp_obj_t mp_interrupt_disable_callback(mp_obj_t id_in)
+{
+	api_int_id_t id = mp_obj_get_int(id_in);
+
+	// TODO: throw error if id is out of range or epic_interrupt_enable failed
+	if (epic_interrupt_disable(id) < 0) {
+	}
+
+	return mp_const_none;
+}
+
+
 STATIC MP_DEFINE_CONST_FUN_OBJ_2(
 	interrupt_set_callback_obj, mp_interrupt_set_callback
 );
 STATIC MP_DEFINE_CONST_FUN_OBJ_1(
 	interrupt_enable_callback_obj, mp_interrupt_enable_callback
 );
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(
+	interrupt_disable_callback_obj, mp_interrupt_disable_callback
+);
 
 STATIC const mp_rom_map_elem_t interrupt_module_globals_table[] = {
 	{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_interrupt) },
@@ -62,6 +78,8 @@ STATIC const mp_rom_map_elem_t interrupt_module_globals_table[] = {
 	  MP_ROM_PTR(&interrupt_set_callback_obj) },
 	{ MP_ROM_QSTR(MP_QSTR_enable_callback),
 	  MP_ROM_PTR(&interrupt_enable_callback_obj) },
+	{ MP_ROM_QSTR(MP_QSTR_disable_callback),
+	  MP_ROM_PTR(&interrupt_disable_callback_obj) },
 	{ MP_ROM_QSTR(MP_QSTR_BHI160), MP_OBJ_NEW_SMALL_INT(2) },
 };
 STATIC MP_DEFINE_CONST_DICT(
diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h
index 048ed267b..4c7e2f753 100644
--- a/pycardium/modules/qstrdefs.h
+++ b/pycardium/modules/qstrdefs.h
@@ -28,4 +28,5 @@ Q(vibrate)
 
 Q(set_callback)
 Q(enable_callback)
+Q(disable_callback)
 Q(BHI160)
-- 
GitLab