/*! * @brief Initialize everything so the application is fully operable after a call to this function. * * @return SUCCESS or an appropriate error code. */ static OSC_ERR init(const int argc, const char * * argv) { OSC_ERR err = SUCCESS; /* Set log levels. */ OscLogSetConsoleLogLevel(EMERG); OscLogSetFileLogLevel(EMERG); /* Create the framework */ err = OscCreate(&hFramework); if (err != SUCCESS) { fprintf(stderr, "%s: error: Unable to create framework.\n", __func__); return err; } /* Load the framework module dependencies. */ err = OscLoadDependencies(hFramework, deps, length (deps)); if (err != SUCCESS) { fprintf(stderr, "%s: error: Unable to load dependencies! (%d)\n", __func__, err); goto dep_err; } /* Seed the random generator */ srand(OscSupCycGet()); return SUCCESS; dep_err: OscDestroy(&hFramework); return err; }
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; }
OSC_ERR OscDmaSync(void *hChainHandle) { struct DMA_CHAIN *pChain = (struct DMA_CHAIN*)hChainHandle; uint32 waitCyc; #ifdef DMA_TIMEOUT_WORKAROUND uint32 startCyc = OscSupCycGet(); while(OscSupCycToMicroSecs(OscSupCycGet() - startCyc) < DMA_TIMEOUT) { FLUSHINV(&pChain->syncFlag); SSYNC; if(pChain->syncFlag != 0) return SUCCESS; waitCyc = OscSupCycGet(); while(OscSupCycGet() - waitCyc < DMA_WAIT_POLL_FREQ_CYCLES) { asm("nop;"); asm("nop;"); asm("nop;"); asm("nop;"); asm("nop;"); asm("nop;"); } } printf("DMA timeout!\n"); return -ETIMEOUT; #else uint32 timeout = DMA_SYNC_TIMEOUT_LOOPS; while(timeout--) { FLUSHINV(&pChain->syncFlag); SSYNC; if(pChain->syncFlag != 0) return SUCCESS; waitCyc = OscSupCycGet(); while(OscSupCycToMicroSecs(OscSupCycGet() - waitCyc) < DMA_WAIT_POLL_FREQ_CYCLES) { asm("nop;"); asm("nop;"); asm("nop;"); asm("nop;"); asm("nop;"); asm("nop;"); } } return -ETIMEOUT; #endif /* DMA_TIMEOUT_WORKAROUND */ }
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->showthresh); } else if(data.ipc.state.nImageType == EROSION) { STATE_TRAN(me, &me->showerosion); } else if(data.ipc.state.nImageType == DILATION) { STATE_TRAN(me, &me->showdilation); } else if(data.ipc.state.nImageType == LABELIMG) { STATE_TRAN(me, &me->showlabel); } 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_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.nResetProcessing = false; data.AddBufSize = 0; data.ipc.state.nExposureTime = 25; data.ipc.state.nStepCounter = 0; data.ipc.state.nThreshold = 0; 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*/ #if NUM_COLORS == 1 OscVisDebayerGreyscaleHalfSize(data.pCurRawImg, OSC_CAM_MAX_IMAGE_WIDTH, OSC_CAM_MAX_IMAGE_HEIGHT, ROW_YUYV, data.u8TempImage[SENSORIMG]); #else memcpy(data.u8TempImage[SENSORIMG], data.pCurRawImg, NUM_COLORS*OSC_CAM_MAX_IMAGE_HEIGHT*OSC_CAM_MAX_IMAGE_WIDTH); #endif /* Process the image. */ //set data buffer to zero before each step data.AddBufSize = 0; ProcessFrame(); return 0; } case IPC_SET_IMAGE_TYPE_EVT: { if(data.ipc.state.nImageType == SENSORIMG) { 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; }
/*********************************************************************//*! * @brief Initialize everything so the application is fully operable * after a call to this function. * * @return SUCCESS or an appropriate error code. *//*********************************************************************/ static OSC_ERR init(const int argc, const char * argv[]) { OSC_ERR err = SUCCESS; uint8 multiBufferIds[2] = {0, 1}; memset(&data, 0, sizeof(struct TEMPLATE)); /******* Create the framework **********/ err = OscCreate(&data.hFramework); if (err < 0) { fprintf(stderr, "%s: Unable to create framework.\n", __func__); return err; } /******* Load the framework module dependencies. **********/ err = OscLoadDependencies(data.hFramework, deps, sizeof(deps)/sizeof(struct OSC_DEPENDENCY)); if (err != SUCCESS) { fprintf(stderr, "%s: ERROR: Unable to load dependencies! (%d)\n", __func__, err); goto dep_err; } /********* Seed the random generator *************/ srand(OscSupCycGet()); #if defined(OSC_HOST) || defined(OSC_SIM) err = OscFrdCreateConstantReader(&data.hFileNameReader, TEST_IMAGE_FN); if (err != SUCCESS) { OscLog(ERROR, "%s: Unable to create constant file name reader for %s! (%d)\n", __func__, TEST_IMAGE_FN, err); goto frd_err; } err = OscCamSetFileNameReader(data.hFileNameReader); if (err != SUCCESS) { OscLog(ERROR, "%s: Unable to set file name reader for camera! (%d)\n", __func__, err); goto frd_err; } #endif /* OSC_HOST or OSC_SIM */ /* Set the camera registers to sane default values. */ err = OscCamPresetRegs(); if (err != SUCCESS) { OscLog(ERROR, "%s: Unable to preset camera registers! (%d)\n", __func__, err); goto fb_err; } /* Set up two frame buffers with enough space for the maximum * camera resolution in cached memory. */ err = OscCamSetFrameBuffer(0, OSC_CAM_MAX_IMAGE_WIDTH*OSC_CAM_MAX_IMAGE_HEIGHT, data.u8FrameBuffers[0], TRUE); if (err != SUCCESS) { OscLog(ERROR, "%s: Unable to set up first frame buffer!\n", __func__); goto fb_err; } err = OscCamSetFrameBuffer(1, OSC_CAM_MAX_IMAGE_WIDTH*OSC_CAM_MAX_IMAGE_HEIGHT, data.u8FrameBuffers[1], TRUE); if (err != SUCCESS) { OscLog(ERROR, "%s: Unable to set up second frame buffer!\n", __func__); goto fb_err; } /* Create a double-buffer from the frame buffers initilalized above.*/ err = OscCamCreateMultiBuffer(2, multiBufferIds); if (err != SUCCESS) { OscLog(ERROR, "%s: Unable to set up multi buffer!\n", __func__); goto mb_err; } /* Register an IPC channel to the CGI for the user interface. */ err = OscIpcRegisterChannel(&data.ipc.ipcChan, USER_INTERFACE_SOCKET_PATH, F_IPC_SERVER | F_IPC_NONBLOCKING); if (err != SUCCESS) { OscLog(ERROR, "%s: Unable to initialize IPC channel to web interface! (%d)\n", __func__, err); goto ipc_err; } err |= OscCamPerspectiveStr2Enum("DEFAULT", &data.perspective); if ( err != SUCCESS) { OscLog(ERROR, "%s: Invalid camera perspective.\n", __func__); goto per_err; } OscCamSetupPerspective( data.perspective); return SUCCESS; per_err: ipc_err: mb_err: fb_err: #if defined(OSC_HOST) || defined(OSC_SIM) frd_err: #endif OscUnloadDependencies(data.hFramework, deps, sizeof(deps)/sizeof(struct OSC_DEPENDENCY)); dep_err: OscDestroy(&data.hFramework); return err; }