@@ -32,10 +32,11 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}
32
32
bma.intf_ptr = this ;
33
33
bma.delay_us = user_delay;
34
34
bma.read_write_len = 16 ;
35
+ bma.resolution = 12 ;
35
36
}
36
37
37
38
void Bma421::Init () {
38
- if (not isResetOk)
39
+ if (! isResetOk)
39
40
return ; // Call SoftReset (and reset TWI device) first!
40
41
41
42
auto ret = bma423_init (&bma);
@@ -58,10 +59,6 @@ void Bma421::Init() {
58
59
if (ret != BMA4_OK)
59
60
return ;
60
61
61
- ret = bma4_set_interrupt_mode (BMA4_LATCH_MODE, &bma);
62
- if (ret != BMA4_OK)
63
- return ;
64
-
65
62
ret = bma423_feature_enable (BMA423_STEP_CNTR, 1 , &bma);
66
63
if (ret != BMA4_OK)
67
64
return ;
@@ -74,11 +71,19 @@ void Bma421::Init() {
74
71
if (ret != BMA4_OK)
75
72
return ;
76
73
74
+ ret = bma4_set_fifo_config (BMA4_FIFO_ACCEL, 1 , &bma);
75
+ if (ret != BMA4_OK)
76
+ return ;
77
+ fifo_frame.data = (uint8_t *) &fifo;
78
+ fifo_frame.length = sizeof (fifo);
79
+ fifo_frame.fifo_data_enable = BMA4_FIFO_A_ENABLE;
80
+ fifo_frame.fifo_header_enable = 0 ;
81
+
77
82
struct bma4_accel_config accel_conf;
78
- accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ ;
83
+ accel_conf.odr = BMA4_OUTPUT_DATA_RATE_200HZ ;
79
84
accel_conf.range = BMA4_ACCEL_RANGE_2G;
80
85
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
81
- accel_conf.perf_mode = BMA4_CIC_AVG_MODE ;
86
+ accel_conf.perf_mode = BMA4_CONTINUOUS_MODE ;
82
87
ret = bma4_set_accel_config (&accel_conf, &bma);
83
88
if (ret != BMA4_OK)
84
89
return ;
@@ -102,21 +107,34 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
102
107
Bma421::Values Bma421::Process () {
103
108
if (not isOk)
104
109
return {};
105
- struct bma4_accel data;
106
- bma4_read_accel_xyz (&data, &bma);
110
+
111
+ bma4_read_fifo_data (&fifo_frame, &bma);
112
+ uint16_t length = 32 ; // Should be about 20 (200 Hz ODR / 10 Hz main loop)
113
+ bma4_extract_accel ((bma4_accel*) fifo, &length, &fifo_frame, &bma);
107
114
108
115
uint32_t steps = 0 ;
109
116
bma423_step_counter_output (&steps, &bma);
110
117
111
- int32_t temperature;
118
+ int32_t temperature = 0 ;
112
119
bma4_get_temperature (&temperature, BMA4_DEG, &bma);
113
120
temperature = temperature / 1000 ;
114
121
115
122
uint8_t activity = 0 ;
116
123
bma423_activity_output (&activity, &bma);
117
124
118
- // X and Y axis are swapped because of the way the sensor is mounted in the PineTime
119
- return {steps, data.y , data.x , data.z };
125
+ int16_t avgs[3 ] = {0 };
126
+ for (uint8_t i = 0 ; i < length; i++) {
127
+ // X and Y axis are swapped because of the way the sensor is mounted in the PineTime
128
+ int16_t swap = fifo[i][0 ];
129
+ fifo[i][0 ] = fifo[i][1 ];
130
+ fifo[i][1 ] = swap;
131
+ for (uint8_t j = 0 ; j < 3 ; j++)
132
+ avgs[j] += fifo[i][j];
133
+ }
134
+ for (uint8_t j = 0 ; j < 3 ; j++)
135
+ avgs[j] /= length;
136
+
137
+ return {steps, avgs[0 ], avgs[1 ], avgs[2 ]};
120
138
}
121
139
122
140
bool Bma421::IsOk () const {
0 commit comments