Skip to content

Commit ff93dda

Browse files
committed
add(vector): implement get value for 3D + orientation
1 parent 33a79e9 commit ff93dda

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

src/components/i2c/model.cpp

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,27 @@ GetValueFromSensorsEventVector(wippersnapper_sensor_SensorType sensor_type,
146146
value.y = event->magnetic.y;
147147
value.z = event->magnetic.z;
148148
break;
149+
case wippersnapper_sensor_SensorType_SENSOR_TYPE_ACCELEROMETER:
150+
value.x = event->acceleration.x;
151+
value.y = event->acceleration.y;
152+
value.z = event->acceleration.z;
153+
break;
154+
case wippersnapper_sensor_SensorType_SENSOR_TYPE_GYROSCOPE:
155+
value.x = event->gyro.x;
156+
value.y = event->gyro.y;
157+
value.z = event->gyro.z;
158+
break;
159+
case wippersnapper_sensor_SensorType_SENSOR_TYPE_GRAVITY:
160+
value.x = event->acceleration.x;
161+
value.y = event->acceleration.y;
162+
value.z = event->acceleration.z;
163+
break;
164+
case wippersnapper_sensor_SensorType_SENSOR_TYPE_LINEAR_ACCELERATION:
165+
value.x = event->acceleration.x;
166+
value.y = event->acceleration.y;
167+
value.z = event->acceleration.z;
168+
break;
169+
149170
default:
150171
value.x = 0.0;
151172
value.y = 0.0;
@@ -155,6 +176,38 @@ GetValueFromSensorsEventVector(wippersnapper_sensor_SensorType sensor_type,
155176
return value;
156177
}
157178

179+
/*!
180+
@brief Returns the orientation vector event value mapped to a sensor event.
181+
@param sensor_type
182+
The SensorType.
183+
@param event
184+
The sensors_event_t event.
185+
@returns The value of the SensorType as an orientation vector.
186+
*/
187+
wippersnapper_sensor_SensorEvent_SensorEventOrientation
188+
GetValueFromSensorsEventOrientation(wippersnapper_sensor_SensorType sensor_type,
189+
sensors_event_t *event) {
190+
wippersnapper_sensor_SensorEvent_SensorEventOrientation value = {0.0, 0.0, 0.0};
191+
switch (sensor_type) {
192+
case wippersnapper_sensor_SensorType_SENSOR_TYPE_ORIENTATION:
193+
value.roll = event->orientation.roll;
194+
value.pitch = event->orientation.pitch;
195+
value.heading = event->orientation.heading;
196+
break;
197+
case wippersnapper_sensor_SensorType_SENSOR_TYPE_ROTATION_VECTOR:
198+
value.roll = event->orientation.roll;
199+
value.pitch = event->orientation.pitch;
200+
value.heading = event->orientation.heading;
201+
break;
202+
default:
203+
value.roll = 0.0;
204+
value.pitch = 0.0;
205+
value.heading = 0.0;
206+
break;
207+
}
208+
return value;
209+
}
210+
158211
/*!
159212
@brief Decodes a I2cDeviceRemove message from an input stream.
160213
@param stream
@@ -383,7 +436,11 @@ bool I2cModel::AddI2cDeviceSensorEvent(
383436
.i2c_device_events[_msg_i2c_device_event.i2c_device_events_count]
384437
.type = sensor_type;
385438
if (sensor_type ==
386-
wippersnapper_sensor_SensorType_SENSOR_TYPE_MAGNETIC_FIELD) {
439+
wippersnapper_sensor_SensorType_SENSOR_TYPE_MAGNETIC_FIELD || sensor_type ==
440+
wippersnapper_sensor_SensorType_SENSOR_TYPE_ACCELEROMETER || sensor_type ==
441+
wippersnapper_sensor_SensorType_SENSOR_TYPE_GYROSCOPE || sensor_type ==
442+
wippersnapper_sensor_SensorType_SENSOR_TYPE_GRAVITY || sensor_type ==
443+
wippersnapper_sensor_SensorType_SENSOR_TYPE_LINEAR_ACCELERATION) {
387444
wippersnapper_sensor_SensorEvent_SensorEvent3DVector value_vect = GetValueFromSensorsEventVector(sensor_type, &event);
388445
_msg_i2c_device_event
389446
.i2c_device_events[_msg_i2c_device_event.i2c_device_events_count]
@@ -394,6 +451,19 @@ bool I2cModel::AddI2cDeviceSensorEvent(
394451
_msg_i2c_device_event
395452
.i2c_device_events[_msg_i2c_device_event.i2c_device_events_count]
396453
.value.vector_value.z = value_vect.z;
454+
} else if (sensor_type == wippersnapper_sensor_SensorType_SENSOR_TYPE_ORIENTATION || sensor_type ==
455+
wippersnapper_sensor_SensorType_SENSOR_TYPE_ROTATION_VECTOR) {
456+
wippersnapper_sensor_SensorEvent_SensorEventOrientation value_vect = GetValueFromSensorsEventOrientation(sensor_type, &event);
457+
_msg_i2c_device_event
458+
.i2c_device_events[_msg_i2c_device_event.i2c_device_events_count]
459+
.value.orientation_value.heading = value_vect.heading;
460+
_msg_i2c_device_event
461+
.i2c_device_events[_msg_i2c_device_event.i2c_device_events_count]
462+
.value.orientation_value.roll = value_vect.roll;
463+
_msg_i2c_device_event
464+
.i2c_device_events[_msg_i2c_device_event.i2c_device_events_count]
465+
.value.orientation_value.pitch = value_vect.pitch;
466+
//TODO: Add color RGB(A) vector support
397467
} else {
398468
float value = GetValueFromSensorsEvent(sensor_type, &event);
399469
_msg_i2c_device_event

0 commit comments

Comments
 (0)