Skip to content

Commit a9ad59a

Browse files
authored
Merge pull request #32 from sparkfun/Multiple_SPI_Tests
Correcting printScaledAGMT. Adding multiple-SPI example.
2 parents 3935877 + 30b34c3 commit a9ad59a

File tree

6 files changed

+281
-56
lines changed

6 files changed

+281
-56
lines changed

examples/Arduino/Example1_Basics/Example1_Basics.ino

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ void loop() {
6969
if( myICM.dataReady() ){
7070
myICM.getAGMT(); // The values are only updated when you call 'getAGMT'
7171
// printRawAGMT( myICM.agmt ); // Uncomment this to see the raw values, taken directly from the agmt structure
72-
printScaledAGMT( myICM.agmt); // This function takes into account the sclae settings from when the measurement was made to calculate the values with units
72+
printScaledAGMT( &myICM ); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
7373
delay(30);
7474
}else{
75-
Serial.println("Waiting for data");
75+
SERIAL_PORT.println("Waiting for data");
7676
delay(500);
7777
}
7878

@@ -152,27 +152,31 @@ void printFormattedFloat(float val, uint8_t leading, uint8_t decimals){
152152
}
153153
}
154154

155-
void printScaledAGMT( ICM_20948_AGMT_t agmt){
155+
#ifdef USE_SPI
156+
void printScaledAGMT( ICM_20948_SPI *sensor ){
157+
#else
158+
void printScaledAGMT( ICM_20948_I2C *sensor ){
159+
#endif
156160
SERIAL_PORT.print("Scaled. Acc (mg) [ ");
157-
printFormattedFloat( myICM.accX(), 5, 2 );
161+
printFormattedFloat( sensor->accX(), 5, 2 );
158162
SERIAL_PORT.print(", ");
159-
printFormattedFloat( myICM.accY(), 5, 2 );
163+
printFormattedFloat( sensor->accY(), 5, 2 );
160164
SERIAL_PORT.print(", ");
161-
printFormattedFloat( myICM.accZ(), 5, 2 );
165+
printFormattedFloat( sensor->accZ(), 5, 2 );
162166
SERIAL_PORT.print(" ], Gyr (DPS) [ ");
163-
printFormattedFloat( myICM.gyrX(), 5, 2 );
167+
printFormattedFloat( sensor->gyrX(), 5, 2 );
164168
SERIAL_PORT.print(", ");
165-
printFormattedFloat( myICM.gyrY(), 5, 2 );
169+
printFormattedFloat( sensor->gyrY(), 5, 2 );
166170
SERIAL_PORT.print(", ");
167-
printFormattedFloat( myICM.gyrZ(), 5, 2 );
171+
printFormattedFloat( sensor->gyrZ(), 5, 2 );
168172
SERIAL_PORT.print(" ], Mag (uT) [ ");
169-
printFormattedFloat( myICM.magX(), 5, 2 );
173+
printFormattedFloat( sensor->magX(), 5, 2 );
170174
SERIAL_PORT.print(", ");
171-
printFormattedFloat( myICM.magY(), 5, 2 );
175+
printFormattedFloat( sensor->magY(), 5, 2 );
172176
SERIAL_PORT.print(", ");
173-
printFormattedFloat( myICM.magZ(), 5, 2 );
177+
printFormattedFloat( sensor->magZ(), 5, 2 );
174178
SERIAL_PORT.print(" ], Tmp (C) [ ");
175-
printFormattedFloat( myICM.temp(), 5, 2 );
179+
printFormattedFloat( sensor->temp(), 5, 2 );
176180
SERIAL_PORT.print(" ]");
177181
SERIAL_PORT.println();
178182
}

examples/Arduino/Example2_Advanced/Example2_Advanced.ino

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#define SERIAL_PORT Serial
1717

1818
#define SPI_PORT SPI // Your desired SPI port. Used only when "USE_SPI" is defined
19-
#define SPI_FREQ 10000000// You can override the default SPI frequency
19+
#define SPI_FREQ 5000000// You can override the default SPI frequency
2020
#define CS_PIN 2 // Which pin you connect CS to. Used only when "USE_SPI" is defined
2121

2222
#define WIRE_PORT Wire // Your desired Wire port. Used when "USE_SPI" is not defined
@@ -146,6 +146,13 @@ void setup() {
146146
SERIAL_PORT.print(F("Enable DLPF for Accelerometer returned: ")); SERIAL_PORT.println(myICM.statusString(accDLPEnableStat));
147147
SERIAL_PORT.print(F("Enable DLPF for Gyroscope returned: ")); SERIAL_PORT.println(myICM.statusString(gyrDLPEnableStat));
148148

149+
// Choose whether or not to start the magnetometer
150+
myICM.startupMagnetometer();
151+
if( myICM.status != ICM_20948_Stat_Ok){
152+
SERIAL_PORT.print(F("startupMagnetometer returned: "));
153+
SERIAL_PORT.println(myICM.statusString());
154+
}
155+
149156
SERIAL_PORT.println();
150157
SERIAL_PORT.println(F("Configuration complete!"));
151158
}
@@ -155,10 +162,10 @@ void loop() {
155162
if( myICM.dataReady() ){
156163
myICM.getAGMT(); // The values are only updated when you call 'getAGMT'
157164
// printRawAGMT( myICM.agmt ); // Uncomment this to see the raw values, taken directly from the agmt structure
158-
printScaledAGMT( myICM.agmt); // This function takes into account the sclae settings from when the measurement was made to calculate the values with units
165+
printScaledAGMT( &myICM ); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
159166
delay(30);
160167
}else{
161-
Serial.println("Waiting for data");
168+
SERIAL_PORT.println("Waiting for data");
162169
delay(500);
163170
}
164171

@@ -209,7 +216,6 @@ void printRawAGMT( ICM_20948_AGMT_t agmt){
209216
SERIAL_PORT.println();
210217
}
211218

212-
213219
void printFormattedFloat(float val, uint8_t leading, uint8_t decimals){
214220
float aval = abs(val);
215221
if(val < 0){
@@ -238,27 +244,31 @@ void printFormattedFloat(float val, uint8_t leading, uint8_t decimals){
238244
}
239245
}
240246

241-
void printScaledAGMT( ICM_20948_AGMT_t agmt){
247+
#ifdef USE_SPI
248+
void printScaledAGMT( ICM_20948_SPI *sensor ){
249+
#else
250+
void printScaledAGMT( ICM_20948_I2C *sensor ){
251+
#endif
242252
SERIAL_PORT.print("Scaled. Acc (mg) [ ");
243-
printFormattedFloat( myICM.accX(), 5, 2 );
253+
printFormattedFloat( sensor->accX(), 5, 2 );
244254
SERIAL_PORT.print(", ");
245-
printFormattedFloat( myICM.accY(), 5, 2 );
255+
printFormattedFloat( sensor->accY(), 5, 2 );
246256
SERIAL_PORT.print(", ");
247-
printFormattedFloat( myICM.accZ(), 5, 2 );
257+
printFormattedFloat( sensor->accZ(), 5, 2 );
248258
SERIAL_PORT.print(" ], Gyr (DPS) [ ");
249-
printFormattedFloat( myICM.gyrX(), 5, 2 );
259+
printFormattedFloat( sensor->gyrX(), 5, 2 );
250260
SERIAL_PORT.print(", ");
251-
printFormattedFloat( myICM.gyrY(), 5, 2 );
261+
printFormattedFloat( sensor->gyrY(), 5, 2 );
252262
SERIAL_PORT.print(", ");
253-
printFormattedFloat( myICM.gyrZ(), 5, 2 );
263+
printFormattedFloat( sensor->gyrZ(), 5, 2 );
254264
SERIAL_PORT.print(" ], Mag (uT) [ ");
255-
printFormattedFloat( myICM.magX(), 5, 2 );
265+
printFormattedFloat( sensor->magX(), 5, 2 );
256266
SERIAL_PORT.print(", ");
257-
printFormattedFloat( myICM.magY(), 5, 2 );
267+
printFormattedFloat( sensor->magY(), 5, 2 );
258268
SERIAL_PORT.print(", ");
259-
printFormattedFloat( myICM.magZ(), 5, 2 );
269+
printFormattedFloat( sensor->magZ(), 5, 2 );
260270
SERIAL_PORT.print(" ], Tmp (C) [ ");
261-
printFormattedFloat( myICM.temp(), 5, 2 );
271+
printFormattedFloat( sensor->temp(), 5, 2 );
262272
SERIAL_PORT.print(" ]");
263273
SERIAL_PORT.println();
264274
}

examples/Arduino/Example3_Interrupts/Example3_Interrupts.ino

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ void setup() {
169169
SERIAL_PORT.print(F("Enable DLPF for Accelerometer returned: ")); SERIAL_PORT.println(myICM.statusString(accDLPEnableStat));
170170
SERIAL_PORT.print(F("Enable DLPF for Gyroscope returned: ")); SERIAL_PORT.println(myICM.statusString(gyrDLPEnableStat));
171171

172+
// Choose whether or not to start the magnetometer
173+
myICM.startupMagnetometer();
174+
if( myICM.status != ICM_20948_Stat_Ok){
175+
SERIAL_PORT.print(F("startupMagnetometer returned: "));
176+
SERIAL_PORT.println(myICM.statusString());
177+
}
178+
172179
// Now we're going to set up interrupts. There are a lot of options, but for this test we're just configuring the interrupt pin and enabling interrupts to tell us when new data is ready
173180
/*
174181
ICM_20948_Status_e cfgIntActiveLow ( bool active_low );
@@ -203,7 +210,7 @@ void setup() {
203210
// // Note: weirdness with the Wake on Motion interrupt being always enabled.....
204211
// uint8_t zero_0 = 0xFF;
205212
// ICM_20948_execute_r( &myICM._device, AGB0_REG_INT_ENABLE, (uint8_t*)&zero_0, sizeof(uint8_t) );
206-
// Serial.print("INT_EN was: 0x"); Serial.println(zero_0, HEX);
213+
// SERIAL_PORT.print("INT_EN was: 0x"); SERIAL_PORT.println(zero_0, HEX);
207214
// zero_0 = 0x00;
208215
// ICM_20948_execute_w( &myICM._device, AGB0_REG_INT_ENABLE, (uint8_t*)&zero_0, sizeof(uint8_t) );
209216

@@ -215,6 +222,7 @@ void loop() {
215222
if( isrFired ){ // If our isr flag is set then clear the interrupts on the ICM
216223
isrFired = false;
217224
myICM.getAGMT(); // get the A, G, M, and T readings
225+
printScaledAGMT( &myICM ); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
218226
// myICM.clearInterrupts(); // This would be efficient... but not compatible with Uno
219227
}
220228

@@ -321,27 +329,31 @@ void printFormattedFloat(float val, uint8_t leading, uint8_t decimals){
321329
}
322330
}
323331

324-
void printScaledAGMT( ICM_20948_AGMT_t agmt){
332+
#ifdef USE_SPI
333+
void printScaledAGMT( ICM_20948_SPI *sensor ){
334+
#else
335+
void printScaledAGMT( ICM_20948_I2C *sensor ){
336+
#endif
325337
SERIAL_PORT.print("Scaled. Acc (mg) [ ");
326-
printFormattedFloat( myICM.accX(), 5, 2 );
338+
printFormattedFloat( sensor->accX(), 5, 2 );
327339
SERIAL_PORT.print(", ");
328-
printFormattedFloat( myICM.accY(), 5, 2 );
340+
printFormattedFloat( sensor->accY(), 5, 2 );
329341
SERIAL_PORT.print(", ");
330-
printFormattedFloat( myICM.accZ(), 5, 2 );
342+
printFormattedFloat( sensor->accZ(), 5, 2 );
331343
SERIAL_PORT.print(" ], Gyr (DPS) [ ");
332-
printFormattedFloat( myICM.gyrX(), 5, 2 );
344+
printFormattedFloat( sensor->gyrX(), 5, 2 );
333345
SERIAL_PORT.print(", ");
334-
printFormattedFloat( myICM.gyrY(), 5, 2 );
346+
printFormattedFloat( sensor->gyrY(), 5, 2 );
335347
SERIAL_PORT.print(", ");
336-
printFormattedFloat( myICM.gyrZ(), 5, 2 );
348+
printFormattedFloat( sensor->gyrZ(), 5, 2 );
337349
SERIAL_PORT.print(" ], Mag (uT) [ ");
338-
printFormattedFloat( myICM.magX(), 5, 2 );
350+
printFormattedFloat( sensor->magX(), 5, 2 );
339351
SERIAL_PORT.print(", ");
340-
printFormattedFloat( myICM.magY(), 5, 2 );
352+
printFormattedFloat( sensor->magY(), 5, 2 );
341353
SERIAL_PORT.print(", ");
342-
printFormattedFloat( myICM.magZ(), 5, 2 );
354+
printFormattedFloat( sensor->magZ(), 5, 2 );
343355
SERIAL_PORT.print(" ], Tmp (C) [ ");
344-
printFormattedFloat( myICM.temp(), 5, 2 );
356+
printFormattedFloat( sensor->temp(), 5, 2 );
345357
SERIAL_PORT.print(" ]");
346358
SERIAL_PORT.println();
347359
}

examples/Arduino/Example4_WakeOnMotion/Example4_WakeOnMotion.ino

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ void setup() {
173173
SERIAL_PORT.print(F("Enable DLPF for Accelerometer returned: ")); SERIAL_PORT.println(myICM.statusString(accDLPEnableStat));
174174
SERIAL_PORT.print(F("Enable DLPF for Gyroscope returned: ")); SERIAL_PORT.println(myICM.statusString(gyrDLPEnableStat));
175175

176+
// Choose whether or not to start the magnetometer
177+
myICM.startupMagnetometer();
178+
if( myICM.status != ICM_20948_Stat_Ok){
179+
SERIAL_PORT.print(F("startupMagnetometer returned: "));
180+
SERIAL_PORT.println(myICM.statusString());
181+
}
182+
176183
// Now we're going to set up interrupts. There are a lot of options, but for this test we're just configuring the interrupt pin and enabling interrupts to tell us when new data is ready
177184
/*
178185
ICM_20948_Status_e cfgIntActiveLow ( bool active_low );
@@ -219,7 +226,7 @@ void loop() {
219226
if( isrFired ){ // If our isr flag is set then clear the interrupts on the ICM
220227
isrFired = false;
221228
myICM.getAGMT(); // get the A, G, M, and T readings
222-
// printScaledAGMT( myICM.agmt); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
229+
// printScaledAGMT( &myICM ); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
223230
SERIAL_PORT.println(F("Shock detected"));
224231
digitalWrite(LED_PIN, HIGH);
225232
lastTriggered = millis();
@@ -320,27 +327,31 @@ void printFormattedFloat(float val, uint8_t leading, uint8_t decimals){
320327
}
321328
}
322329

323-
void printScaledAGMT( ICM_20948_AGMT_t agmt){
330+
#ifdef USE_SPI
331+
void printScaledAGMT( ICM_20948_SPI *sensor ){
332+
#else
333+
void printScaledAGMT( ICM_20948_I2C *sensor ){
334+
#endif
324335
SERIAL_PORT.print("Scaled. Acc (mg) [ ");
325-
printFormattedFloat( myICM.accX(), 5, 2 );
336+
printFormattedFloat( sensor->accX(), 5, 2 );
326337
SERIAL_PORT.print(", ");
327-
printFormattedFloat( myICM.accY(), 5, 2 );
338+
printFormattedFloat( sensor->accY(), 5, 2 );
328339
SERIAL_PORT.print(", ");
329-
printFormattedFloat( myICM.accZ(), 5, 2 );
340+
printFormattedFloat( sensor->accZ(), 5, 2 );
330341
SERIAL_PORT.print(" ], Gyr (DPS) [ ");
331-
printFormattedFloat( myICM.gyrX(), 5, 2 );
342+
printFormattedFloat( sensor->gyrX(), 5, 2 );
332343
SERIAL_PORT.print(", ");
333-
printFormattedFloat( myICM.gyrY(), 5, 2 );
344+
printFormattedFloat( sensor->gyrY(), 5, 2 );
334345
SERIAL_PORT.print(", ");
335-
printFormattedFloat( myICM.gyrZ(), 5, 2 );
346+
printFormattedFloat( sensor->gyrZ(), 5, 2 );
336347
SERIAL_PORT.print(" ], Mag (uT) [ ");
337-
printFormattedFloat( myICM.magX(), 5, 2 );
348+
printFormattedFloat( sensor->magX(), 5, 2 );
338349
SERIAL_PORT.print(", ");
339-
printFormattedFloat( myICM.magY(), 5, 2 );
350+
printFormattedFloat( sensor->magY(), 5, 2 );
340351
SERIAL_PORT.print(", ");
341-
printFormattedFloat( myICM.magZ(), 5, 2 );
352+
printFormattedFloat( sensor->magZ(), 5, 2 );
342353
SERIAL_PORT.print(" ], Tmp (C) [ ");
343-
printFormattedFloat( myICM.temp(), 5, 2 );
354+
printFormattedFloat( sensor->temp(), 5, 2 );
344355
SERIAL_PORT.print(" ]");
345356
SERIAL_PORT.println();
346357
}

0 commit comments

Comments
 (0)