bool Camera::setPerspective(enum Camera::Perspective perspective) { // setup oscar wrapper enum EnOscCamPerspective oscPerspective; switch(perspective) { case DEFAULT: oscPerspective = OSC_CAM_PERSPECTIVE_DEFAULT; break; case HORIZONTAL_MIRROR: oscPerspective = OSC_CAM_PERSPECTIVE_HORIZONTAL_MIRROR; break; case VERTICAL_MIRROR: oscPerspective = OSC_CAM_PERSPECTIVE_VERTICAL_MIRROR; break; case ROTATE_180DEG: oscPerspective = OSC_CAM_PERSPECTIVE_180DEG_ROTATE; break; default: oscPerspective = OSC_CAM_PERSPECTIVE_DEFAULT; break; } return (this->lastError = OscCamSetupPerspective(oscPerspective)) != SUCCESS; }
/*********************************************************************//*! * @brief Initialize framework and system parameters * * @param s Pointer to the system state *//*********************************************************************/ void initSystem(struct SYSTEM *s) { OscCreate(&s->hFramework); /******* Load the framework module dependencies. **********/ OscLoadDependencies(s->hFramework, deps, sizeof(deps)/sizeof(struct OSC_DEPENDENCY)); OscLogSetConsoleLogLevel(WARN); OscLogSetFileLogLevel(WARN); #if defined(OSC_HOST) /* Setup file name reader (for host compiled version); read constant image */ OscFrdCreate(s->hFramework); OscFrdCreateConstantReader(&s->hFileNameReader, "EAN13Example.bmp"); OscCamSetFileNameReader(s->hFileNameReader); #endif /* Configure camera */ OscCamPresetRegs(); /* Set AGC and AEC */ OscCamSetRegisterValue(REG_AEC_AGC_ENABLE, 0x3); /* Turn on continuous capture for this application. */ OscCamSetRegisterValue(CAM_REG_CHIP_CONTROL, 0x388); /* Set the undocumented reserved almighty Micron register to the "optimal" value. */ OscCamSetRegisterValue(CAM_REG_RESERVED_0x20, 0x3d5); OscCamSetAreaOfInterest(0, 0, OSC_CAM_MAX_IMAGE_WIDTH, OSC_CAM_MAX_IMAGE_HEIGHT); OscCamSetupPerspective(OSC_CAM_PERSPECTIVE_180DEG_ROTATE); OscCamSetFrameBuffer(0, OSC_CAM_MAX_IMAGE_WIDTH * OSC_CAM_MAX_IMAGE_HEIGHT, s->frameBuffer1, TRUE); OscCamSetFrameBuffer(1, OSC_CAM_MAX_IMAGE_WIDTH * OSC_CAM_MAX_IMAGE_HEIGHT, s->frameBuffer2, TRUE); s->doubleBufferIDs[0] = 0; s->doubleBufferIDs[1] = 1; OscCamCreateMultiBuffer(2, s->doubleBufferIDs); OscCamSetupCapture(OSC_CAM_MULTI_BUFFER); } /* initSystem */
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; } }
/*********************************************************************//*! * @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; }
/*********************************************************************//*! * @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}; char strVersion[15]; struct CFG_KEY configKey; struct CFG_VAL_STR strCfg; #ifdef HAS_CPLD uint16 exposureDelay; #endif /* HAS_CPLD */ memset(&data, 0, sizeof(struct DATA)); /* Print software version */ GetVersionString( strVersion); fprintf(stderr, "Software rich-view version: %s\n", strVersion); /******* 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; } /* Set logging levels */ OscLogSetConsoleLogLevel(INFO); OscLogSetFileLogLevel(DEBUG); /* Print framework version */ OscGetVersionString( strVersion); OscLog(INFO, "Oscar framework version: %s\n", strVersion); /* Disable watchdog (probably activated from previous application) */ OscSupWdtInit(); OscSupWdtClose(); /* Set LED to green, util the idle state is entered */ OscGpioSetTestLed( TRUE); OscGpioSetTestLedColor(FALSE, TRUE); /* R, G*/ /* Register configuration file */ err = OscCfgRegisterFile(&data.hConfig, CONFIG_FILE_NAME, CONFIG_FILE_SIZE); if (err != SUCCESS) { OscLog(ERROR, "Cannot access config file.\n"); goto cfg_err; } /* Get perspective setting from config file- */ configKey.strSection = NULL; configKey.strTag = "PER"; strcpy( strCfg.str, ""); err = OscCfgGetStr( data.hConfig, &configKey, &strCfg); err |= OscCamPerspectiveStr2Enum( strCfg.str, &data.perspective); if( err != SUCCESS) { OscLog(WARN, "%s: No (valid) camera-scene perspective configured (%s). " "Use default (%s).\n", __func__, strCfg.str, OSC_CAM_PERSPECTIVE_DEFAULT); data.perspective = OSC_CAM_PERSPECTIVE_DEFAULT; } /* Get exposure time setting from configuration. */ configKey.strSection = NULL; configKey.strTag = "EXP"; err = OscCfgGetUInt32( data.hConfig, &configKey, &data.exposureTime); if( err != SUCCESS) { OscLog(WARN, "%s: No (valid) Exposure Time defined in configuration (%d). " "Use default (%d).\n", __func__, data.exposureTime, DEFAULT_EXPOSURE_TIME); data.exposureTime = DEFAULT_EXPOSURE_TIME; } #ifdef HAS_CPLD /* Get exposure delay setting from configuration. */ configKey.strSection = NULL; configKey.strTag = "DEL"; err = OscCfgGetUInt16Range( data.hConfig, &configKey, &exposureDelay, 0, FINECLK2CLK_RATIO-1); data.exposureDelay = exposureDelay & 0x00ff; if( err != SUCCESS) { OscLog(WARN, "%s: No (valid) Exposure Delay defined in configuration (%d). " "Use default (%d).\n", __func__, data.exposureDelay, DEFAULT_EXPOSURE_DELAY); data.exposureDelay = DEFAULT_EXPOSURE_DELAY; } #endif /* HAS_CPLD */ #ifdef HAS_CPLD /* Get firmware version */ err = OscCpldRget(OSC_LGX_FWREV, &data.firmwareRevision); if(err != SUCCESS) { OscLog(ERROR, "Cannot read firmware version. (%d)\n", err); goto cpld_err; } /* Apply exposure delay to CPLD and disable. */ err = OscCpldRset(OSC_LGX_CLKDELAY, (const uint8)(data.exposureDelay & !OSC_LGX_CLKDELAY_ENABLE)); if(err != SUCCESS) { OscLog(ERROR, "Cannot disable clock-delay in CPLD.\n"); goto cpld_err; } /* Set CPLD to synchronous mode. */ err = OscCpldFset(OSC_LGX_VARCTRL, OSC_LGX_VARCTRL_SYNCOUT, OSC_LGX_VARCTRL_SYNCOUT); if(err != SUCCESS) { OscLog(ERROR, "Cannot set CPLD to synchronous mode.\n"); goto cpld_err; } #endif /* HAS_CPLD */ /* 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 cam_err; } /* Set up two frame buffers with enough space for the maximum * camera resolution in cached memory. */ err = OscCamSetFrameBuffer(0, IMAGE_AERA, data.u8FrameBuffers[0], TRUE); if (err != SUCCESS) { OscLog(ERROR, "%s: Unable to set up first frame buffer!\n", __func__); goto cam_err; } err = OscCamSetFrameBuffer(1, IMAGE_AERA, data.u8FrameBuffers[1], TRUE); if (err != SUCCESS) { OscLog(ERROR, "%s: Unable to set up second frame buffer!\n", __func__); goto cam_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; } OscCamSetupPerspective( data.perspective); /* Make the register file known to the communication protocol. */ data.comm.pRegFile = regfile; data.comm.nRegs = (sizeof(regfile)/sizeof(struct CBP_PARAM)); /* Init communication sockets. */ err = Comm_Init(&data.comm); if (err != SUCCESS) { OscLog(ERROR, "Communication initialization failed.\n"); goto comm_err; } return SUCCESS; comm_err: cfg_err: #ifdef HAS_CPLD cpld_err: #endif /* HAS_CPLD */ mb_err: cam_err: OscUnloadDependencies(data.hFramework, deps, sizeof(deps)/sizeof(struct OSC_DEPENDENCY)); dep_err: OscDestroy(&data.hFramework); return err; }
/*********************************************************************//*! * @brief Program entry. * * @param argc Command line argument count. * @param argv Command line argument string. * @return 0 on success *//*********************************************************************/ int main(const int argc, const char * argv[]) { /* Handle to framework instance. */ void *hFramework; #if defined(OSC_HOST) || defined(OSC_SIM) /* Handle to file name reader, if compiled for host platform. */ void *hFileNameReader; #endif /* Camera parameters */ uint32 shutterWidth; uint16 x, y, width, height; uint16 blackOffset; uint8 bufferIDs[2]; /* Frame buffers. */ uint8 frameBuffer0[OSC_CAM_MAX_IMAGE_WIDTH * OSC_CAM_MAX_IMAGE_HEIGHT]; uint8 frameBuffer1[OSC_CAM_MAX_IMAGE_WIDTH * OSC_CAM_MAX_IMAGE_HEIGHT]; /* Pointer to captured picture */ void *pic; struct OSC_PICTURE p; OSC_ERR err = SUCCESS; /* Create framework */ OscCreate(&hFramework); /* Load camera module */ OscCamCreate(hFramework); /* Load GPIO module */ OscGpioCreate(hFramework); p.width = OSC_CAM_MAX_IMAGE_WIDTH; p.height = OSC_CAM_MAX_IMAGE_HEIGHT; p.type = OSC_PICTURE_GREYSCALE; /* Get camera settings */ OscCamGetShutterWidth(&shutterWidth); OscCamGetAreaOfInterest(&x, &y, &width, &height); OscCamGetBlackLevelOffset(&blackOffset); printf("ShutterWidth=%lu\n",shutterWidth); printf("AreaOfInterest: x=%u y=%u width=%u height=%u\n", x, y, width, height); printf("BlackLevelOffset=%u\n",blackOffset); /* Process settings */ /* ---------------- */ /* Set new camera settings */ shutterWidth = 50000; /* set shutter to 50ms, 0 => automatic */ OscCamSetShutterWidth(shutterWidth); OscCamSetAreaOfInterest(0,0,OSC_CAM_MAX_IMAGE_WIDTH, OSC_CAM_MAX_IMAGE_HEIGHT); OscCamSetBlackLevelOffset(blackOffset); OscCamSetupPerspective(OSC_CAM_PERSPECTIVE_VERTICAL_MIRROR); #if defined(OSC_HOST) || defined(OSC_SIM) /* Setup file name reader if compiled for host platform */ OscFrdCreateConstantReader(&hFileNameReader, "imgCapture.bmp"); OscCamSetFileNameReader(hFileNameReader); #endif /* Setup framebuffers - double buffering */ OscCamSetFrameBuffer(0, OSC_CAM_MAX_IMAGE_WIDTH*OSC_CAM_MAX_IMAGE_HEIGHT, frameBuffer0, TRUE); OscCamSetFrameBuffer(1, OSC_CAM_MAX_IMAGE_WIDTH*OSC_CAM_MAX_IMAGE_HEIGHT, frameBuffer1, TRUE); bufferIDs[0] = 0; bufferIDs[1] = 1; OscCamCreateMultiBuffer(2, bufferIDs); /* Setup capture to first frame buffer */ err = OscCamSetupCapture(OSC_CAM_MULTI_BUFFER); if(err != SUCCESS){ printf("%s: Unable to setup initial capture (%d)!\n", __func__, err); return err; } /* Trigger image capturing */ err = OscGpioTriggerImage(); if(err != SUCCESS){ printf("%s: Unable to trigger capture (%d)!\n", __func__, err); return err; } /* Do something ... */ /* ---------------- */ /* Read Picture */ err = OscCamReadPicture(OSC_CAM_MULTI_BUFFER, (void *) &pic, 0, 0); if(err != SUCCESS){ printf("%s: Unable to read picture (%d)!\n", __func__, err); return err; } /* Process picture */ /* --------------- */ /* Capture picture to second frame buffer */ err = OscCamSetupCapture(OSC_CAM_MULTI_BUFFER); if(err != SUCCESS){ printf("%s: Unable to setup initial capture (%d)!\n", __func__, err); return err; } /* Do something ... */ /* ---------------- */ err = OscCamReadPicture(OSC_CAM_MULTI_BUFFER, (void *) &pic, 0, 0); if(err != SUCCESS){ printf("%s: Unable to read picture (%d)!\n", __func__, err); return err; } /* Process picture */ /* --------------- */ /* Unload camera module */ OscCamDestroy(hFramework); /* Unload GPIO module */ OscGpioDestroy(hFramework); /* Destroy framework */ OscDestroy(hFramework); return 0; }