CameraC328

Dependents:   CameraC328_TestProgram CameraC328_Thresholding Camera_TestProgram_2015 Camera_TestProgram_2015 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CameraC328.h Source File

CameraC328.h

00001 /**
00002  * C328-7640 device driver class (Version 0.0.6)
00003  * Reference documents: C328-7640 User Manual v3.0 2004.8.19
00004  *
00005  * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
00006  * http://shinta.main.jp/
00007  */
00008 
00009 #include "mbed.h"
00010 #include "SerialBuffered.h"
00011 
00012 #ifndef _CAMERA_C328_H_
00013 #define _CAMERA_C328_H_
00014 
00015 /*
00016  * Class: CameraC328
00017  */
00018 class CameraC328 {
00019 public:
00020 
00021     /**
00022      * Color type.
00023      */
00024     enum ColorType {
00025         GrayScale2bit = 0x01,   // 2bit for Y only
00026         GrayScale4bit = 0x02,   // 4bit for Y only
00027         GrayScale8bit = 0x03,   // 8bit for Y only
00028         Color12bit = 0x05,      // 444 (RGB)
00029         Color16bit = 0x06,      // 565 (RGB)
00030         Jpeg = 0x07
00031     };
00032 
00033     /**
00034      * Raw resolution.
00035      */
00036     enum RawResolution {
00037         RawResolution80x60 = 0x01,
00038         RawResolution160x120 = 0x03
00039     };
00040 
00041     /**
00042      * JPEG resolution.
00043      */
00044     enum JpegResolution {
00045         JpegResolution80x64 = 0x01,
00046         JpegResolution160x128 = 0x03,
00047         JpegResolution320x240 = 0x05,
00048         JpegResolution640x480 = 0x07
00049     };
00050 
00051     /**
00052      * Error number.
00053      */
00054     enum ErrorNumber {
00055         NoError = 0x00,
00056         PictureTypeError = 0x01,
00057         PictureUpScale = 0x02,
00058         PictureScaleError = 0x03,
00059         UnexpectedReply = 0x04,
00060         SendPictureTimeout = 0x05,
00061         UnexpectedCommand = 0x06,
00062         SramJpegTypeError = 0x07,
00063         SramJpegSizeError = 0x08,
00064         PictureFormatError = 0x09,
00065         PictureSizeError = 0x0a,
00066         ParameterError = 0x0b,
00067         SendRegisterTimeout = 0x0c,
00068         CommandIdError = 0x0d,
00069         PictureNotReady = 0x0f,
00070         TransferPackageNumberError = 0x10,
00071         SetTransferPackageSizeWrong = 0x11,
00072         CommandHeaderError = 0xf0,
00073         CommandLengthError = 0xf1,
00074         SendPictureError = 0xf5,
00075         SendCommandError = 0xff
00076     };
00077 
00078     /**
00079      * Picture type.
00080      */
00081     enum PictureType {
00082         SnapshotPicture = 0x01,
00083         PreviewPicture = 0x02,
00084         JpegPreviewPicture = 0x05
00085     };
00086 
00087     /**
00088      * Snapshot type.
00089      */
00090     enum SnapshotType {
00091         CompressedPicture = 0x00,
00092         UncompressedPicture = 0x01
00093     };
00094 
00095     /**
00096      * Baud rate.
00097      */
00098     enum Baud {
00099         Baud7200 = 7200,
00100         Baud9600 = 9600,
00101         Baud14400 = 14400,
00102         Baud19200 = 19200,  // Default.
00103         Baud28800 = 28800,
00104         Baud38400 = 38400,
00105         Baud57600 = 57600,
00106         Baud115200 = 115200
00107     };
00108 
00109     /**
00110      * Reset type.
00111      */
00112     enum ResetType {
00113         ResetWholeSystem = 0x00,
00114         ResetStateMachines = 0x01
00115     };
00116 
00117     /**
00118      * Data type.
00119      */
00120     enum DataType {
00121         DataTypeSnapshotPicture = 0x01,
00122         DataTypePreviewPicture = 0x02,
00123         DataTypeJpegPreviewPicture = 0x05
00124     };
00125 
00126     /**
00127      * Constructor.
00128      *
00129      * @param tx A pin for transmit.
00130      * @param rx A pin for receive.
00131      * @param baud Baud rate. (Default is Baud19200.)
00132      */
00133     CameraC328(PinName tx, PinName rx, Baud baud = Baud19200);
00134 
00135     /**
00136      * Destructor.
00137      */
00138     ~CameraC328();
00139 
00140     /**
00141      * Make a sync. for baud rate.
00142      */
00143     ErrorNumber sync();
00144 
00145     /**
00146      * Initialize.
00147      *
00148      * @param ct Color type.
00149      * @param rr Raw resolution.
00150      * @param jr JPEG resolution.
00151      */
00152     ErrorNumber init(ColorType ct, RawResolution rr, JpegResolution jr);
00153 
00154     /**
00155      * Get uncompressed snapshot picture.
00156      *
00157      * @param func A pointer to a callback function.
00158      *             Please do NOT block this callback function.
00159      *             Because the camera module transmit image datas continuously.
00160      * @return Status of the error.
00161      */
00162     ErrorNumber getUncompressedSnapshotPicture(void(*func)(size_t done, size_t total, char c));
00163 
00164     /**
00165      * Get uncompressed preview picture.
00166      *
00167      * @param func A pointer to a callback function.
00168      *             Please do NOT block this callback function.
00169      *             Because the camera module transmit image datas continuously.
00170      * @return Status of the error.
00171      */
00172     ErrorNumber getUncompressedPreviewPicture(void(*func)(size_t done, size_t total, char c));
00173 
00174     /**
00175      * Get JPEG snapshot picture.
00176      *
00177      * @param func A pointer to a callback function.
00178      *             You can block this function until saving the image datas.
00179      * @return Status of the error.
00180      */
00181     ErrorNumber getJpegSnapshotPicture(void(*func)(char *buf, size_t siz));
00182 
00183     /**
00184      * Get JPEG preview picture.
00185      *
00186      * @param func A pointer to a callback function.
00187      *             You can block this function until saving the image datas.
00188      * @return Status of the error.
00189      */
00190     ErrorNumber getJpegPreviewPicture(void(*func)(char *buf, size_t siz));
00191 
00192 private:
00193     SerialBuffered serial;
00194     static const int COMMAND_LENGTH = 6;
00195     static const int SYNCMAX = 60;
00196     static const int packageSize = 512;
00197 
00198     ErrorNumber sendInitial(ColorType ct, RawResolution rr, JpegResolution jr);
00199     ErrorNumber sendGetPicture(PictureType pt);
00200     ErrorNumber sendSnapshot(SnapshotType st, uint16_t skipFrames);
00201     ErrorNumber sendSetPackageSize(uint16_t packageSize);
00202     ErrorNumber sendSetBaudrate(Baud baud);
00203     ErrorNumber sendReset(ResetType rt, bool specialReset);
00204     ErrorNumber sendPowerOff();
00205     ErrorNumber recvData(DataType *dt, uint32_t *length);
00206     ErrorNumber sendSync();
00207     ErrorNumber recvSync();
00208     ErrorNumber sendAck(uint8_t commandId, uint16_t packageId);
00209     ErrorNumber recvAckOrNck();
00210 
00211     bool sendBytes(char *buf, size_t len, int timeout_us = 20000);
00212     bool recvBytes(char *buf, size_t len, int timeout_us = 20000);
00213     bool waitRecv();
00214     bool waitIdle();
00215 };
00216 
00217 #endif