Skip to content

Commit c99a512

Browse files
authored
Merge pull request #104 from arduino/aliphys/sebCommentsBHY2
[AE-28][AE-51] Merged improvements to Arduino_BHY2 and Arduino_BHY2Host
2 parents b442527 + d4f8918 commit c99a512

File tree

10 files changed

+327
-134
lines changed

10 files changed

+327
-134
lines changed

Arduino_BHY2/docs/readme.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Bosch BHY2 Sensor Library for Arduino
22

3-
The Bosch BHY2 Sensor Library provides an interface with the [Bosch BHY2-Sensor-API](https://github.com/BoschSensortec/BHY2-Sensor-API) for communicating with the BHI260AP and a custom library for the BME688 sensors of the Nicla Sense ME.
4-
3+
This library allows the Nicla Sense ME to interface with the onboard 6-axis IMU (BHI260AP) and environmental sensor (BME688).
54
## Features
65

7-
- Easy access to data from Nicla Sense ME sensors
6+
- Easy access to data from Nicla Sense ME sensors using sensor ID
87
- Wrapper for [Bosch BHY2-Sensor-API](https://github.com/BoschSensortec/BHY2-Sensor-API)
98
- DFU (Device Firmware Update) of the ANNA-B112 and the BHI260AP
109
- All functionality avaliable over both ESLOV and BLE
@@ -31,18 +30,16 @@ A UML diagram of the main library classes are provided in the diagram below, pro
3130

3231
For additional information on the Arduino_BHY2 library (including a list of Sensor IDs) and how you can use it with the Nicla Sense ME, see the [Arduino Nicla Sense ME Cheat Sheet](https://docs.arduino.cc/tutorials/nicla-sense-me/cheat-sheet) page.
3332

34-
35-
3633
## Examples
3734

3835
- [App](https://github.com/arduino-libraries/Arduino_BHY2/blob/main/examples/App/App.ino) : Control Nicla Sense ME from an external device acting as a host via I2C or ESLOV
3936
- [AppLowDelay](https://github.com/arduino-libraries/Arduino_BHY2/blob/main/examples/AppLowDelay/AppLowDelay.ino) : Control Nicla Sense ME from an external device acting as a host via I2C or ESLOV with a lower delay
40-
- [BHYFirmwareUpdate](https://github.com/arduino-libraries/Arduino_BHY2/tree/main/examples/BHYFirmwareUpdate) : Update ANNA-B112 and BHI260 firmware
41-
- [Fail_Safe_flasher](https://github.com/arduino-libraries/Arduino_BHY2/blob/main/examples/Fail_Safe_flasher/Fail_Safe_flasher.ino) : Fail Safe flashing of a RGB blink sketch
42-
- [ReadSensorConfiguration](https://github.com/arduino-libraries/Arduino_BHY2/blob/main/examples/ReadSensorConfiguration/ReadSensorConfiguration.ino) : Read motion, temperature and gas data and send over Serial
37+
- [BHYFirmwareUpdate](https://github.com/arduino-libraries/Arduino_BHY2/tree/main/examples/BHYFirmwareUpdate) : Update BHI260 firmware to latest version. Can also be used to upload custom firmware binary.
38+
- [Fail_Safe_flasher](https://github.com/arduino-libraries/Arduino_BHY2/blob/main/examples/Fail_Safe_flasher/Fail_Safe_flasher.ino) : Fail Safe flashing of a RGB blink sketch. Writes binary file to the SPI flash, then peforms CRC to ensure successful writing of binary file to flash memory.
39+
- [ReadSensorConfiguration](https://github.com/arduino-libraries/Arduino_BHY2/blob/main/examples/ReadSensorConfiguration/ReadSensorConfiguration.ino) : Send sample rate, latency and range for accelerator, gyroscope, rotation, temperature and gas sensor over Serial
4340
- [ShowSensorList](https://github.com/arduino-libraries/Arduino_BHY2/blob/main/examples/ShowSensorList/ShowSensorList.ino) : List sensors of the Nicla Sense ME board
4441
- [Standalone](https://github.com/arduino-libraries/Arduino_BHY2/blob/main/examples/Standalone/Standalone.ino) : Read motion, temperature and gas data and send over Serial
45-
- [StandaloneFlashStorage](https://github.com/arduino-libraries/Arduino_BHY2/blob/main/examples/StandaloneFlashStorage/StandaloneFlashStorage.ino) : Read motion, temperature and gas data and send over and save on Flash storage
42+
- [StandaloneFlashStorage](https://github.com/arduino-libraries/Arduino_BHY2/blob/main/examples/StandaloneFlashStorage/StandaloneFlashStorage.ino) : Save accelerator, gryoscope, temperature, gas and rotation sensor values to the onboard SPI flash in a .CSV file.
4643

4744
## License
4845

Arduino_BHY2/src/Arduino_BHY2.h

Lines changed: 105 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,175 +16,203 @@
1616
#include "sensors/SensorID.h"
1717

1818
/**
19-
* @brief Enumeration for defining wiring configuration over ESLOV or Shield.
19+
* @brief Enumeration for defining wiring configuration between host board and Nicla client. We can select between an I2C connection over ESLOV (NICLA_VIA_ESLOV) or as a Shield for the MKR boards (NICAL_AS_SHIELD).
2020
*
2121
* For NICLA_AS_SHIELD configuration, see https://docs.arduino.cc/tutorials/nicla-sense-me/use-as-mkr-shield
2222
*
2323
*/
2424
enum NiclaWiring {
25-
NICLA_VIA_ESLOV = 0x10,
26-
NICLA_AS_SHIELD = 0x20
25+
NICLA_VIA_ESLOV = 0x10, /*!< Host connects to Nicla board via ESLOV */
26+
NICLA_AS_SHIELD = 0x20 /*!< Host connects to Nicla board as a Shield */
2727
};
2828

2929
/**
30-
* @brief Enumeration for defining I2C or BLE communication configuration.
30+
* @brief Enumeration for defining I2C (wired) or BLE communication configuration between host board and Nicla client. Alternatively, the Nicla Sense ME can also run in a standalone configuration.
31+
*
32+
* @note Both ESLOV and Shield should be considered as implementations of I2C communication (NICLA_I2C)
3133
*
32-
* @see Arduino_BHY2::begin()
3334
*/
3435
enum NiclaConfig {
35-
NICLA_I2C = 0x1, /*!< I2C */
36-
NICLA_BLE = 0x2, /*!< Bluetooth via ANNA-B112 module */
37-
NICLA_BLE_AND_I2C = NICLA_I2C | NICLA_BLE, /*!< Enable I2C and BLE simultaneously */
36+
NICLA_I2C = 0x1, /*!< I2C Configuration, relavent for both ESLOV and Shield configurations */
37+
NICLA_BLE = 0x2, /*!< Bluetooth via the onboard ANNA-B112 module */
38+
NICLA_BLE_AND_I2C = NICLA_I2C | NICLA_BLE,/*!< Enable I2C (ESLOV/Shield) and BLE simultaneously */
3839
NICLA_STANDALONE = 0x4 /*!< Operate in standalone, without external data transfer */
3940
};
4041

4142
/**
42-
* @brief Class for storing Nicla state
43+
* @brief Class for configuring the host-client communication, based on the @ref NiclaConfig enumeration.
44+
*
45+
* Example:
46+
* @code
47+
* NiclaSettings niclaSettings(NICLA_I2C);
48+
* @endcode
4349
*
50+
* @ref Arduino_BHY2::NiclaConfig Arduino_BHY2::NiclaWiring
51+
*
4452
*/
45-
class NiclaSettings {
53+
class NiclaSettings
54+
{
4655
public:
47-
NiclaSettings(uint8_t conf1 = 0, uint8_t conf2 = 0, uint8_t conf3 = 0, uint8_t conf4 = 0) {
56+
NiclaSettings(uint8_t conf1 = 0, uint8_t conf2 = 0, uint8_t conf3 = 0, uint8_t conf4 = 0)
57+
{
4858
conf = conf1 | conf2 | conf3 | conf4;
4959
}
5060

51-
uint8_t getConfiguration() const {
61+
uint8_t getConfiguration() const
62+
{
5263
return conf;
5364
}
65+
5466
private:
5567
uint8_t conf = 0;
5668
};
5769

5870
/**
59-
* @brief Main class for initialising communication with sensors
71+
* @brief Class to interface with the Bosch BHI260 sensor hub on the Nicla Sense ME.
72+
* Provides functionality for reading/configuring sensors based on sensor ID and accessing debug features.
6073
*/
6174
class Arduino_BHY2 {
75+
6276
public:
6377
Arduino_BHY2();
6478
virtual ~Arduino_BHY2();
6579

66-
// Necessary API. Update function should be continuously polled
80+
// Necessary API. Update function should be continuously polled
6781
/**
68-
* @param config Configuration for set @ref NiclaConfig state
69-
* @param niclaConnection Configuration for set @ref NiclaWiring state
82+
* @brief Initialise the BHY2 functionality for the Nicla Sense ME, for a given @ref NiclaSettings configuration.
7083
*
71-
*/
72-
bool begin(NiclaConfig config = NICLA_BLE_AND_I2C, NiclaWiring niclaConnection = NICLA_VIA_ESLOV);
73-
bool begin(NiclaSettings& settings);
74-
/**
75-
* @brief Pings sensor
84+
* @note When called without input parameters, I2C and BLE are enabled by default. I2C communication is over ESLOV.
85+
*
86+
* @param NiclaConfig Define communication configuration (NICLA_I2C, NICLA_BLE, NICLA_BLE_AND_I2C or NICLA_STANDALONE). @see NiclaConfig
87+
* @param NiclaWiring Defining I2C configuration (NICLA_VIA_ESLOV or NICLA_AS_SHIELD). @see NiclaWiring
7688
*
77-
* @note Sensor must be continuously polled to prevent sleep
89+
* Configuring for operation as a Shield:
90+
* @code
91+
* BHY2.begin(NICLA_I2C, NICLA_AS_SHIELD);
92+
* @endcode
7893
*/
79-
void update(); // remove this to enforce a sleep
94+
bool begin(NiclaConfig config = NICLA_BLE_AND_I2C, NiclaWiring niclaConnection = NICLA_VIA_ESLOV);
95+
bool begin(NiclaSettings &settings);
8096
/**
81-
* @brief Pings sensor and then sleep
97+
* @brief Update sensor data by reading the FIFO buffer on the BHI260 and then pass it to a suitable parser.
8298
*
83-
* @note Delay does not stall microcontroller to sleep. See https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay
99+
* @param ms (optional) Time (in milliseconds) to wait before returning data.
84100
*/
101+
void update(); // remove this to enforce a sleep
85102
void update(unsigned long ms); // Update and then sleep
86103
/**
87-
* @brief delay method to be used instead of Arduino delay().
104+
* @brief Delay method to be used instead of Arduino delay().
105+
*
106+
* @note Unlike the standard Arduino delay, this modified delay method uses a function to check if the elapsed time has passed.
107+
* This does not stall the microprocessor.
108+
*
109+
* @param ms Time (in milliseconds) to enforce delay.
110+
*
111+
* @code
112+
* BHY2.delay(100);
113+
* @endcode
88114
*/
89115
void delay(unsigned long ms); // to be used instead of arduino delay()
90116

91-
// API for using the bosch sensortec from sketch
117+
// API for using the Bosch sensortec from sketch
92118
/**
93-
* @brief Poll Bosch method
119+
* @brief Configure a virtual sensor on the BHI260 to have a set sample rate (Hz) and latency (milliseconds)
120+
* This can be achieved
94121
*
95-
* @param config Sensor configuration
96-
*/
97-
void configureSensor(SensorConfigurationPacket& config);
98-
/**
99-
* @brief Setup virtual sensors on the BHI260
122+
* @param config Instance of @see SensorConfigurationPacket class, with sensorID, sampleRate and latency
123+
*
124+
* @code
125+
* #set virtual sensor SENSOR_ID_DEVICE_ORI_WU to have sample rate of 1 Hz and latency of 500 milliseconds
126+
* SensorConfigurationPacket config(70, 1, 500);
127+
* BHY2.configureSensor(config)
128+
* @endcode
129+
*
130+
* @note Alternatively, we can directly configure the virtual sensor without creating a SensorConfigurationPacket class
100131
*
101-
* @param sensorId Sensor ID for sensor
102-
* @param sampleRate Polling rate for sensor
103-
* @param latency Latency in milliseconds
132+
* @param sensorId SensorID for virtual sensor
133+
* @param sampleRate Polling rate for sensor in Hz
134+
* @param latency Latency in milliseconds
135+
*
136+
* @note For list of SensorID, see src/SensorID.h. Or see Table 79 in the <a href="https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bhi260ab-ds000.pdf">BHI260 datasheet</a>
104137
*
105-
* @note For list of SensorID, see src/SensorID.h
106138
*/
139+
void configureSensor(SensorConfigurationPacket& config);
107140
void configureSensor(uint8_t sensorId, float sampleRate, uint32_t latency);
108141
/**
109-
* @brief Handlo FIFO of data queue
142+
* @brief Extract data from the FIFO buffer for a specific SensorID and save into an instance of the SensorDataPacket struct. Eliminate oldest data when sensor queue is full.
110143
*
111-
* @param sensorData Data packet from sensor
144+
* @param sensorData Struct containing SensorID (uint8_t), data payload size (uint8_t) and data corresponding to SensorID (uint8_t).
112145
*/
113146
void addSensorData(SensorDataPacket &sensorData);
114147
/**
115-
* @brief Handlo FIFO of data queue for long sensor data
116-
*
117-
* @param sensorData Data packet from sensor
148+
* @brief Extract data from the FIFO buffer for a specific SensorID and save into an instance of the SensorLongDataPacket struct. Eliminate oldest data when sensor queue is full.
149+
*
150+
* @param sensorData Struct containing SensorID (uint8_t), long data payload size (uint8_t) and data corresponding to SensorID (uint8_t).
118151
*/
119152
void addLongSensorData(SensorLongDataPacket &sensorData);
120153
/**
121-
* @brief Return available sensor data
122-
*
123-
* @return uint8_t
154+
* @brief Return available sensor data within the FIFO buffer queue
155+
*
156+
* @return uint8_t The amount of data in bytes
124157
*/
125158
uint8_t availableSensorData();
126159
/**
127-
* @brief Return available long sensor data
128-
*
129-
* @return uint8_t
160+
* @brief Return available long sensor data within the FIFO buffer queue
161+
*
162+
* @return uint8_t The amount of data in bytes
130163
*/
131164
uint8_t availableLongSensorData();
132165
/**
133-
* @brief Read sensor data
134-
*
135-
* @param data
136-
* @return true
137-
* @return false
166+
* @brief Read sensor data from the top element of the queue
167+
*
168+
* @param data Structure including sensorID, sampleRate and latency
138169
*/
139170
bool readSensorData(SensorDataPacket &data);
140171
/**
141-
* @brief Read long sensor data
142-
*
143-
* @param data
144-
* @return true
145-
* @return false
172+
* @brief Read long sensor data from the top element of the queue
173+
*
174+
* @param data Structure including sensorID, sampleRate and latency
146175
*/
147176
bool readLongSensorData(SensorLongDataPacket &data);
148177
/**
149-
* @brief Check existence of sensor
150-
*
151-
* @param sensorId Selected virtual sensor
152-
* @return true - Sensor is present
153-
* @return false - Sensor is not present
178+
* @brief Check existence of a given sensorID
179+
*
180+
* @param sensorID Selected virtual sensor
181+
* @return True Sensor is present
154182
*/
155183
bool hasSensor(uint8_t sensorId);
156184
/**
157-
* @brief Parse XYZ Cartesian data
158-
*
159-
* @param data Data packet including SensorID
185+
* @brief Parse XYZ Cartesian data from a given data packet
186+
*
187+
* @param data data packet including SensorID
160188
* @param vector vector with XYZ
161189
*/
162-
void parse(SensorDataPacket& data, DataXYZ& vector);
190+
void parse(SensorDataPacket &data, DataXYZ &vector);
163191
/**
164-
* @brief Parse orientation
165-
*
192+
* @brief Parse orientation from a given data packet
193+
*
166194
* @param data Data packet including SensorID
167195
* @param vector Vector with heading, pitch and roll
168196
*/
169-
void parse(SensorDataPacket& data, DataOrientation& vector);
197+
void parse(SensorDataPacket &data, DataOrientation &vector);
170198
/**
171199
* @brief Parse orientation with scale factor
172-
*
200+
*
173201
* @param data Data packet including SensorID
174202
* @param vector Vector with heading, pitch and roll
175203
* @param scaleFactor scale factor for vector
176204
*/
177-
void parse(SensorDataPacket& data, DataOrientation& vector, float scaleFactor);
205+
void parse(SensorDataPacket &data, DataOrientation &vector, float scaleFactor);
178206
/**
179207
* @brief Define LDO regulator timeout time
180-
*
208+
*
181209
* @param time in milliseconds. 120000 ms by default.
182210
*/
183211
void setLDOTimeout(int time);
184212
/**
185-
* @brief Stream
186-
*
187-
* @param stream
213+
* @brief Prints debug stream of multiple library components to the specified stream object.
214+
*
215+
* @param Stream object that implements the Print() function.
188216
*/
189217
void debug(Stream &stream);
190218

@@ -203,7 +231,7 @@ class Arduino_BHY2 {
203231

204232
/**
205233
* @brief The Arduino_BHY2 class can be externally linked to as BHY2 in your sketch
206-
*
234+
*
207235
*/
208236
extern Arduino_BHY2 BHY2;
209237

Arduino_BHY2/src/BLEHandler.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ class BLEHandler {
2222
/**
2323
* @brief Set advertised local name and initialise BLE advertising for DFU and sensor data transfer
2424
*
25-
* @return true Successful initialisation of BLE device
26-
* @return false Failure in initialisation of BLE device
25+
* @return true Successful initialisation of BLE device
2726
*/
2827
bool begin();
2928
/**
30-
* @brief Poll data over Bluetooth and read data to FIFO buffer.
31-
*
29+
* @brief Update sensor data by reading the FIFO buffer on the BHI260 and then pass it to a suitable parser.
30+
*
3231
*/
3332
void update();
33+
3434
/**
3535
* @brief Poll BLE data
3636
*
@@ -61,29 +61,29 @@ class BLEHandler {
6161
* @brief Method for reading and processing DFU packet. Prints size of DFU packet to debug output
6262
*
6363
* @param dfuType Selects device to update firmware. DFU_INTERNAL for the ANNA-B112 and DFU_EXTERNAL for the BHY2 sensor.
64-
* @param characteristic Selected BLE characteristic
64+
* @param characteristic Selected BLE characteristic. See https://reference.arduino.cc/reference/en/libraries/arduinoble/blecharacteristic/
6565
*/
6666
void processDFUPacket(DFUType dfuType, BLECharacteristic characteristic);
6767

6868
/**
69-
* @brief Method for recieving BLE packet to ANNA-B112 module
69+
* @brief Method for receiving and forwarding BLE packet to ANNA-B112 module
7070
*
7171
* @param central Instance of BLEDevice
72-
* @param characteristic Selected BLE characteristic
72+
* @param characteristic Selected BLE characteristic. See https://reference.arduino.cc/reference/en/libraries/arduinoble/blecharacteristic/
7373
*/
7474
static void receivedInternalDFU(BLEDevice central, BLECharacteristic characteristic);
7575
/**
76-
* @brief Method for recieving BLE packet to BHI260AP sensor
76+
* @brief Method for receiving and forwarding BLE packet to BHI260AP sensor
7777
*
7878
* @param central Instance of BLEDevice
79-
* @param characteristic Selected BLE characteristic
79+
* @param characteristic Selected BLE characteristic. See https://reference.arduino.cc/reference/en/libraries/arduinoble/blecharacteristic/
8080
*/
8181
static void receivedExternalDFU(BLEDevice central, BLECharacteristic characteristic);
8282
/**
8383
* @brief Read sensor configuration data and output to debug
8484
*
8585
* @param central Instance of BLEDevice
86-
* @param characteristic Selected BLE characteristic
86+
* @param characteristic Selected BLE characteristic. See https://reference.arduino.cc/reference/en/libraries/arduinoble/blecharacteristic/
8787
*/
8888
static void receivedSensorConfig(BLEDevice central, BLECharacteristic characteristic);
8989
};

Arduino_BHY2/src/BoschParser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class BoschParser {
4141
* @brief Parse Meta Events from the Bosch BHI260 Sensor
4242
*
4343
* @param callback_info Pointer containing information to be parsed
44-
* @param callback_ref
44+
* @param callback_ref Reserved for future use
4545
*/
4646
static void parseMetaEvent(const struct bhy2_fifo_parse_data_info *callback_info, void *callback_ref);
4747
/**
@@ -52,7 +52,7 @@ class BoschParser {
5252
*/
5353
static void parseGeneric(const struct bhy2_fifo_parse_data_info *callback_info, void *callback_ref);
5454
/**
55-
* @brief Print out debug message
55+
* @brief Parse debug message
5656
*
5757
* @param callback_info Pointer containing information to be parsed
5858
* @param callback_ref Reserved for future use

0 commit comments

Comments
 (0)