/*********************************************************************//*! * @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; }
HRESULT CMatrixBox::OnItemDragEnd(CXFrame* pEventSource) { MarkRelayout(); _tagMatrixItemMoved info; info.pItem = pEventSource; info.nStard = m_nDragLastIndex; info.nEnd = GetIndexByItem((CMatrixItem*)pEventSource); ThrowEvent(EVENT_MATRIX_ITEM_MOVED,&info); SaveChildDC(FALSE); return S_OK; }
/*********************************************************************//*! * @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; }
HRESULT CMatrixBox::OnItemDrag(CMatrixItem* pEventSource) { if(m_pParentFrame == NULL) { return E_FAIL; } CPoint pt; GetCursorPos(&pt); CRect rcScreen = GetScreenRect(); CRect rcScreenParent = m_pParentFrame->GetScreenRect(); CRect rcClip; ::IntersectRect(&rcClip,&rcScreen,&rcScreenParent); CRect rcDrag = pEventSource->GetDragRect(); int nWidth = rcDrag.Width()/2 + 9 ; int nHeight = rcDrag.Height()/2 + 3; CRect rcDragItem = pEventSource->GetRect(); int nOverFlow = 0; if(pt.x < rcClip.left + nWidth) { nOverFlow = 1; pt.x = rcClip.left + nWidth; } if(pt.y < rcClip.top + nHeight) { nOverFlow = 2; pt.y = rcClip.top + nHeight; } if(pt.x > rcClip.right - (rcDragItem.Width() - nWidth)) { nOverFlow = 3; pt.x = rcClip.right - (rcDragItem.Width() - nWidth); } if(pt.y > rcClip.bottom - (rcDragItem.Height() - nHeight)) { nOverFlow = 4; pt.y = rcClip.bottom - (rcDragItem.Height() - nHeight); } pt.x = pt.x - nWidth - rcScreen.left ; pt.y = pt.y - nHeight - rcScreen.top ; CRect rcOldPaint = pEventSource->GetPaintRect(); pEventSource->SetPosition(pt); pt.x += nWidth; pt.y += nHeight; vector<CXFrame*> vec = m_vecItem; int nIndexMine = 0; for(vector<CXFrame*>::iterator it = vec.begin();it != vec.end();it++) { if(*it == pEventSource) { vec.erase(it); break; } nIndexMine ++; } BOOL bMoved = FALSE; int nIndex = 0; for(vector<CXFrame*>::iterator it = vec.begin();it != vec.end();it++) { CMatrixItem* pItem = (CMatrixItem*)*it; CRect rcItem = pItem->GetRect(); if(pItem->GetEnableDrag() && rcItem.PtInRect(pt)) { if(nIndex < nIndexMine) { vec.insert(it,pEventSource); } else if(nIndex == vec.size() - 1) { vec.push_back(pEventSource); } else { vec.insert(it+1,pEventSource); } bMoved = TRUE; break; } nIndex ++; } if(bMoved) { m_vecItem.clear(); m_vecItem = vec; Relayout(); QuickPaint(GetPaintRect()); } else { pEventSource->Relayout(); CRect rcDst; CRect rcCur = pEventSource->GetPaintRect(); UnionRect(&rcDst,&rcOldPaint,&rcCur); QuickPaint(rcDst); } if(nOverFlow) { ThrowEvent(EVENT_MATRIX_DRAG_OVERFLOW,&nOverFlow); } return S_OK; }
OSC_ERR SetConfigRegister(void *pMainState, struct CBP_PARAM *pReg) { OSC_ERR err; struct CFG_KEY configKey; struct CFG_VAL_STR strCfg; struct MainState *pHsm = (struct MainState *)pMainState; #ifdef HAS_CPLD uint8 cpldReg; int exposureDelay; #endif /* HAS_CPLD */ #ifdef UNSUPPORTED int i; #endif /* UNSUPPORTED */ switch(pReg->id) { case REG_ID_AQUISITION_MODE: if(pReg->val == 0) { ThrowEvent(pHsm, CMD_GO_IDLE_EVT); break; } if(pReg->val == 1) { ThrowEvent(pHsm, CMD_GO_ACQ_EVT); break; } return -EUNSUPPORTED; case REG_ID_TRIGGER_MODE: if(pReg->val == 0) { ThrowEvent(pHsm, CMD_USE_INTERN_TRIGGER_EVT); break; } if(pReg->val == 1) { ThrowEvent(pHsm, CMD_USE_EXTERN_TRIGGER_EVT); break; } return -EUNSUPPORTED; case REG_ID_EXP_TIME: /* Apply exposure time and store to configuration. */ data.exposureTime = pReg->val; /* Apply value */ err = OscCamSetShutterWidth( data.exposureTime); if( err != SUCCESS) { OscLog(ERROR, "%s: Failed to modify exposure time! (%d)\n", __func__, err); break; } OscLog(INFO, "%s: Exposure time stored and applied to %d us\n", __func__, data.exposureTime); /* Store to configuration. */ configKey.strSection = NULL; configKey.strTag = "EXP"; sprintf(strCfg.str, "%ld", data.exposureTime); err = OscCfgSetStr( data.hConfig, &configKey, strCfg.str); err |= OscCfgFlushContent(data.hConfig); return err; #ifdef UNSUPPORTED /* This code is not yet ported to the new communication scheme and thus unsupported. */ case REG_ID_MAC_ADDR: /* Store Mac/Ip persistent. Applied on next reboot. */ for (i=0; i<6; i++) { macAddr[i] = (uint8)data.cmdpkt.data[i]; } /* Compose strings */ sprintf(strMac, "%02x:%02x:%02x:%02x:%02x:%02x", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]); OscLog(INFO, "Set MAC addr environment variable: %s (one time programmable)\n",strMac); /* Write to persistent u-boot environment ethaddr. */ pF = fopen("/tmp/mac", "w"); fprintf(pF, "%s", strMac); fflush(pF); fclose(pF); system("fw_setenv ethaddr `more /tmp/mac`"); case CmdPerspective: /* Apply perspective setting and store to configuration */ data.perspective = (enum EnOscCamPerspective)data.cmdpkt.data[0]; /* Apply to sensor */ err = OscCamSetupPerspective( data.perspective); /* Store to configuration */ configKey.strSection = NULL; configKey.strTag = "PER"; err = OscCamPerspectiveEnum2Str( data.perspective, strCfg.str); err = OscCfgSetStr( data.hConfig, &configKey, strCfg.str); err |= OscCfgFlushContent(data.hConfig); break; #endif /* UNSUPPORTED */ return -EUNSUPPORTED; #ifdef HAS_CPLD case REG_ID_EXP_DELAY: /* Apply exposure delay to CPLD. Keep enable bit as currently set. */ exposureDelay = pReg->val; if (exposureDelay > 99) { OscLog(ERROR, "Invalid exposure delay value (%d). Valid range: 0..99\n", exposureDelay); return -EINVALID_PARAMETER; } /* Store to data struct */ data.exposureDelay = exposureDelay; /* Apply to CPLD. Preserve current enable bit state. */ err = OscCpldRget(OSC_LGX_CLKDELAY, &cpldReg); cpldReg = OSC_LGX_CLKDELAY_ENABLE; if( cpldReg & OSC_LGX_CLKDELAY_ENABLE) { cpldReg = exposureDelay | OSC_LGX_CLKDELAY_ENABLE; } else { cpldReg = exposureDelay; } err |= OscCpldRset(OSC_LGX_CLKDELAY, cpldReg); if( err != SUCCESS) { OscLog(ERROR, "%s: Failed to apply exposure delay to CPLD!\n", __func__); return err; } OscLog(INFO, "%s: Exposure applied to CPLD: %d fine clocks.\n", __func__, data.exposureDelay); return SUCCESS; case REG_ID_STORE_CUR_EXP_DELAY: /* Read current fine clock position from CPLD. Store this offset in * configuration and apply to CPLD exposure delay. */ err = OscCpldRget(OSC_LGX_FASTCLKCOUNT, &cpldReg); exposureDelay = cpldReg; /* Value 0 is reserved with the current CPLD version */ if( 0 == exposureDelay) { exposureDelay++; } OscLog(INFO, "%s: Read current fine clock position from CPLD: %d\n", __func__, exposureDelay); /* Store exposure delay to configuration. */ configKey.strSection = NULL; configKey.strTag = "DEL"; sprintf(strCfg.str, "%d", exposureDelay); err = OscCfgSetStr( data.hConfig, &configKey, strCfg.str); err |= OscCfgFlushContent(data.hConfig); if( err != SUCCESS) { OscLog(ERROR, "%s: Failed to store exposure delay to configuration!\n", __func__); break; } OscLog(INFO, "%s: Exposure delay stored to configuration: %d fine clocks.\n", __func__, exposureDelay); /* Apply delay to CPLD. Preserve the current enable bit state */ err = OscCpldRget(OSC_LGX_CLKDELAY, &cpldReg); cpldReg = OSC_LGX_CLKDELAY_ENABLE; if( cpldReg & OSC_LGX_CLKDELAY_ENABLE) { cpldReg = exposureDelay | OSC_LGX_CLKDELAY_ENABLE; } else { cpldReg = exposureDelay; } err |= OscCpldRset(OSC_LGX_CLKDELAY, cpldReg); if( err != SUCCESS) { OscLog(ERROR, "%s: Failed to apply exposure delay to CPLD!\n", __func__); break; } OscLog(INFO, "%s: Exposure applied to CPLD: %d fine clocks.\n", __func__, exposureDelay); break; #endif /* HAS_CPLD */ default: OscLog(WARN, "%s: Invalid register (%#x)!\n", __func__, pReg->id); return -EUNSUPPORTED; } /* Evaluate the success or failure of the commands that invoked the state machine. */ if(data.comm.enReqState == REQ_STATE_ACK_PENDING) { /* Success. */ return SUCCESS; } else if(data.comm.enReqState == REQ_STATE_NACK_PENDING) { return -EDEVICE; } else { OscLog(ERROR, "%s: Change of register %d was not handled by the state machine!\n", __func__, pReg->id); return -EDEVICE; } }
OSC_ERR StateControl( void) { OSC_ERR err; MainState mainState; uint8 *pCurRawImg = NULL; /* Setup main state machine. Start with idle mode. */ MainStateConstruct(&mainState); HsmOnStart((Hsm *)&mainState); /*----------- infinite main loop */ while( TRUE) { /*----------- wait for captured picture */ while (TRUE) { /*----------- Alternating a) check for new connections * b) check for commands (and do process) * c) check for available picture */ err = Comm_AcceptConnections(&data.comm, ACCEPT_CONNS_TIMEOUT); if(err != SUCCESS && err != -ETIMEOUT) { OscLog(ERROR, "%s: Error accepting new connections (%d)!\n", __func__, err); } err = Comm_HandleCommands(&data.comm, &mainState, GET_CMDS_TIMEOUT); if(err != SUCCESS && err != -ETIMEOUT) { OscLog(ERROR, "%s: Error handling commands (%d)!\n", __func__, err); } else if(err == SUCCESS) { OscLog(INFO, "Command received.\n"); } err = OscCamReadPicture(OSC_CAM_MULTI_BUFFER, &pCurRawImg, 0, CAMERA_TIMEOUT); if ((err != -ETIMEOUT) ||(err != -ENO_CAPTURE_STARTED) ) { /* Anything other than a timeout or no pending capture means that we should * stop trying and analyze the situation. */ break; } } if( err == SUCCESS) /* only if breaked due to CamReadPic() */ { data.pCurRawImg = pCurRawImg; OscLog(DEBUG, "---image available\n"); } else { pCurRawImg = NULL; } /*----------- process frame by state engine (pre-setup) Sequentially with next capture */ if( pCurRawImg) { ThrowEvent(&mainState, FRAMESEQ_EVT); } /*----------- prepare next capture */ if( pCurRawImg) { err = OscCamSetupCapture( OSC_CAM_MULTI_BUFFER); if (err != SUCCESS) { OscLog(ERROR, "%s: Unable to setup capture (%d)!\n", __func__, err); break; } } /*----------- do self-triggering (if required) */ ThrowEvent(&mainState, TRIGGER_EVT); /*----------- process frame by state engine (post-setup) Parallel with next capture */ if( pCurRawImg) { ThrowEvent(&mainState, FRAMEPAR_EVT); } } /* end while ever */ return SUCCESS; }
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; }