Skip to content

Commit 88488f9

Browse files
committed
Address issue #5
This completes the handling of issue #5, started with commits 32aca21 and 159af65. Instead of returning the actual values through the pointer I changed to returning the values that the user wanted through the pointer. Then the actual .settings member of the object is modified to reflect the current settings. There is now an example to illustrate how to check if the settings have been chnged by the 'begin' method.
1 parent 159af65 commit 88488f9

File tree

3 files changed

+127
-28
lines changed

3 files changed

+127
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/******************************************************************************
2+
MinimalistExample.ino
3+
4+
Owen Lyke @ SparkFun Electronics
5+
March 13, 2019
6+
https://github.com/sparkfun/LSM6DS3_Breakout
7+
https://github.com/sparkfun/SparkFun_LSM6DS3_Arduino_Library
8+
9+
Description:
10+
Most basic example of use - except now you can see if your settings got changed
11+
12+
Example using the LSM6DS3 with basic settings. This sketch collects Gyro and
13+
Accelerometer data every second, then presents it on the serial monitor.
14+
15+
Resources:
16+
Uses Wire.h for i2c operation
17+
Uses SPI.h for SPI operation
18+
Either can be omitted if not used
19+
20+
Development environment specifics:
21+
Arduino IDE 1.6.4
22+
Teensy loader 1.23
23+
24+
Hardware connections:
25+
Connect I2C SDA line to A4
26+
Connect I2C SCL line to A5
27+
Connect GND and 3.3v power to the IMU
28+
29+
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
30+
31+
Please review the LICENSE.md file included with this example. If you have any questions
32+
or concerns with licensing, please contact [email protected].
33+
34+
Distributed as-is; no warranty is given.
35+
******************************************************************************/
36+
37+
#include "SparkFunLSM6DS3.h"
38+
#include "Wire.h"
39+
#include "SPI.h"
40+
41+
LSM6DS3 myIMU; //Default constructor is I2C, addr 0x6B
42+
43+
void setup() {
44+
// put your setup code here, to run once:
45+
Serial.begin(9600);
46+
delay(1000); //relax...
47+
Serial.println("Processor came out of reset.\n");
48+
49+
// Let's choose an unsupported setting...
50+
myIMU.settings.accelSampleRate = 404; // Typo, 'meant' to type '104'
51+
52+
// Make a SensorSettings object to remember what you wanted to set everyhting to
53+
SensorSettings settingsIWanted;
54+
55+
//Call .begin() to configure the IMU - supplying pointer to the SensorSettings structure
56+
myIMU.begin(&settingsIWanted);
57+
58+
// Compare the sensor settings structure to know if anything was changed
59+
compareSettings(settingsIWanted);
60+
61+
}
62+
63+
64+
void loop()
65+
{
66+
//Get all parameters
67+
Serial.print("\nAccelerometer:\n");
68+
Serial.print(" X = ");
69+
Serial.println(myIMU.readFloatAccelX(), 4);
70+
Serial.print(" Y = ");
71+
Serial.println(myIMU.readFloatAccelY(), 4);
72+
Serial.print(" Z = ");
73+
Serial.println(myIMU.readFloatAccelZ(), 4);
74+
75+
Serial.print("\nGyroscope:\n");
76+
Serial.print(" X = ");
77+
Serial.println(myIMU.readFloatGyroX(), 4);
78+
Serial.print(" Y = ");
79+
Serial.println(myIMU.readFloatGyroY(), 4);
80+
Serial.print(" Z = ");
81+
Serial.println(myIMU.readFloatGyroZ(), 4);
82+
83+
Serial.print("\nThermometer:\n");
84+
Serial.print(" Degrees C = ");
85+
Serial.println(myIMU.readTempC(), 4);
86+
Serial.print(" Degrees F = ");
87+
Serial.println(myIMU.readTempF(), 4);
88+
89+
delay(1000);
90+
}
91+
92+
void compareSettings(SensorSettings desiredSettings){
93+
if(myIMU.settings.accelBandWidth != desiredSettings.accelBandWidth ) { Serial.println("'accelBandWidth' was changed!"); }
94+
if(myIMU.settings.accelRange != desiredSettings.accelRange ) { Serial.println("'accelRange' was changed!"); }
95+
if(myIMU.settings.accelSampleRate != desiredSettings.accelSampleRate ) { Serial.println("'accelSampleRate' was changed!"); }
96+
if(myIMU.settings.gyroRange != desiredSettings.gyroRange ) { Serial.println("'gyroRange' was changed!"); }
97+
if(myIMU.settings.gyroSampleRate != desiredSettings.gyroSampleRate ) { Serial.println("'gyroSampleRate' was changed!"); }
98+
// That's all the settings that might get changed in 'begin()'
99+
}

