Hepta_Cam&GPS
Dependents: HEPTA2_assembly_0720 HEPTA2_ALL HEPTA2_ALL_ver0803_02
Fork of HeptaSerial by
CameraC1098.cpp@0:5a6468b4164d, 2012-07-09 (annotated)
- Committer:
- sunifu
- Date:
- Mon Jul 09 10:23:32 2012 +0000
- Revision:
- 0:5a6468b4164d
- Child:
- 1:d16a4c3a272a
v0.95
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sunifu | 0:5a6468b4164d | 1 | /** |
sunifu | 0:5a6468b4164d | 2 | * C1098-SS device driver class (Version 1.0) |
sunifu | 0:5a6468b4164d | 3 | * Reference documents: C1098-SS User Manual v1.0 2012.4.1 |
sunifu | 0:5a6468b4164d | 4 | * |
sunifu | 0:5a6468b4164d | 5 | * CameraC328Library |
sunifu | 0:5a6468b4164d | 6 | * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) |
sunifu | 0:5a6468b4164d | 7 | * http://shinta.main.jp/ |
sunifu | 0:5a6468b4164d | 8 | * |
sunifu | 0:5a6468b4164d | 9 | * CameraC1098-SS Library |
sunifu | 0:5a6468b4164d | 10 | * Copyright (C) 2012 Tadao Iida |
sunifu | 0:5a6468b4164d | 11 | */ |
sunifu | 0:5a6468b4164d | 12 | |
sunifu | 0:5a6468b4164d | 13 | #include "CameraC1098.h" |
sunifu | 0:5a6468b4164d | 14 | |
sunifu | 0:5a6468b4164d | 15 | #define WAITIDLE waitIdle |
sunifu | 0:5a6468b4164d | 16 | #define SENDFUNC sendBytes |
sunifu | 0:5a6468b4164d | 17 | #define RECVFUNC recvBytes |
sunifu | 0:5a6468b4164d | 18 | #define WAITFUNC waitRecv |
sunifu | 0:5a6468b4164d | 19 | |
sunifu | 0:5a6468b4164d | 20 | /** |
sunifu | 0:5a6468b4164d | 21 | * Constructor. |
sunifu | 0:5a6468b4164d | 22 | * |
sunifu | 0:5a6468b4164d | 23 | * @param tx A pin for transmit. |
sunifu | 0:5a6468b4164d | 24 | * @param rx A pin for receive. |
sunifu | 0:5a6468b4164d | 25 | * @param baud Baud rate. (Default is Baud14400.) |
sunifu | 0:5a6468b4164d | 26 | */ |
sunifu | 0:5a6468b4164d | 27 | CameraC1098::CameraC1098(PinName tx, PinName rx, int baud) : serial(tx, rx) { |
sunifu | 0:5a6468b4164d | 28 | serial.baud(baud); |
sunifu | 0:5a6468b4164d | 29 | } |
sunifu | 0:5a6468b4164d | 30 | |
sunifu | 0:5a6468b4164d | 31 | /** |
sunifu | 0:5a6468b4164d | 32 | * Destructor. |
sunifu | 0:5a6468b4164d | 33 | */ |
sunifu | 0:5a6468b4164d | 34 | CameraC1098::~CameraC1098() { |
sunifu | 0:5a6468b4164d | 35 | } |
sunifu | 0:5a6468b4164d | 36 | |
sunifu | 0:5a6468b4164d | 37 | /** |
sunifu | 0:5a6468b4164d | 38 | * Make a sync. for baud rate. |
sunifu | 0:5a6468b4164d | 39 | */ |
sunifu | 0:5a6468b4164d | 40 | CameraC1098::ErrorNumber CameraC1098::sync() { |
sunifu | 0:5a6468b4164d | 41 | for (int i = 0; i < SYNCMAX; i++) { |
sunifu | 0:5a6468b4164d | 42 | if (NoError == sendSync()) { |
sunifu | 0:5a6468b4164d | 43 | if (NoError == recvAckOrNck()) { |
sunifu | 0:5a6468b4164d | 44 | if (NoError == recvSync()) { |
sunifu | 0:5a6468b4164d | 45 | if (NoError == sendAck(0x0D, 0x00)) { |
sunifu | 0:5a6468b4164d | 46 | /* |
sunifu | 0:5a6468b4164d | 47 | * After synchronization, the camera needs a little time for AEC and AGC to be stable. |
sunifu | 0:5a6468b4164d | 48 | * Users should wait for 1-2 seconds before capturing the first picture. |
sunifu | 0:5a6468b4164d | 49 | */ |
sunifu | 0:5a6468b4164d | 50 | wait(2.0); |
sunifu | 0:5a6468b4164d | 51 | return NoError; |
sunifu | 0:5a6468b4164d | 52 | } |
sunifu | 0:5a6468b4164d | 53 | } |
sunifu | 0:5a6468b4164d | 54 | } |
sunifu | 0:5a6468b4164d | 55 | } |
sunifu | 0:5a6468b4164d | 56 | wait_ms(50); |
sunifu | 0:5a6468b4164d | 57 | } |
sunifu | 0:5a6468b4164d | 58 | return UnexpectedReply; |
sunifu | 0:5a6468b4164d | 59 | } |
sunifu | 0:5a6468b4164d | 60 | |
sunifu | 0:5a6468b4164d | 61 | /** |
sunifu | 0:5a6468b4164d | 62 | * Initialize. |
sunifu | 0:5a6468b4164d | 63 | * |
sunifu | 0:5a6468b4164d | 64 | * @param baud Camera Interface Speed. |
sunifu | 0:5a6468b4164d | 65 | * @param jr JPEG resolution. |
sunifu | 0:5a6468b4164d | 66 | */ |
sunifu | 0:5a6468b4164d | 67 | CameraC1098::ErrorNumber CameraC1098::init(Baud baud,JpegResolution jr) { |
sunifu | 0:5a6468b4164d | 68 | int i ; |
sunifu | 0:5a6468b4164d | 69 | ErrorNumber en; |
sunifu | 0:5a6468b4164d | 70 | WAITIDLE(); |
sunifu | 0:5a6468b4164d | 71 | setmbedBaud((Baud)(0x07)) ; |
sunifu | 0:5a6468b4164d | 72 | |
sunifu | 0:5a6468b4164d | 73 | for ( i = 1 ; i < 7 ; i++ ) { |
sunifu | 0:5a6468b4164d | 74 | if ( NoError == sendSync() ) { |
sunifu | 0:5a6468b4164d | 75 | if ( NoError == recvAckOrNck() ){ |
sunifu | 0:5a6468b4164d | 76 | if ( NoError == recvSync() ) { |
sunifu | 0:5a6468b4164d | 77 | if ( NoError == sendAck(0x0D, 0x00) ) { |
sunifu | 0:5a6468b4164d | 78 | en = sendInitial(baud,jr); |
sunifu | 0:5a6468b4164d | 79 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 80 | return en; |
sunifu | 0:5a6468b4164d | 81 | } |
sunifu | 0:5a6468b4164d | 82 | en = recvAckOrNck(); |
sunifu | 0:5a6468b4164d | 83 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 84 | return en; |
sunifu | 0:5a6468b4164d | 85 | } |
sunifu | 0:5a6468b4164d | 86 | wait_ms(50) ; |
sunifu | 0:5a6468b4164d | 87 | setmbedBaud(baud); |
sunifu | 0:5a6468b4164d | 88 | //wait_ms(50) ; |
sunifu | 0:5a6468b4164d | 89 | static bool alreadySetupPackageSize = false; |
sunifu | 0:5a6468b4164d | 90 | if (!alreadySetupPackageSize) { |
sunifu | 0:5a6468b4164d | 91 | en = sendSetPackageSize(packageSize); |
sunifu | 0:5a6468b4164d | 92 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 93 | return en; |
sunifu | 0:5a6468b4164d | 94 | } |
sunifu | 0:5a6468b4164d | 95 | WAITFUNC(); |
sunifu | 0:5a6468b4164d | 96 | en = recvAckOrNck(); |
sunifu | 0:5a6468b4164d | 97 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 98 | return en; |
sunifu | 0:5a6468b4164d | 99 | } |
sunifu | 0:5a6468b4164d | 100 | alreadySetupPackageSize = true; |
sunifu | 0:5a6468b4164d | 101 | } |
sunifu | 0:5a6468b4164d | 102 | |
sunifu | 0:5a6468b4164d | 103 | wait(2.0); |
sunifu | 0:5a6468b4164d | 104 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 105 | /* |
sunifu | 0:5a6468b4164d | 106 | * After synchronization, the camera needs a little time for AEC and AGC to be stable. |
sunifu | 0:5a6468b4164d | 107 | * Users should wait for 1-2 seconds before capturing the first picture. |
sunifu | 0:5a6468b4164d | 108 | */ |
sunifu | 0:5a6468b4164d | 109 | } |
sunifu | 0:5a6468b4164d | 110 | } |
sunifu | 0:5a6468b4164d | 111 | }else{ |
sunifu | 0:5a6468b4164d | 112 | setmbedBaud((Baud)(i+1)) ; |
sunifu | 0:5a6468b4164d | 113 | } |
sunifu | 0:5a6468b4164d | 114 | } |
sunifu | 0:5a6468b4164d | 115 | wait_ms(50); |
sunifu | 0:5a6468b4164d | 116 | } |
sunifu | 0:5a6468b4164d | 117 | return UnexpectedReply; |
sunifu | 0:5a6468b4164d | 118 | } |
sunifu | 0:5a6468b4164d | 119 | |
sunifu | 0:5a6468b4164d | 120 | |
sunifu | 0:5a6468b4164d | 121 | /** |
sunifu | 0:5a6468b4164d | 122 | * Get JPEG snapshot picture. |
sunifu | 0:5a6468b4164d | 123 | * |
sunifu | 0:5a6468b4164d | 124 | * @param func A pointer to a callback function. |
sunifu | 0:5a6468b4164d | 125 | * You can block this function until saving the image datas. |
sunifu | 0:5a6468b4164d | 126 | * @return Status of the error. |
sunifu | 0:5a6468b4164d | 127 | */ |
sunifu | 0:5a6468b4164d | 128 | CameraC1098::ErrorNumber CameraC1098::getJpegSnapshotPicture(void(*func)(char *buf, size_t siz)) { |
sunifu | 0:5a6468b4164d | 129 | WAITIDLE(); |
sunifu | 0:5a6468b4164d | 130 | ErrorNumber en; |
sunifu | 0:5a6468b4164d | 131 | |
sunifu | 0:5a6468b4164d | 132 | |
sunifu | 0:5a6468b4164d | 133 | en = sendSnapshot(); |
sunifu | 0:5a6468b4164d | 134 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 135 | return en; |
sunifu | 0:5a6468b4164d | 136 | } |
sunifu | 0:5a6468b4164d | 137 | WAITFUNC(); |
sunifu | 0:5a6468b4164d | 138 | en = recvAckOrNck(); |
sunifu | 0:5a6468b4164d | 139 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 140 | return en; |
sunifu | 0:5a6468b4164d | 141 | } |
sunifu | 0:5a6468b4164d | 142 | |
sunifu | 0:5a6468b4164d | 143 | en = sendGetPicture(); |
sunifu | 0:5a6468b4164d | 144 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 145 | return en; |
sunifu | 0:5a6468b4164d | 146 | } |
sunifu | 0:5a6468b4164d | 147 | WAITFUNC(); |
sunifu | 0:5a6468b4164d | 148 | en = recvAckOrNck(); |
sunifu | 0:5a6468b4164d | 149 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 150 | return en; |
sunifu | 0:5a6468b4164d | 151 | } |
sunifu | 0:5a6468b4164d | 152 | |
sunifu | 0:5a6468b4164d | 153 | /* |
sunifu | 0:5a6468b4164d | 154 | * Data : snapshot picture |
sunifu | 0:5a6468b4164d | 155 | */ |
sunifu | 0:5a6468b4164d | 156 | uint32_t length = 0; |
sunifu | 0:5a6468b4164d | 157 | WAITFUNC(); |
sunifu | 0:5a6468b4164d | 158 | en = recvData(&length); |
sunifu | 0:5a6468b4164d | 159 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 160 | return en; |
sunifu | 0:5a6468b4164d | 161 | } |
sunifu | 0:5a6468b4164d | 162 | en = sendAck(0x00, 0); |
sunifu | 0:5a6468b4164d | 163 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 164 | return en; |
sunifu | 0:5a6468b4164d | 165 | } |
sunifu | 0:5a6468b4164d | 166 | |
sunifu | 0:5a6468b4164d | 167 | char databuf[packageSize - 6]; |
sunifu | 0:5a6468b4164d | 168 | uint16_t pkg_total = length / (packageSize - 6); |
sunifu | 0:5a6468b4164d | 169 | for (int i = 0; i <= (int)pkg_total; i++) { |
sunifu | 0:5a6468b4164d | 170 | uint16_t checksum = 0; |
sunifu | 0:5a6468b4164d | 171 | // ID. |
sunifu | 0:5a6468b4164d | 172 | char idbuf[2]; |
sunifu | 0:5a6468b4164d | 173 | WAITFUNC(); |
sunifu | 0:5a6468b4164d | 174 | if (!RECVFUNC(idbuf, sizeof(idbuf))) { |
sunifu | 0:5a6468b4164d | 175 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 176 | } |
sunifu | 0:5a6468b4164d | 177 | checksum += idbuf[0]; |
sunifu | 0:5a6468b4164d | 178 | checksum += idbuf[1]; |
sunifu | 0:5a6468b4164d | 179 | uint16_t id = (idbuf[1] << 8) | (idbuf[0] << 0); |
sunifu | 0:5a6468b4164d | 180 | if (id != i) { |
sunifu | 0:5a6468b4164d | 181 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 182 | } |
sunifu | 0:5a6468b4164d | 183 | |
sunifu | 0:5a6468b4164d | 184 | // Size of the data. |
sunifu | 0:5a6468b4164d | 185 | char dsbuf[2]; |
sunifu | 0:5a6468b4164d | 186 | WAITFUNC(); |
sunifu | 0:5a6468b4164d | 187 | if (!RECVFUNC(dsbuf, sizeof(dsbuf))) { |
sunifu | 0:5a6468b4164d | 188 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 189 | } |
sunifu | 0:5a6468b4164d | 190 | |
sunifu | 0:5a6468b4164d | 191 | // Received the data. |
sunifu | 0:5a6468b4164d | 192 | checksum += dsbuf[0]; |
sunifu | 0:5a6468b4164d | 193 | checksum += dsbuf[1]; |
sunifu | 0:5a6468b4164d | 194 | uint16_t ds = (dsbuf[1] << 8) | (dsbuf[0] << 0); |
sunifu | 0:5a6468b4164d | 195 | WAITFUNC(); |
sunifu | 0:5a6468b4164d | 196 | if (!RECVFUNC(&databuf[0], ds)) { |
sunifu | 0:5a6468b4164d | 197 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 198 | } |
sunifu | 0:5a6468b4164d | 199 | for (int j = 0; j < ds; j++) { |
sunifu | 0:5a6468b4164d | 200 | checksum += databuf[j]; |
sunifu | 0:5a6468b4164d | 201 | } |
sunifu | 0:5a6468b4164d | 202 | |
sunifu | 0:5a6468b4164d | 203 | // Verify code. |
sunifu | 0:5a6468b4164d | 204 | char vcbuf[2]; |
sunifu | 0:5a6468b4164d | 205 | WAITFUNC(); |
sunifu | 0:5a6468b4164d | 206 | if (!RECVFUNC(vcbuf, sizeof(vcbuf))) { |
sunifu | 0:5a6468b4164d | 207 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 208 | } |
sunifu | 0:5a6468b4164d | 209 | uint16_t vc = (vcbuf[1] << 8) | (vcbuf[0] << 0); |
sunifu | 0:5a6468b4164d | 210 | if (vc != (checksum & 0xff)) { |
sunifu | 0:5a6468b4164d | 211 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 212 | } |
sunifu | 0:5a6468b4164d | 213 | |
sunifu | 0:5a6468b4164d | 214 | /* |
sunifu | 0:5a6468b4164d | 215 | * Call a call back function. |
sunifu | 0:5a6468b4164d | 216 | * You can block this function while working. |
sunifu | 0:5a6468b4164d | 217 | */ |
sunifu | 0:5a6468b4164d | 218 | func(databuf, ds); |
sunifu | 0:5a6468b4164d | 219 | |
sunifu | 0:5a6468b4164d | 220 | /* |
sunifu | 0:5a6468b4164d | 221 | * We should wait for camera working before reply a ACK. |
sunifu | 0:5a6468b4164d | 222 | */ |
sunifu | 0:5a6468b4164d | 223 | wait_ms(100); |
sunifu | 0:5a6468b4164d | 224 | en = sendAck(0x00, 1 + i); |
sunifu | 0:5a6468b4164d | 225 | if (NoError != en) { |
sunifu | 0:5a6468b4164d | 226 | return en; |
sunifu | 0:5a6468b4164d | 227 | } |
sunifu | 0:5a6468b4164d | 228 | } |
sunifu | 0:5a6468b4164d | 229 | |
sunifu | 0:5a6468b4164d | 230 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 231 | } |
sunifu | 0:5a6468b4164d | 232 | |
sunifu | 0:5a6468b4164d | 233 | |
sunifu | 0:5a6468b4164d | 234 | |
sunifu | 0:5a6468b4164d | 235 | CameraC1098::ErrorNumber CameraC1098::sendInitial(Baud baud, JpegResolution jr) { |
sunifu | 0:5a6468b4164d | 236 | char send[COMMAND_LENGTH]; |
sunifu | 0:5a6468b4164d | 237 | |
sunifu | 0:5a6468b4164d | 238 | send[0] = 0xAA; |
sunifu | 0:5a6468b4164d | 239 | send[1] = 0x01; |
sunifu | 0:5a6468b4164d | 240 | send[2] = (char)baud; |
sunifu | 0:5a6468b4164d | 241 | send[3] = 0x07; |
sunifu | 0:5a6468b4164d | 242 | send[4] = 0x00; |
sunifu | 0:5a6468b4164d | 243 | send[5] = (char)jr; |
sunifu | 0:5a6468b4164d | 244 | |
sunifu | 0:5a6468b4164d | 245 | if (!SENDFUNC(send, sizeof(send))) { |
sunifu | 0:5a6468b4164d | 246 | return (ErrorNumber)SendRegisterTimeout; |
sunifu | 0:5a6468b4164d | 247 | } |
sunifu | 0:5a6468b4164d | 248 | |
sunifu | 0:5a6468b4164d | 249 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 250 | } |
sunifu | 0:5a6468b4164d | 251 | |
sunifu | 0:5a6468b4164d | 252 | CameraC1098::ErrorNumber CameraC1098::sendGetPicture(void) { |
sunifu | 0:5a6468b4164d | 253 | char send[COMMAND_LENGTH]; |
sunifu | 0:5a6468b4164d | 254 | |
sunifu | 0:5a6468b4164d | 255 | send[0] = 0xAA; |
sunifu | 0:5a6468b4164d | 256 | send[1] = 0x04; |
sunifu | 0:5a6468b4164d | 257 | send[2] = 0x01; |
sunifu | 0:5a6468b4164d | 258 | send[3] = 0x00; |
sunifu | 0:5a6468b4164d | 259 | send[4] = 0x00; |
sunifu | 0:5a6468b4164d | 260 | send[5] = 0x00; |
sunifu | 0:5a6468b4164d | 261 | |
sunifu | 0:5a6468b4164d | 262 | if (!SENDFUNC(send, sizeof(send))) { |
sunifu | 0:5a6468b4164d | 263 | return (ErrorNumber)SendRegisterTimeout; |
sunifu | 0:5a6468b4164d | 264 | } |
sunifu | 0:5a6468b4164d | 265 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 266 | } |
sunifu | 0:5a6468b4164d | 267 | |
sunifu | 0:5a6468b4164d | 268 | CameraC1098::ErrorNumber CameraC1098::sendSnapshot(void) { |
sunifu | 0:5a6468b4164d | 269 | char send[COMMAND_LENGTH]; |
sunifu | 0:5a6468b4164d | 270 | send[0] = 0xAA; |
sunifu | 0:5a6468b4164d | 271 | send[1] = 0x05; |
sunifu | 0:5a6468b4164d | 272 | send[2] = 0x00; |
sunifu | 0:5a6468b4164d | 273 | send[3] = 0x00; |
sunifu | 0:5a6468b4164d | 274 | send[4] = 0x00; |
sunifu | 0:5a6468b4164d | 275 | send[5] = 0x00; |
sunifu | 0:5a6468b4164d | 276 | |
sunifu | 0:5a6468b4164d | 277 | if (!SENDFUNC(send, sizeof(send))) { |
sunifu | 0:5a6468b4164d | 278 | return (ErrorNumber)SendRegisterTimeout; |
sunifu | 0:5a6468b4164d | 279 | } |
sunifu | 0:5a6468b4164d | 280 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 281 | } |
sunifu | 0:5a6468b4164d | 282 | |
sunifu | 0:5a6468b4164d | 283 | CameraC1098::ErrorNumber CameraC1098::sendSetPackageSize(uint16_t packageSize) { |
sunifu | 0:5a6468b4164d | 284 | char send[COMMAND_LENGTH]; |
sunifu | 0:5a6468b4164d | 285 | send[0] = 0xAA; |
sunifu | 0:5a6468b4164d | 286 | send[1] = 0x06; |
sunifu | 0:5a6468b4164d | 287 | send[2] = 0x08; |
sunifu | 0:5a6468b4164d | 288 | send[3] = (packageSize >> 0) & 0xff; |
sunifu | 0:5a6468b4164d | 289 | send[4] = (packageSize >> 8) & 0xff; |
sunifu | 0:5a6468b4164d | 290 | send[5] = 0x00; |
sunifu | 0:5a6468b4164d | 291 | |
sunifu | 0:5a6468b4164d | 292 | if (!SENDFUNC(send, sizeof(send))) { |
sunifu | 0:5a6468b4164d | 293 | return (ErrorNumber)SendRegisterTimeout; |
sunifu | 0:5a6468b4164d | 294 | } |
sunifu | 0:5a6468b4164d | 295 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 296 | } |
sunifu | 0:5a6468b4164d | 297 | |
sunifu | 0:5a6468b4164d | 298 | void CameraC1098::setmbedBaud(Baud baud){ |
sunifu | 0:5a6468b4164d | 299 | switch((int)baud){ |
sunifu | 0:5a6468b4164d | 300 | case 0x02: |
sunifu | 0:5a6468b4164d | 301 | serial._baud(460800); |
sunifu | 0:5a6468b4164d | 302 | break; |
sunifu | 0:5a6468b4164d | 303 | case 0x03: |
sunifu | 0:5a6468b4164d | 304 | serial._baud(230400); |
sunifu | 0:5a6468b4164d | 305 | break; |
sunifu | 0:5a6468b4164d | 306 | case 0x04: |
sunifu | 0:5a6468b4164d | 307 | serial._baud(115200); |
sunifu | 0:5a6468b4164d | 308 | break; |
sunifu | 0:5a6468b4164d | 309 | case 0x05: |
sunifu | 0:5a6468b4164d | 310 | serial._baud(57600); |
sunifu | 0:5a6468b4164d | 311 | break; |
sunifu | 0:5a6468b4164d | 312 | case 0x06: |
sunifu | 0:5a6468b4164d | 313 | serial._baud((int)28800); |
sunifu | 0:5a6468b4164d | 314 | break; |
sunifu | 0:5a6468b4164d | 315 | case 0x07: |
sunifu | 0:5a6468b4164d | 316 | serial._baud(14400); |
sunifu | 0:5a6468b4164d | 317 | break; |
sunifu | 0:5a6468b4164d | 318 | default: |
sunifu | 0:5a6468b4164d | 319 | serial._baud(14400); |
sunifu | 0:5a6468b4164d | 320 | } |
sunifu | 0:5a6468b4164d | 321 | } |
sunifu | 0:5a6468b4164d | 322 | |
sunifu | 0:5a6468b4164d | 323 | |
sunifu | 0:5a6468b4164d | 324 | CameraC1098::ErrorNumber CameraC1098::sendReset(ResetType specialReset) { |
sunifu | 0:5a6468b4164d | 325 | char send[COMMAND_LENGTH]; |
sunifu | 0:5a6468b4164d | 326 | send[0] = 0xAA; |
sunifu | 0:5a6468b4164d | 327 | send[1] = 0x08; |
sunifu | 0:5a6468b4164d | 328 | send[2] = 0x00; |
sunifu | 0:5a6468b4164d | 329 | send[3] = 0x00; |
sunifu | 0:5a6468b4164d | 330 | send[4] = 0x00; |
sunifu | 0:5a6468b4164d | 331 | send[5] = specialReset; |
sunifu | 0:5a6468b4164d | 332 | /* |
sunifu | 0:5a6468b4164d | 333 | * Special reset : If the parameter is 0xFF, the command is a special Reset command and the firmware responds to it immediately. |
sunifu | 0:5a6468b4164d | 334 | */ |
sunifu | 0:5a6468b4164d | 335 | |
sunifu | 0:5a6468b4164d | 336 | if (!SENDFUNC(send, sizeof(send))) { |
sunifu | 0:5a6468b4164d | 337 | return (ErrorNumber)SendRegisterTimeout; |
sunifu | 0:5a6468b4164d | 338 | } |
sunifu | 0:5a6468b4164d | 339 | |
sunifu | 0:5a6468b4164d | 340 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 341 | } |
sunifu | 0:5a6468b4164d | 342 | |
sunifu | 0:5a6468b4164d | 343 | |
sunifu | 0:5a6468b4164d | 344 | CameraC1098::ErrorNumber CameraC1098::recvData(uint32_t *length) { |
sunifu | 0:5a6468b4164d | 345 | char recv[COMMAND_LENGTH]; |
sunifu | 0:5a6468b4164d | 346 | if (!RECVFUNC(recv, sizeof(recv))) { |
sunifu | 0:5a6468b4164d | 347 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 348 | } |
sunifu | 0:5a6468b4164d | 349 | if ((0xAA != recv[0]) || (0x0A != recv[1])) { |
sunifu | 0:5a6468b4164d | 350 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 351 | } |
sunifu | 0:5a6468b4164d | 352 | recv[2] = (char)0x01; |
sunifu | 0:5a6468b4164d | 353 | *length = (recv[5] << 16) | (recv[4] << 8) | (recv[3] << 0); |
sunifu | 0:5a6468b4164d | 354 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 355 | } |
sunifu | 0:5a6468b4164d | 356 | |
sunifu | 0:5a6468b4164d | 357 | CameraC1098::ErrorNumber CameraC1098::sendSync() { |
sunifu | 0:5a6468b4164d | 358 | char send[COMMAND_LENGTH]; |
sunifu | 0:5a6468b4164d | 359 | send[0] = 0xAA; |
sunifu | 0:5a6468b4164d | 360 | send[1] = 0x0D; |
sunifu | 0:5a6468b4164d | 361 | send[2] = 0x00; |
sunifu | 0:5a6468b4164d | 362 | send[3] = 0x00; |
sunifu | 0:5a6468b4164d | 363 | send[4] = 0x00; |
sunifu | 0:5a6468b4164d | 364 | send[5] = 0x00; |
sunifu | 0:5a6468b4164d | 365 | |
sunifu | 0:5a6468b4164d | 366 | |
sunifu | 0:5a6468b4164d | 367 | if (!SENDFUNC(send, sizeof(send))) { |
sunifu | 0:5a6468b4164d | 368 | return (ErrorNumber)SendRegisterTimeout; |
sunifu | 0:5a6468b4164d | 369 | } |
sunifu | 0:5a6468b4164d | 370 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 371 | } |
sunifu | 0:5a6468b4164d | 372 | |
sunifu | 0:5a6468b4164d | 373 | CameraC1098::ErrorNumber CameraC1098::recvSync() { |
sunifu | 0:5a6468b4164d | 374 | char recv[COMMAND_LENGTH]; |
sunifu | 0:5a6468b4164d | 375 | |
sunifu | 0:5a6468b4164d | 376 | if (!RECVFUNC(recv, sizeof(recv))) { |
sunifu | 0:5a6468b4164d | 377 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 378 | } |
sunifu | 0:5a6468b4164d | 379 | if ((0xAA != recv[0]) || (0x0D != recv[1])) { |
sunifu | 0:5a6468b4164d | 380 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 381 | } |
sunifu | 0:5a6468b4164d | 382 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 383 | } |
sunifu | 0:5a6468b4164d | 384 | |
sunifu | 0:5a6468b4164d | 385 | /** |
sunifu | 0:5a6468b4164d | 386 | * Send ACK. |
sunifu | 0:5a6468b4164d | 387 | * |
sunifu | 0:5a6468b4164d | 388 | * @param commandId The command with that ID is acknowledged by this command. |
sunifu | 0:5a6468b4164d | 389 | * @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. |
sunifu | 0:5a6468b4164d | 390 | */ |
sunifu | 0:5a6468b4164d | 391 | CameraC1098::ErrorNumber CameraC1098::sendAck(uint8_t commandId, uint16_t packageId) { |
sunifu | 0:5a6468b4164d | 392 | char send[COMMAND_LENGTH]; |
sunifu | 0:5a6468b4164d | 393 | |
sunifu | 0:5a6468b4164d | 394 | send[0] = 0xAA; |
sunifu | 0:5a6468b4164d | 395 | send[1] = 0x0E; |
sunifu | 0:5a6468b4164d | 396 | send[2] = commandId; |
sunifu | 0:5a6468b4164d | 397 | send[3] = 0x00; // ACK counter is not used. |
sunifu | 0:5a6468b4164d | 398 | send[4] = (packageId >> 0) & 0xff; |
sunifu | 0:5a6468b4164d | 399 | send[5] = (packageId >> 8) & 0xff; |
sunifu | 0:5a6468b4164d | 400 | if (!SENDFUNC(send, sizeof(send))) { |
sunifu | 0:5a6468b4164d | 401 | return (ErrorNumber)SendRegisterTimeout; |
sunifu | 0:5a6468b4164d | 402 | } |
sunifu | 0:5a6468b4164d | 403 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 404 | } |
sunifu | 0:5a6468b4164d | 405 | |
sunifu | 0:5a6468b4164d | 406 | /** |
sunifu | 0:5a6468b4164d | 407 | * Receive ACK or NCK. |
sunifu | 0:5a6468b4164d | 408 | * |
sunifu | 0:5a6468b4164d | 409 | * @return Error number. |
sunifu | 0:5a6468b4164d | 410 | */ |
sunifu | 0:5a6468b4164d | 411 | CameraC1098::ErrorNumber CameraC1098::recvAckOrNck() { |
sunifu | 0:5a6468b4164d | 412 | char recv[COMMAND_LENGTH]; |
sunifu | 0:5a6468b4164d | 413 | |
sunifu | 0:5a6468b4164d | 414 | if (!RECVFUNC(recv, sizeof(recv))) { |
sunifu | 0:5a6468b4164d | 415 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 416 | } |
sunifu | 0:5a6468b4164d | 417 | if ((0xAA == recv[0]) && (0x0E == recv[1])) { |
sunifu | 0:5a6468b4164d | 418 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 419 | } |
sunifu | 0:5a6468b4164d | 420 | if ((0xAA == recv[0]) && (0x0F == recv[1]) && (0x00 == recv[2])) { |
sunifu | 0:5a6468b4164d | 421 | return (ErrorNumber)NoError; |
sunifu | 0:5a6468b4164d | 422 | } |
sunifu | 0:5a6468b4164d | 423 | if ((0xAA == recv[0]) && (0x0F == recv[1])) { |
sunifu | 0:5a6468b4164d | 424 | return (ErrorNumber)recv[4]; |
sunifu | 0:5a6468b4164d | 425 | } |
sunifu | 0:5a6468b4164d | 426 | return (ErrorNumber)UnexpectedReply; |
sunifu | 0:5a6468b4164d | 427 | } |
sunifu | 0:5a6468b4164d | 428 | |
sunifu | 0:5a6468b4164d | 429 | /** |
sunifu | 0:5a6468b4164d | 430 | * Send bytes to camera module. |
sunifu | 0:5a6468b4164d | 431 | * |
sunifu | 0:5a6468b4164d | 432 | * @param buf Pointer to the data buffer. |
sunifu | 0:5a6468b4164d | 433 | * @param len Length of the data buffer. |
sunifu | 0:5a6468b4164d | 434 | * |
sunifu | 0:5a6468b4164d | 435 | * @return True if the data sended. |
sunifu | 0:5a6468b4164d | 436 | */ |
sunifu | 0:5a6468b4164d | 437 | bool CameraC1098::sendBytes(char *buf, size_t len, int timeout_us) { |
sunifu | 0:5a6468b4164d | 438 | for (uint32_t i = 0; i < (uint32_t)len; i++) { |
sunifu | 0:5a6468b4164d | 439 | int cnt = 0; |
sunifu | 0:5a6468b4164d | 440 | while (!serial.writeable()) { |
sunifu | 0:5a6468b4164d | 441 | wait_us(1); |
sunifu | 0:5a6468b4164d | 442 | cnt++; |
sunifu | 0:5a6468b4164d | 443 | if (timeout_us < cnt) { |
sunifu | 0:5a6468b4164d | 444 | return false; |
sunifu | 0:5a6468b4164d | 445 | } |
sunifu | 0:5a6468b4164d | 446 | } |
sunifu | 0:5a6468b4164d | 447 | serial.putc(buf[i]); |
sunifu | 0:5a6468b4164d | 448 | } |
sunifu | 0:5a6468b4164d | 449 | return true; |
sunifu | 0:5a6468b4164d | 450 | } |
sunifu | 0:5a6468b4164d | 451 | |
sunifu | 0:5a6468b4164d | 452 | /** |
sunifu | 0:5a6468b4164d | 453 | * Receive bytes from camera module. |
sunifu | 0:5a6468b4164d | 454 | * |
sunifu | 0:5a6468b4164d | 455 | * @param buf Pointer to the data buffer. |
sunifu | 0:5a6468b4164d | 456 | * @param len Length of the data buffer. |
sunifu | 0:5a6468b4164d | 457 | * |
sunifu | 0:5a6468b4164d | 458 | * @return True if the data received. |
sunifu | 0:5a6468b4164d | 459 | */ |
sunifu | 0:5a6468b4164d | 460 | bool CameraC1098::recvBytes(char *buf, size_t len, int timeout_us) { |
sunifu | 0:5a6468b4164d | 461 | for (uint32_t i = 0; i < (uint32_t)len; i++) { |
sunifu | 0:5a6468b4164d | 462 | int cnt = 0; |
sunifu | 0:5a6468b4164d | 463 | while (!serial.readable()) { |
sunifu | 0:5a6468b4164d | 464 | wait_us(1); |
sunifu | 0:5a6468b4164d | 465 | cnt++; |
sunifu | 0:5a6468b4164d | 466 | if (timeout_us < cnt) { |
sunifu | 0:5a6468b4164d | 467 | return false; |
sunifu | 0:5a6468b4164d | 468 | } |
sunifu | 0:5a6468b4164d | 469 | } |
sunifu | 0:5a6468b4164d | 470 | buf[i] = serial.getc(); |
sunifu | 0:5a6468b4164d | 471 | } |
sunifu | 0:5a6468b4164d | 472 | return true; |
sunifu | 0:5a6468b4164d | 473 | } |
sunifu | 0:5a6468b4164d | 474 | |
sunifu | 0:5a6468b4164d | 475 | /** |
sunifu | 0:5a6468b4164d | 476 | * Wait received. |
sunifu | 0:5a6468b4164d | 477 | * |
sunifu | 0:5a6468b4164d | 478 | * @return True if the data received. |
sunifu | 0:5a6468b4164d | 479 | */ |
sunifu | 0:5a6468b4164d | 480 | bool CameraC1098::waitRecv() { |
sunifu | 0:5a6468b4164d | 481 | while (!serial.readable()) { |
sunifu | 0:5a6468b4164d | 482 | } |
sunifu | 0:5a6468b4164d | 483 | return true; |
sunifu | 0:5a6468b4164d | 484 | } |
sunifu | 0:5a6468b4164d | 485 | |
sunifu | 0:5a6468b4164d | 486 | /** |
sunifu | 0:5a6468b4164d | 487 | * Wait idle state. |
sunifu | 0:5a6468b4164d | 488 | */ |
sunifu | 0:5a6468b4164d | 489 | bool CameraC1098::waitIdle() { |
sunifu | 0:5a6468b4164d | 490 | while (serial.readable()) { |
sunifu | 0:5a6468b4164d | 491 | serial.getc(); |
sunifu | 0:5a6468b4164d | 492 | } |
sunifu | 0:5a6468b4164d | 493 | return true; |
sunifu | 0:5a6468b4164d | 494 | } |