Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit e2a7a87

Browse files
committed
♻️ refactors code to follow style guide
1 parent 2797609 commit e2a7a87

5 files changed

Lines changed: 386 additions & 476 deletions

File tree

demos/applications/icm20948.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "../hardware_map.hpp"
1616
#include <libhal-icm/icm20948.hpp>
17-
#include <libhal-icm/xyzFloat.hpp>
1817
#include <libhal-util/serial.hpp>
1918
#include <libhal-util/steady_clock.hpp>
2019

@@ -31,7 +30,7 @@ hal::status application(hardware_map& p_map)
3130
(void)hal::delay(clock, 200ms);
3231
auto icm_device = HAL_CHECK(hal::icm::icm20948::create(i2c, 0x69));
3332
(void)hal::delay(clock, 200ms);
34-
icm_device.autoOffsets();
33+
icm_device.auto_offsets();
3534
(void)hal::delay(clock, 100ms);
3635

3736

include/libhal-icm/icm20948.hpp

Lines changed: 107 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -24,66 +24,8 @@ class icm20948
2424
{
2525

2626
public:
27-
struct accel_read_t
28-
{
29-
float x;
30-
float y;
31-
float z;
32-
};
33-
34-
struct gyro_read_t
35-
{
36-
float x;
37-
float y;
38-
float z;
39-
};
40-
41-
struct mag_read_t
42-
{
43-
float x;
44-
float y;
45-
float z;
46-
};
4727

48-
struct temp_read_t
49-
{
50-
float temp;
51-
};
52-
53-
54-
/**
55-
* @brief Read acceleration data from out_x_msb_r, out_x_lsb_r,
56-
* out_y_msb_r, out_y_lsb_r, out_z_msb_r, out_z_lsb_r
57-
* and perform acceleration conversion to g.
58-
*/
59-
[[nodiscard]] hal::result<accel_read_t> read_acceleration();
60-
61-
/**
62-
* @brief Read gyroscope data from out_x_msb_r, out_x_lsb_r,
63-
* out_y_msb_r, out_y_lsb_r, out_z_msb_r, out_z_lsb_r
64-
* and perform gyroscope conversion to rad/s.
65-
*/
66-
[[nodiscard]] hal::result<gyro_read_t> read_gyroscope();
67-
68-
/**
69-
* @brief Read magnetometer data from out_x_msb_r, out_x_lsb_r,
70-
* out_y_msb_r, out_y_lsb_r, out_z_msb_r, out_z_lsb_r
71-
* and perform magnetometer conversion to uT.
72-
*/
73-
[[nodiscard]] hal::result<mag_read_t> read_magnetometer();
74-
75-
76-
/**
77-
* @brief Read pressure data from out_t_msb_r and out_t_lsb_r
78-
* and perform temperature conversion to celsius.
79-
*/
80-
[[nodiscard]] hal::result<temp_read_t> read_temperature();
81-
82-
83-
84-
static result<icm20948> create(hal::i2c& p_i2c, hal::byte p_device_address);
85-
86-
typedef enum ICM20948_CYCLE
28+
typedef enum ICM20948_CYCLE
8729
{
8830
ICM20948_NO_CYCLE = 0x00,
8931
ICM20948_GYR_CYCLE = 0x10,
@@ -196,93 +138,134 @@ class icm20948
196138
ICM20948_YX_1
197139
} ICM20948_orientation;
198140

199-
/* Basic settings */
200141

201-
hal::status init();
202-
hal::status defaultSetup();
203-
hal::status autoOffsets();
204-
hal::status setAccOffsets(float xMin,
205-
float xMax,
206-
float yMin,
207-
float yMax,
208-
float zMin,
209-
float zMax);
210-
hal::status setGyrOffsets(float xOffset, float yOffset, float zOffset);
211-
hal::result<hal::byte> whoAmI();
212-
213-
// Delete Later
214-
hal::result<hal::byte> sleep_check();
215-
hal::result<hal::byte> accel_check();
216-
217-
hal::status enableAcc(bool enAcc);
218-
hal::status setAccRange(ICM20948_accRange accRange);
219-
hal::status setAccDLPF(ICM20948_dlpf dlpf);
220-
hal::status setAccSampleRateDivider(uint16_t accSplRateDiv);
221-
hal::status enableGyr(bool enGyr);
222-
hal::status setGyrRange(ICM20948_gyroRange gyroRange);
223-
hal::status setGyrDLPF(ICM20948_dlpf dlpf);
224-
hal::status setGyrSampleRateDivider(hal::byte gyrSplRateDiv);
225-
hal::status setTempDLPF(ICM20948_dlpf dlpf);
226-
227-
hal::status readSensor();
228142

229-
/* Power, Sleep, Standby */
230-
hal::status enableCycle(ICM20948_cycle cycle);
231-
hal::status enableLowPower(bool enLP);
232-
hal::status setGyrAverageInCycleMode(ICM20948_gyroAvgLowPower avg);
233-
hal::status setAccAverageInCycleMode(ICM20948_accAvgLowPower avg);
234-
hal::status sleep(bool sleep);
143+
struct accel_read_t
144+
{
145+
float x;
146+
float y;
147+
float z;
148+
};
235149

236-
/* Angles and Orientation */
150+
struct gyro_read_t
151+
{
152+
float x;
153+
float y;
154+
float z;
155+
};
156+
157+
struct mag_read_t
158+
{
159+
float x;
160+
float y;
161+
float z;
162+
};
237163

238-
// xyzFloat getAngles();
239-
// ICM20948_orientation getOrientation();
240-
// std::string getOrientationAsString();
241-
// float getPitch();
242-
// float getRoll();
164+
struct temp_read_t
165+
{
166+
float temp;
167+
};
243168

244-
/* Magnetometer */
169+
/**
170+
* @brief Read acceleration data from out_x_msb_r, out_x_lsb_r,
171+
* out_y_msb_r, out_y_lsb_r, out_z_msb_r, out_z_lsb_r
172+
* and perform acceleration conversion to g.
173+
*/
174+
[[nodiscard]] hal::result<accel_read_t> read_acceleration();
245175

246-
hal::status initMagnetometer();
247-
hal::result<hal::byte> whoAmIMag();
248-
void setMagOpMode(AK09916_opMode opMode);
249-
void resetMag();
176+
/**
177+
* @brief Read gyroscope data from out_x_msb_r, out_x_lsb_r,
178+
* out_y_msb_r, out_y_lsb_r, out_z_msb_r, out_z_lsb_r
179+
* and perform gyroscope conversion to rad/s.
180+
*/
181+
[[nodiscard]] hal::result<gyro_read_t> read_gyroscope();
182+
183+
/**
184+
* @brief Read magnetometer data from out_x_msb_r, out_x_lsb_r,
185+
* out_y_msb_r, out_y_lsb_r, out_z_msb_r, out_z_lsb_r
186+
* and perform magnetometer conversion to uT.
187+
*/
188+
[[nodiscard]] hal::result<mag_read_t> read_magnetometer();
189+
190+
191+
/**
192+
* @brief Read pressure data from out_t_msb_r and out_t_lsb_r
193+
* and perform temperature conversion to celsius.
194+
*/
195+
[[nodiscard]] hal::result<temp_read_t> read_temperature();
196+
197+
198+
199+
[[nodiscard]] static result<icm20948> create(hal::i2c& p_i2c, hal::byte p_device_address);
200+
201+
hal::status init();
202+
hal::status auto_offsets();
203+
hal::status set_acc_offsets(float p_xMin,
204+
float p_xMax,
205+
float p_yMin,
206+
float p_yMax,
207+
float p_zMin,
208+
float p_zMax);
209+
hal::status set_gyr_offsets(float p_xOffset, float p_yOffset, float p_zOffset);
210+
hal::result<hal::byte> whoami();
211+
212+
hal::status enable_acc(bool p_enAcc);
213+
hal::status set_acc_range(ICM20948_accRange p_accRange);
214+
hal::status set_acc_DLPF(ICM20948_dlpf p_dlpf);
215+
hal::status set_acc_sample_rate_div(uint16_t p_accSplRateDiv);
216+
hal::status enable_gyr(bool p_enGyr);
217+
hal::status set_gyr_range(ICM20948_gyroRange gyroRange);
218+
hal::status set_gyr_DLPF(ICM20948_dlpf p_dlpf);
219+
hal::status set_gyr_sample_rate_div(hal::byte p_gyrSplRateDiv);
220+
hal::status set_temp_DLPF(ICM20948_dlpf p_dlpf);
221+
222+
/* Power, Sleep, Standby */
223+
hal::status enable_cycle(ICM20948_cycle p_cycle);
224+
hal::status enable_low_power(bool p_enLP);
225+
hal::status set_gyr_Averg_cycle_mode(ICM20948_gyroAvgLowPower p_avg);
226+
hal::status set_acc_Averg_cycle_mode(ICM20948_accAvgLowPower p_avg);
227+
hal::status sleep(bool p_sleep);
228+
229+
/* Magnetometer */
230+
hal::status init_mag();
231+
[[nodiscard]] hal::result<hal::byte> whoami_mag();
232+
void set_mag_op_mode(AK09916_opMode p_opMode);
233+
void reset_mag();
250234

251235
private:
252236
hal::i2c* m_i2c;
253237
hal::byte m_address;
254238
hal::byte m_gscale = 0x00;
255-
hal::byte currentBank;
256-
accel_read_t accOffsetVal;
257-
accel_read_t accCorrFactor;
258-
gyro_read_t gyrOffsetVal;
259-
hal::byte accRangeFactor;
260-
hal::byte gyrRangeFactor;
261-
hal::byte regVal; // intermediate storage of register values
239+
hal::byte m_currentBank;
240+
accel_read_t m_accOffsetVal;
241+
accel_read_t m_accCorrFactor;
242+
gyro_read_t m_gyrOffsetVal;
243+
hal::byte m_accRangeFactor;
244+
hal::byte m_gyrRangeFactor;
245+
hal::byte m_regVal; // intermediate storage of register values
262246

263247
explicit icm20948(hal::i2c& p_i2c, hal::byte p_device_address)
264248
: m_i2c(&p_i2c)
265249
, m_address(p_device_address)
266250
{
267251
}
268252

269-
hal::status setClockToAutoSelect();
270-
hal::status switchBank(hal::byte newBank);
271-
hal::status writeRegister8(hal::byte bank, hal::byte reg, hal::byte val);
272-
hal::status writeRegister16(hal::byte bank, hal::byte reg, int16_t val);
253+
hal::status set_clock_auto_select();
254+
hal::status switch_bank(hal::byte p_newBank);
255+
hal::status write_register8(hal::byte p_bank, hal::byte p_reg, hal::byte p_val);
256+
hal::status write_register16(hal::byte p_bank, hal::byte reg, int16_t p_val);
273257

274-
hal::result<hal::byte> readRegister8(hal::byte bank, hal::byte reg);
275-
hal::result<hal::byte> readRegister16(hal::byte bank, hal::byte reg);
258+
[[nodiscard]] hal::result<hal::byte> read_register8(hal::byte p_bank, hal::byte p_reg);
259+
[[nodiscard]] hal::result<hal::byte> read_register16(hal::byte p_bank, hal::byte p_reg);
276260

277-
hal::status readAllData(std::array<hal::byte, 20>& data);
278-
hal::status writeAK09916Register8(hal::byte reg, hal::byte val);
279-
hal::result<hal::byte> readAK09916Register8(hal::byte reg);
280-
hal::result<int16_t> readAK09916Register16(hal::byte reg);
261+
hal::status write_AK09916_register8(hal::byte reg, hal::byte p_val);
262+
[[nodiscard]] hal::result<hal::byte> read_AK09916_register8(hal::byte p_reg);
263+
[[nodiscard]] hal::result<int16_t> read_AK09916_register16(hal::byte p_reg);
281264

282-
hal::status reset_ICM20948();
283-
hal::status enableI2CMaster();
265+
hal::status reset_icm20948();
266+
hal::status enable_i2c_host();
284267

285-
hal::status enableMagDataRead(hal::byte reg, hal::byte bytes);
268+
hal::status enable_mag_data_read(hal::byte p_reg, hal::byte p_bytes);
286269
};
287270

288271
} // namespace hal::icm

include/libhal-icm/xyzFloat.hpp

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)