Skip to content

Commit b7e49e6

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 208d631 + 7d57b0d commit b7e49e6

File tree

3 files changed

+185
-12
lines changed

3 files changed

+185
-12
lines changed

src/Arduino_BHY2Host.h

+94-5
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,123 @@
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 Establishes a connection with the client (Nicla Sense ME) using the given type of communication (ESLOV/BLE/As shield).
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 niclaConnection 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 Requests new sensor data from the client (Nicla Sense ME) and saves it locally so it can be retrieved via `Sensor` objects.
63+
*
64+
* @param ms (optional) Time (in milliseconds) to wait before returning data.
65+
*/
3966
void update();
67+
/**
68+
* @brief Requests new sensor data from the client (Nicla Sense ME) and saves it locally so it can be retrieved via `Sensor` objects. The Nicla Sense ME's sensors are then put to sleep for the given amount of milliseconds.
69+
*
70+
* @param ms (optional) time to set Nicla Sense ME to sleep in milliseconds
71+
*/
4072
void update(unsigned long ms); // Update and then sleep
4173

4274
// Functions for controlling the BHY when PASSTHROUGH is DISABLED
75+
/**
76+
* @brief Configures a sensor to be read using a given sample rate and latency. The configuration is packaged up in a `SensorConfigurationPacket` object.
77+
*
78+
* @param config Instance of @see SensorConfigurationPacket class, with sensorID, sampleRate and latency
79+
*
80+
* @code
81+
* #set virtual sensor SENSOR_ID_DEVICE_ORI_WU to have sample rate of 1 Hz and latency of 500 milliseconds
82+
* SensorConfigurationPacket config(70, 1, 500);
83+
* BHY2.configureSensor(config)
84+
* @endcode
85+
*
86+
* @note Alternatively, we can directly configure the virtual sensor without creating a SensorConfigurationPacket class
87+
*
88+
* @param sensorId SensorID for virtual sensor
89+
* @param sampleRate Polling rate for sensor in Hz
90+
* @param latency Latency in milliseconds
91+
*
92+
* @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>
93+
*
94+
*/
4395
void configureSensor(SensorConfigurationPacket& config);
4496
void configureSensor(uint8_t sensorId, float sampleRate, uint32_t latency);
97+
/**
98+
* @brief Recieve acknowledgement from Nicla board over ESLOV
99+
*
100+
* @return uint8_t One byte of data read from the I2C bus.
101+
*/
45102
uint8_t requestAck();
103+
/**
104+
* @brief Return available sensor data within the FIFO buffer queue
105+
*
106+
* @return uint8_t Amount of bytes.
107+
*/
46108
uint8_t availableSensorData();
109+
/**
110+
* @brief Return available long sensor data within the FIFO buffer queue
111+
*
112+
* @return uint8_t Returns the amount of 16 bit chunks of the available sensor data.
113+
*/
47114
uint8_t availableSensorLongData();
115+
/**
116+
* @brief Read sensor data from the top element of the queue
117+
*
118+
* @param data SensorDataPacket object including SensorID, size, and data payload
119+
*/
48120
bool readSensorData(SensorDataPacket &data);
121+
/**
122+
* @brief Read long sensor data from the top element of the queue
123+
*
124+
* @param data SensorDataPacket object including SensorID, size , and data payload
125+
*/
49126
bool readSensorLongData(SensorLongDataPacket &data);
50-
127+
/**
128+
* @brief Parse XYZ Cartesian data from `SensorDataPacket` and store within `DataXYZ` vector
129+
*
130+
* @param data SensorDataPacket object including SensorID, size, and data payload
131+
* @param vector DataXYZ vector with XYZ values stores as int16_t
132+
*/
51133
void parse(SensorDataPacket& data, DataXYZ& vector);
134+
/**
135+
* @brief Parse orientation data from `SensorDataPacket` and store within `DataOrientation` vector. Optional argument for scale factor
136+
*
137+
* @param data SensorDataPacket object including SensorID, size, and data payload
138+
* @param vector DataOrientation vector with heading, pitch and roll stored as float
139+
* @param scaleFactor (optional) scale factor for vector. Defined by format "Euler". See section 15.1.2 in datasheet.
140+
*/
52141
void parse(SensorDataPacket& data, DataOrientation& vector);
53142
void parse(SensorDataPacket& data, DataOrientation& vector, float scaleFactor);
54143

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

+72-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@
1313

1414
#define I2C_INT_PIN (0)
1515

