Skip to content
Snippets Groups Projects
Commit 230016c4 authored by schneider's avatar schneider
Browse files

feat(bhi160): Initial Python support for the accelerometer

parent e0e0e834
No related branches found
No related tags found
No related merge requests found
......@@ -99,8 +99,8 @@ API(API_INTERRUPT_DISABLE, int epic_interrupt_disable(api_int_id_t int_id));
/** ``^C`` interrupt. See :c:func:`epic_isr_ctrl_c` for details. */
#define EPIC_INT_CTRL_C 1
/* Debug interrupt, please ignore */
#define EPIC_INT_BHI160_TEST 2
API_ISR(EPIC_INT_BHI160_TEST, epic_isr_bhi160_test);
#define EPIC_INT_BHI160_ACCELEROMETER 2
API_ISR(EPIC_INT_BHI160_ACCELEROMETER, epic_isr_bhi160_accelerometer);
/* Number of defined interrupts. */
#define EPIC_INT_NUM 3
......
......@@ -11,6 +11,7 @@
#include "semphr.h"
#include "queue.h"
#include "api/interrupt-sender.h"
#include "epicardium.h"
#include "modules/log.h"
#include "modules/modules.h"
......@@ -215,6 +216,9 @@ bhi160_handle_packet(bhy_data_type_t data_type, bhy_data_generic_t *sensor_data)
&data_vector,
BHI160_MUTEX_WAIT_MS
);
if(sensor_id == VS_ID_ACCELEROMETER_WAKEUP) {
api_interrupt_trigger(EPIC_INT_BHI160_ACCELEROMETER);
}
break;
default:
break;
......
......@@ -62,7 +62,7 @@ static void enqueue_char(char chr)
if (chr == 0x0e) {
/* Control-N */
api_interrupt_trigger(EPIC_INT_BHI160_TEST);
/* Unused at the moment. Used to trigger test functions when needed */
}
if (xQueueSend(read_queue, &chr, 100) == errQUEUE_FULL) {
......
name = 'pycardium'
modsrc = files(
'modules/bhi160-sys.c',
'modules/interrupt.c',
'modules/leds.c',
'modules/sys_display.c',
......
#include "py/obj.h"
#include "py/runtime.h"
#include "py/builtin.h"
#include "epicardium.h"
#include "api/common.h"
#include "mphalport.h"
STATIC mp_obj_t mp_bhi160_enable_sensor(size_t n_args, const mp_obj_t *args)
{
int sensor_type = mp_obj_get_int(args[0]);
struct bhi160_sensor_config cfg = { 0 };
cfg.sample_buffer_len = mp_obj_get_int(args[1]);
cfg.sample_rate = mp_obj_get_int(args[2]);
cfg.dynamic_range = mp_obj_get_int(args[3]);
//cfg.sample_buffer_len = 200;
//cfg.sample_rate = 4;
//cfg.dynamic_range = 2;
int sd = epic_bhi160_enable_sensor(sensor_type, &cfg);
return MP_OBJ_NEW_SMALL_INT(sd);
}
STATIC mp_obj_t mp_bhi160_read_sensor(mp_obj_t stream_id_in)
{
struct bhi160_data_vector buf[100];
int sd = mp_obj_get_int(stream_id_in);
int n = epic_stream_read(sd, buf, sizeof(buf));
return MP_OBJ_NEW_SMALL_INT(n);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mp_bhi160_enable_sensor_obj, 4, 4, mp_bhi160_enable_sensor
);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(
mp_bhi160_read_sensor_obj, mp_bhi160_read_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),
MP_ROM_PTR(&mp_bhi160_enable_sensor_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_sensor),
MP_ROM_PTR(&mp_bhi160_read_sensor_obj) },
};
STATIC MP_DEFINE_CONST_DICT(bhi160_module_globals, bhi160_module_globals_table);
// Define module object.
const mp_obj_module_t bhi160_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&bhi160_module_globals,
};
/* clang-format off */
// Register the module to make it available in Python
MP_REGISTER_MODULE(MP_QSTR_sys_bhi160, bhi160_module, MODULE_BHI160_ENABLED);
......@@ -81,7 +81,7 @@ static const mp_rom_map_elem_t interrupt_module_globals_table[] = {
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) },
{ MP_ROM_QSTR(MP_QSTR_BHI160_ACCELEROMETER), MP_OBJ_NEW_SMALL_INT(2) },
};
static MP_DEFINE_CONST_DICT(
interrupt_module_globals, interrupt_module_globals_table
......
import sys_bhi160
import interrupt
class BHI160Accelerometer:
def __init__(self,sample_rate=4, dynamic_range=2, callback=None, sample_buffer_len=200):
interrupt.disable_callback(interrupt.BHI160_ACCELEROMETER)
interrupt.set_callback(interrupt.BHI160_ACCELEROMETER, self._accelerometer_interrupt)
self.acc_sd = sys_bhi160.enable_sensor(0, sample_buffer_len, sample_rate, dynamic_range)
self._callback = callback
if callback:
interrupt.enable_callback(interrupt.BHI160_ACCELEROMETER)
def __enter__(self):
return self
def __exit__(self, _et, _ev, _t):
self.close()
def close():
if self.acc_sd != None:
self.acc_sd = None
self.acc_sd = sys_bhi160.disable_sensor(0)
interrupt.disable_callback(interrupt.BHI160_ACCELEROMETER)
interrupt.set_callback(interrupt.BHI160_ACCELEROMETER, None)
def _accelerometer_interrupt(self, _):
if self.acc_sd != None:
data = sys_bhi160.read_sensor(self.acc_sd)
print(data)
if self._callback:
self._callback(data)
python_modules = files(
'bhi160.py',
'color.py',
'htmlcolor.py',
'display.py',
......
......@@ -29,7 +29,11 @@ Q(vibrate)
Q(set_callback)
Q(enable_callback)
Q(disable_callback)
Q(BHI160)
Q(BHI160_ACCELEROMETER)
Q(sys_bhi160)
Q(enable_sensor)
Q(read_sensor)
/* display */
Q(sys_display)
......
......@@ -38,6 +38,7 @@
/* Modules */
#define MODULE_UTIME_ENABLED (1)
#define MODULE_BHI160_ENABLED (1)
#define MODULE_LEDS_ENABLED (1)
#define MODULE_VIBRA_ENABLED (1)
#define MODULE_INTERRUPT_ENABLED (1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment