diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index d2565cd75621932a5cb27befd2c3e1e1f423acb1..3f0c9fa66dbeb39c70aa00816d2ef285dcad3f60 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 271825d721860cb18161625a6c690e1d1c1b7674..6e141df990f752f2d60a2911b73d92243d3073c8 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 1173977a70f8a3a1acb4475ef6e42005eb282fee..3b2c0696e127e9681344404ca0dba19d95c5913c 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 ff94f0b9b72df2531576e13f35e71ffe8b897fa0..e732581541517ef69b2d18c1e6c5713b9b4317cf 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 88b0766da2138472cb957c08cbe81e5a13a72751..4d23fe83168e75c0f9446b53d8ce0b9abab110b4 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),