Skip to content

Commit 67de9f2

Browse files
Now contains functional writeRegister code
See function setPresenceStart() for correct implementation of this
1 parent 700fac3 commit 67de9f2

File tree

4 files changed

+204
-10
lines changed

4 files changed

+204
-10
lines changed

examples/Example04_PresenceBasicReadings/Example04_PresenceBasicReadings.ino

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,76 @@ void setup()
5353
while(1); // Runs forever
5454
}
5555

56-
// uint32_t regVal;
57-
// int32_t retVal;
58-
// retVal = radarSensor.returnRegister(&regVal);
59-
// Serial.println(regVal, HEX);
60-
// Serial.println(retVal);
56+
delay(200);
57+
58+
int32_t sensorStartError = radarSensor.presenceDetectorStart();
59+
Serial.println("here");
60+
if(sensorStartError != 0)
61+
{
62+
Serial.println("Sensor Started Successfully");
63+
}
64+
else
65+
{
66+
Serial.print("Error upon Setup: ");
67+
Serial.println(sensorStartError);
68+
}
69+
70+
// // Set Start to 1000mm
71+
// if(radarSensor.setPresenceStart(1000) != 0)
72+
// {
73+
// Serial.println("Presence Start Error");
74+
// }
75+
// else
76+
// {
77+
// Serial.println("Presence Start Set: 1000");
78+
// }
79+
80+
// delay(500);
81+
82+
// // Set end at 5000mm
83+
// if(radarSensor.setPresenceEnd(5000) != 0)
84+
// {
85+
// Serial.println("Presence End Error");
86+
// }
87+
// else
88+
// {
89+
// Serial.println("Presence End Set: 5000");
90+
// }
91+
92+
// // Apply configuration
93+
// if(radarSensor.setPresenceCommand(XM125_PRESENCE_APPLY_CONFIGURATION) != 0)
94+
// {
95+
// Serial.println("Presence Command Error");
96+
// }
97+
// else
98+
// {
99+
// Serial.println("Presence Configuration applied to device");
100+
// }
101+
102+
// // Wait for configuration to be done
103+
// if(radarSensor.presenceBusyWait() != 0)
104+
// {
105+
// Serial.println("Device is busy");
106+
// }
107+
108+
// // Start detector -- mask 0x02 with Presence Reg command address
109+
// if(radarSensor.startPresenceDetector() != 0)
110+
// {
111+
// Serial.println("Presence Detector Start Error");
112+
// }
113+
// else
114+
// {
115+
// Serial.println("Presence Detector Started!");
116+
// }
117+
118+
// // Wait for configuration to be done
119+
// if(radarSensor.presenceBusyWait() != 0)
120+
// {
121+
// Serial.println("Device is busy");
122+
// }
123+
124+
Serial.println("Presence Setup Complete");
61125

62-
delay(2000);
63126

64127
}
65128

@@ -81,5 +144,8 @@ void loop()
81144
Serial.println("mm");
82145
}
83146

147+
148+
149+
84150
delay(100);
85151
}

src/sfeQwiicXM125.cpp

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool QwDevXM125::begin(sfeTkII2C *theBus)
4545
}
4646

4747

