Skip to content

Commit 9b3acca

Browse files
committed
added method to get/set the current spad map; added example that uses those methods
1 parent 208f074 commit 9b3acca

File tree

3 files changed

+239
-74
lines changed

3 files changed

+239
-74
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
TODO - Fix Header
3+
*/
4+
5+
#include <SparkFun_TMF882X_Library.h>
6+
7+
8+
9+
static struct tmf882x_msg_meas_results myResults;
10+
11+
12+
SparkFun_TMF882X myTMF882X;
13+
14+
#define NEW_SPAD_MAP 2
15+
16+
void setup(){
17+
18+
delay(1000);
19+
Serial.begin(115200);
20+
Serial.println("");
21+
22+
Serial.println("In setup");
23+
Serial.println("==============================");
24+
25+
if(!myTMF882X.begin())
26+
{
27+
Serial.println("Error - The TMF882X failed to initialize - is the board connected?");
28+
while(1){}
29+
}else
30+
Serial.println("TMF882X started.");
31+
32+
33+
// Let's change the SPAD map in use on this device.
34+
//
35+
// Get the current SPAD Map ID
36+
int spadMap = myTMF882X.getCurrentSPADMap();
37+
Serial.println();
38+
Serial.print("Current SPAD Map ID: ");
39+
Serial.println(spadMap);
40+
41+
// Now switch
42+
Serial.println("Switching SPAD Map to ID 2 - 3x3 Macro 1 off center");
43+
Serial.println();
44+
45+
if (!myTMF882X.setCurrentSPADMap(NEW_SPAD_MAP))
46+
{
47+
Serial.println("Error - Failed to set the SPAD Map - halting");
48+
while(1){}
49+
}
50+
51+
// Let's make sure it worked
52+
spadMap = myTMF882X.getCurrentSPADMap();
53+
54+
if(spadMap != NEW_SPAD_MAP)
55+
{
56+
Serial.println("Error - Failed to set the SPAD Map - halting");
57+
while(1){}
58+
}
59+
60+
Serial.print("The new SPAD Map ID: ");
61+
Serial.println(spadMap);
62+
63+
// First set some config parameters to support the spad map
64+
struct tmf882x_mode_app_config tofConfig;
65+
if (!myTMF882X.getTMF882XConfig(tofConfig)) {
66+
Serial.println("Error - unable to get device configuration.");
67+
while(1){}
68+
}
69+
70+
// Change the APP configuration
71+
// - set the reporting period to 500 milliseconds
72+
tofConfig.report_period_ms = 500;
73+
74+
if (!myTMF882X.setTMF882XConfig(tofConfig)) {
75+
Serial.println("Error - unable to set device configuration.");
76+
while(1){}
77+
}
78+
79+
}
80+
81+
void loop()
82+
{
83+
delay(2000);
84+
85+
// get a myResultsurment
86+
if(myTMF882X.startMeasuring(myResults))
87+
{
88+
// print out results
89+
Serial.println("Measurement:");
90+
Serial.print(" Result Number: "); Serial.print(myResults.result_num);
91+
Serial.print(" Number of Results: "); Serial.println(myResults.num_results);
92+
93+
for (uint32_t i = 0; i < myResults.num_results; ++i) {
94+
Serial.print(" conf: "); Serial.print(myResults.results[i].confidence);
95+
Serial.print(" distance mm: "); Serial.print(myResults.results[i].distance_mm);
96+
Serial.print(" channel: "); Serial.print(myResults.results[i].channel);
97+
Serial.print(" sub_capture: "); Serial.println(myResults.results[i].sub_capture);
98+
99+
}
100+
Serial.print(" photon: "); Serial.print(myResults.photon_count);
101+
Serial.print(" ref photon: "); Serial.print(myResults.ref_photon_count);
102+
Serial.print(" ALS: "); Serial.println(myResults.ambient_light); Serial.println();
103+
104+
}
105+
}

src/qwiic_tmf882x.cpp

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
4545
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4646

