Skip to content
Snippets Groups Projects
Verified Commit afcef864 authored by koalo's avatar koalo Committed by rahix
Browse files

feat(bhi160): Enable disable

parent 96d3843f
No related branches found
No related tags found
No related merge requests found
...@@ -182,18 +182,27 @@ int epic_bhi160_disable_sensor(enum bhi160_sensor_type sensor_type) ...@@ -182,18 +182,27 @@ int epic_bhi160_disable_sensor(enum bhi160_sensor_type sensor_type)
return -ENODEV; return -ENODEV;
} }
int ret = hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100)); int lockret = hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100));
if (ret < 0) { if (lockret < 0) {
return ret; return lockret;
} }
if (xSemaphoreTake(bhi160_mutex, LOCK_WAIT) == pdTRUE) { if (xSemaphoreTake(bhi160_mutex, LOCK_WAIT) == pdTRUE) {
struct stream_info *stream = &bhi160_streams[sensor_type]; struct stream_info *stream = &bhi160_streams[sensor_type];
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);
return streamret;
}
vQueueDelete(stream->queue); vQueueDelete(stream->queue);
stream->queue = NULL; stream->queue = NULL;
int bhyret = bhy_disable_virtual_sensor(vs_id, VS_WAKEUP);
bhy_disable_virtual_sensor(vs_id, VS_WAKEUP); if (bhyret < 0) {
xSemaphoreGive(bhi160_mutex);
hwlock_release(HWLOCK_I2C);
return bhyret;
}
xSemaphoreGive(bhi160_mutex); xSemaphoreGive(bhi160_mutex);
} else { } else {
hwlock_release(HWLOCK_I2C); hwlock_release(HWLOCK_I2C);
......
...@@ -4,9 +4,9 @@ import utime ...@@ -4,9 +4,9 @@ import utime
disp = display.open() disp = display.open()
bhi = bhi160.BHI160Accelerometer()
while True: while True:
with bhi160.BHI160Accelerometer(sample_rate=20) as bhi:
utime.sleep(0.25)
samples = bhi.read() samples = bhi.read()
if len(samples) > 0: if len(samples) > 0:
disp.clear() disp.clear()
...@@ -19,4 +19,3 @@ while True: ...@@ -19,4 +19,3 @@ while True:
disp.update() disp.update()
utime.sleep(0.1)
...@@ -21,6 +21,10 @@ STATIC mp_obj_t mp_bhi160_enable_sensor(size_t n_args, const mp_obj_t *args) ...@@ -21,6 +21,10 @@ STATIC mp_obj_t mp_bhi160_enable_sensor(size_t n_args, const mp_obj_t *args)
return MP_OBJ_NEW_SMALL_INT(stream_id); return MP_OBJ_NEW_SMALL_INT(stream_id);
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mp_bhi160_enable_sensor_obj, 4, 4, mp_bhi160_enable_sensor
);
STATIC mp_obj_t mp_bhi160_read_sensor(mp_obj_t stream_id_in) STATIC mp_obj_t mp_bhi160_read_sensor(mp_obj_t stream_id_in)
{ {
struct bhi160_data_vector buf[100]; struct bhi160_data_vector buf[100];
...@@ -44,20 +48,32 @@ STATIC mp_obj_t mp_bhi160_read_sensor(mp_obj_t stream_id_in) ...@@ -44,20 +48,32 @@ STATIC mp_obj_t mp_bhi160_read_sensor(mp_obj_t stream_id_in)
return MP_OBJ_FROM_PTR(list); return MP_OBJ_FROM_PTR(list);
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( STATIC MP_DEFINE_CONST_FUN_OBJ_1(
mp_bhi160_enable_sensor_obj, 4, 4, mp_bhi160_enable_sensor mp_bhi160_read_sensor_obj, mp_bhi160_read_sensor
); );
STATIC mp_obj_t mp_bhi160_disable_sensor(mp_obj_t sensor_type_in)
{
int sensor_type = mp_obj_get_int(sensor_type_in);
int ret = epic_bhi160_disable_sensor(sensor_type);
return MP_OBJ_NEW_SMALL_INT(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1( STATIC MP_DEFINE_CONST_FUN_OBJ_1(
mp_bhi160_read_sensor_obj, mp_bhi160_read_sensor mp_bhi160_disable_sensor_obj, mp_bhi160_disable_sensor
); );
STATIC const mp_rom_map_elem_t bhi160_module_globals_table[] = { 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___name__), MP_ROM_QSTR(MP_QSTR_sys_bhi160) },
{ MP_ROM_QSTR(MP_QSTR_enable_sensor), { MP_ROM_QSTR(MP_QSTR_enable_sensor),
MP_ROM_PTR(&mp_bhi160_enable_sensor_obj) }, MP_ROM_PTR(&mp_bhi160_enable_sensor_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_sensor), { MP_ROM_QSTR(MP_QSTR_read_sensor),
MP_ROM_PTR(&mp_bhi160_read_sensor_obj) }, MP_ROM_PTR(&mp_bhi160_read_sensor_obj) },
{ MP_ROM_QSTR(MP_QSTR_disable_sensor),
MP_ROM_PTR(&mp_bhi160_disable_sensor_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(bhi160_module_globals, bhi160_module_globals_table); STATIC MP_DEFINE_CONST_DICT(bhi160_module_globals, bhi160_module_globals_table);
......
...@@ -27,7 +27,11 @@ class BHI160: ...@@ -27,7 +27,11 @@ class BHI160:
def close(self): def close(self):
if self.active: if self.active:
self.active = False self.active = False
sys_bhi160.disable_sensor(self.sensor_id) ret = sys_bhi160.disable_sensor(self.sensor_id)
if ret < 0:
raise ValueError("Disable sensor returned %i", ret)
interrupt.disable_callback(self.interrupt_id) interrupt.disable_callback(self.interrupt_id)
interrupt.set_callback(self.interrupt_id, None) interrupt.set_callback(self.interrupt_id, None)
......
...@@ -65,6 +65,7 @@ Q(RTC_ALARM) ...@@ -65,6 +65,7 @@ Q(RTC_ALARM)
/* bhi160 */ /* bhi160 */
Q(sys_bhi160) Q(sys_bhi160)
Q(enable_sensor) Q(enable_sensor)
Q(disable_sensor)
Q(read_sensor) Q(read_sensor)
Q(x) Q(x)
Q(y) Q(y)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment