Skip to content

Commit 91c6b50

Browse files
committed
BMA421: Use AccelerationSensor base class
1 parent 7a0d09c commit 91c6b50

File tree

2 files changed

+22
-61
lines changed

2 files changed

+22
-61
lines changed

src/drivers/Bma421.cpp

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#include "drivers/Bma421.h"
2-
#include <libraries/delay/nrf_delay.h>
3-
#include <libraries/log/nrf_log.h>
42
#include "drivers/TwiMaster.h"
53
#include <drivers/Bma421_C/bma423.h>
4+
#include <libraries/delay/nrf_delay.h>
65

76
using namespace Pinetime::Drivers;
8-
97
namespace {
108
int8_t user_i2c_read(uint8_t reg_addr, uint8_t* reg_data, uint32_t length, void* intf_ptr) {
119
auto bma421 = static_cast<Bma421*>(intf_ptr);
@@ -24,7 +22,7 @@ namespace {
2422
}
2523
}
2624

27-
Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}, deviceAddress {twiAddress} {
25+
Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : AccelerationSensor(twiMaster, twiAddress) {
2826
bma.intf = BMA4_I2C_INTF;
2927
bma.bus_read = user_i2c_read;
3028
bma.bus_write = user_i2c_write;
@@ -35,45 +33,44 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}
3533
}
3634

3735
void Bma421::Init() {
38-
if (not isResetOk)
36+
if (!isResetOk)
3937
return; // Call SoftReset (and reset TWI device) first!
4038

39+
// Initialize interface
4140
auto ret = bma423_init(&bma);
4241
if (ret != BMA4_OK)
4342
return;
4443

44+
// Identify chip by ID. The driver code has been modified to handle BMA421 as BMA423
4545
switch (bma.chip_id) {
4646
case BMA423_CHIP_ID:
47-
deviceType = DeviceTypes::BMA421;
47+
deviceType = AccelerationDeviceTypes::BMA421;
4848
break;
4949
case BMA425_CHIP_ID:
50-
deviceType = DeviceTypes::BMA425;
50+
deviceType = AccelerationDeviceTypes::BMA425;
5151
break;
5252
default:
53-
deviceType = DeviceTypes::Unknown;
53+
deviceType = AccelerationDeviceTypes::Unknown;
5454
break;
5555
}
5656

57+
// Load proprietary firmware blob required for step counting engine
5758
ret = bma423_write_config_file(&bma);
5859
if (ret != BMA4_OK)
5960
return;
6061

61-
ret = bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma);
62-
if (ret != BMA4_OK)
63-
return;
64-
62+
// Enable step counter and accelerometer, disable step detector
6563
ret = bma423_feature_enable(BMA423_STEP_CNTR, 1, &bma);
6664
if (ret != BMA4_OK)
6765
return;
68-
6966
ret = bma423_step_detector_enable(0, &bma);
7067
if (ret != BMA4_OK)
7168
return;
72-
7369
ret = bma4_set_accel_enable(1, &bma);
7470
if (ret != BMA4_OK)
7571
return;
7672

73+
// Configure accelerometer
7774
struct bma4_accel_config accel_conf;
7875
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
7976
accel_conf.range = BMA4_ACCEL_RANGE_2G;
@@ -83,25 +80,13 @@ void Bma421::Init() {
8380
if (ret != BMA4_OK)
8481
return;
8582

86-
isOk = true;
83+
isInitialized = true;
8784
}
8885

89-
void Bma421::Reset() {
90-
uint8_t data = 0xb6;
91-
twiMaster.Write(deviceAddress, 0x7E, &data, 1);
92-
}
93-
94-
void Bma421::Read(uint8_t registerAddress, uint8_t* buffer, size_t size) {
95-
twiMaster.Read(deviceAddress, registerAddress, buffer, size);
96-
}
97-
98-
void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
99-
twiMaster.Write(deviceAddress, registerAddress, data, size);
100-
}
101-
102-
Bma421::Values Bma421::Process() {
103-
if (not isOk)
86+
AccelerationValues Bma421::Process() {
87+
if (!isInitialized)
10488
return {};
89+
10590
struct bma4_accel data;
10691
bma4_read_accel_xyz(&data, &bma);
10792

@@ -116,10 +101,7 @@ Bma421::Values Bma421::Process() {
116101
bma423_activity_output(&activity, &bma);
117102

118103
// 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};
120-
}
121-
bool Bma421::IsOk() const {
122-
return isOk;
104+
return {steps, data.y, data.x, data.z, (int16_t*) fifo, 0};
123105
}
124106

125107
void Bma421::ResetStepCounter() {
@@ -133,6 +115,3 @@ void Bma421::SoftReset() {
133115
nrf_delay_ms(1);
134116
}
135117
}
136-
Bma421::DeviceTypes Bma421::DeviceType() const {
137-
return deviceType;
138-
}

src/drivers/Bma421.h

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
#pragma once
2+
3+
#include "drivers/AccelerationSensor.h"
24
#include <drivers/Bma421_C/bma4_defs.h>
35

46
namespace Pinetime {
57
namespace Drivers {
6-
class TwiMaster;
7-
class Bma421 {
8+
class Bma421 : public AccelerationSensor {
89
public:
9-
enum class DeviceTypes : uint8_t { Unknown, BMA421, BMA425 };
10-
struct Values {
11-
uint32_t steps;
12-
int16_t x;
13-
int16_t y;
14-
int16_t z;
15-
};
1610
Bma421(TwiMaster& twiMaster, uint8_t twiAddress);
1711
Bma421(const Bma421&) = delete;
1812
Bma421& operator=(const Bma421&) = delete;
@@ -23,24 +17,12 @@ namespace Pinetime {
2317
/// Init() method to allow the caller to uninit and then reinit the TWI device after the softreset.
2418
void SoftReset();
2519
void Init();
26-
Values Process();
20+
AccelerationValues Process();
2721
void ResetStepCounter();
2822

29-
void Read(uint8_t registerAddress, uint8_t* buffer, size_t size);
30-
void Write(uint8_t registerAddress, const uint8_t* data, size_t size);
31-
32-
bool IsOk() const;
33-
DeviceTypes DeviceType() const;
34-
3523
private:
36-
void Reset();
37-
38-
TwiMaster& twiMaster;
39-
uint8_t deviceAddress = 0x18;
40-
struct bma4_dev bma;
41-
bool isOk = false;
4224
bool isResetOk = false;
43-
DeviceTypes deviceType = DeviceTypes::Unknown;
25+
struct bma4_dev bma;
4426
};
4527
}
46-
}
28+
}

0 commit comments

Comments
 (0)