-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainstate.c
328 lines (289 loc) · 9.04 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
/* Copying and distribution of this file, with or without modification,
* are permitted in any medium without royalty. This file is offered as-is,
* without any warranty.
*/
/*! @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_NEW_IMG_EVT },
{ IPC_SET_IMAGE_TYPE_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;
struct IPC_DATA *pIpc = &data.ipc;
struct OSC_IPC_REQUEST *pReq = &pIpc->req;
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_NEW_IMG:
/* Request for the live image. */
ThrowEvent(pMainState, IPC_GET_NEW_IMG_EVT);
break;
case SET_IMAGE_TYPE:
{
/* Set the new image type. */
unsigned int ImgTyp = *((unsigned int*)data.ipc.req.pAddr);
if(MAX_NUM_IMG <= ImgTyp)
{
OscLog(ERROR, "%obtained unknown image type: %u! Will leave unchanged\n", data.ipc.state.nImageType);
}
else
{
data.ipc.state.nImageType = ImgTyp;
ThrowEvent(pMainState, IPC_SET_IMAGE_TYPE_EVT);
}
break;
}
case SET_EXPOSURE_TIME:
// a new exposure time was given
if(data.ipc.state.nExposureTime != *((int*)pReq->pAddr))
{
data.nExposureTimeChanged = true;
data.ipc.state.nExposureTime = *((int*)pReq->pAddr);
}
data.ipc.enReqState = REQ_STATE_ACK_PENDING;//we return immediately
break;
case SET_THRESHOLD:
// a new exposure time was given
if(data.ipc.state.nThreshold != *((int*)pReq->pAddr))
{
data.ipc.state.nThreshold = *((int*)pReq->pAddr);
}
data.ipc.enReqState = REQ_STATE_ACK_PENDING;//we return immediately
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)
{
struct APPLICATION_STATE *pState;
switch (msg->evt)
{
case START_EVT:
/* initialize the whole stuff - is this the right place ? */
STATE_START(me, &me->showGray);
data.ipc.state.enAppMode = APP_CAPTURE_ON;
data.pCurRawImg = data.u8FrameBuffers[0];
data.nExposureTimeChanged = true;
data.ipc.state.nExposureTime = 25;
data.ipc.state.nStepCounter = 0;
data.ipc.state.nThreshold = 30;
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 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(4000);
return 0;
case FRAMEPAR_EVT:
{
/* we have a new image increase counter: here and only here! */
data.ipc.state.nStepCounter++;
/* debayer the image first -> to half size*/
OscVisDebayerGreyscaleHalfSize( data.pCurRawImg, OSC_CAM_MAX_IMAGE_WIDTH, OSC_CAM_MAX_IMAGE_HEIGHT, ROW_BGBG, data.u8TempImage[GRAYSCALE]);
/* Process the image. */
/* the parameter is not really required */
ProcessFrame(data.u8TempImage[GRAYSCALE]);
return 0;
}
case IPC_SET_IMAGE_TYPE_EVT:
{
if(data.ipc.state.nImageType == GRAYSCALE) {
STATE_TRAN(me, &me->showGray);
}
else if(data.ipc.state.nImageType == THRESHOLD) {
STATE_TRAN(me, &me->showThreshold);
}
else if(data.ipc.state.nImageType == BACKGROUND) {
STATE_TRAN(me, &me->showBackground);
}
else {
data.ipc.enReqState = REQ_STATE_NACK_PENDING;
}
data.ipc.enReqState = REQ_STATE_ACK_PENDING;
return 0;
}
case IPC_GET_NEW_IMG_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_ShowGray(MainState *me, Msg *msg)
{
switch (msg->evt)
{
case IPC_GET_NEW_IMG_EVT:
{
/* Write out the current gray image to the address space of the CGI. */
memcpy(data.ipc.req.pAddr, data.u8TempImage[GRAYSCALE], sizeof(data.u8TempImage[GRAYSCALE]));
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;
}
}
return msg;
}
Msg const *MainState_ShowThreshold(MainState *me, Msg *msg)
{
switch (msg->evt)
{
case IPC_GET_NEW_IMG_EVT:
{
/* Write out the image to the address space of the CGI. */
memcpy(data.ipc.req.pAddr, data.u8TempImage[BACKGROUND], sizeof(data.u8TempImage[BACKGROUND]));
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;
}
}
return msg;
}
Msg const *MainState_ShowBackground(MainState *me, Msg *msg)
{
switch (msg->evt)
{
case IPC_GET_NEW_IMG_EVT:
{
/* Write out the current gray image to the address space of the CGI. */
memcpy(data.ipc.req.pAddr, data.u8TempImage[DILATION], sizeof(data.u8TempImage[DILATION]));
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;
}
}
return msg;
}
void MainStateConstruct(MainState *me)
{
HsmCtor((Hsm *)me, "MainState", (EvtHndlr)MainState_top);
StateCtor(&me->showGray, "Show Gray", &((Hsm *)me)->top, (EvtHndlr)MainState_ShowGray);
StateCtor(&me->showThreshold, "Show Threshold", &((Hsm *)me)->top, (EvtHndlr)MainState_ShowThreshold);
StateCtor(&me->showBackground, "Show Background", &((Hsm *)me)->top, (EvtHndlr)MainState_ShowBackground);
}
OscFunction( StateControl)
OSC_ERR camErr;
MainState mainState;
uint8 *pCurRawImg = NULL;
/* Setup main state machine */
MainStateConstruct(&mainState);
HsmOnStart((Hsm *)&mainState);
OscSimInitialize();
/* Prologue: initial acquisition setup */
OscCall( OscCamSetupCapture, OSC_CAM_MULTI_BUFFER);
OscCall( OscGpioTriggerImage);
/* Body: infinite acquisition loop */
while (TRUE)
{
/* Wait for captured picture. While a timeout is reported we do service
* web interface and GPIO meanwhile. Otherwise we quick. One IPC request
* is processed at least. */
while (TRUE)
{
OscCall( HandleIpcRequests, &mainState);
camErr = OscCamReadPicture(OSC_CAM_MULTI_BUFFER, &pCurRawImg, 0, 4);
if( camErr == -ETIMEOUT)
{
OscCall( HandleIpcRequests, &mainState);
}
else
{
break;
}
}
/* A valid image is expected. */
OscAssert_s( camErr == SUCCESS);
data.pCurRawImg = pCurRawImg;
/* Process frame by state engine. Sequentially with next capture */
ThrowEvent(&mainState, FRAMESEQ_EVT);
/* set new shutter speed */
if(data.nExposureTimeChanged)
{
OscCamSetShutterWidth(data.ipc.state.nExposureTime * 100);
data.nExposureTimeChanged = false;
}
/* Prepare next capture */
OscCall( OscCamSetupCapture, OSC_CAM_MULTI_BUFFER);
OscCall( OscGpioTriggerImage);
/* Process frame by state engine. Parallel with next capture */
ThrowEvent(&mainState, FRAMEPAR_EVT);
/* Advance the simulation step counter. */
OscSimStep();
} /* end while ever */
OscFunctionCatch()
OscFunctionEnd()