From f1abe3d5634424a0eaf0b93e083537a193e6677e Mon Sep 17 00:00:00 2001
From: koalo <koalo@koalo.de>
Date: Wed, 21 Aug 2019 18:50:32 +0200
Subject: [PATCH] Working

---
 pycardium/modules/bhi160-sys.c | 23 +++++++++++------------
 pycardium/modules/py/bhi160.py | 14 ++++++++++++++
 pycardium/modules/qstrdefs.h   |  6 +++---
 3 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/pycardium/modules/bhi160-sys.c b/pycardium/modules/bhi160-sys.c
index 0477286f..77ffb9fa 100644
--- a/pycardium/modules/bhi160-sys.c
+++ b/pycardium/modules/bhi160-sys.c
@@ -39,19 +39,18 @@ STATIC mp_obj_t mp_bhi160_read_sensor(mp_obj_t stream_id_in)
 
 	int n = epic_stream_read(sd, buf, sizeof(buf));
 
-//	mp_obj_list_t *list = mp_obj_new_list(0, NULL);
-	//for(int i = 0; i < n; i++) {
+	mp_obj_list_t *list = mp_obj_new_list(0, NULL);
+	for(int i = 0; i < n; i++) {
 		bhi160_sample_obj_t *o = m_new_obj(bhi160_sample_obj_t);
 		o->base.type         = &mp_type_bhi160_sample;
-		o->x                 = buf[0].x;
-		o->y                 = buf[0].y;
-		o->z                 = buf[0].z;
+		o->x                 = buf[i].x;
+		o->y                 = buf[i].y;
+		o->z                 = buf[i].z;
 
-		//mp_obj_list_append(list, o);
-	//}
+		mp_obj_list_append(list, MP_OBJ_FROM_PTR(o));
+	}
 
-	//return MP_OBJ_FROM_PTR(list);
-	return MP_OBJ_FROM_PTR(o);
+	return MP_OBJ_FROM_PTR(list);
 }
 
 STATIC mp_obj_t mp_bhi160_x(mp_obj_t type)
@@ -76,9 +75,9 @@ STATIC mp_obj_t mp_bhi160_z(mp_obj_t type)
 static MP_DEFINE_CONST_FUN_OBJ_1(mp_bhi160_z_obj, mp_bhi160_z);
 
 STATIC const mp_rom_map_elem_t bhi160_sample_locals_dict_table[] = {
-	{ MP_ROM_QSTR(MP_QSTR_get_x), MP_ROM_PTR(&mp_bhi160_x_obj) },
-	{ MP_ROM_QSTR(MP_QSTR_get_y), MP_ROM_PTR(&mp_bhi160_y_obj) },
-	{ MP_ROM_QSTR(MP_QSTR_get_z), MP_ROM_PTR(&mp_bhi160_z_obj) },
+	{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&mp_bhi160_x_obj) },
+	{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&mp_bhi160_y_obj) },
+	{ MP_ROM_QSTR(MP_QSTR_z), MP_ROM_PTR(&mp_bhi160_z_obj) },
 };
 
 STATIC MP_DEFINE_CONST_DICT(bhi160_sample_locals_dict, bhi160_sample_locals_dict_table);
diff --git a/pycardium/modules/py/bhi160.py b/pycardium/modules/py/bhi160.py
index 4d13895f..694eaeca 100644
--- a/pycardium/modules/py/bhi160.py
+++ b/pycardium/modules/py/bhi160.py
@@ -30,6 +30,20 @@ class BHI160Accelerometer:
             interrupt.disable_callback(interrupt.BHI160_ACCELEROMETER)
             interrupt.set_callback(interrupt.BHI160_ACCELEROMETER, None)
 
+    def convert(self,value):
+        return 2*value/32768.0;
+        
+    def read(self):
+        result = []
+        if self.acc_sd is not None:
+            for sample in sys_bhi160.read_sensor(self.acc_sd):
+                result.append(dict({
+                    "x": self.convert(sample.x()),
+                    "y": self.convert(sample.y()),
+                    "z": self.convert(sample.z())
+                }))
+        return result
+
     def _accelerometer_interrupt(self, _):
         if self.acc_sd is not None:
             data = sys_bhi160.read_sensor(self.acc_sd)
diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h
index c40d0ed6..3289c189 100644
--- a/pycardium/modules/qstrdefs.h
+++ b/pycardium/modules/qstrdefs.h
@@ -65,9 +65,9 @@ Q(sys_bhi160)
 Q(enable_sensor)
 Q(read_sensor)
 Q(BHI160Sample)
-Q(get_x)
-Q(get_y)
-Q(get_z)
+Q(x)
+Q(y)
+Q(z)
 
 /* display */
 Q(sys_display)
-- 
GitLab