?? cmapi.c
字號:
/* * cmAPI.c - Module for the 1670 implementation of the Camera Module * API interface * * Modification History: * * 1.3 - Added cmRegisterIoctl() to be able to set delays after * read/write and autobaud functions. */#include <stdio.h>#include "cmApi.h"#include "cmApiCom.h"#include "cmRegisterIO.h"#include "cmIniFile.h"#include "cmJpgHdr.h"/* Global variables */int g_debug = 0; /* debug level */char g_revision_string[] = "1.3";HANDLE g_comh; /* COM port handle */int g_videomode = 1; /* 1 = enabled, 0 = disabled */int g_packetsize = 2048; /* packet size on image transfers */int g_getImageMode = CM_PACKETCODES;int g_image_timeout = 3; /* timeout counter for get image call */Image_t g_defaultStill; /* default settings */Image_t g_defaultVideo; /* default settings *//* forward declarations */static int IsVideoEnabled(void);/* * Function: cmRevisionGet * * Description: Retrieve the API revision * * Inputs: None * Outputs: None * Returns: * revision string */char * cmRevisionGet(void){ return g_revision_string; }/* * Function cmIdGet * * Description: Retrieve the Camera ID * * id_p Pointer to location to store ID * * Outputs: * id_p The camera ID * Returns: * < 0 Error */int cmIdGet (unsigned int *id_p){ int status = 0; status = cmRegisterRead (CM_REG_ID, id_p); return status;}/* * Function: cmInit * * Description: Initialize the 1670 upon startup. * * Inputs: * comName For the 1670, this is the com port name "com1" or * "com2". * filename Name of an initialization file. Register settings * can be set here. If the filename field is NULL, do * not override any register values. * Outpus: None * Returns: * < 0 Error */int cmInit (char *comName, char *filename){ int status = 0; unsigned int data; /* Open up COM port */ if (comName != NULL) { g_comh = cmComOpen (comName); if (g_comh == INVALID_HANDLE_VALUE) { printf ("Could not open COM port, %s, Error = %d\n", comName, GetLastError()); status = -1; return status; } /* perform autobaud detection for RS232 */ cmAutoBaud (); } /* read initialization file */ if (filename != NULL) { status = cmIniFileRead (filename); if (status < 0) { printf ("\nError processing initialization file\n"); } } /* Initialize the image_t structures */ /* video */ cmRegisterRead (CM_REG_SZR_IN_W_VID, &data); g_defaultVideo.szr_in_w = data; cmRegisterRead (CM_REG_SZR_IN_H_VID, &data); g_defaultVideo.szr_in_h = data; cmRegisterRead (CM_REG_SZR_OUT_W_VID, &data); g_defaultVideo.szr_out_w = data; cmRegisterRead (CM_REG_SZR_OUT_H_VID, &data); g_defaultVideo.szr_out_h = data; /* still */ cmRegisterRead (CM_REG_SZR_IN_W_STL, &data); g_defaultStill.szr_in_w = data; cmRegisterRead (CM_REG_SZR_IN_H_STL, &data); g_defaultStill.szr_in_h = data; cmRegisterRead (CM_REG_SZR_OUT_W_STL, &data); g_defaultStill.szr_out_w = data; cmRegisterRead (CM_REG_SZR_OUT_H_STL, &data); g_defaultStill.szr_out_h = data; cmRegisterRead (CM_REG_UART_PCKT_SIZE, &data); g_packetsize = data; return status;}int cmImageSizeGet (int *width_p, int *height_p) { unsigned int data; int status = 0; cmRegisterRead (CM_REG_SZR_OUT_W_STL, &data); *width_p = data; cmRegisterRead (CM_REG_SZR_OUT_H_STL, &data); *height_p = data; return status;}/* * Function: cmImageGet * * Description: Retrieve an image from the imager * * The data is retrieved form the imager (jpeg format), and the * correct jpeg header is built. * * The initialization file would either have either started the * camera in video mode or still mode. If it is in still mode, the * application must call the cmImageSmap function to take a picture * * The data streamed from the 1670 is jpeg data, but it does not * include the header. Therefore, the header must be built. * * Inputs: * buffer A pointer to a buffer that is sized large enough for * the image. The size can be determined by the camera * ID type and the settings. For the 1670, the maximum * possible size can be 640*480*3. * nbytes A pointer to a location to put the length of the * data written to the buffer in bytes. * Outputs: * buffer jpeg data written to the buffer * nbytes length of data written to the buffer. * * Returns: * < 0 Error, no image * 0 No problems * > 0 Image status */int cmImageGet (unsigned char *buffer_p, int *length_p){ int status = 0;// fix this unsigned char inbuf[4096]; /* add 3 bytes for SOP EOP BRK */// end fix this int bytesread = 0; int i; int data_len = 0; /* # bytes in buffer */ int timeout_count = 0; unsigned char sop; /* start of packet protocol code */ unsigned char eop; /* end of packet protocol code */ int offset = 0; int eop_offset = 0; int image_done = 0; int bytecount = 0; int timeout = 0; cmFlowCtlSet (1); if (g_debug > 0) { fflush(NULL); } /* Read in the jpeg data. */ /* The only exit from this loop is if we have an EOP condition */ while (!image_done) { /* image not done */ bytecount = 0; timeout_count = 0; bytesread = 0; timeout = 0; /* note that the last packet nay be less than the packet size */ while (bytecount < (g_packetsize+3)) { status = cmComRecv (g_comh, &inbuf[bytecount], 4096, &bytesread); if (status < 0) { printf ("Read error retrieving image %d\n", GetLastError()); break; } if (bytesread == 0) { ++timeout_count; if (timeout_count > g_image_timeout) { /* this is primarily for the last packet whichis always less than the packet size */ timeout = 1; // not an error case. This happens every image. break; } //Sleep (20); continue; // otherwise continue } bytecount = bytecount + bytesread; if (g_debug > 0) { printf ("bytes = %d\n", bytecount); fflush(NULL); } } // end while //if (timeout == 1) break; /* done with this one */ /* * At this point, we have a full packet */ /* check if SOP is present */ sop = inbuf[0]; offset = 0; eop_offset = 0; switch (sop) { case 0x11: offset = 1; /* skip this byte */ eop_offset = eop_offset + 1; break; default: offset = 0; break; } eop = inbuf[bytecount - 2]; switch (eop) { case 0x04: /* end of the packet, use CREDIT flow control */ cmFlowCtlSet (1); eop_offset = eop_offset + 2; /* skip 3 end bytes*/ break; case 0x02: /* End of the image */ eop_offset = eop_offset + 2; image_done = 1; break; default: printf ("Warning: not EOP code \n"); eop_offset = eop_offset + 0; /* continue reading */ break; } /* check the EOP conditons */ for (i = 0; i < (bytecount - eop_offset); ++i) { buffer_p[(data_len+i)] = inbuf[i+offset]; } data_len = data_len + i; } if (g_debug > 0) { printf ("total bytes = %d\n",data_len); fflush(NULL); } *length_p = data_len; return status;}/* * Function: cmFlowCtlSet * * Description: Set the number of credits for flow control * * Inputs: * credits The number of credits. 0 = disable. * * Outputs: * None * * Return: * < 0 Error */int cmFlowCtlSet (int credits){ int status = 0; status = cmRegisterWrite (CM_REG_UART_CREDITS, credits); return status;}/* * Function: cmDestroy * * Description: Perform the opposite of cmInit * * Applications need to call this before they exit. It cleans up the * communication port. * * Inputs: None * * Outputs: None * * Returns: void */void cmDestroy (void){ cmComClose (g_comh);}/* * Function: cmDebugLevelSet * * Description: Set the debug level to control debug output * * Inputs: * debug_level 0 = no debug output * * Outputs: * None * Returns: void */void cmDebugLevelSet (int debug_level){ g_debug = debug_level; if (debug_level >= 5) { cmComIoctl (CMCOMIOCTL_TAG_DBG, &g_debug); }}/* * Function: IsVideoEnabled * * Description: Returns true if video mode is enabled * * The value is stored in a global variable g_videomode, so * that the register does not have to be read each time to get * the state. * * Input: None * Output: None * * Returns: * 1 Video Mode enabled * 0 Video Mode disabled */static int IsVideoEnabled(void){ int status = 1;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -