/
gyro_sensor.cpp
57 lines (46 loc) · 2.02 KB
/
gyro_sensor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Implements a simulated Gyroscope Sensor with 3 sensor values (x, y, z).
// Demonstrates how we may return multiple float values as sensor data.
#include "platform.h"
#include <string.h>
#include <stdlib.h>
#include <cocoos.h>
#include "display.h"
#include "sensor.h"
#include "gyro_sensor.h"
#ifdef SENSOR_DATA
// These are the sensor functions that we will implement in this file.
static void init_sensor(void);
static uint8_t poll_sensor(float *data, uint8_t size);
// Number of floats that this sensor returns as sensor data.
#define sensorDataSize 3
// Construct a sensor object with the sensor functions.
static Sensor sensor(
"gyr", // Name of sensor. The Structured Message field will use this name.
&init_sensor, // Function for initialising the sensor.
&poll_sensor // Function for polling sensor data.
);
static SensorContext sensorContext; // Remembers the sensor context.
static float sensorDataArray[sensorDataSize]; // Array of floats for remembering the sensor data.
static void init_sensor(void) {
// Initialise the sensor if necessary. sensor and sensorContext objects have been populated.
}
static uint8_t poll_sensor(float *data, uint8_t size) {
// Poll the sensor for new data. Copy the received sensor data into the provided data buffer.
// Return the number of floats copied. If no data is available, return 0.
debug(sensor.info.name, F(" >> poll_sensor")); ////
// Simulated sensor data with random numbers.
sensorDataArray[0] = (rand() % 500) / 10.0;
sensorDataArray[1] = (rand() % 500) / 10.0;
sensorDataArray[2] = (rand() % 500) / 10.0;
// Copy the received sensor data and return the number of floats copied.
return receive_sensor_data(sensorDataArray, sensorDataSize, data, size);
}
SensorContext *setup_gyro_sensor(
uint16_t pollInterval,
uint8_t displayTaskID
) {
// Set up and return the sensor context.
setup_sensor_context(&sensorContext, &sensor, pollInterval, displayTaskID);
return &sensorContext;
}
#endif // SENSOR_DATA