Skip to content

Commit 7139b60

Browse files
committed
Add self test example
1 parent 8140c25 commit 7139b60

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "SparkFun_Qwiic_OTOS_Arduino_Library.h"
2+
#include "Wire.h"
3+
4+
// Create an OTOS object
5+
QwiicOTOS myOtos;
6+
7+
void setup()
8+
{
9+
// Start serial
10+
Serial.begin(115200);
11+
Serial.println("Qwiic OTOS Example 8 - Self Test");
12+
13+
Wire.begin();
14+
15+
// Attempt to begin the sensor
16+
while (myOtos.begin() == false)
17+
{
18+
Serial.println("OTOS not connected, check your wiring and I2C address!");
19+
delay(1000);
20+
}
21+
22+
Serial.println("OTOS connected!");
23+
24+
// Perform the self test
25+
sfeTkError_t result = myOtos.selfTest();
26+
27+
// Check if the self test passed
28+
if(result == kSTkErrOk)
29+
{
30+
Serial.println("Self test passed!");
31+
}
32+
else
33+
{
34+
Serial.println("Self test failed!");
35+
}
36+
}
37+
38+
void loop()
39+
{
40+
// Nothing to do in this example
41+
}

src/sfeQwiicOtos.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,37 @@ sfeTkError_t sfeQwiicOtos::getVersionInfo(otos_version_t &hwVersion, otos_versio
7373
return kSTkErrOk;
7474
}
7575

76+
sfeTkError_t sfeQwiicOtos::selfTest()
77+
{
78+
// Write the self-test register to start the test
79+
sfe_otos_config_self_test_t selfTest;
80+
selfTest.start = 1;
81+
sfeTkError_t err = _commBus->writeRegisterByte(kOtosRegSelfTest, selfTest.value);
82+
if(err != kSTkErrOk)
83+
return err;
84+
85+
// Loop until self-test is done, should only take ~20ms as of firmware v1.0
86+
for(int i = 0; i < 10; i++)
87+
{
88+
// Give a short delay between reads
89+
delayMs(5);
90+
91+
// Read the self-test register
92+
err = _commBus->readRegisterByte(kOtosRegSelfTest, selfTest.value);
93+
if(err != kSTkErrOk)
94+
return err;
95+
96+
// Check if the self-test is done
97+
if(selfTest.inProgress == 0)
98+
{
99+
break;
100+
}
101+
}
102+
103+
// Check if the self-test passed
104+
return (selfTest.pass == 1) ? kSTkErrOk : kSTkErrFail;
105+
}
106+
76107
sfeTkError_t sfeQwiicOtos::calibrateImu(uint8_t numSamples, bool waitUntilDone)
77108
{
78109
// Write the number of samples to the device

src/sfeQwiicOtos.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const uint8_t kOtosRegScalarXY = 0x04;
1515
const uint8_t kOtosRegScalarH = 0x05;
1616
const uint8_t kOtosRegImuCalib = 0x06;
1717
const uint8_t kOtosRegReset = 0x07;
18-
const uint8_t kOtosRegSelfTest = 0x0E;
18+
const uint8_t kOtosRegSelfTest = 0x0F;
1919

2020
const uint8_t kOtosRegOffXL = 0x10;
2121
const uint8_t kOtosRegOffXH = 0x11;
@@ -126,6 +126,19 @@ typedef union {
126126
uint8_t value;
127127
} otos_version_t;
128128

129+
// Self test register bit fields
130+
typedef union {
131+
struct
132+
{
133+
uint8_t start : 1;
134+
uint8_t inProgress : 1;
135+
uint8_t pass : 1;
136+
uint8_t fail : 1;
137+
uint8_t reserved : 4;
138+
};
139+
uint8_t value;
140+
} sfe_otos_config_self_test_t;
141+
129142
class sfeQwiicOtos
130143
{
131144
public:
@@ -143,6 +156,8 @@ class sfeQwiicOtos
143156

144157
sfeTkError_t getVersionInfo(otos_version_t &hwVersion, otos_version_t &fwVersion);
145158

159+
sfeTkError_t selfTest();
160+
146161
sfeTkError_t calibrateImu(uint8_t numSamples = 255, bool waitUntilDone = true);
147162

148163
otos_linear_unit_t getLinearUnit();

0 commit comments

Comments
 (0)