forked from scs/app-template
/
mainstate.c
373 lines (332 loc) · 10.7 KB
/
mainstate.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* A collection of example applications for the LeanXcam platform.
Copyright (C) 2008 Supercomputing Systems AG
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*! @file mainstate.c
* @brief Main State machine for template application.
*
* Makes use of Framework HSM module.
************************************************************************/
#include "template.h"
#include "mainstate.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
const Msg mainStateMsg[] = {
{ FRAMESEQ_EVT },
{ FRAMEPAR_EVT },
{ IPC_GET_APP_STATE_EVT },
{ IPC_GET_COLOR_IMG_EVT },
{ IPC_GET_RAW_IMG_EVT },
{ IPC_SET_CAPTURE_MODE_EVT }
};
/*********************************************************************//*!
* @brief Inline function to throw an event to be handled by the statemachine.
*
* @param pHsm Pointer to state machine
* @param evt Event to be thrown.
*//*********************************************************************/
void ThrowEvent(struct MainState *pHsm, unsigned int evt)
{
const Msg *pMsg = &mainStateMsg[evt];
HsmOnEvent((Hsm*)pHsm, pMsg);
}
/*********************************************************************//*!
* @brief Checks for IPC events, schedules their handling and
* acknowledges any executed ones.
*
* @param pMainState Initalized HSM main state variable.
* @return 0 on success or an appropriate error code.
*//*********************************************************************/
static OSC_ERR HandleIpcRequests(MainState *pMainState)
{
OSC_ERR err;
uint32 paramId;
err = CheckIpcRequests(¶mId);
if (err == SUCCESS)
{
/* We have a request. See to it that it is handled
* depending on the state we're in. */
switch(paramId)
{
case GET_APP_STATE:
/* Request for the current state of the application. */
ThrowEvent(pMainState, IPC_GET_APP_STATE_EVT);
break;
case GET_COLOR_IMG:
/* Request for the live image. */
ThrowEvent(pMainState, IPC_GET_COLOR_IMG_EVT);
break;
case GET_RAW_IMG:
/* Request for the live image. */
ThrowEvent(pMainState, IPC_GET_RAW_IMG_EVT);
break;
case SET_CAPTURE_MODE:
/* Set the debayering option. */
ThrowEvent(pMainState, IPC_SET_CAPTURE_MODE_EVT);
break;
default:
OscLog(ERROR, "%s: Unkown IPC parameter ID (%d)!\n", __func__, paramId);
data.ipc.enReqState = REQ_STATE_NACK_PENDING;
break;
}
}
else if (err == -ENO_MSG_AVAIL)
{
/* No new message available => do nothing. */
}
else
{
/* Error.*/
OscLog(ERROR, "%s: IPC request error! (%d)\n", __func__, err);
return err;
}
/* Try to acknowledge the new or any old unacknowledged
* requests. It may take several tries to succeed.*/
err = AckIpcRequests();
if (err != SUCCESS)
{
OscLog(ERROR, "%s: IPC acknowledge error! (%d)\n", __func__, err);
}
return err;
}
Msg const *MainState_top(MainState *me, Msg *msg)
{
switch (msg->evt)
{
case START_EVT:
STATE_START(me, &me->captureColor);
return 0;
case IPC_GET_COLOR_IMG_EVT:
case IPC_GET_RAW_IMG_EVT:
case IPC_GET_APP_STATE_EVT:
case IPC_SET_CAPTURE_MODE_EVT:
/* If the IPC event is not handled in the actual substate, a negative acknowledge is returned by default. */
data.ipc.enReqState = REQ_STATE_NACK_PENDING;
return 0;
}
return msg;
}
Msg const *MainState_CaptureColor(MainState *me, Msg *msg)
{
struct APPLICATION_STATE *pState;
bool bCaptureColor;
switch (msg->evt)
{
case ENTRY_EVT:
data.ipc.state.enAppMode = APP_CAPTURE_COLOR;
data.pCurRawImg = data.u8FrameBuffers[0];
return 0;
case FRAMESEQ_EVT:
/* Sleep here for a short while in order not to violate the vertical
* blank time of the camera sensor when triggering a new image
* right after receiving the old one. This can be removed if some
* heavy calculations are done here. */
usleep(1000);
return 0;
case FRAMEPAR_EVT:
/* Process the image. */
ProcessFrame(data.pCurRawImg);
/* Timestamp the capture of the image. */
data.ipc.state.imageTimeStamp = OscSupCycGet();
data.ipc.state.bNewImageReady = TRUE;
return 0;
case IPC_GET_APP_STATE_EVT:
/* Fill in the response and schedule an acknowledge for the request. */
pState = (struct APPLICATION_STATE*)data.ipc.req.pAddr;
memcpy(pState, &data.ipc.state, sizeof(struct APPLICATION_STATE));
data.ipc.enReqState = REQ_STATE_ACK_PENDING;
return 0;
case IPC_GET_COLOR_IMG_EVT:
/* Write out the image to the address space of the CGI. */
memcpy(data.ipc.req.pAddr, data.u8ResultImage, sizeof(data.u8ResultImage));
data.ipc.state.bNewImageReady = FALSE;
/* Mark the request as executed, so it will be acknowledged later. */
data.ipc.enReqState = REQ_STATE_ACK_PENDING;
return 0;
case IPC_SET_CAPTURE_MODE_EVT:
/* Read the option from the address space of the CGI. */
bCaptureColor = *((bool*)data.ipc.req.pAddr);
if (bCaptureColor == FALSE)
{
/* Need to capture raw images from now on, this is done in the captureRaw state. */
STATE_TRAN(me, &me->captureRaw);
}
data.ipc.enReqState = REQ_STATE_ACK_PENDING;
return 0;
}
return msg;
}
Msg const *MainState_CaptureRaw(MainState *me, Msg *msg)
{
struct APPLICATION_STATE *pState;
bool bCaptureColor;
switch (msg->evt)
{
case ENTRY_EVT:
data.ipc.state.enAppMode = APP_CAPTURE_RAW;
data.pCurRawImg = data.u8FrameBuffers[0];
return 0;
case FRAMESEQ_EVT:
/* Timestamp the capture of the image. */
data.ipc.state.imageTimeStamp = OscSupCycGet();
data.ipc.state.bNewImageReady = TRUE;
/* Sleep here for a short while in order not to violate the vertical
* blank time of the camera sensor when triggering a new image
* right after receiving the old one. This can be removed if some
* heavy calculations are done here. */
usleep(1000);
return 0;
case FRAMEPAR_EVT:
return 0;
case IPC_GET_APP_STATE_EVT:
/* Fill in the response and schedule an acknowledge for the request. */
pState = (struct APPLICATION_STATE*)data.ipc.req.pAddr;
memcpy(pState, &data.ipc.state, sizeof(struct APPLICATION_STATE));
data.ipc.enReqState = REQ_STATE_ACK_PENDING;
return 0;
case IPC_GET_RAW_IMG_EVT:
/* Write out the raw image to the address space of the CGI. */
memcpy(data.ipc.req.pAddr, data.pCurRawImg, OSC_CAM_MAX_IMAGE_WIDTH*OSC_CAM_MAX_IMAGE_HEIGHT);
data.ipc.state.bNewImageReady = FALSE;
/* Mark the request as executed, so it will be acknowledged later. */
data.ipc.enReqState = REQ_STATE_ACK_PENDING;
return 0;
case IPC_SET_CAPTURE_MODE_EVT:
/* Read the option from the address space of the CGI. */
bCaptureColor = *((bool*)data.ipc.req.pAddr);
if (bCaptureColor == TRUE)
{
/* Need to capture colored images from now on, this is done in the captureRaw state. */
STATE_TRAN(me, &me->captureColor);
}
data.ipc.enReqState = REQ_STATE_ACK_PENDING;
return 0;
}
return msg;
}
void MainStateConstruct(MainState *me)
{
HsmCtor((Hsm *)me, "MainState", (EvtHndlr)MainState_top);
StateCtor(&me->captureRaw, "Capture Raw", &((Hsm *)me)->top, (EvtHndlr)MainState_CaptureRaw);
StateCtor(&me->captureColor, "Capture Color", &((Hsm *)me)->top, (EvtHndlr)MainState_CaptureColor);
}
OSC_ERR StateControl( void)
{
OSC_ERR camErr, err;
MainState mainState;
uint8 *pCurRawImg = NULL;
/* Setup main state machine */
MainStateConstruct(&mainState);
HsmOnStart((Hsm *)&mainState);
OscSimInitialize();
/*----------- initial capture preparation*/
camErr = OscCamSetupCapture( OSC_CAM_MULTI_BUFFER);
if (camErr != SUCCESS)
{
OscLog(ERROR, "%s: Unable to setup initial capture (%d)!\n", __func__, camErr);
return camErr;
}
err = OscGpioTriggerImage();
if (err != SUCCESS)
{
OscLog(ERROR, "%s: Unable to trigger initial capture (%d)!\n", __func__, err);
}
/*----------- infinite main loop */
while (TRUE)
{
/*----------- wait for captured picture */
while (TRUE)
{
err = HandleIpcRequests(&mainState);
if (err != SUCCESS)
{
OscLog(ERROR, "%s: IPC error! (%d)\n", __func__, err);
Unload();
return err;
}
camErr = OscCamReadPicture(OSC_CAM_MULTI_BUFFER, &pCurRawImg, 0, CAMERA_TIMEOUT);
if (camErr != -ETIMEOUT)
{
/* Anything other than a timeout means that we should
* stop trying and analyze the situation. */
break;
}
else
{
/*----------- procress CGI request
* Check for CGI request only if ReadPicture generated a
* time out. Process request directely or involve state
* engine with event */
/* Read request. */
err = HandleIpcRequests(&mainState);
if (err != SUCCESS)
{
OscLog(ERROR, "%s: IPC error! (%d)\n", __func__, err);
Unload();
return err;
}
}
}
if (camErr == -EPICTURE_TOO_OLD)
{
/* We have a picture, but it already has been laying
* around for a while. Most likely we won't be able to
* make the deadline for this picture, so we better just
* give it up and don't portrude our delay to the next
* frame. */
OscLog(WARN, "%s: Captured picture too old!\n", __func__);
/*----------- prepare next capture */
camErr = OscCamSetupCapture( OSC_CAM_MULTI_BUFFER);
if (camErr != SUCCESS)
{
OscLog(ERROR, "%s: Unable to setup capture (%d)!\n", __func__, camErr);
break;
}
err = OscGpioTriggerImage();
if (err != SUCCESS)
{
OscLog(ERROR, "%s: Unable to trigger capture (%d)!\n", __func__, err);
break;
}
continue;
}
else if (camErr != SUCCESS)
{
/* Fatal error, giving up. */
OscLog(ERROR, "%s: Unable to read picture from cam!\n", __func__);
break;
}
data.pCurRawImg = pCurRawImg;
/*----------- process frame by state engine (pre-setup) Sequentially with next capture */
ThrowEvent(&mainState, FRAMESEQ_EVT);
/*----------- prepare next capture */
camErr = OscCamSetupCapture( OSC_CAM_MULTI_BUFFER);
if (camErr != SUCCESS)
{
OscLog(ERROR, "%s: Unable to setup capture (%d)!\n", __func__, camErr);
break;
}
err = OscGpioTriggerImage();
if (err != SUCCESS)
{
OscLog(ERROR, "%s: Unable to trigger capture (%d)!\n", __func__, err);
break;
}
/*----------- process frame by state engine (post-setup) Parallel with next capture */
ThrowEvent(&mainState, FRAMEPAR_EVT);
/* Advance the simulation step counter. */
OscSimStep();
} /* end while ever */
return SUCCESS;
}