Skip to content

Commit

Permalink
cleanup power/sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
uvwxy committed May 14, 2021
1 parent 313d413 commit bb26f12
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 70 deletions.
6 changes: 3 additions & 3 deletions src/hal/power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ void OswHal::deepSleep() {
// rtc_gpio_isolate(GPIO_NUM_33);
// rtc_gpio_isolate(GPIO_NUM_34);
// rtc_gpio_isolate(GPIO_NUM_35);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_0 /* BTN_0 */, LOW);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_35 /* BMA_INT_2 / TAP */, HIGH);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_34, HIGH);
// esp_sleep_enable_ext0_wakeup(GPIO_NUM_0 /* BTN_0 */, LOW);
// esp_sleep_enable_ext0_wakeup(GPIO_NUM_35 /* BMA_INT_2 / TAP */, HIGH);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_34 /* BMA_INT_1 */, HIGH);


esp_deep_sleep_start();
Expand Down
123 changes: 56 additions & 67 deletions src/hal/sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,59 @@ int8_t bma400_interface_init(struct bma400_dev *bma400, uint8_t intf) {
return rslt;
}

void setupTiltToWake() {
int8_t rslt = 0;

// get current state of 0x1F register
uint8_t regSet = 0;
rslt = bma400_get_regs(0x1f, &regSet, 1, &bma);
bma400_check_rslt("bma400_get_regs 0x1f", rslt);

if (rslt != BMA400_OK) {
// we were NOT successful getting the 0x1f register
return;
}

// set orientation change used for interrupt
uint8_t data = BMA400_AXIS_X_EN & BMA400_DATA_SRC_ACCEL_FILT_LP;
rslt = bma400_set_regs(0x35, &data, 1, &bma);

// set the threshold for the twist
data = 0x7F;
rslt = bma400_set_regs(0x36, &data, 1, &bma);
bma400_check_rslt("bma400_set_regs 0x36", rslt);

// set stable time in 50ths of a second
data = 0x19;
rslt = bma400_set_regs(0x38, &data, 1, &bma);
bma400_check_rslt("bma400_set_regs 0x38", rslt);

// add orientation change to current interrupt settings
regSet = regSet & BMA400_AXIS_X_EN;
rslt = bma400_set_regs(0x1f, &regSet, 1, &bma);
bma400_check_rslt("bms400_set_regs 0x1f", rslt);

// get the current setting for 0x21
rslt = bma400_get_regs(0x21, &regSet, 1, &bma);
bma400_check_rslt("bma400_get_regs 0x21", rslt);

// add orientch to int1 map
if (rslt == BMA400_OK) {
regSet = regSet & 0x02;
rslt = bma400_set_regs(0x21, &regSet, 1, &bma);
} else {
data = 0x02;
rslt = bma400_set_regs(0x21, &data, 1, &bma);
}
}

// BlueDot_BMA400 bma400 = BlueDot_BMA400();
void IRAM_ATTR isrStep() { Serial.println("Step"); }
void IRAM_ATTR isrStep() {
// TODO: read INT_STAT0,INT_STAT1,INT_STAT2
// check which interrupt fired
}
void IRAM_ATTR isrTap() { Serial.println("Tap"); }

void OswHal::setupSensors() {
struct bma400_sensor_conf accel_setting[3] = {{}};
struct bma400_int_enable int_en[3];
Expand All @@ -173,67 +223,25 @@ void OswHal::setupSensors() {

rslt = bma400_get_sensor_conf(accel_setting, 3, &bma);
bma400_check_rslt("bma400_get_sensor_conf", rslt);
rslt = bma400_get_sensor_conf(accel_setting, 3, &bma);
bma400_check_rslt("bma400_get_sensor_conf", rslt);

// get current state of 0x1F register
uint8_t regSet = 0;
rslt = bma400_get_regs(0x1f, &regSet, 1, &bma);
bma400_check_rslt("bma400_get_regs 0x1f", rslt);

if (rslt == BMA400_OK) {
// we were successful getting the 0x1f register

// set orientation change used for interrupt
uint8_t data = BMA400_AXIS_X_EN & BMA400_DATA_SRC_ACCEL_FILT_LP;
rslt = bma400_set_regs(0x35, &data, 1, &bma);

// set the threshold for the twist
data = 0x7F;
rslt = bma400_set_regs(0x36, &data, 1, &bma);
bma400_check_rslt("bma400_set_regs 0x36", rslt);

// set stable time in 50ths of a second

data = 0x19;
rslt = bma400_set_regs(0x38, &data, 1, &bma);
bma400_check_rslt("bma400_set_regs 0x38", rslt);
setupTiltToWake(); // registers tilt on INT_CHANNEL_1

// add orientation change to current interrupt settings
regSet = regSet & BMA400_AXIS_X_EN;
rslt = bma400_set_regs(0x1f, &regSet, 1, &bma);
bma400_check_rslt("bms400_set_regs 0x1f", rslt);
accel_setting[0].param.step_cnt.int_chan = BMA400_INT_CHANNEL_2;

// get the current setting for 0x21
rslt = bma400_get_regs(0x21, &regSet, 1, &bma);
bma400_check_rslt("bma400_get_regs 0x21", rslt);

// add orientch to int1 map

if (rslt == BMA400_OK) {
regSet = regSet & 0x02;
rslt = bma400_set_regs(0x21, &regSet, 1, &bma);
} else {
data = 0x02;
rslt = bma400_set_regs(0x21, &data, 1, &bma);
}
}
accel_setting[0].param.step_cnt.int_chan = BMA400_INT_CHANNEL_1;

accel_setting[1].param.tap.int_chan = BMA400_INT_CHANNEL_2;
accel_setting[1].param.tap.int_chan = BMA400_INT_CHANNEL_1;
accel_setting[1].param.tap.axes_sel = BMA400_TAP_Z_AXIS_EN; // BMA400_TAP_X_AXIS_EN | BMA400_TAP_Y_AXIS_EN |
accel_setting[1].param.tap.sensitivity = BMA400_TAP_SENSITIVITY_5;
accel_setting[1].param.tap.tics_th = BMA400_TICS_TH_6_DATA_SAMPLES;
accel_setting[1].param.tap.quiet = BMA400_QUIET_60_DATA_SAMPLES;
accel_setting[1].param.tap.quiet_dt = BMA400_QUIET_DT_4_DATA_SAMPLES;

// settings required for tap detection to work:
// settings required for tap detection to work
accel_setting[2].param.accel.odr = BMA400_ODR_200HZ;
accel_setting[2].param.accel.range = BMA400_RANGE_16G;
accel_setting[2].param.accel.data_src = BMA400_DATA_SRC_ACCEL_FILT_1;
accel_setting[2].param.accel.filt1_bw = BMA400_ACCEL_FILT1_BW_1;

/* Set the desired configurations to the sensor */
// Set the desired configurations to the sensor
rslt = bma400_set_sensor_conf(accel_setting, 3, &bma);
bma400_check_rslt("bma400_set_sensor_conf", rslt);

Expand All @@ -250,20 +258,6 @@ void OswHal::setupSensors() {
rslt = bma400_enable_interrupt(int_en, 3, &bma);
bma400_check_rslt("bma400_enable_interrupt", rslt);

// See: https://platformio.org/lib/show/7125/BlueDot%20BMA400%20Library
// bma400.parameter.I2CAddress = 0x14; // default I2C address
// bma400.parameter.powerMode = 0x02; // normal mode
// bma400.parameter.measurementRange = BMA400_RANGE_2G; // 2g range
// bma400.parameter.outputDataRate = BMA400_ODR_200HZ; // 200 Hz req. for tap detection
// bma400.parameter.oversamplingRate = 0x03; // highest oversampling

// _hasBMA400 = bma400.init() == 0x90;

// bma400.enableStepCounter();

// TODO: why is chip ID 0 ?
// Serial.println(bma400.checkID(), 16);

pinMode(BMA_INT_1, INPUT);
pinMode(BMA_INT_2, INPUT);

Expand All @@ -277,11 +271,6 @@ void OswHal::updateAccelerometer(void) {
int8_t rslt = BMA400_OK;
struct bma400_sensor_data data;

// bma400.readData();
// uint16_t int_status;
// rslt = bma400_get_interrupt_status(&int_status, &bma);
// bma400_check_rslt("bma400_get_interrupt_status", rslt);

rslt = bma400_get_steps_counted(&step_count, &act_int, &bma);
bma400_check_rslt("bma400_get_steps_counted", rslt);

Expand Down

0 comments on commit bb26f12

Please sign in to comment.