-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdemo.c
286 lines (243 loc) · 8.49 KB
/
demo.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright (c) 2022-2023 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include "main.h"
#include "cmsis_vio.h"
#include "cmsis_os2.h"
#include "sds_rec.h"
#include "sensor_drv.h"
#include "sensor_config.h"
// Configuration
#ifndef REC_BUF_SIZE_ACCELEROMETER
#define REC_BUF_SIZE_ACCELEROMETER 8192U
#endif
#ifndef REC_BUF_SIZE_GYROSCOPE
#define REC_BUF_SIZE_GYROSCOPE 8192U
#endif
#ifndef REC_BUF_SIZE_TEMPERATURE_SENSOR
#define REC_BUF_SIZE_TEMPERATURE_SENSOR 256U
#endif
#ifndef REC_IO_THRESHOLD_ACCELEROMETER
#define REC_IO_THRESHOLD_ACCELEROMETER 512
#endif
#ifndef REC_IO_THRESHOLD_GYROSCOPE
#define REC_IO_THRESHOLD_GYROSCOPE 512
#endif
#ifndef REC_IO_THRESHOLD_TEMPERATURE_SENSOR
#define REC_IO_THRESHOLD_TEMPERATURE_SENSOR 0
#endif
#ifndef SENSOR_POLLING_INTERVAL
#define SENSOR_POLLING_INTERVAL 50U // 50ms
#endif
#ifndef SENSOR_BUF_SIZE
#define SENSOR_BUF_SIZE 8192U
#endif
// Sensor identifiers
static sensorId_t sensorId_accelerometer;
static sensorId_t sensorId_gyroscope;
static sensorId_t sensorId_temperatureSensor;
// Sensor configuration
static sensorConfig_t *sensorConfig_accelerometer;
static sensorConfig_t *sensorConfig_gyroscope;
static sensorConfig_t *sensorConfig_temperatureSensor;
// Recorder identifiers
static sdsRecId_t recId_accelerometer = NULL;
static sdsRecId_t recId_gyroscope = NULL;
static sdsRecId_t recId_temperatureSensor = NULL;
// Recorder buffers
static uint8_t recBuf_accelerometer[REC_BUF_SIZE_ACCELEROMETER];
static uint8_t recBuf_gyroscope[REC_BUF_SIZE_GYROSCOPE];
static uint8_t recBuf_temperatureSensor[REC_BUF_SIZE_TEMPERATURE_SENSOR];
// Temporary sensor buffer
static uint8_t sensorBuf[SENSOR_BUF_SIZE];
// Sensor close flag
static uint8_t close_flag = 0U;
// Event close sent flag
static uint8_t event_close_sent;
// Thread identifiers
static osThreadId_t thrId_demo;
#define EVENT_CLOSE (1U << 0)
// External functions
extern int32_t socket_startup (void);
// Read sensor thread
static __NO_RETURN void read_sensors (void *argument) {
uint32_t num, buf_size;
uint32_t timestamp;
(void) argument;
timestamp = osKernelGetTickCount();
for (;;) {
if (close_flag == 0U) {
if (sensorGetStatus(sensorId_accelerometer).active != 0U) {
num = sizeof(sensorBuf) / sensorConfig_accelerometer->sample_size;
num = sensorReadSamples(sensorId_accelerometer, num, sensorBuf, sizeof(sensorBuf));
if (num != 0U) {
buf_size = num * sensorConfig_accelerometer->sample_size;
num = sdsRecWrite(recId_accelerometer, timestamp, sensorBuf, buf_size);
if (num != buf_size) {
printf("%s: Recorder write failed\r\n", sensorConfig_accelerometer->name);
}
}
}
if (sensorGetStatus(sensorId_gyroscope).active != 0U) {
num = sizeof(sensorBuf) / sensorConfig_gyroscope->sample_size;
num = sensorReadSamples(sensorId_gyroscope, num, sensorBuf, sizeof(sensorBuf));
if (num != 0U) {
buf_size = num * sensorConfig_gyroscope->sample_size;
num = sdsRecWrite(recId_gyroscope, timestamp, sensorBuf, buf_size);
if (num != buf_size) {
printf("%s: Recorder write failed\r\n", sensorConfig_gyroscope->name);
}
}
}
if (sensorGetStatus(sensorId_temperatureSensor).active != 0U) {
num = sizeof(sensorBuf) / sensorConfig_temperatureSensor->sample_size;
num = sensorReadSamples(sensorId_temperatureSensor, num, sensorBuf, sizeof(sensorBuf));
if (num != 0U) {
buf_size = num * sensorConfig_temperatureSensor->sample_size;
num = sdsRecWrite(recId_temperatureSensor, timestamp, sensorBuf, buf_size);
if (num != buf_size) {
printf("%s: Recorder write failed\r\n", sensorConfig_temperatureSensor->name);
}
}
}
} else {
if (event_close_sent == 0U) {
event_close_sent = 1U;
osThreadFlagsSet(thrId_demo, EVENT_CLOSE);
}
}
timestamp += SENSOR_POLLING_INTERVAL;
osDelayUntil(timestamp);
}
}
// Recorder event callback
static void recorder_event_callback (sdsRecId_t id, uint32_t event) {
if (event & SDS_REC_EVENT_IO_ERROR) {
if (id == recId_accelerometer) {
printf("%s: Recorder event - I/O error\r\n", sensorConfig_accelerometer->name);
}
if (id == recId_gyroscope) {
printf("%s: Recorder event - I/O error\r\n", sensorConfig_gyroscope->name);
}
if (id == recId_temperatureSensor) {
printf("%s: Recorder event - I/O error\r\n", sensorConfig_temperatureSensor->name);
}
}
}
// Recorder start
static void recorder_start (void) {
// Accelerometer
// Open Recorder
recId_accelerometer = sdsRecOpen("Accelerometer",
recBuf_accelerometer,
sizeof(recBuf_accelerometer),
REC_IO_THRESHOLD_ACCELEROMETER);
sensorEnable(sensorId_accelerometer);
printf("Accelerometer enabled\r\n");
// Gyroscope
// Open Recorder
recId_gyroscope = sdsRecOpen("Gyroscope",
recBuf_gyroscope,
sizeof(recBuf_gyroscope),
REC_IO_THRESHOLD_GYROSCOPE);
sensorEnable(sensorId_gyroscope);
printf("Gyroscope enabled\r\n");
// Temperature sensor
// Open Recorder
recId_temperatureSensor = sdsRecOpen("Temperature",
recBuf_temperatureSensor,
sizeof(recBuf_temperatureSensor),
REC_IO_THRESHOLD_TEMPERATURE_SENSOR);
sensorEnable(sensorId_temperatureSensor);
printf("Temperature sensor enabled\r\n");
}
// Recorder stop
static void recorder_stop (void) {
uint32_t flags;
event_close_sent = 0U;
close_flag = 1U;
flags = osThreadFlagsWait(EVENT_CLOSE, osFlagsWaitAny, osWaitForever);
if ((flags & osFlagsError) == 0U) {
// Accelerometer disable
sensorDisable(sensorId_accelerometer);
// Close Recorder
sdsRecClose(recId_accelerometer);
recId_accelerometer = NULL;
printf("Accelerometer disabled\r\n");
// Gyroscope disable
sensorDisable(sensorId_gyroscope);
// Close Recorder
sdsRecClose(recId_gyroscope);
recId_gyroscope = NULL;
printf("Gyroscope disabled\r\n");
// Temperature sensor disable
sensorDisable(sensorId_temperatureSensor);
// Close Recorder
sdsRecClose(recId_temperatureSensor);
recId_temperatureSensor = NULL;
printf("Temperature sensor disabled\r\n");
}
close_flag = 0U;
}
// Demo
static __NO_RETURN void demo (void *argument) {
uint32_t value;
uint32_t value_last = 0U;
uint8_t rec_active = 0U;
(void) argument;
if (socket_startup() != 0) {
printf("Socket startup failed\r\n");
osThreadExit();
}
thrId_demo = osThreadGetId();
// Get sensor identifier
sensorId_accelerometer = sensorGetId("Accelerometer");
sensorId_gyroscope = sensorGetId("Gyroscope");
sensorId_temperatureSensor = sensorGetId("Temperature");
// Get sensor configuration
sensorConfig_accelerometer = sensorGetConfig(sensorId_accelerometer);
sensorConfig_gyroscope = sensorGetConfig(sensorId_gyroscope);
sensorConfig_temperatureSensor = sensorGetConfig(sensorId_temperatureSensor);
// Initialize recorder
sdsRecInit(recorder_event_callback);
// Create sensor thread
osThreadNew(read_sensors, NULL, NULL);
for(;;) {
// Monitor user button
value = vioGetSignal(vioBUTTON0);
if (value != value_last) {
value_last = value;
if (value == vioBUTTON0) {
// Button pressed
if (rec_active == 0U) {
rec_active = 1U;
recorder_start();
} else {
rec_active = 0U;
recorder_stop();
}
}
}
osDelay(100U);
}
}
// Application initialization
int32_t app_initialize (void) {
osThreadNew(demo, NULL, NULL);
return 0;
}