src/SparkFunLSM6DS3.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -388,35 +388,35 @@ LSM6DS3::LSM6DS3( uint8_t busType, uint8_t inputArg ) : LSM6DS3Core( busType, in
388388
// "myIMU.settings.accelEnabled = 1;" to configure before calling .begin();
389389
//
390390
//****************************************************************************//
391-
status_t LSM6DS3::begin(SensorSettings* pActualSettings)
391+
status_t LSM6DS3::begin(SensorSettings* pSettingsYouWanted)
392392
{
393393
//Check the settings structure values to determine how to setup the device
394394
uint8_t dataToWrite = 0; //Temporary variable
395395

396396
//Begin the inherited core. This gets the physical wires connected
397397
status_t returnError = beginCore();
398398

399-
// Copy the values from the user's settings into the output 'pActualSettings'
400-
// that will reflect if any default choices had to be made
401-
if(pActualSettings != NULL){
402-
pActualSettings->gyroEnabled = settings.gyroEnabled;
403-
pActualSettings->gyroRange = settings.gyroRange;
404-
pActualSettings->gyroSampleRate = settings.gyroSampleRate;
405-
pActualSettings->gyroBandWidth = settings.gyroBandWidth;
406-
pActualSettings->gyroFifoEnabled = settings.gyroFifoEnabled;
407-
pActualSettings->gyroFifoDecimation = settings.gyroFifoDecimation;
408-
pActualSettings->accelEnabled = settings.accelEnabled;
409-
pActualSettings->accelODROff = settings.accelODROff;
410-
pActualSettings->accelRange = settings.accelRange;
411-
pActualSettings->accelSampleRate = settings.accelSampleRate;
412-
pActualSettings->accelBandWidth = settings.accelBandWidth;
413-
pActualSettings->accelFifoEnabled = settings.accelFifoEnabled;
414-
pActualSettings->accelFifoDecimation = settings.accelFifoDecimation;
415-
pActualSettings->tempEnabled = settings.tempEnabled;
416-
pActualSettings->commMode = settings.commMode;
417-
pActualSettings->fifoThreshold = settings.fifoThreshold;
418-
pActualSettings->fifoSampleRate = settings.fifoSampleRate;
419-
pActualSettings->fifoModeWord = settings.fifoModeWord;
399+
// Copy the values from the user's settings into the output 'pSettingsYouWanted'
400+
// compare settings with 'pSettingsYouWanted' after 'begin' to see if anything changed
401+
if(pSettingsYouWanted != NULL){
402+
pSettingsYouWanted->gyroEnabled = settings.gyroEnabled;
403+
pSettingsYouWanted->gyroRange = settings.gyroRange;
404+
pSettingsYouWanted->gyroSampleRate = settings.gyroSampleRate;
405+
pSettingsYouWanted->gyroBandWidth = settings.gyroBandWidth;
406+
pSettingsYouWanted->gyroFifoEnabled = settings.gyroFifoEnabled;
407+
pSettingsYouWanted->gyroFifoDecimation = settings.gyroFifoDecimation;
408+
pSettingsYouWanted->accelEnabled = settings.accelEnabled;
409+
pSettingsYouWanted->accelODROff = settings.accelODROff;
410+
pSettingsYouWanted->accelRange = settings.accelRange;
411+
pSettingsYouWanted->accelSampleRate = settings.accelSampleRate;
412+
pSettingsYouWanted->accelBandWidth = settings.accelBandWidth;
413+
pSettingsYouWanted->accelFifoEnabled = settings.accelFifoEnabled;
414+
pSettingsYouWanted->accelFifoDecimation = settings.accelFifoDecimation;
415+
pSettingsYouWanted->tempEnabled = settings.tempEnabled;
416+
pSettingsYouWanted->commMode = settings.commMode;
417+
pSettingsYouWanted->fifoThreshold = settings.fifoThreshold;
418+
pSettingsYouWanted->fifoSampleRate = settings.fifoSampleRate;
419+
pSettingsYouWanted->fifoModeWord = settings.fifoModeWord;
420420
}
421421

422422
//Setup the accelerometer******************************
@@ -435,7 +435,7 @@ status_t LSM6DS3::begin(SensorSettings* pActualSettings)
435435
dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_200Hz;
436436
break;
437437
default: //set default case to max passthrough
438-
if(pActualSettings != NULL){ pActualSettings->accelBandWidth = 400; }
438+
settings.accelEnabled = 400;
439439
case 400:
440440
dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_400Hz;
441441
break;
@@ -452,7 +452,7 @@ status_t LSM6DS3::begin(SensorSettings* pActualSettings)
452452
dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_8g;
453453
break;
454454
default: //set default case to 16(max)
455-
if(pActualSettings != NULL){ pActualSettings->accelRange = 16; }
455+
settings.accelRange = 16;
456456
case 16:
457457
dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_16g;
458458
break;
@@ -469,7 +469,7 @@ status_t LSM6DS3::begin(SensorSettings* pActualSettings)
469469
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_52Hz;
470470
break;
471471
default: //Set default to 104
472-
if(pActualSettings != NULL){ pActualSettings->accelSampleRate = 104; }
472+
settings.accelSampleRate = 104;
473473
case 104:
474474
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_104Hz;
475475
break;
@@ -531,7 +531,7 @@ status_t LSM6DS3::begin(SensorSettings* pActualSettings)
531531
dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_1000dps;
532532
break;
533533
default: //Default to full 2000DPS range
534-
if(pActualSettings != NULL){ pActualSettings->gyroRange = 2000; }
534+
settings.gyroRange = 2000;
535535
case 2000:
536536
dataToWrite |= LSM6DS3_ACC_GYRO_FS_G_2000dps;
537537
break;
@@ -548,7 +548,7 @@ status_t LSM6DS3::begin(SensorSettings* pActualSettings)
548548
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_52Hz;
549549
break;
550550
default: //Set default to 104
551-
if(pActualSettings != NULL){ pActualSettings->gyroSampleRate = gyroSampleRate; }
551+
settings.gyroSampleRate = 104;
552552
case 104:
553553
dataToWrite |= LSM6DS3_ACC_GYRO_ODR_G_104Hz;
554554
break;

src/SparkFunLSM6DS3.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class LSM6DS3 : public LSM6DS3Core
149149
~LSM6DS3() = default;
150150

151151
//Call to apply SensorSettings
152-
status_t begin(SensorSettings* pActualSettings = NULL);
152+
status_t begin(SensorSettings* pSettingsYouWanted = NULL);
153153

154154
//Returns the raw bits from the sensor cast as 16-bit signed integers
155155
int16_t readRawAccelX( void );

0 commit comments

Comments
 (0)