48-
/*Test functions below - delete once complete with library */
48+
/* ************Test functions below - delete once complete with library************ */
4949
int32_t QwDevXM125::returnRegister(uint32_t *regVal)
5050
{
5151
return _theBus->read16BitRegisterRegion(SFE_XM125_DISTANCE_VERSION, (uint8_t*)regVal, 4);
@@ -407,6 +407,66 @@ int32_t QwDevXM125::setDistanceCommand(sfe_xm125_distance_command_t *command)
407407

408408
// --------------------- I2C Presence Detector Functions ---------------------
409409

410+
int32_t QwDevXM125::presenceDetectorStart()
411+
{
412+
Serial.println("Presence Sensor Start function");
413+
// Set Start to 1000mm
414+
if(setPresenceStart(1000) != 0)
415+
{
416+
return -1;
417+
}
418+
else
419+
{
420+
Serial.println("Presence Start Set: 1000");
421+
}
422+
423+
//delay(500);
424+
Serial.println("Presence Sensor End function");
425+
// Set end at 5000mm
426+
if(setPresenceEnd(5000) != 0)
427+
{
428+
return -2;
429+
}
430+
else
431+
{
432+
Serial.println("Presence End Set: 5000");
433+
}
434+
435+
// Apply configuration
436+
if(setPresenceCommand(XM125_PRESENCE_APPLY_CONFIGURATION) != 0)
437+
{
438+
return -3;
439+
}
440+
else
441+
{
442+
Serial.println("Presence Configuration applied to device");
443+
}
444+
445+
// Wait for configuration to be done
446+
if(presenceBusyWait() != 0)
447+
{
448+
return -4;
449+
}
450+
451+
// Start detector -- mask 0x02 with Presence Reg command address
452+
if(startPresenceDetector() != 0)
453+
{
454+
return -5;
455+
}
456+
else
457+
{
458+
Serial.println("Presence Detector Started!");
459+
}
460+
461+
// Wait for configuration to be done
462+
if(presenceBusyWait() != 0)
463+
{
464+
return -6;
465+
}
466+
467+
return 0;
468+
}
469+
410470
int32_t QwDevXM125::getPresenceDetectorVersion(uint8_t *major, uint8_t *minor, uint8_t *patch)
411471
{
412472
int32_t retVal;
@@ -676,17 +736,26 @@ int32_t QwDevXM125::getPresenceManualStepLength(uint32_t *length)
676736

677737
int32_t QwDevXM125::setPresenceManualStepLength(uint32_t length)
678738
{
679-
return _theBus->write16BitRegisterRegion(SFE_XM125_PRESENCE_MANUAL_STEP_LENGTH, (uint8_t*)length, 4);
739+
uint8_t foo[] = uint32To8(length);
740+
return _theBus->write16BitRegisterRegion(SFE_XM125_PRESENCE_MANUAL_STEP_LENGTH, foo, 4);
680741
}
681742

682743
int32_t QwDevXM125::getPresenceStart(uint32_t *start)
683744
{
684-
return _theBus->read16BitRegisterRegion(SFE_XM125_PRESENCE_START, (uint8_t*)start, 4);
745+
uint8_t data[4];
746+
sfeTkError_t error = _theBus->read16BitRegisterRegion(SFE_XM125_PRESENCE_START, data, 4);
747+
748+
if(error != kSTkErrOk)
749+
return error;
750+
751+
*start = uint8To32(data);
752+
return kSTkErrOk;
685753
}
686754

687755
int32_t QwDevXM125::setPresenceStart(uint32_t start)
688756
{
689-
return _theBus->write16BitRegisterRegion(SFE_XM125_PRESENCE_START, (uint8_t*)start, 4);
757+
uint8_t foo[] = uint32To8(start);
758+
return _theBus->write16BitRegisterRegion(SFE_XM125_PRESENCE_START, foo, 4);
690759
}
691760

692761
int32_t QwDevXM125::getPresenceEnd(uint32_t *end)
@@ -733,3 +802,35 @@ int32_t QwDevXM125::setPresenceCommand(sfe_xm125_presence_command_t cmd)
733802
{
734803
return _theBus->write16BitRegisterRegion(SFE_XM125_PRESENCE_COMMAND, (uint8_t*)cmd, 4);
735804
}
805+
806+
int32_t QwDevXM125::presenceBusyWait()
807+
{
808+
uint32_t stat = 0;
809+
do
810+
{
811+
812+
if(_theBus->read16BitRegisterRegion(SFE_XM125_PRESENCE_DETECTOR_STATUS, (uint8_t*)stat, 4) != 0)
813+
{
814+
return -1;
815+
}
816+
}
817+
while ((stat & SFE_XM125_PRESENCE_DETECTOR_STATUS_MASK) != 0);
818+
819+
return 0; // 0 on success
820+
}
821+
822+
int32_t QwDevXM125::startPresenceDetector()
823+
{
824+
return _theBus->write16BitRegisterRegion(SFE_XM125_PRESENCE_COMMAND, (uint8_t*)XM125_PRESENCE_START_DETECTOR, 4);
825+
}
826+
827+
int32_t QwDevXM125::stopPresenceDetector()
828+
{
829+
return _theBus->write16BitRegisterRegion(SFE_XM125_PRESENCE_COMMAND, (uint8_t*)XM125_PRESENCE_STOP_DETECTOR, 4);
830+
831+
}
832+
833+
int32_t QwDevXM125::presenceReset()
834+
{
835+
return _theBus->write16BitRegisterRegion(SFE_XM125_PRESENCE_COMMAND, (uint8_t*)XM125_PRESENCE_RESET_MODULE, 4);
836+
}

src/sfeQwiicXM125.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ class QwDevXM125
350350

351351
// --------------------- I2C Presence Detector Functions ---------------------
352352

353+
/// @brief This function sets all the beginning values for a basic I2C
354+
/// example to be run on the device for presence sensing.
355+
/// @return Error code (0 no error)
356+
int32_t presenceDetectorStart();
357+
353358
/// @brief This function returns the RSS version number
354359
/// @param version Version number
355360
/// @param patch Patch version number
@@ -746,7 +751,25 @@ class QwDevXM125
746751
/// @param cmd command to send to device
747752
/// @return Error code (0 no error)
748753
int32_t setPresenceCommand(sfe_xm125_presence_command_t cmd);
754+
755+
/// @brief Completes a busy wait loop while the device is uploading
756+
/// information by waiting for the status
757+
/// @param stat Status of device - busy or not
758+
/// @return Error code (0 no error)
759+
int32_t presenceBusyWait();
760+
761+
/// @brief This functions starts the presence detector
762+
/// @return Error code (0 no error)
763+
int32_t startPresenceDetector();
764+
765+
/// @brief This function stops the presence detector
766+
/// @return Error code (0 no error)
767+
int32_t stopPresenceDetector();
749768

769+
/// @brief This function resets the presence detector settings
770+
/// of the device
771+
/// @return Error code (0 no error)
772+
int32_t presenceReset();
750773

751774
protected:
752775
sfeTkII2C *_theBus;

src/sfeXM125Regs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ typedef enum
225225
/* ****************************** Presence Values ****************************** */
226226

227227
const uint16_t SFE_XM125_PRESENCE_VERSION = 0x00;
228+
const uint32_t SFE_XM125_PRESENCE_DETECTOR_STATUS_MASK = 0b10010000111111110000000011111111;
229+
230+
#define uint32To8(foo) {(uint8_t)(((uint32_t) foo >> 24) & 0xFF), (uint8_t)(((uint32_t) foo >> 16) & 0xFF), (uint8_t)(((uint32_t) foo >> 8) & 0xFF), (uint8_t)(((uint32_t) foo >> 0) & 0xFF)}
231+
#define uint8To32(foo) (((uint32_t) foo[0] << 24) | ((uint32_t) foo[1] << 16) | ((uint32_t) foo[2] << 8) | ((uint32_t) foo[3] << 0))
228232
typedef struct
229233
{
230234
uint32_t presence_major : 16;

0 commit comments

Comments
 (0)