Skip to content

Commit 9f09121

Browse files
committed
Updates to BHY2Host
1 parent 208d631 commit 9f09121

File tree

3 files changed

+178
-12
lines changed

3 files changed

+178
-12
lines changed

src/Arduino_BHY2Host.h

+91-5
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,121 @@
2121
#endif
2222

2323

24-
24+
/**
25+
* @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).
26+
*
27+
* For NICLA_AS_SHIELD configuration, see https://docs.arduino.cc/tutorials/nicla-sense-me/use-as-mkr-shield
28+
*
29+
*/
2530
enum NiclaWiring {
26-
NICLA_VIA_ESLOV = 0,
27-
NICLA_AS_SHIELD,
28-
NICLA_VIA_BLE
31+
NICLA_VIA_ESLOV = 0, /*!< Host connects to Nicla board via ESLOV */
32+
NICLA_AS_SHIELD, /*!< Host connects to Nicla board as a Shield */
33+
NICLA_VIA_BLE /*!< Host connects to Nicla board via BLE */
2934
};
3035

3136

37+
/**
38+
* @brief Class to interface with the Bosch BHI260 sensor hub on the Nicla Sense ME.
39+
* Provides functionality for reading/configuring sensors based on sensor ID and accessing debug features.
40+
*/
3241
class Arduino_BHY2Host {
3342
public:
3443
Arduino_BHY2Host();
3544
virtual ~Arduino_BHY2Host();
3645

3746
// Necessary API. Update function should be continuously polled if PASSTHORUGH is ENABLED
47+
/**
48+
* @brief Initialise the BHY2 functionality on the host board, for a given @ref NiclaWiring configuration.
49+
*
50+
* @note When called without input parameters, I2C communication is over ESLOV by default.
51+
*
52+
* @param passthrough Define passthrough state. Disabled by default
53+
* @param NiclaWiring Defining I2C configuration (NICLA_VIA_ESLOV, NICLA_AS_SHIELD or NICLA_VIA_BLE). @see NiclaWiring
54+
*
55+
* Configuring for operation as a Shield:
56+
* @code
57+
* BHY2Host.begin(NICLA_VIA_BLE);
58+
* @endcode
59+
*/
3860
bool begin(bool passthrough = false, NiclaWiring niclaConnection = NICLA_VIA_ESLOV);
61+
/**
62+
* @brief Update sensor data by reading the FIFO buffer on the BHI260 and then pass it to a suitable parser.
63+
*
64+
* @param ms (optional) Time (in milliseconds) to wait before returning data.
65+
*/
3966
void update();
4067
void update(unsigned long ms); // Update and then sleep
4168

4269
// Functions for controlling the BHY when PASSTHROUGH is DISABLED
70+
/**
71+
* @brief Configure a virtual sensor on the BHI260 to have a set sample rate (Hz) and latency (milliseconds)
72+
* This can be achieved
73+
*
74+
* @param config Instance of @see SensorConfigurationPacket class, with sensorID, sampleRate and latency
75+
*
76+
* @code
77+
* #set virtual sensor SENSOR_ID_DEVICE_ORI_WU to have sample rate of 1 Hz and latency of 500 milliseconds
78+
* SensorConfigurationPacket config(70, 1, 500);
79+
* BHY2.configureSensor(config)
80+
* @endcode
81+
*
82+
* @note Alternatively, we can directly configure the virtual sensor without creating a SensorConfigurationPacket class
83+
*
84+
* @param sensorId SensorID for virtual sensor
85+
* @param sampleRate Polling rate for sensor in Hz
86+
* @param latency Latency in milliseconds
87+
*
88+
* @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>
89+
*
90+
*/
4391
void configureSensor(SensorConfigurationPacket& config);
4492
void configureSensor(uint8_t sensorId, float sampleRate, uint32_t latency);
4593
uint8_t requestAck();
94+
/**
95+
* @brief Return available sensor data within the FIFO buffer queue
96+
*
97+
* @return uint8_t The amount of data in bytes
98+
*/
4699
uint8_t availableSensorData();
100+
/**
101+
* @brief Return available long sensor data within the FIFO buffer queue
102+
*
103+
* @return uint8_t The amount of data in bytes
104+
*/
47105
uint8_t availableSensorLongData();
106+
/**
107+
* @brief Read sensor data from the top element of the queue
108+
*
109+
* @param data Structure including sensorID, sampleRate and latency
110+
*/
48111
bool readSensorData(SensorDataPacket &data);
112+
/**
113+
* @brief Read long sensor data from the top element of the queue
114+
*
115+
* @param data Structure including sensorID, sampleRate and latency
116+
*/
49117
bool readSensorLongData(SensorLongDataPacket &data);
50-
118+
/**
119+
* @brief Parse XYZ Cartesian data from a given data packet
120+
*
121+
* @param data data packet including SensorID
122+
* @param vector vector with XYZ
123+
*/
51124
void parse(SensorDataPacket& data, DataXYZ& vector);
125+
/**
126+
* @brief Parse orientation from a given data packet
127+
*
128+
* @param data Data packet including SensorID
129+
* @param vector Vector with heading, pitch and roll
130+
*/
52131
void parse(SensorDataPacket& data, DataOrientation& vector);
132+
/**
133+
* @brief Parse orientation with scale factor
134+
*
135+
* @param data Data packet including SensorID
136+
* @param vector Vector with heading, pitch and roll
137+
* @param scaleFactor scale factor for vector
138+
*/
53139
void parse(SensorDataPacket& data, DataOrientation& vector, float scaleFactor);
54140

55141
NiclaWiring getNiclaConnection();

src/BLEHandler.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,29 @@ class BLEHandler {
1010
public:
1111
BLEHandler();
1212
virtual ~BLEHandler();
13-
13+
/**
14+
* @brief Initialise BLE for DFU and sensor data transfer and connect to Nicla device
15+
*
16+
* @return true successful connection to Nicla over BLE
17+
*
18+
*/
1419
bool begin();
20+
/**
21+
* @brief Read data from the Nicla over BLE to the host device.
22+
*
23+
*/
1524
void update();
25+
/**
26+
* @brief Disconnected Nicla from host and close BLE connection
27+
*
28+
*/
1629
void end();
1730

31+
/**
32+
* @brief Write a configuration packet to the Nicla over BLE. First byte sets the opcode
33+
*
34+
* @param config Instance of @see SensorConfigurationPacket class, with sensorID, sampleRate and latency
35+
*/
1836
void writeConfigPacket(SensorConfigurationPacket& config);
1937

2038
private:

src/EslovHandler.h

+68-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313

1414
#define I2C_INT_PIN (0)
1515

16+
/**
17+
* @brief Enumerator for ESLOV operational state
18+
*
19+
*/
1620
enum EslovOpcode {
17-
ESLOV_DFU_INTERNAL_OPCODE,
18-
ESLOV_DFU_EXTERNAL_OPCODE,
19-
ESLOV_SENSOR_CONFIG_OPCODE,
20-
ESLOV_SENSOR_STATE_OPCODE
21+
ESLOV_DFU_INTERNAL_OPCODE, /*!< ESLOV DFU ANNA-B112 */
22+
ESLOV_DFU_EXTERNAL_OPCODE, /*!< ESLOV DFU BHY260 */
23+
ESLOV_SENSOR_CONFIG_OPCODE, /*!< ESLOV Sensor Configuration */
24+
ESLOV_SENSOR_STATE_OPCODE /*!< ESLOV Sensor State */
2125
};
2226

2327
enum HostOpcode {
@@ -28,31 +32,89 @@ enum HostOpcode {
2832
HOST_READ_LONG_SENSOR_OPCODE
2933
};
3034

35+
/**
36+
* @brief Enumeration for various states over ESLOV
37+
*
38+
*/
3139
enum EslovState {
3240
ESLOV_AVAILABLE_SENSOR_STATE = 0x00,
3341
ESLOV_READ_SENSOR_STATE = 0x01,
3442
ESLOV_DFU_ACK_STATE = 0x02,
3543
ESLOV_SENSOR_ACK_STATE = 0x03,
3644
ESLOV_AVAILABLE_LONG_SENSOR_STATE = 0x04,
3745
ESLOV_READ_LONG_SENSOR_STATE = 0x05
38-
3946
};
4047

48+
/**
49+
* @brief Class to manage communication over ESLOV
50+
*
51+
*/
4152
class EslovHandler {
4253
public:
4354
EslovHandler();
4455
virtual ~EslovHandler();
4556

57+
/**
58+
* @brief Start I2C communication over ESLOV between host board and Nicla
59+
*
60+
* @return true I2C communication initialised successfully.
61+
*/
4662
bool begin(bool passthrough);
63+
/**
64+
* @brief Reads incoming data to the host board based on the opcode @see EslovState.
65+
*
66+
*/
4767
void update();
48-
68+
/**
69+
* @brief Write a DFU (Device Firmware Update) packet to the Nicla Board over ESLOV
70+
*
71+
* @param data pointer to data to be uploaded to Nicla board (uint8_t)
72+
* @param length length of the data to be written to the Nicla in bytes (int)
73+
*/
4974
void writeDfuPacket(uint8_t *data, uint8_t length);
75+
/**
76+
* @brief Waits for the ESLOV interrupt pin to be pulled high, then sends a packet with the @see ESLOV_SENSOR_STATE_OPCODE to over I2C
77+
*
78+
* @param state enumeration of EslovState to be written over ESLOV to the Nicla
79+
*/
5080
void writeStateChange(EslovState state);
81+
/**
82+
* @brief Write a configuration packet to the Nicla over ESLOV. First byte sets the opcode
83+
*
84+
* @param config Instance of @see SensorConfigurationPacket class, with sensorID, sampleRate and latency
85+
*/
5186
void writeConfigPacket(SensorConfigurationPacket& config);
87+
/**
88+
* @brief Requests an acknowledgment packet from the Nicla over ESLOV.
89+
*
90+
* @return uint8_t acknowledge packet recieved from the Nicla over ESLOV
91+
*/
5292
uint8_t requestPacketAck();
93+
/**
94+
* @brief Change state of Nicla to ESLOV_AVAILABLE_SENSOR_STATE and wait for the interrupt pin to go high. Then read the avaliable sensor data over I2C.
95+
*
96+
* @return uint8_t Number of available sensor data packets.
97+
*/
5398
uint8_t requestAvailableData();
99+
/**
100+
* @brief Change state of Nicla to ESLOV_AVAILABLE_SENSOR_STATE and wait for the interrupt pin to go high. Then read the avaliable long sensor data over I2C.
101+
*
102+
* @return uint8_t Number of available long sensor data packets.
103+
*/
54104
uint8_t requestAvailableLongData();
105+
/**
106+
* @brief Change state of Nicla to ESLOV_READ_SENSOR_STATE and wait for the interrupt pin to go high. Then read the avaliable sensor data over I2C.
107+
*
108+
* @param sData data packet containing sensorID, payload size and data payload
109+
* @return true Successful request of sensor data
110+
*/
55111
bool requestSensorData(SensorDataPacket &sData);
112+
/**
113+
* @brief Change state of Nicla to ESLOV_READ_SENSOR_STATE and wait for the interrupt pin to go high. Then read the avaliable long sensor data over I2C.
114+
*
115+
* @param sData data packet containing sensorID, payload size and data payload
116+
* @return true Successful request of sensor data
117+
*/
56118
bool requestSensorLongData(SensorLongDataPacket &sData);
57119

58120
protected:

0 commit comments

Comments
 (0)