From 1aaffea6baa1888c5ef0ac2e97e554d0371ca85f Mon Sep 17 00:00:00 2001 From: koalo <koalo@koalo.de> Date: Thu, 22 Aug 2019 12:05:49 +0200 Subject: [PATCH] feat(bhi160): Disable all sensors in hardware_reset --- epicardium/epicardium.h | 6 ++++++ epicardium/modules/bhi.c | 25 +++++++++++++++++++++++-- epicardium/modules/hardware.c | 5 +++++ preload/apps/bhi160/__init__.py | 23 ++++++++++++----------- pycardium/modules/bhi160-sys.c | 1 - 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index d2565cd75..3f0c9fa66 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -113,6 +113,7 @@ typedef _Bool bool; #define API_BHI160_ENABLE 0xe0 #define API_BHI160_DISABLE 0xe1 +#define API_BHI160_DISABLE_ALL 0xe2 /* clang-format on */ @@ -1029,6 +1030,11 @@ API(API_BHI160_DISABLE, int epic_bhi160_disable_sensor( enum bhi160_sensor_type sensor_type )); +/** + * Disable all BHI160 sensors. + */ +API(API_BHI160_DISABLE_ALL, void epic_bhi160_disable_all_sensors()); + /** * Vibration Motor * =============== diff --git a/epicardium/modules/bhi.c b/epicardium/modules/bhi.c index 271825d72..6e141df99 100644 --- a/epicardium/modules/bhi.c +++ b/epicardium/modules/bhi.c @@ -62,6 +62,9 @@ static SemaphoreHandle_t bhi160_mutex = NULL; /* Streams */ static struct stream_info bhi160_streams[10]; +/* Active */ +static bool bhi160_sensor_active[10] = { 0 }; + /* -- Utilities -------------------------------------------------------- {{{ */ /* * Retrieve the data size for a sensor. This value is needed for the creation @@ -165,6 +168,9 @@ int epic_bhi160_enable_sensor( hwlock_release(HWLOCK_I2C); return bhyret; } + + bhi160_sensor_active[sensor_type] = true; + xSemaphoreGive(bhi160_mutex); } else { hwlock_release(HWLOCK_I2C); @@ -189,7 +195,9 @@ int epic_bhi160_disable_sensor(enum bhi160_sensor_type sensor_type) if (xSemaphoreTake(bhi160_mutex, LOCK_WAIT) == pdTRUE) { struct stream_info *stream = &bhi160_streams[sensor_type]; - int streamret = stream_deregister(bhi160_lookup_sd(sensor_type), stream); + int streamret = stream_deregister( + bhi160_lookup_sd(sensor_type), stream + ); if (streamret < 0) { xSemaphoreGive(bhi160_mutex); hwlock_release(HWLOCK_I2C); @@ -197,12 +205,15 @@ int epic_bhi160_disable_sensor(enum bhi160_sensor_type sensor_type) } vQueueDelete(stream->queue); stream->queue = NULL; - int bhyret = bhy_disable_virtual_sensor(vs_id, VS_WAKEUP); + int bhyret = bhy_disable_virtual_sensor(vs_id, VS_WAKEUP); if (bhyret < 0) { xSemaphoreGive(bhi160_mutex); hwlock_release(HWLOCK_I2C); return bhyret; } + + bhi160_sensor_active[sensor_type] = false; + xSemaphoreGive(bhi160_mutex); } else { hwlock_release(HWLOCK_I2C); @@ -212,6 +223,16 @@ int epic_bhi160_disable_sensor(enum bhi160_sensor_type sensor_type) hwlock_release(HWLOCK_I2C); return 0; } + +void epic_bhi160_disable_all_sensors() +{ + for (int i = 0; i < sizeof(bhi160_sensor_active); i++) { + if (bhi160_sensor_active[i]) { + epic_bhi160_disable_sensor(i); + } + } +} + /* }}} */ /* -- Driver ----------------------------------------------------------- {{{ */ diff --git a/epicardium/modules/hardware.c b/epicardium/modules/hardware.c index 1173977a7..3b2c0696e 100644 --- a/epicardium/modules/hardware.c +++ b/epicardium/modules/hardware.c @@ -259,6 +259,11 @@ int hardware_reset(void) */ display_init_slim(); + /* + * BHI160 + */ + epic_bhi160_disable_all_sensors(); + /* * BME680 Sensor */ diff --git a/preload/apps/bhi160/__init__.py b/preload/apps/bhi160/__init__.py index ff94f0b9b..e73258154 100644 --- a/preload/apps/bhi160/__init__.py +++ b/preload/apps/bhi160/__init__.py @@ -4,18 +4,19 @@ import utime disp = display.open() +bhi = bhi160.BHI160Accelerometer() + while True: - with bhi160.BHI160Accelerometer(sample_rate=20) as bhi: - utime.sleep(0.25) - samples = bhi.read() - if len(samples) > 0: - disp.clear() - sample = samples[0] + samples = bhi.read() + if len(samples) > 0: + disp.clear() + sample = samples[0] - disp.print("Accelerometer", posy=0) - disp.print("X: %f" % sample["x"], posy=20) - disp.print("Y: %f" % sample["y"], posy=40) - disp.print("Z: %f" % sample["z"], posy=60) + disp.print("Accelerometer", posy=0) + disp.print("X: %f" % sample["x"], posy=20) + disp.print("Y: %f" % sample["y"], posy=40) + disp.print("Z: %f" % sample["z"], posy=60) - disp.update() + disp.update() + utime.sleep(0.1) diff --git a/pycardium/modules/bhi160-sys.c b/pycardium/modules/bhi160-sys.c index 88b0766da..4d23fe831 100644 --- a/pycardium/modules/bhi160-sys.c +++ b/pycardium/modules/bhi160-sys.c @@ -65,7 +65,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1( mp_bhi160_disable_sensor_obj, mp_bhi160_disable_sensor ); - STATIC const mp_rom_map_elem_t bhi160_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys_bhi160) }, { MP_ROM_QSTR(MP_QSTR_enable_sensor), -- GitLab