Skip to content

Commit fd47500

Browse files
committed
Add boolean sensor type
1 parent e1ae1d0 commit fd47500

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/Wippersnapper_demo.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ ws_adapter_offline wipper;
2323

2424
void setup() {
2525
Serial.begin(115200);
26-
while(!Serial);
26+
// Wait for serial to connect when dev mode, but don't wait forever
27+
uint8_t countdown = 200;
28+
while(!Serial && countdown-- > 0) {
29+
delay(10);
30+
}
2731
wipper.provision();
2832
wipper.connect();
2933
}

src/components/i2c/drivers/drvBase.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,15 @@ class drvBase {
511511
return false;
512512
}
513513

514+
/*!
515+
@brief Gets a sensor's Boolean value.
516+
@param booleanEvent
517+
The Boolean value.
518+
@returns True if the sensor value was obtained successfully, False
519+
otherwise.
520+
*/
521+
virtual bool getEventBoolean(sensors_event_t *booleanEvent) { return false; }
522+
514523
/*!
515524
@brief Gets a sensor's Raw value.
516525
@param rawEvent
@@ -633,6 +642,10 @@ class drvBase {
633642

634643
// Maps SensorType to function calls
635644
std::map<wippersnapper_sensor_SensorType, fnGetEvent> SensorEventHandlers = {
645+
{wippersnapper_sensor_SensorType_SENSOR_TYPE_BOOLEAN,
646+
[this](sensors_event_t *event) -> bool {
647+
return this->getEventBoolean(event);
648+
}},
636649
{wippersnapper_sensor_SensorType_SENSOR_TYPE_UNSPECIFIED,
637650
[this](sensors_event_t *event) -> bool {
638651
return this->getEventRaw(event);

src/components/i2c/model.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,28 @@ GetValueFromSensorsEventOrientation(wippersnapper_sensor_SensorType sensor_type,
210210
return value;
211211
}
212212

213+
/*!
214+
@brief Returns the boolean event value mapped to a sensor
215+
event.
216+
@param sensor_type
217+
The SensorType.
218+
@param event
219+
The sensors_event_t event.
220+
@returns The value of the SensorType as a boolean.
221+
*/
222+
bool
223+
GetValueFromSensorsEventBoolean(wippersnapper_sensor_SensorType sensor_type,
224+
sensors_event_t *event) {
225+
bool value = false;
226+
WS_DEBUG_PRINTLN("[i2c] Getting boolean event value...");
227+
WS_DEBUG_PRINT("[i2c] Event data[0]: ");
228+
WS_DEBUG_PRINTLN(event->data[0]);
229+
value = event->data[0] > 0.0f;
230+
WS_DEBUG_PRINT("[i2c] Boolean value (x>0): ");
231+
WS_DEBUG_PRINTLN(value);
232+
return value;
233+
}
234+
213235
/*!
214236
@brief Decodes a I2cDeviceRemove message from an input stream.
215237
@param stream
@@ -472,6 +494,12 @@ bool I2cModel::AddI2cDeviceSensorEvent(
472494
.i2c_device_events[_msg_i2c_device_event.i2c_device_events_count]
473495
.value.orientation_value.pitch = value_vect.pitch;
474496
// TODO: Add color RGB(A) vector support
497+
} else if (sensor_type ==
498+
wippersnapper_sensor_SensorType_SENSOR_TYPE_BOOLEAN) {
499+
bool value = GetValueFromSensorsEventBoolean(sensor_type, &event);
500+
_msg_i2c_device_event
501+
.i2c_device_events[_msg_i2c_device_event.i2c_device_events_count]
502+
.value.bool_value = value;
475503
} else {
476504
float value = GetValueFromSensorsEvent(sensor_type, &event);
477505
_msg_i2c_device_event

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,13 @@ bool ws_sdcard::LogEventI2c(
14801480
if (!LogJSONDoc(doc))
14811481
return false;
14821482
continue;
1483+
} else if (msg_device_event->i2c_device_events[i].type ==
1484+
wippersnapper_sensor_SensorType_SENSOR_TYPE_BOOLEAN) {
1485+
doc["value"] = msg_device_event->i2c_device_events[i].value.bool_value;
1486+
doc["si_unit"] =
1487+
SensorTypeToSIUnit(msg_device_event->i2c_device_events[i].type);
1488+
if (!LogJSONDoc(doc))
1489+
return false;
14831490
} else { // normal scalar float value
14841491
doc["value"] = msg_device_event->i2c_device_events[i].value.float_value;
14851492
doc["si_unit"] =

0 commit comments

Comments
 (0)