47-
4847
#include <stdint.h>
4948
#include <stdio.h>
5049
#include <stdlib.h>
@@ -103,7 +102,7 @@ bool QwDevTMF882X::initializeTMF882x(void)
103102
//////////////////////////////////////////////////////////////////////////////
104103
// loadFirmware()
105104
//
106-
// Loads the provided firmware array into the connected device.
105+
// Loads the provided firmware array into the connected device.
107106
//
108107
bool QwDevTMF882X::loadFirmware(const unsigned char *firmwareBinImage, unsigned long length)
109108
{
@@ -178,7 +177,7 @@ bool QwDevTMF882X::setI2CAddress(uint8_t address)
178177
{
179178
// Initialized? Is the address legal?
180179
if (!_isInitialized || address < 0x08 || address > 0x77)
181-
false;
180+
false;
182181

183182
// is the address the same as already set?
184183
if (address == _i2cAddress)
@@ -200,15 +199,14 @@ bool QwDevTMF882X::setI2CAddress(uint8_t address)
200199

201200
// Now tell the device to switch to the address in the config page.
202201
uint8_t cmdCode = TMF8X2X_COM_CMD_STAT__cmd_stat__CMD_I2C_SLAVE_ADDRESS;
203-
if ( _i2cBus->writeRegisterRegion(_i2cAddress, TMF8X2X_COM_CMD_STAT, &cmdCode, sizeof(uint8_t)) )
202+
if (_i2cBus->writeRegisterRegion(_i2cAddress, TMF8X2X_COM_CMD_STAT, &cmdCode, sizeof(uint8_t)))
204203
return false;
205204

206-
// Potential TODO - check status register... .. need to test.
205+
// Potential TODO - check status register... .. need to test.
207206

208-
// If we are here, the address should of been changed
207+
// If we are here, the address should of been changed
209208
_i2cAddress = address;
210209
return true;
211-
212210
}
213211
//////////////////////////////////////////////////////////////////////////////
214212
//
@@ -353,8 +351,7 @@ int QwDevTMF882X::measurementLoop(uint16_t reqMeasurements, uint32_t timeout)
353351
_nMeasurements = 0; // internal counter
354352

355353
// if you want to measure forever, you need CB function, or a timeout set
356-
if (reqMeasurements == 0 &&
357-
!(_measurementHandlerCB || _histogramHandlerCB || _messageHandlerCB || timeout) )
354+
if (reqMeasurements == 0 && !(_measurementHandlerCB || _histogramHandlerCB || _messageHandlerCB || timeout))
358355
return -1;
359356

360357
if (tmf882x_start(&_TOF))
@@ -406,7 +403,7 @@ int32_t QwDevTMF882X::sdkMessageHandler(struct tmf882x_msg *msg)
406403
return false;
407404

408405
// Do we have a general handler set
409-
if(_messageHandlerCB)
406+
if (_messageHandlerCB)
410407
_messageHandlerCB(msg);
411408

412409
// Check the message type - call type handler methods if we have one
@@ -433,12 +430,12 @@ int32_t QwDevTMF882X::sdkMessageHandler(struct tmf882x_msg *msg)
433430
if (_statsHandlerCB)
434431
_statsHandlerCB(&msg->meas_stat_msg);
435432
break;
436-
433+
437434
case ID_ERROR:
438435

439436
if (_errorHandlerCB)
440437
_errorHandlerCB(&msg->err_msg);
441-
break;
438+
break;
442439

443440
default:
444441
break;
@@ -447,7 +444,6 @@ int32_t QwDevTMF882X::sdkMessageHandler(struct tmf882x_msg *msg)
447444
return 0;
448445
}
449446

450-
451447
//////////////////////////////////////////////////////////////////////////////
452448
// setMeasurementHandler()
453449
//
@@ -541,6 +537,47 @@ bool QwDevTMF882X::setTMF882XConfig(struct tmf882x_mode_app_config &tofConfig)
541537
return false;
542538
return true;
543539
}
540+
541+
////////////////////////////////////////////////////////////////////////////////////
542+
// getCurrentSPADMap()
543+
//
544+
// Returns the ID of the current SPAD in use on the device
545+
//
546+
// Returns 0 on error, > 0 is a valid SPAD map ID.
547+
//
548+
// See the datasheet for SPAD map ID values
549+
550+
uint8_t QwDevTMF882X::getCurrentSPADMap(void)
551+
{
552+
struct tmf882x_mode_app_config tofConfig;
553+
554+
if (!getTMF882XConfig(tofConfig))
555+
return 0;
556+
557+
return tofConfig.spad_map_id;
558+
}
559+
560+
////////////////////////////////////////////////////////////////////////////////////
561+
// setCurrentSPADMap()
562+
//
563+
// Change the current SPAD Map used by the device.
564+
//
565+
// Returns true on success, false on error
566+
//
567+
// See the datasheet for SPAD map ID values
568+
569+
bool QwDevTMF882X::setCurrentSPADMap(uint8_t idSPAD)
570+
{
571+
struct tmf882x_mode_app_config tofConfig;
572+
573+
if (!getTMF882XConfig(tofConfig))
574+
return false;
575+
576+
tofConfig.spad_map_id = idSPAD;
577+
578+
return setTMF882XConfig(tofConfig);
579+
}
580+
544581
////////////////////////////////////////////////////////////////////////////////////
545582
// getSPADConfig()
546583
//
@@ -589,7 +626,7 @@ bool QwDevTMF882X::setSPADConfig(struct tmf882x_mode_app_spad_config &spadConfig
589626

590627
void QwDevTMF882X::setCommBus(QwI2C &theBus, uint8_t idBus)
591628
{
592-
_i2cBus = &theBus;
629+
_i2cBus = &theBus;
593630
_i2cAddress = idBus;
594631
}
595632

0 commit comments

Comments
 (0)