16+
/**
17+
* @brief Enumerator for ESLOV operational state. Defines what state the I2C protocol of the ESLOV device should be in. Used to set DFU (Device Firmware Update) mode in the ANNA-B112 and BHY260 of the Nicla Sense ME.
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

27+
/**
28+
* @brief Enumeration for the host device operational status.
29+
*
30+
*/
2331
enum HostOpcode {
2432
HOST_DFU_INTERNAL_OPCODE = ESLOV_DFU_INTERNAL_OPCODE,
2533
HOST_DFU_EXTERNAL_OPCODE = ESLOV_DFU_EXTERNAL_OPCODE,
@@ -28,31 +36,89 @@ enum HostOpcode {
2836
HOST_READ_LONG_SENSOR_OPCODE
2937
};
3038

39+
/**
40+
* @brief Enumeration for various states over ESLOV
41+
*
42+
*/
3143
enum EslovState {
3244
ESLOV_AVAILABLE_SENSOR_STATE = 0x00,
3345
ESLOV_READ_SENSOR_STATE = 0x01,
3446
ESLOV_DFU_ACK_STATE = 0x02,
3547
ESLOV_SENSOR_ACK_STATE = 0x03,
3648
ESLOV_AVAILABLE_LONG_SENSOR_STATE = 0x04,
3749
ESLOV_READ_LONG_SENSOR_STATE = 0x05
38-
3950
};
4051

52+
/**
53+
* @brief Class to manage communication over ESLOV
54+
*
55+
*/
4156
class EslovHandler {
4257
public:
4358
EslovHandler();
4459
virtual ~EslovHandler();
4560

61+
/**
62+
* @brief Start I2C communication over ESLOV between host board and Nicla
63+
*
64+
* @return true I2C communication initialised successfully.
65+
*/
4666
bool begin(bool passthrough);
67+
/**
68+
* @brief Reads incoming data to the host board based on the opcode @see EslovState.
69+
*
70+
*/
4771
void update();
48-
72+
/**
73+
* @brief Write a DFU (Device Firmware Update) packet to the Nicla Board over ESLOV. On the last packet, the built-in LED is pulled down.
74+
*
75+
* @param data pointer to data to be uploaded to Nicla board as a byte array
76+
* @param length amount of data to be written to the Nicla in bytes (int)
77+
*/
4978
void writeDfuPacket(uint8_t *data, uint8_t length);
79+
/**
80+
* @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
81+
*
82+
* @param state State value sent to Nicla Sense ME based on @ref EslovState enumerator.
83+
*/
5084
void writeStateChange(EslovState state);
85+
/**
86+
* @brief Write a configuration packet to the Nicla over ESLOV. First byte sets the opcode
87+
*
88+
* @param config Instance of @see SensorConfigurationPacket class, with sensorID, sampleRate and latency
89+
*/
5190
void writeConfigPacket(SensorConfigurationPacket& config);
91+
/**
92+
* @brief Requests an acknowledgment packet from the Nicla over ESLOV.
93+
*
94+
* @return uint8_t acknowledge packet recieved from the Nicla over ESLOV
95+
*/
5296
uint8_t requestPacketAck();
97+
/**
98+
* @brief Change `EslovState` of Nicla to `ESLOV_AVAILABLE_SENSOR_STATE` and wait for the interrupt pin to go high. Then read the avaliable sensor data over I2C.
99+
*
100+
* @return uint8_t Number of available sensor data packets.
101+
*/
53102
uint8_t requestAvailableData();
103+
/**
104+
* @brief Change `EslovState` 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.
105+
*
106+
* @return uint8_t Number of available long sensor data packets.
107+
*/
54108
uint8_t requestAvailableLongData();
109+
/**
110+
* @brief Change `EslovState` of Nicla to `ESLOV_READ_SENSOR_STATE` and wait for the interrupt pin to go high. Then read the avaliable sensor data over I2C.
111+
*
112+
* @param sData data packet containing sensorID, payload size and data payload
113+
* @return true Successful request of sensor data
114+
*/
55115
bool requestSensorData(SensorDataPacket &sData);
116+
/**
117+
* @brief Change `EslovState` 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.
118+
*
119+
* @param sData data packet containing sensorID, payload size and data payload
120+
* @return true Successful request of sensor data
121+
*/
56122
bool requestSensorLongData(SensorLongDataPacket &sData);
57123

58124
protected:

0 commit comments

Comments
 (0)