エレキジャック Web版 マイコン・カーを製作してみよう<21> マイコンカー制御プログラムです。http://www.eleki-jack.com/arm/2012/10/21.html このプログラムは次回の記事でアップされるパソコン用プログラムを使ってマイコンカーを制御するプログラムです。マイコンカーとパソコンの通信はXbeeを使っています。
Revision 4:887ced27d5d6, committed 2012-05-07
- Comitter:
- sunifu
- Date:
- Mon May 07 05:57:12 2012 +0000
- Parent:
- 3:ba97046adcd1
- Child:
- 5:7f85cf99d0f6
- Commit message:
- 0.0.1
Changed in this revision
--- a/CameraC1098.cpp Sun May 06 10:54:43 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,495 +0,0 @@ -/** - * C1098-SS device driver class (Version 1.0) - * Reference documents: C1098-SS User Manual v1.0 2012.4.1 - * - * CameraC328Library - * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) - * http://shinta.main.jp/ - * - * CameraC1098-SS Library - * Copyright (C) 2012 Tadao Iida - */ - -#include "CameraC1098.h" - -#define WAITIDLE waitIdle -#define SENDFUNC sendBytes -#define RECVFUNC recvBytes -#define WAITFUNC waitRecv - -/** - * Constructor. - * - * @param tx A pin for transmit. - * @param rx A pin for receive. - * @param baud Baud rate. (Default is Baud14400.) - */ -CameraC1098::CameraC1098(PinName tx, PinName rx, Baud baud) : serial(tx, rx) { - serial.baud((int)baud); -} - -/** - * Destructor. - */ -CameraC1098::~CameraC1098() { -} - -/** - * Make a sync. for baud rate. - */ -CameraC1098::ErrorNumber CameraC1098::sync() { - for (int i = 0; i < SYNCMAX; i++) { - if (NoError == sendSync()) { - if (NoError == recvAckOrNck()) { - if (NoError == recvSync()) { - if (NoError == sendAck(0x0D, 0x00)) { - /* - * After synchronization, the camera needs a little time for AEC and AGC to be stable. - * Users should wait for 1-2 seconds before capturing the first picture. - */ - wait(2); - return NoError; - } - } - } - } - wait_ms(50); - } - return UnexpectedReply; -} - -/** - * Initialize. - * - * @param baud Camera Interface Speed. - * @param jr JPEG resolution. - */ -CameraC1098::ErrorNumber CameraC1098::init(Baud baud,JpegResolution jr) { - int i ; - ErrorNumber en; - WAITIDLE(); - setmbedBaud((Baud)(0x07)) ; - - for ( i = 1 ; i < 7 ; i++ ) { - if ( NoError == sendSync() ) { - if ( NoError == recvAckOrNck() ){ - if ( NoError == recvSync() ) { - if ( NoError == sendAck(0x0D, 0x00) ) { - en = sendInitial(baud,jr); - if (NoError != en) { - return en; - } - en = recvAckOrNck(); - if (NoError != en) { - return en; - } - wait_ms(50) ; - static bool alreadySetupPackageSize = false; - if (!alreadySetupPackageSize) { - en = sendSetPackageSize(packageSize); - if (NoError != en) { - return en; - } - WAITFUNC(); - en = recvAckOrNck(); - if (NoError != en) { - return en; - } - alreadySetupPackageSize = true; - } - setmbedBaud(baud); - wait(2.0); - return (ErrorNumber)NoError; - /* - * After synchronization, the camera needs a little time for AEC and AGC to be stable. - * Users should wait for 1-2 seconds before capturing the first picture. - */ - } - } - }else{ - setmbedBaud((Baud)(i+1)) ; - } - } - wait_ms(50); - } - return UnexpectedReply; -} - - -/** - * Get JPEG snapshot picture. - * - * @param func A pointer to a callback function. - * You can block this function until saving the image datas. - * @return Status of the error. - */ -CameraC1098::ErrorNumber CameraC1098::getJpegSnapshotPicture(void(*func)(char *buf, size_t siz)) { - WAITIDLE(); - ErrorNumber en; - - - en = sendSnapshot(); - if (NoError != en) { - return en; - } - WAITFUNC(); - en = recvAckOrNck(); - if (NoError != en) { - return en; - } - - en = sendGetPicture(); - if (NoError != en) { - return en; - } - WAITFUNC(); - en = recvAckOrNck(); - if (NoError != en) { - return en; - } - - /* - * Data : snapshot picture - */ - uint32_t length = 0; - WAITFUNC(); - en = recvData(&length); - if (NoError != en) { - return en; - } - en = sendAck(0x00, 0); - if (NoError != en) { - return en; - } - - char databuf[packageSize - 6]; - uint16_t pkg_total = length / (packageSize - 6); - for (int i = 0; i <= (int)pkg_total; i++) { - uint16_t checksum = 0; - // ID. - char idbuf[2]; - WAITFUNC(); - if (!RECVFUNC(idbuf, sizeof(idbuf))) { - return (ErrorNumber)UnexpectedReply; - } - checksum += idbuf[0]; - checksum += idbuf[1]; - uint16_t id = (idbuf[1] << 8) | (idbuf[0] << 0); - if (id != i) { - return (ErrorNumber)UnexpectedReply; - } - - // Size of the data. - char dsbuf[2]; - WAITFUNC(); - if (!RECVFUNC(dsbuf, sizeof(dsbuf))) { - return (ErrorNumber)UnexpectedReply; - } - - // Received the data. - checksum += dsbuf[0]; - checksum += dsbuf[1]; - uint16_t ds = (dsbuf[1] << 8) | (dsbuf[0] << 0); - WAITFUNC(); - if (!RECVFUNC(&databuf[0], ds)) { - return (ErrorNumber)UnexpectedReply; - } - for (int j = 0; j < ds; j++) { - checksum += databuf[j]; - } - - // Verify code. - char vcbuf[2]; - WAITFUNC(); - if (!RECVFUNC(vcbuf, sizeof(vcbuf))) { - return (ErrorNumber)UnexpectedReply; - } - uint16_t vc = (vcbuf[1] << 8) | (vcbuf[0] << 0); - if (vc != (checksum & 0xff)) { - return (ErrorNumber)UnexpectedReply; - } - - /* - * Call a call back function. - * You can block this function while working. - */ - func(databuf, ds); - - /* - * We should wait for camera working before reply a ACK. - */ - wait_ms(100); - en = sendAck(0x00, 1 + i); - if (NoError != en) { - return en; - } - } - - return (ErrorNumber)NoError; -} - - - -CameraC1098::ErrorNumber CameraC1098::sendInitial(Baud baud, JpegResolution jr) { - char send[COMMAND_LENGTH]; - - send[0] = 0xAA; - send[1] = 0x01; - send[2] = (char)baud; - send[3] = 0x07; - send[4] = 0x00; - send[5] = (char)jr; - - if (!SENDFUNC(send, sizeof(send))) { - return (ErrorNumber)SendRegisterTimeout; - } - - return (ErrorNumber)NoError; -} - -CameraC1098::ErrorNumber CameraC1098::sendGetPicture(void) { - char send[COMMAND_LENGTH]; - - send[0] = 0xAA; - send[1] = 0x04; - send[2] = 0x01; - send[3] = 0x00; - send[4] = 0x00; - send[5] = 0x00; - - if (!SENDFUNC(send, sizeof(send))) { - return (ErrorNumber)SendRegisterTimeout; - } - return (ErrorNumber)NoError; -} - -CameraC1098::ErrorNumber CameraC1098::sendSnapshot(void) { - char send[COMMAND_LENGTH]; - send[0] = 0xAA; - send[1] = 0x05; - send[2] = 0x00; - send[3] = 0x00; - send[4] = 0x00; - send[5] = 0x00; - - if (!SENDFUNC(send, sizeof(send))) { - return (ErrorNumber)SendRegisterTimeout; - } - return (ErrorNumber)NoError; -} - -CameraC1098::ErrorNumber CameraC1098::sendSetPackageSize(uint16_t packageSize) { - char send[COMMAND_LENGTH]; - send[0] = 0xAA; - send[1] = 0x06; - send[2] = 0x08; - send[3] = (packageSize >> 0) & 0xff; - send[4] = (packageSize >> 8) & 0xff; - send[5] = 0x00; - - if (!SENDFUNC(send, sizeof(send))) { - return (ErrorNumber)SendRegisterTimeout; - } - return (ErrorNumber)NoError; -} - -void CameraC1098::setmbedBaud(Baud baud) { - switch((int)baud){ - case 2: - serial.baud(460800); - break; - case 3: - serial.baud(230400); - break; - case 4: - serial.baud(115200); - break; - case 5: - serial.baud(57600); - break; - case 6: - serial.baud(28800); - break; - case 7: - serial.baud(14400); - break; - default: - serial.baud(14400); - } -} - - -CameraC1098::ErrorNumber CameraC1098::sendReset(ResetType specialReset) { - char send[COMMAND_LENGTH]; - send[0] = 0xAA; - send[1] = 0x08; - send[2] = 0x00; - send[3] = 0x00; - send[4] = 0x00; - send[5] = specialReset; - /* - * Special reset : If the parameter is 0xFF, the command is a special Reset command and the firmware responds to it immediately. - */ - - if (!SENDFUNC(send, sizeof(send))) { - return (ErrorNumber)SendRegisterTimeout; - } - - return (ErrorNumber)NoError; -} - - -CameraC1098::ErrorNumber CameraC1098::recvData(uint32_t *length) { - char recv[COMMAND_LENGTH]; - if (!RECVFUNC(recv, sizeof(recv))) { - return (ErrorNumber)UnexpectedReply; - } - if ((0xAA != recv[0]) || (0x0A != recv[1])) { - return (ErrorNumber)UnexpectedReply; - } - recv[2] = (char)0x01; - *length = (recv[5] << 16) | (recv[4] << 8) | (recv[3] << 0); - return (ErrorNumber)NoError; -} - -CameraC1098::ErrorNumber CameraC1098::sendSync() { - char send[COMMAND_LENGTH]; - send[0] = 0xAA; - send[1] = 0x0D; - send[2] = 0x00; - send[3] = 0x00; - send[4] = 0x00; - send[5] = 0x00; - - - if (!SENDFUNC(send, sizeof(send))) { - return (ErrorNumber)SendRegisterTimeout; - } - - return (ErrorNumber)NoError; -} - -CameraC1098::ErrorNumber CameraC1098::recvSync() { - char recv[COMMAND_LENGTH]; - - if (!RECVFUNC(recv, sizeof(recv))) { - return (ErrorNumber)UnexpectedReply; - } - if ((0xAA != recv[0]) || (0x0D != recv[1])) { - return (ErrorNumber)UnexpectedReply; - } - - return (ErrorNumber)NoError; -} - -/** - * Send ACK. - * - * @param commandId The command with that ID is acknowledged by this command. - * @param packageId For acknowledging Data command, these two bytes represent the requested package ID. While for acknowledging other commands, these two bytes are set to 00h. - */ -CameraC1098::ErrorNumber CameraC1098::sendAck(uint8_t commandId, uint16_t packageId) { - char send[COMMAND_LENGTH]; - - send[0] = 0xAA; - send[1] = 0x0E; - send[2] = commandId; - send[3] = 0x00; // ACK counter is not used. - send[4] = (packageId >> 0) & 0xff; - send[5] = (packageId >> 8) & 0xff; - if (!SENDFUNC(send, sizeof(send))) { - return (ErrorNumber)SendRegisterTimeout; - } - return (ErrorNumber)NoError; -} - -/** - * Receive ACK or NCK. - * - * @return Error number. - */ -CameraC1098::ErrorNumber CameraC1098::recvAckOrNck() { - char recv[COMMAND_LENGTH]; - - if (!RECVFUNC(recv, sizeof(recv))) { - return (ErrorNumber)UnexpectedReply; - } - if ((0xAA == recv[0]) && (0x0E == recv[1])) { - return (ErrorNumber)NoError; - } - if ((0xAA == recv[0]) && (0x0F == recv[1]) && (0x00 == recv[2])) { - return (ErrorNumber)NoError; - } - if ((0xAA == recv[0]) && (0x0F == recv[1])) { - return (ErrorNumber)recv[4]; - } - - return (ErrorNumber)UnexpectedReply; -} - -/** - * Send bytes to camera module. - * - * @param buf Pointer to the data buffer. - * @param len Length of the data buffer. - * - * @return True if the data sended. - */ -bool CameraC1098::sendBytes(char *buf, size_t len, int timeout_us) { - for (uint32_t i = 0; i < (uint32_t)len; i++) { - int cnt = 0; - while (!serial.writeable()) { - wait_us(1); - cnt++; - if (timeout_us < cnt) { - return false; - } - } - serial.putc(buf[i]); - } - return true; -} - -/** - * Receive bytes from camera module. - * - * @param buf Pointer to the data buffer. - * @param len Length of the data buffer. - * - * @return True if the data received. - */ -bool CameraC1098::recvBytes(char *buf, size_t len, int timeout_us) { - for (uint32_t i = 0; i < (uint32_t)len; i++) { - int cnt = 0; - while (!serial.readable()) { - wait_us(1); - cnt++; - if (timeout_us < cnt) { - return false; - } - } - buf[i] = serial.getc(); - } - return true; -} - -/** - * Wait received. - * - * @return True if the data received. - */ -bool CameraC1098::waitRecv() { - while (!serial.readable()) { - } - return true; -} - -/** - * Wait idle state. - */ -bool CameraC1098::waitIdle() { - while (serial.readable()) { - serial.getc(); - } - return true; -}
--- a/CameraC1098.h Sun May 06 10:54:43 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,136 +0,0 @@ -/** - * C1098-SS device driver class (Version 1.0) - * Reference documents: C1098-SS User Manual v1.0 2012.5.6 - * - * CameraC328Library - * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) - * http://shinta.main.jp/ - * - * CameraC1098-SS Library - * Copyright (C) 2012 Tadao Iida - */ - -#include "mbed.h" -#include "SerialBuffered.h" - -#ifndef _CAMERA_C1098_H_ -#define _CAMERA_C1098_H_ - -/* - * Class: CameraC1098 - */ - -class CameraC1098 { -public: - - /** - * JPEG resolution. - */ - enum JpegResolution { - JpegResolution80x64 = 0x01, // unofficial - JpegResolution160x128 = 0x03, // unofficial - JpegResolution320x240 = 0x05, // QVGA - JpegResolution640x480 = 0x07 // VGA - }; - - /** - * Error number. - */ - enum ErrorNumber { - NoError = 0x00, - UnexpectedReply = 0x04, - ParameterError = 0x0b, - SendRegisterTimeout = 0x0c, - CommandIdError = 0x0d, - CommandHeaderError = 0xf0, - SetTransferPackageSizeWrong = 0x11 - }; - - /** - * Baud rate. - */ - enum Baud { - Baud460800 = 0x02, - Baud230400 = 0x03, - Baud115200 = 0x04, - Baud57600 = 0x05, - Baud28800 = 0x06, - Baud14400 = 0x07 // Default. - }; - - /** - * Reset type. - */ - enum ResetType { - Nomal = 0x00, - High = 0xff - }; - - /** - * Constructor. - * - * @param tx A pin for transmit. - * @param rx A pin for receive. - * @param baud Baud rate. (Default is Baud14400.) - */ - CameraC1098(PinName tx, PinName rx, Baud baud = Baud14400); - - /** - * Destructor. - */ - ~CameraC1098(); - - /** - * Make a sync. for baud rate. - */ - ErrorNumber sync(); - - /** - * Initialize. - * - * @param baud Camera Interface Speed. - * @param jr JPEG resolution. - */ - ErrorNumber init(Baud baud, JpegResolution jr); - - /** - * Get JPEG snapshot picture. - * - * @param func A pointer to a callback function. - * You can block this function until saving the image datas. - * @return Status of the error. - */ - ErrorNumber getJpegSnapshotPicture(void(*func)(char *buf, size_t siz)); - - /** - * mbed Interface Speed. - * - * @param baud mbed Interface Speed. - */ - void setmbedBaud(Baud baud); - -private: - SerialBuffered serial; - static const int COMMAND_LENGTH = 6; - static const int SYNCMAX = 60; - static const int packageSize = 256; - - ErrorNumber sendInitial(Baud band, JpegResolution jr); - ErrorNumber sendGetPicture(void); - ErrorNumber sendSnapshot(void); - ErrorNumber sendReset(ResetType specialReset); - ErrorNumber sendSetPackageSize(uint16_t packageSize); - - ErrorNumber recvData(uint32_t *length); - ErrorNumber sendSync(); - ErrorNumber recvSync(); - ErrorNumber sendAck(uint8_t commandId, uint16_t packageId); - ErrorNumber recvAckOrNck(); - - bool sendBytes(char *buf, size_t len, int timeout_us = 20000); - bool recvBytes(char *buf, size_t len, int timeout_us = 20000); - bool waitRecv(); - bool waitIdle(); -}; - -#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CameraC1098/.lib Mon May 07 05:57:12 2012 +0000 @@ -0,0 +1,1 @@ + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CameraC1098/CameraC1098.cpp Mon May 07 05:57:12 2012 +0000 @@ -0,0 +1,505 @@ +/** + * C1098-SS device driver class (Version 1.0) + * Reference documents: C1098-SS User Manual v1.0 2012.4.1 + * + * CameraC328Library + * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) + * http://shinta.main.jp/ + * + * CameraC1098-SS Library + * Copyright (C) 2012 Tadao Iida + */ + +#include "CameraC1098.h" + +#define WAITIDLE waitIdle +#define SENDFUNC sendBytes +#define RECVFUNC recvBytes +#define WAITFUNC waitRecv + +/** + * Constructor. + * + * @param tx A pin for transmit. + * @param rx A pin for receive. + * @param baud Baud rate. (Default is Baud14400.) + */ +CameraC1098::CameraC1098(PinName tx, PinName rx, Baud baud) : serial(tx, rx) { + serial.baud((int)baud); +} + +/** + * Destructor. + */ +CameraC1098::~CameraC1098() { +} + +/** + * Make a sync. for baud rate. + */ +CameraC1098::ErrorNumber CameraC1098::sync() { + for (int i = 0; i < SYNCMAX; i++) { + if (NoError == sendSync()) { + if (NoError == recvAckOrNck()) { + if (NoError == recvSync()) { + if (NoError == sendAck(0x0D, 0x00)) { + /* + * After synchronization, the camera needs a little time for AEC and AGC to be stable. + * Users should wait for 1-2 seconds before capturing the first picture. + */ + wait(2.0); + return NoError; + } + } + } + } + wait_ms(50); + } + return UnexpectedReply; +} + +/** + * Initialize. + * + * @param baud Camera Interface Speed. + * @param jr JPEG resolution. + */ +CameraC1098::ErrorNumber CameraC1098::init(Baud baud,JpegResolution jr) { + int i ; + ErrorNumber en; + WAITIDLE(); + setmbedBaud((Baud)(0x07)) ; + + for ( i = 1 ; i < 7 ; i++ ) { + if ( NoError == sendSync() ) { + if ( NoError == recvAckOrNck() ){ + if ( NoError == recvSync() ) { + if ( NoError == sendAck(0x0D, 0x00) ) { + en = sendInitial(baud,jr); + if (NoError != en) { + printf("sendInitalError\r\n"); + return en; + } + printf("sendInital OK\r\n"); + en = recvAckOrNck(); + if (NoError != en) { + printf("recvAckOrNckError\r\n"); + return en; + } + wait_ms(50) ; + static bool alreadySetupPackageSize = false; + if (!alreadySetupPackageSize) { + en = sendSetPackageSize(packageSize); + if (NoError != en) { + printf("sendSetPackageSizeError\r\n"); + return en; + } + WAITFUNC(); + en = recvAckOrNck(); + if (NoError != en) { + return en; + } + alreadySetupPackageSize = true; + } + setmbedBaud(baud); + wait(2.0); + return (ErrorNumber)NoError; + /* + * After synchronization, the camera needs a little time for AEC and AGC to be stable. + * Users should wait for 1-2 seconds before capturing the first picture. + */ + } + } + }else{ + setmbedBaud((Baud)(i+1)) ; + } + } + wait_ms(50); + } + return UnexpectedReply; +} + + +/** + * Get JPEG snapshot picture. + * + * @param func A pointer to a callback function. + * You can block this function until saving the image datas. + * @return Status of the error. + */ +CameraC1098::ErrorNumber CameraC1098::getJpegSnapshotPicture(void(*func)(char *buf, size_t siz)) { + WAITIDLE(); + ErrorNumber en; + + + en = sendSnapshot(); + if (NoError != en) { + return en; + } + WAITFUNC(); + en = recvAckOrNck(); + if (NoError != en) { + return en; + } + + en = sendGetPicture(); + if (NoError != en) { + return en; + } + WAITFUNC(); + en = recvAckOrNck(); + if (NoError != en) { + return en; + } + + /* + * Data : snapshot picture + */ + uint32_t length = 0; + WAITFUNC(); + en = recvData(&length); + if (NoError != en) { + return en; + } + en = sendAck(0x00, 0); + if (NoError != en) { + return en; + } + + char databuf[packageSize - 6]; + uint16_t pkg_total = length / (packageSize - 6); + for (int i = 0; i <= (int)pkg_total; i++) { + uint16_t checksum = 0; + // ID. + char idbuf[2]; + WAITFUNC(); + if (!RECVFUNC(idbuf, sizeof(idbuf))) { + return (ErrorNumber)UnexpectedReply; + } + checksum += idbuf[0]; + checksum += idbuf[1]; + uint16_t id = (idbuf[1] << 8) | (idbuf[0] << 0); + if (id != i) { + return (ErrorNumber)UnexpectedReply; + } + + // Size of the data. + char dsbuf[2]; + WAITFUNC(); + if (!RECVFUNC(dsbuf, sizeof(dsbuf))) { + return (ErrorNumber)UnexpectedReply; + } + + // Received the data. + checksum += dsbuf[0]; + checksum += dsbuf[1]; + uint16_t ds = (dsbuf[1] << 8) | (dsbuf[0] << 0); + WAITFUNC(); + if (!RECVFUNC(&databuf[0], ds)) { + return (ErrorNumber)UnexpectedReply; + } + for (int j = 0; j < ds; j++) { + checksum += databuf[j]; + } + + // Verify code. + char vcbuf[2]; + WAITFUNC(); + if (!RECVFUNC(vcbuf, sizeof(vcbuf))) { + return (ErrorNumber)UnexpectedReply; + } + uint16_t vc = (vcbuf[1] << 8) | (vcbuf[0] << 0); + if (vc != (checksum & 0xff)) { + return (ErrorNumber)UnexpectedReply; + } + + /* + * Call a call back function. + * You can block this function while working. + */ + func(databuf, ds); + + /* + * We should wait for camera working before reply a ACK. + */ + wait_ms(100); + en = sendAck(0x00, 1 + i); + if (NoError != en) { + return en; + } + } + + return (ErrorNumber)NoError; +} + + + +CameraC1098::ErrorNumber CameraC1098::sendInitial(Baud baud, JpegResolution jr) { + char send[COMMAND_LENGTH]; + + send[0] = 0xAA; + send[1] = 0x01; + send[2] = (char)baud; + send[3] = 0x07; + send[4] = 0x00; + send[5] = (char)jr; + + if (!SENDFUNC(send, sizeof(send))) { + return (ErrorNumber)SendRegisterTimeout; + } + + return (ErrorNumber)NoError; +} + +CameraC1098::ErrorNumber CameraC1098::sendGetPicture(void) { + char send[COMMAND_LENGTH]; + + send[0] = 0xAA; + send[1] = 0x04; + send[2] = 0x01; + send[3] = 0x00; + send[4] = 0x00; + send[5] = 0x00; + + if (!SENDFUNC(send, sizeof(send))) { + return (ErrorNumber)SendRegisterTimeout; + } + return (ErrorNumber)NoError; +} + +CameraC1098::ErrorNumber CameraC1098::sendSnapshot(void) { + char send[COMMAND_LENGTH]; + send[0] = 0xAA; + send[1] = 0x05; + send[2] = 0x00; + send[3] = 0x00; + send[4] = 0x00; + send[5] = 0x00; + + if (!SENDFUNC(send, sizeof(send))) { + return (ErrorNumber)SendRegisterTimeout; + } + return (ErrorNumber)NoError; +} + +CameraC1098::ErrorNumber CameraC1098::sendSetPackageSize(uint16_t packageSize) { + char send[COMMAND_LENGTH]; + send[0] = 0xAA; + send[1] = 0x06; + send[2] = 0x08; + send[3] = (packageSize >> 0) & 0xff; + send[4] = (packageSize >> 8) & 0xff; + send[5] = 0x00; + + if (!SENDFUNC(send, sizeof(send))) { + return (ErrorNumber)SendRegisterTimeout; + } + return (ErrorNumber)NoError; +} + +void CameraC1098::setmbedBaud(Baud baud){ + switch((int)baud){ + case 2: + serial.baud(460800); + break; + case 3: + serial.baud(230400); + break; + case 4: + serial.baud(115200); + break; + case 5: + serial.baud(57600); + break; + case 6: + serial.baud(28800); + break; + case 7: + serial.baud(14400); + break; + default: + serial.baud(14400); + } +} + + +CameraC1098::ErrorNumber CameraC1098::sendReset(ResetType specialReset) { + char send[COMMAND_LENGTH]; + send[0] = 0xAA; + send[1] = 0x08; + send[2] = 0x00; + send[3] = 0x00; + send[4] = 0x00; + send[5] = specialReset; + /* + * Special reset : If the parameter is 0xFF, the command is a special Reset command and the firmware responds to it immediately. + */ + + if (!SENDFUNC(send, sizeof(send))) { + return (ErrorNumber)SendRegisterTimeout; + } + + return (ErrorNumber)NoError; +} + + +CameraC1098::ErrorNumber CameraC1098::recvData(uint32_t *length) { + char recv[COMMAND_LENGTH]; + if (!RECVFUNC(recv, sizeof(recv))) { + return (ErrorNumber)UnexpectedReply; + } + if ((0xAA != recv[0]) || (0x0A != recv[1])) { + return (ErrorNumber)UnexpectedReply; + } + recv[2] = (char)0x01; + *length = (recv[5] << 16) | (recv[4] << 8) | (recv[3] << 0); + return (ErrorNumber)NoError; +} + +CameraC1098::ErrorNumber CameraC1098::sendSync() { + char send[COMMAND_LENGTH]; + send[0] = 0xAA; + send[1] = 0x0D; + send[2] = 0x00; + send[3] = 0x00; + send[4] = 0x00; + send[5] = 0x00; + + + if (!SENDFUNC(send, sizeof(send))) { + return (ErrorNumber)SendRegisterTimeout; + } + + return (ErrorNumber)NoError; +} + +CameraC1098::ErrorNumber CameraC1098::recvSync() { + char recv[COMMAND_LENGTH]; + + if (!RECVFUNC(recv, sizeof(recv))) { + printf("recvSync UnexpectedReply0\r\n"); + return (ErrorNumber)UnexpectedReply; + } + if ((0xAA != recv[0]) || (0x0D != recv[1])) { + printf("recvSync UnexpectedReply1\r\n"); + return (ErrorNumber)UnexpectedReply; + } + printf("recvSync NoError\r\n"); + return (ErrorNumber)NoError; +} + +/** + * Send ACK. + * + * @param commandId The command with that ID is acknowledged by this command. + * @param packageId For acknowledging Data command, these two bytes represent the requested package ID. While for acknowledging other commands, these two bytes are set to 00h. + */ +CameraC1098::ErrorNumber CameraC1098::sendAck(uint8_t commandId, uint16_t packageId) { + char send[COMMAND_LENGTH]; + + send[0] = 0xAA; + send[1] = 0x0E; + send[2] = commandId; + send[3] = 0x00; // ACK counter is not used. + send[4] = (packageId >> 0) & 0xff; + send[5] = (packageId >> 8) & 0xff; + if (!SENDFUNC(send, sizeof(send))) { + return (ErrorNumber)SendRegisterTimeout; + } + return (ErrorNumber)NoError; +} + +/** + * Receive ACK or NCK. + * + * @return Error number. + */ +CameraC1098::ErrorNumber CameraC1098::recvAckOrNck() { + char recv[COMMAND_LENGTH]; + + if (!RECVFUNC(recv, sizeof(recv))) { + printf("recvAckOrNck UnexpectedReply0\r\n"); + return (ErrorNumber)UnexpectedReply; + } + if ((0xAA == recv[0]) && (0x0E == recv[1])) { + printf("recvAckOrNck\r\n"); + return (ErrorNumber)NoError; + } + if ((0xAA == recv[0]) && (0x0F == recv[1]) && (0x00 == recv[2])) { + printf("recvAckOrNck\r\n"); + return (ErrorNumber)NoError; + } + if ((0xAA == recv[0]) && (0x0F == recv[1])) { + printf("recvAckOrNck Error[%02x]\r\n",recv[4]); + return (ErrorNumber)recv[4]; + } + printf("recvAckOrNck UnexpectedReply1\r\n"); + return (ErrorNumber)UnexpectedReply; +} + +/** + * Send bytes to camera module. + * + * @param buf Pointer to the data buffer. + * @param len Length of the data buffer. + * + * @return True if the data sended. + */ +bool CameraC1098::sendBytes(char *buf, size_t len, int timeout_us) { + for (uint32_t i = 0; i < (uint32_t)len; i++) { + int cnt = 0; + while (!serial.writeable()) { + wait_us(1); + cnt++; + if (timeout_us < cnt) { + return false; + } + } + serial.putc(buf[i]); + } + return true; +} + +/** + * Receive bytes from camera module. + * + * @param buf Pointer to the data buffer. + * @param len Length of the data buffer. + * + * @return True if the data received. + */ +bool CameraC1098::recvBytes(char *buf, size_t len, int timeout_us) { + for (uint32_t i = 0; i < (uint32_t)len; i++) { + int cnt = 0; + while (!serial.readable()) { + wait_us(1); + cnt++; + if (timeout_us < cnt) { + return false; + } + } + buf[i] = serial.getc(); + } + return true; +} + +/** + * Wait received. + * + * @return True if the data received. + */ +bool CameraC1098::waitRecv() { + while (!serial.readable()) { + } + return true; +} + +/** + * Wait idle state. + */ +bool CameraC1098::waitIdle() { + while (serial.readable()) { + serial.getc(); + } + return true; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CameraC1098/CameraC1098.h Mon May 07 05:57:12 2012 +0000 @@ -0,0 +1,134 @@ +/** + * C1098-SS device driver class (Version 1.0) + * Reference documents: C1098-SS User Manual v1.0 2012.5.6 + * + * CameraC328Library + * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) + * http://shinta.main.jp/ + * + * CameraC1098-SS Library + * Copyright (C) 2012 Tadao Iida + */ + +#include "mbed.h" +#include "SerialBuffered.h" + +#ifndef _CAMERA_C1098_H_ +#define _CAMERA_C1098_H_ + +/* + * Class: CameraC1098 + */ + +class CameraC1098 { +public: + + /** + * @enum JPEG resolution. + */ + enum JpegResolution { + JpegResolution80x64 = 0x01, // unofficial + JpegResolution160x128 = 0x03, // unofficial + JpegResolution320x240 = 0x05, // QVGA + JpegResolution640x480 = 0x07 // VGA + }; + + /** + * @enum Error number. + */ + enum ErrorNumber { + NoError = 0x00, + UnexpectedReply = 0x04, + ParameterError = 0x0b, + SendRegisterTimeout = 0x0c, + CommandIdError = 0x0d, + CommandHeaderError = 0xf0, + SetTransferPackageSizeWrong = 0x11 + }; + + /** + * @enum Baud rate. + */ + enum Baud { + Baud460800 = 0x02, + Baud230400 = 0x03, + Baud115200 = 0x04, + Baud57600 = 0x05, + Baud28800 = 0x06, + Baud14400 = 0x07 // Default. + }; + + /** + * @enum Reset type. + */ + enum ResetType { + Nomal = 0x00, + High = 0xff + }; + + /** Constructor. + * + * @param tx A pin for transmit. + * @param rx A pin for receive. + * @param baud Baud rate. (Default is Baud14400.) + */ + CameraC1098(PinName tx, PinName rx, Baud baud = Baud14400); + + /** Destructor. + * + */ + ~CameraC1098(); + + /** sync + * Make a sync. for baud rate. + */ + ErrorNumber sync(); + + /** Initialize. + * + * + * @param baud Camera Interface Speed. + * @param jr JPEG resolution. + */ + ErrorNumber init(Baud baud, JpegResolution jr); + + /** getJpegSnapshotPicture + * Get JPEG snapshot picture. + * + * @param func A pointer to a callback function. + * You can block this function until saving the image datas. + * @return Status of the error. + */ + ErrorNumber getJpegSnapshotPicture(void(*func)(char *buf, size_t siz)); + + /** setmbedBaud + * mbed Interface Speed. + * + * @param baud mbed Interface Speed. + */ + void setmbedBaud(Baud baud); + +private: + SerialBuffered serial; + static const int COMMAND_LENGTH = 6; + static const int SYNCMAX = 60; + static const int packageSize = 256; + + ErrorNumber sendInitial(Baud band, JpegResolution jr); + ErrorNumber sendGetPicture(void); + ErrorNumber sendSnapshot(void); + ErrorNumber sendSetPackageSize(uint16_t packageSize); + ErrorNumber sendReset(ResetType specialReset); + ErrorNumber recvData(uint32_t *length); + ErrorNumber sendSync(); + ErrorNumber recvSync(); + ErrorNumber sendAck(uint8_t commandId, uint16_t packageId); + ErrorNumber recvAckOrNck(); + + bool sendBytes(char *buf, size_t len, int timeout_us = 20000); + bool recvBytes(char *buf, size_t len, int timeout_us = 20000); + bool waitRecv(); + bool waitIdle(); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CameraC1098/SerialBuffered.cpp Mon May 07 05:57:12 2012 +0000 @@ -0,0 +1,99 @@ +#include "mbed.h" +#include "SerialBuffered.h" + +/** + * Create a buffered serial class. + * + * @param tx A pin for transmit. + * @param rx A pin for receive. + */ +SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) { + indexContentStart = 0; + indexContentEnd = 0; + timeout = 1; + attach(this, &SerialBuffered::handleInterrupt); +} + +/** + * Destroy. + */ +SerialBuffered::~SerialBuffered() { +} + +/** + * Set timeout for getc(). + * + * @param ms milliseconds. (-1:Disable timeout) + */ +void SerialBuffered::setTimeout(int ms) { + timeout = ms; +} + +/** + * Read requested bytes. + * + * @param bytes A pointer to a buffer. + * @param requested Length. + * + * @return Readed byte length. + */ +size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) { + int i = 0; + while (i < requested) { + int c = getc(); + if (c < 0) { + break; + } + bytes[i] = c; + i++; + } + return i; +} + +/** + * Get a character. + * + * @return A character. (-1:timeout) + */ +int SerialBuffered::getc() { + timer.reset(); + timer.start(); + while (indexContentStart == indexContentEnd) { + wait_ms(1); + if ((timeout > 0) && (timer.read_ms() > timeout)) { + /* + * Timeout occured. + */ + // printf("Timeout occured.\n"); + return EOF; + } + } + timer.stop(); + + uint8_t result = buffer[indexContentStart++]; + indexContentStart = indexContentStart % BUFFERSIZE; + + return result; +} + +/** + * Returns 1 if there is a character available to read, 0 otherwise. + */ +int SerialBuffered::readable() { + return indexContentStart != indexContentEnd; +} + +void SerialBuffered::handleInterrupt() { + while (Serial::readable()) { + if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) { + /* + * Buffer overrun occured. + */ + // printf("Buffer overrun occured.\n"); + Serial::getc(); + } else { + buffer[indexContentEnd++] = Serial::getc(); + indexContentEnd = indexContentEnd % BUFFERSIZE; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CameraC1098/SerialBuffered.h Mon May 07 05:57:12 2012 +0000 @@ -0,0 +1,61 @@ +#ifndef _SERIAL_BUFFERED_H_ +#define _SERIAL_BUFFERED_H_ + +/** + * Buffered serial class. + */ +class SerialBuffered : public Serial { +public: + /** + * Create a buffered serial class. + * + * @param tx A pin for transmit. + * @param rx A pin for receive. + */ + SerialBuffered(PinName tx, PinName rx); + + /** + * Destroy. + */ + virtual ~SerialBuffered(); + + /** + * Get a character. + * + * @return A character. (-1:timeout) + */ + int getc(); + + /** + * Returns 1 if there is a character available to read, 0 otherwise. + */ + int readable(); + + /** + * Set timeout for getc(). + * + * @param ms milliseconds. (-1:Disable timeout) + */ + void setTimeout(int ms); + + /** + * Read requested bytes. + * + * @param bytes A pointer to a buffer. + * @param requested Length. + * + * @return Readed byte length. + */ + size_t readBytes(uint8_t *bytes, size_t requested); + +private: + void handleInterrupt(); + static const int BUFFERSIZE = 4096; + uint8_t buffer[BUFFERSIZE]; // points at a circular buffer, containing data from m_contentStart, for m_contentSize bytes, wrapping when you get to the end + uint16_t indexContentStart; // index of first bytes of content + uint16_t indexContentEnd; // index of bytes after last byte of content + int timeout; + Timer timer; +}; + +#endif
--- a/SerialBuffered.cpp Sun May 06 10:54:43 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,99 +0,0 @@ -#include "mbed.h" -#include "SerialBuffered.h" - -/** - * Create a buffered serial class. - * - * @param tx A pin for transmit. - * @param rx A pin for receive. - */ -SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) { - indexContentStart = 0; - indexContentEnd = 0; - timeout = 1; - attach(this, &SerialBuffered::handleInterrupt); -} - -/** - * Destroy. - */ -SerialBuffered::~SerialBuffered() { -} - -/** - * Set timeout for getc(). - * - * @param ms milliseconds. (-1:Disable timeout) - */ -void SerialBuffered::setTimeout(int ms) { - timeout = ms; -} - -/** - * Read requested bytes. - * - * @param bytes A pointer to a buffer. - * @param requested Length. - * - * @return Readed byte length. - */ -size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) { - int i = 0; - while (i < requested) { - int c = getc(); - if (c < 0) { - break; - } - bytes[i] = c; - i++; - } - return i; -} - -/** - * Get a character. - * - * @return A character. (-1:timeout) - */ -int SerialBuffered::getc() { - timer.reset(); - timer.start(); - while (indexContentStart == indexContentEnd) { - wait_ms(1); - if ((timeout > 0) && (timer.read_ms() > timeout)) { - /* - * Timeout occured. - */ - // printf("Timeout occured.\n"); - return EOF; - } - } - timer.stop(); - - uint8_t result = buffer[indexContentStart++]; - indexContentStart = indexContentStart % BUFFERSIZE; - - return result; -} - -/** - * Returns 1 if there is a character available to read, 0 otherwise. - */ -int SerialBuffered::readable() { - return indexContentStart != indexContentEnd; -} - -void SerialBuffered::handleInterrupt() { - while (Serial::readable()) { - if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) { - /* - * Buffer overrun occured. - */ - // printf("Buffer overrun occured.\n"); - Serial::getc(); - } else { - buffer[indexContentEnd++] = Serial::getc(); - indexContentEnd = indexContentEnd % BUFFERSIZE; - } - } -}
--- a/SerialBuffered.h Sun May 06 10:54:43 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -#ifndef _SERIAL_BUFFERED_H_ -#define _SERIAL_BUFFERED_H_ - -/** - * Buffered serial class. - */ -class SerialBuffered : public Serial { -public: - /** - * Create a buffered serial class. - * - * @param tx A pin for transmit. - * @param rx A pin for receive. - */ - SerialBuffered(PinName tx, PinName rx); - - /** - * Destroy. - */ - virtual ~SerialBuffered(); - - /** - * Get a character. - * - * @return A character. (-1:timeout) - */ - int getc(); - - /** - * Returns 1 if there is a character available to read, 0 otherwise. - */ - int readable(); - - /** - * Set timeout for getc(). - * - * @param ms milliseconds. (-1:Disable timeout) - */ - void setTimeout(int ms); - - /** - * Read requested bytes. - * - * @param bytes A pointer to a buffer. - * @param requested Length. - * - * @return Readed byte length. - */ - size_t readBytes(uint8_t *bytes, size_t requested); - -private: - void handleInterrupt(); - static const int BUFFERSIZE = 4096; - uint8_t buffer[BUFFERSIZE]; // points at a circular buffer, containing data from m_contentStart, for m_contentSize bytes, wrapping when you get to the end - uint16_t indexContentStart; // index of first bytes of content - uint16_t indexContentEnd; // index of bytes after last byte of content - int timeout; - Timer timer; -}; - -#endif
--- a/main.cpp Sun May 06 10:54:43 2012 +0000 +++ b/main.cpp Mon May 07 05:57:12 2012 +0000 @@ -20,16 +20,13 @@ /* * Definitions. */ -#define USE_JPEG_HIGH_RESOLUTION 1 #define USE_SD_CARD 1 /* * Variables. */ static const int CAPTURE_FRAMES = 3; -static const int RAWIMG_X = 320; -static const int RAWIMG_Y = 240; -static char buf[RAWIMG_X * RAWIMG_Y * 2]; +static char buf[256+1]; static FILE *fp_jpeg; /* @@ -102,16 +99,16 @@ printf("CameraC1098\r\n"); printf("==========\r\n"); CameraC1098::ErrorNumber err = CameraC1098::NoError; - err = camera.init(CameraC1098::Baud460800, CameraC1098::JpegResolution160x128); + err = camera.init(CameraC1098::Baud28800, CameraC1098::JpegResolution80x64); if (CameraC1098::NoError == err) { printf("[ OK ] : CameraC1098::init\r\n"); } else { printf("[FAIL] : CameraC1098::init (Error=%02X)\r\n", (int)err); } - // - sync(); - test_jpeg_snapshot_picture(); + // + //sync(); + //test_jpeg_snapshot_picture(); return 0; }