たけし みわ
/
y_CameraC1098_ES_01
CameraC1098/CameraC1098.cpp@0:5bf7e3564c3b, 2012-06-17 (annotated)
- Committer:
- esmiwa
- Date:
- Sun Jun 17 01:15:35 2012 +0000
- Revision:
- 0:5bf7e3564c3b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
esmiwa | 0:5bf7e3564c3b | 1 | #include "mbed.h" |
esmiwa | 0:5bf7e3564c3b | 2 | #include "CameraC1098.h" |
esmiwa | 0:5bf7e3564c3b | 3 | //Camera() class |
esmiwa | 0:5bf7e3564c3b | 4 | CameraC1098::CameraC1098(PinName tx, PinName rx, Baud baud) : serial(tx, rx) |
esmiwa | 0:5bf7e3564c3b | 5 | { |
esmiwa | 0:5bf7e3564c3b | 6 | serial.baud((int)baud); |
esmiwa | 0:5bf7e3564c3b | 7 | } |
esmiwa | 0:5bf7e3564c3b | 8 | //inherited class |
esmiwa | 0:5bf7e3564c3b | 9 | CameraC1098::~CameraC1098() {}; |
esmiwa | 0:5bf7e3564c3b | 10 | //sync |
esmiwa | 0:5bf7e3564c3b | 11 | bool CameraC1098::sync() |
esmiwa | 0:5bf7e3564c3b | 12 | { |
esmiwa | 0:5bf7e3564c3b | 13 | waitIdle(); |
esmiwa | 0:5bf7e3564c3b | 14 | |
esmiwa | 0:5bf7e3564c3b | 15 | for (int i = 0; i < SYNCMAX; i++) { |
esmiwa | 0:5bf7e3564c3b | 16 | if (true == sendSync()) { |
esmiwa | 0:5bf7e3564c3b | 17 | if (true == recvAckOrNck()) { |
esmiwa | 0:5bf7e3564c3b | 18 | if (true == recvSync()) { |
esmiwa | 0:5bf7e3564c3b | 19 | if (true == sendAck(0x0D, 0x00)) { |
esmiwa | 0:5bf7e3564c3b | 20 | wait_ms(500); |
esmiwa | 0:5bf7e3564c3b | 21 | return true; |
esmiwa | 0:5bf7e3564c3b | 22 | } |
esmiwa | 0:5bf7e3564c3b | 23 | } |
esmiwa | 0:5bf7e3564c3b | 24 | } |
esmiwa | 0:5bf7e3564c3b | 25 | } |
esmiwa | 0:5bf7e3564c3b | 26 | wait_ms(10); |
esmiwa | 0:5bf7e3564c3b | 27 | } |
esmiwa | 0:5bf7e3564c3b | 28 | return false; |
esmiwa | 0:5bf7e3564c3b | 29 | } |
esmiwa | 0:5bf7e3564c3b | 30 | //initial |
esmiwa | 0:5bf7e3564c3b | 31 | bool CameraC1098::init(JpegResolution jr) |
esmiwa | 0:5bf7e3564c3b | 32 | { |
esmiwa | 0:5bf7e3564c3b | 33 | waitIdle(); |
esmiwa | 0:5bf7e3564c3b | 34 | bool en; |
esmiwa | 0:5bf7e3564c3b | 35 | |
esmiwa | 0:5bf7e3564c3b | 36 | en = sendInitial(jr); |
esmiwa | 0:5bf7e3564c3b | 37 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 38 | waitRecv(); |
esmiwa | 0:5bf7e3564c3b | 39 | en = recvAckOrNck(); |
esmiwa | 0:5bf7e3564c3b | 40 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 41 | return true; |
esmiwa | 0:5bf7e3564c3b | 42 | } |
esmiwa | 0:5bf7e3564c3b | 43 | //new |
esmiwa | 0:5bf7e3564c3b | 44 | bool CameraC1098::setupPackageSize(uint16_t packageSize) |
esmiwa | 0:5bf7e3564c3b | 45 | { |
esmiwa | 0:5bf7e3564c3b | 46 | static bool alreadySetupPackageSize = false; |
esmiwa | 0:5bf7e3564c3b | 47 | bool en; |
esmiwa | 0:5bf7e3564c3b | 48 | if (!alreadySetupPackageSize) { |
esmiwa | 0:5bf7e3564c3b | 49 | en = sendSetPackageSize(packageSize); |
esmiwa | 0:5bf7e3564c3b | 50 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 51 | waitRecv(); |
esmiwa | 0:5bf7e3564c3b | 52 | en = recvAckOrNck(); |
esmiwa | 0:5bf7e3564c3b | 53 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 54 | alreadySetupPackageSize = true; |
esmiwa | 0:5bf7e3564c3b | 55 | } |
esmiwa | 0:5bf7e3564c3b | 56 | |
esmiwa | 0:5bf7e3564c3b | 57 | return true; |
esmiwa | 0:5bf7e3564c3b | 58 | } |
esmiwa | 0:5bf7e3564c3b | 59 | //JpegSnapshotPicture |
esmiwa | 0:5bf7e3564c3b | 60 | bool CameraC1098::getJpegSnapshotPicture(void(*func)(char *buf, size_t siz)) |
esmiwa | 0:5bf7e3564c3b | 61 | { |
esmiwa | 0:5bf7e3564c3b | 62 | waitIdle(); |
esmiwa | 0:5bf7e3564c3b | 63 | bool en; |
esmiwa | 0:5bf7e3564c3b | 64 | |
esmiwa | 0:5bf7e3564c3b | 65 | en = sendSnapshot(CompressedPicture, 1); |
esmiwa | 0:5bf7e3564c3b | 66 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 67 | waitRecv(); |
esmiwa | 0:5bf7e3564c3b | 68 | en = recvAckOrNck(); |
esmiwa | 0:5bf7e3564c3b | 69 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 70 | |
esmiwa | 0:5bf7e3564c3b | 71 | en = sendGetPicture(SnapshotPicture); |
esmiwa | 0:5bf7e3564c3b | 72 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 73 | waitRecv(); |
esmiwa | 0:5bf7e3564c3b | 74 | en = recvAckOrNck(); |
esmiwa | 0:5bf7e3564c3b | 75 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 76 | //snapshot picture Data |
esmiwa | 0:5bf7e3564c3b | 77 | DataType dt; |
esmiwa | 0:5bf7e3564c3b | 78 | uint32_t length = 0; |
esmiwa | 0:5bf7e3564c3b | 79 | waitRecv(); |
esmiwa | 0:5bf7e3564c3b | 80 | en = recvData(&dt, &length); |
esmiwa | 0:5bf7e3564c3b | 81 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 82 | en = sendAck(0x00, 0); |
esmiwa | 0:5bf7e3564c3b | 83 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 84 | |
esmiwa | 0:5bf7e3564c3b | 85 | char databuf[packageSize - 6]; |
esmiwa | 0:5bf7e3564c3b | 86 | uint16_t pkg_total = length / (packageSize - 6); |
esmiwa | 0:5bf7e3564c3b | 87 | for (int i = 0; i <= (int)pkg_total; i++) { |
esmiwa | 0:5bf7e3564c3b | 88 | uint16_t checksum = 0; |
esmiwa | 0:5bf7e3564c3b | 89 | // ID. |
esmiwa | 0:5bf7e3564c3b | 90 | char idbuf[2]; |
esmiwa | 0:5bf7e3564c3b | 91 | waitRecv(); |
esmiwa | 0:5bf7e3564c3b | 92 | if (!recvBytes(idbuf, sizeof(idbuf))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 93 | checksum += idbuf[0]; |
esmiwa | 0:5bf7e3564c3b | 94 | checksum += idbuf[1]; |
esmiwa | 0:5bf7e3564c3b | 95 | uint16_t id = (idbuf[1] << 8) | (idbuf[0] << 0); |
esmiwa | 0:5bf7e3564c3b | 96 | if (id != i) {return false;} |
esmiwa | 0:5bf7e3564c3b | 97 | // Size of the data. |
esmiwa | 0:5bf7e3564c3b | 98 | char dsbuf[2]; |
esmiwa | 0:5bf7e3564c3b | 99 | waitRecv(); |
esmiwa | 0:5bf7e3564c3b | 100 | if (!recvBytes(dsbuf, sizeof(dsbuf))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 101 | // Received the data. |
esmiwa | 0:5bf7e3564c3b | 102 | checksum += dsbuf[0]; |
esmiwa | 0:5bf7e3564c3b | 103 | checksum += dsbuf[1]; |
esmiwa | 0:5bf7e3564c3b | 104 | uint16_t ds = (dsbuf[1] << 8) | (dsbuf[0] << 0); |
esmiwa | 0:5bf7e3564c3b | 105 | waitRecv(); |
esmiwa | 0:5bf7e3564c3b | 106 | if (!recvBytes(&databuf[0], ds)) {return false;} |
esmiwa | 0:5bf7e3564c3b | 107 | for (int j = 0; j < ds; j++) {checksum += databuf[j];} |
esmiwa | 0:5bf7e3564c3b | 108 | // Verify code. |
esmiwa | 0:5bf7e3564c3b | 109 | char vcbuf[2]; |
esmiwa | 0:5bf7e3564c3b | 110 | waitRecv(); |
esmiwa | 0:5bf7e3564c3b | 111 | if (!recvBytes(vcbuf, sizeof(vcbuf))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 112 | uint16_t vc = (vcbuf[1] << 8) | (vcbuf[0] << 0); |
esmiwa | 0:5bf7e3564c3b | 113 | if (vc != (checksum & 0xff)) {return false;} |
esmiwa | 0:5bf7e3564c3b | 114 | //Call a call back function.You can block this function while working. |
esmiwa | 0:5bf7e3564c3b | 115 | func(databuf, ds); |
esmiwa | 0:5bf7e3564c3b | 116 | //We should wait for camera working before reply a ACK. |
esmiwa | 0:5bf7e3564c3b | 117 | wait_ms(50); |
esmiwa | 0:5bf7e3564c3b | 118 | en = sendAck(0x00, 1 + i); |
esmiwa | 0:5bf7e3564c3b | 119 | if (true != en) {return en;} |
esmiwa | 0:5bf7e3564c3b | 120 | } |
esmiwa | 0:5bf7e3564c3b | 121 | return true; |
esmiwa | 0:5bf7e3564c3b | 122 | } |
esmiwa | 0:5bf7e3564c3b | 123 | //NEW change baud rate 14400 -> 115200bps |
esmiwa | 0:5bf7e3564c3b | 124 | bool CameraC1098::getnewbaud() |
esmiwa | 0:5bf7e3564c3b | 125 | { |
esmiwa | 0:5bf7e3564c3b | 126 | newbaud(); |
esmiwa | 0:5bf7e3564c3b | 127 | return true; |
esmiwa | 0:5bf7e3564c3b | 128 | } |
esmiwa | 0:5bf7e3564c3b | 129 | //sendInitial |
esmiwa | 0:5bf7e3564c3b | 130 | bool CameraC1098::sendInitial(JpegResolution jr) |
esmiwa | 0:5bf7e3564c3b | 131 | { |
esmiwa | 0:5bf7e3564c3b | 132 | char send[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 133 | send[0] = 0xAA; |
esmiwa | 0:5bf7e3564c3b | 134 | send[1] = 0x01; |
esmiwa | 0:5bf7e3564c3b | 135 | send[2] = 0x04;//115200bau |
esmiwa | 0:5bf7e3564c3b | 136 | send[3] = 0x07;//JpegColor |
esmiwa | 0:5bf7e3564c3b | 137 | send[4] = 0x00;//default |
esmiwa | 0:5bf7e3564c3b | 138 | send[5] = (char)jr; |
esmiwa | 0:5bf7e3564c3b | 139 | |
esmiwa | 0:5bf7e3564c3b | 140 | if (!sendBytes(send, sizeof(send))) {return false; } |
esmiwa | 0:5bf7e3564c3b | 141 | return true; |
esmiwa | 0:5bf7e3564c3b | 142 | } |
esmiwa | 0:5bf7e3564c3b | 143 | //sendgetPictyure |
esmiwa | 0:5bf7e3564c3b | 144 | bool CameraC1098::sendGetPicture(PictureType pt) { |
esmiwa | 0:5bf7e3564c3b | 145 | char send[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 146 | send[0] = 0xAA; |
esmiwa | 0:5bf7e3564c3b | 147 | send[1] = 0x04; |
esmiwa | 0:5bf7e3564c3b | 148 | send[2] = 0x01;//(char)pt; |
esmiwa | 0:5bf7e3564c3b | 149 | send[3] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 150 | send[4] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 151 | send[5] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 152 | |
esmiwa | 0:5bf7e3564c3b | 153 | if (!sendBytes(send, sizeof(send))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 154 | return true; |
esmiwa | 0:5bf7e3564c3b | 155 | } |
esmiwa | 0:5bf7e3564c3b | 156 | //sendSnapshot |
esmiwa | 0:5bf7e3564c3b | 157 | bool CameraC1098::sendSnapshot(SnapshotType st, uint16_t skipFrames) |
esmiwa | 0:5bf7e3564c3b | 158 | { |
esmiwa | 0:5bf7e3564c3b | 159 | char send[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 160 | send[0] = 0xAA; |
esmiwa | 0:5bf7e3564c3b | 161 | send[1] = 0x05; |
esmiwa | 0:5bf7e3564c3b | 162 | send[2] = 0x00;//(char)st; |
esmiwa | 0:5bf7e3564c3b | 163 | send[3] = (skipFrames >> 0) & 0xff; |
esmiwa | 0:5bf7e3564c3b | 164 | send[4] = (skipFrames >> 8) & 0xff; |
esmiwa | 0:5bf7e3564c3b | 165 | send[5] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 166 | |
esmiwa | 0:5bf7e3564c3b | 167 | if (!sendBytes(send, sizeof(send))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 168 | return true; |
esmiwa | 0:5bf7e3564c3b | 169 | } |
esmiwa | 0:5bf7e3564c3b | 170 | //sendSetOackagesize |
esmiwa | 0:5bf7e3564c3b | 171 | bool CameraC1098::sendSetPackageSize(uint16_t packageSize) |
esmiwa | 0:5bf7e3564c3b | 172 | { |
esmiwa | 0:5bf7e3564c3b | 173 | char send[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 174 | send[0] = 0xAA; |
esmiwa | 0:5bf7e3564c3b | 175 | send[1] = 0x06; |
esmiwa | 0:5bf7e3564c3b | 176 | send[2] = 0x08; |
esmiwa | 0:5bf7e3564c3b | 177 | send[3] = (packageSize >> 0) & 0xff; |
esmiwa | 0:5bf7e3564c3b | 178 | send[4] = (packageSize >> 8) & 0xff; |
esmiwa | 0:5bf7e3564c3b | 179 | send[5] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 180 | |
esmiwa | 0:5bf7e3564c3b | 181 | if (!sendBytes(send, sizeof(send))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 182 | return true; |
esmiwa | 0:5bf7e3564c3b | 183 | } |
esmiwa | 0:5bf7e3564c3b | 184 | //sendSetBaudrate |
esmiwa | 0:5bf7e3564c3b | 185 | //bool CameraC1098::sendSetBaudrate(Baud baud) |
esmiwa | 0:5bf7e3564c3b | 186 | //{ |
esmiwa | 0:5bf7e3564c3b | 187 | // char send[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 188 | // static struct baud_list { |
esmiwa | 0:5bf7e3564c3b | 189 | // Baud baud; |
esmiwa | 0:5bf7e3564c3b | 190 | // uint8_t div1st; |
esmiwa | 0:5bf7e3564c3b | 191 | // uint8_t div2nd; |
esmiwa | 0:5bf7e3564c3b | 192 | // } baudtable [] = { |
esmiwa | 0:5bf7e3564c3b | 193 | // { Baud7200, 0xff, 0x01 }, |
esmiwa | 0:5bf7e3564c3b | 194 | // { Baud9600, 0xbf, 0x01 }, |
esmiwa | 0:5bf7e3564c3b | 195 | // { Baud14400, 0x7f, 0x01 }, |
esmiwa | 0:5bf7e3564c3b | 196 | // { Baud19200, 0x5f, 0x01 }, |
esmiwa | 0:5bf7e3564c3b | 197 | // { Baud28800, 0x3f, 0x01 }, |
esmiwa | 0:5bf7e3564c3b | 198 | // { Baud38400, 0x2f, 0x01 }, |
esmiwa | 0:5bf7e3564c3b | 199 | // { Baud57600, 0x1f, 0x01 }, |
esmiwa | 0:5bf7e3564c3b | 200 | // { Baud115200, 0x0f, 0x01 } |
esmiwa | 0:5bf7e3564c3b | 201 | // }; |
esmiwa | 0:5bf7e3564c3b | 202 | // |
esmiwa | 0:5bf7e3564c3b | 203 | // uint8_t div1st = 0x00, div2nd = 0x00; |
esmiwa | 0:5bf7e3564c3b | 204 | // struct baud_list *p = &baudtable[0]; |
esmiwa | 0:5bf7e3564c3b | 205 | // for (int i = 0; i < sizeof(baudtable) / sizeof(baudtable[0]); i++) { |
esmiwa | 0:5bf7e3564c3b | 206 | // if (p->baud == baud) { |
esmiwa | 0:5bf7e3564c3b | 207 | // div1st = p->div1st; |
esmiwa | 0:5bf7e3564c3b | 208 | // div2nd = p->div2nd; |
esmiwa | 0:5bf7e3564c3b | 209 | // } |
esmiwa | 0:5bf7e3564c3b | 210 | // p++; |
esmiwa | 0:5bf7e3564c3b | 211 | // } |
esmiwa | 0:5bf7e3564c3b | 212 | |
esmiwa | 0:5bf7e3564c3b | 213 | // send[0] = 0xAA; |
esmiwa | 0:5bf7e3564c3b | 214 | // send[1] = 0x07; |
esmiwa | 0:5bf7e3564c3b | 215 | // send[2] = div1st; |
esmiwa | 0:5bf7e3564c3b | 216 | // send[3] = div2nd; |
esmiwa | 0:5bf7e3564c3b | 217 | // send[4] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 218 | // send[5] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 219 | // |
esmiwa | 0:5bf7e3564c3b | 220 | // if (!sendBytes(send, sizeof(send))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 221 | // |
esmiwa | 0:5bf7e3564c3b | 222 | // return true; |
esmiwa | 0:5bf7e3564c3b | 223 | //} |
esmiwa | 0:5bf7e3564c3b | 224 | //sendReset |
esmiwa | 0:5bf7e3564c3b | 225 | bool CameraC1098::sendReset(ResetType rt, bool specialReset) |
esmiwa | 0:5bf7e3564c3b | 226 | { |
esmiwa | 0:5bf7e3564c3b | 227 | char send[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 228 | send[0] = 0xAA; |
esmiwa | 0:5bf7e3564c3b | 229 | send[1] = 0x08; |
esmiwa | 0:5bf7e3564c3b | 230 | send[2] = (int)rt; |
esmiwa | 0:5bf7e3564c3b | 231 | send[3] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 232 | send[4] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 233 | send[5] = 0x00;//specialReset ? 0xff : 0x00; |
esmiwa | 0:5bf7e3564c3b | 234 | /* |
esmiwa | 0:5bf7e3564c3b | 235 | * Special reset : If the parameter is 0xFF, the command is a special Reset command and the firmware responds to it immediately. |
esmiwa | 0:5bf7e3564c3b | 236 | */ |
esmiwa | 0:5bf7e3564c3b | 237 | |
esmiwa | 0:5bf7e3564c3b | 238 | if (!sendBytes(send, sizeof(send))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 239 | return true; |
esmiwa | 0:5bf7e3564c3b | 240 | } |
esmiwa | 0:5bf7e3564c3b | 241 | //sendPowerOff |
esmiwa | 0:5bf7e3564c3b | 242 | bool CameraC1098::sendPowerOff() |
esmiwa | 0:5bf7e3564c3b | 243 | { |
esmiwa | 0:5bf7e3564c3b | 244 | char send[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 245 | send[0] = 0xAA; |
esmiwa | 0:5bf7e3564c3b | 246 | send[1] = 0x09; |
esmiwa | 0:5bf7e3564c3b | 247 | send[2] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 248 | send[3] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 249 | send[4] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 250 | send[5] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 251 | |
esmiwa | 0:5bf7e3564c3b | 252 | if (!sendBytes(send, sizeof(send))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 253 | return true; |
esmiwa | 0:5bf7e3564c3b | 254 | } |
esmiwa | 0:5bf7e3564c3b | 255 | //recvData |
esmiwa | 0:5bf7e3564c3b | 256 | bool CameraC1098::recvData(DataType *dt, uint32_t *length) |
esmiwa | 0:5bf7e3564c3b | 257 | { |
esmiwa | 0:5bf7e3564c3b | 258 | char recv[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 259 | if (!recvBytes(recv, sizeof(recv))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 260 | if ((0xAA != recv[0]) || (0x0A != recv[1])) {return false;} |
esmiwa | 0:5bf7e3564c3b | 261 | *dt = (DataType)recv[2]; |
esmiwa | 0:5bf7e3564c3b | 262 | *length = (recv[5] << 16) | (recv[4] << 8) | (recv[3] << 0); |
esmiwa | 0:5bf7e3564c3b | 263 | return true; |
esmiwa | 0:5bf7e3564c3b | 264 | } |
esmiwa | 0:5bf7e3564c3b | 265 | //sendSync |
esmiwa | 0:5bf7e3564c3b | 266 | bool CameraC1098::sendSync() |
esmiwa | 0:5bf7e3564c3b | 267 | { |
esmiwa | 0:5bf7e3564c3b | 268 | char send[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 269 | send[0] = 0xAA; |
esmiwa | 0:5bf7e3564c3b | 270 | send[1] = 0x0D; |
esmiwa | 0:5bf7e3564c3b | 271 | send[2] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 272 | send[3] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 273 | send[4] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 274 | send[5] = 0x00; |
esmiwa | 0:5bf7e3564c3b | 275 | if (!sendBytes(send, sizeof(send))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 276 | return true; |
esmiwa | 0:5bf7e3564c3b | 277 | } |
esmiwa | 0:5bf7e3564c3b | 278 | //recvSync |
esmiwa | 0:5bf7e3564c3b | 279 | bool CameraC1098::recvSync() |
esmiwa | 0:5bf7e3564c3b | 280 | { |
esmiwa | 0:5bf7e3564c3b | 281 | char recv[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 282 | if (!recvBytes(recv, sizeof(recv))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 283 | if ((0xAA != recv[0]) || (0x0D != recv[1])) {return false;} |
esmiwa | 0:5bf7e3564c3b | 284 | return true; |
esmiwa | 0:5bf7e3564c3b | 285 | } |
esmiwa | 0:5bf7e3564c3b | 286 | // SendAck |
esmiwa | 0:5bf7e3564c3b | 287 | //@param commandId The command with that ID is acknowledged by this command. |
esmiwa | 0:5bf7e3564c3b | 288 | //@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. |
esmiwa | 0:5bf7e3564c3b | 289 | bool CameraC1098::sendAck(uint8_t commandId, uint16_t packageId) |
esmiwa | 0:5bf7e3564c3b | 290 | { |
esmiwa | 0:5bf7e3564c3b | 291 | char send[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 292 | send[0] = 0xAA; |
esmiwa | 0:5bf7e3564c3b | 293 | send[1] = 0x0E; |
esmiwa | 0:5bf7e3564c3b | 294 | send[2] = commandId; |
esmiwa | 0:5bf7e3564c3b | 295 | send[3] = 0x00; // ACK counter is not used. |
esmiwa | 0:5bf7e3564c3b | 296 | send[4] = (packageId >> 0) & 0xff; |
esmiwa | 0:5bf7e3564c3b | 297 | send[5] = (packageId >> 8) & 0xff; |
esmiwa | 0:5bf7e3564c3b | 298 | |
esmiwa | 0:5bf7e3564c3b | 299 | if (!sendBytes(send, sizeof(send))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 300 | return true; |
esmiwa | 0:5bf7e3564c3b | 301 | } |
esmiwa | 0:5bf7e3564c3b | 302 | // Receive ACK or NCK. |
esmiwa | 0:5bf7e3564c3b | 303 | bool CameraC1098::recvAckOrNck() |
esmiwa | 0:5bf7e3564c3b | 304 | { |
esmiwa | 0:5bf7e3564c3b | 305 | char recv[COMMAND_LENGTH]; |
esmiwa | 0:5bf7e3564c3b | 306 | if (!recvBytes(recv, sizeof(recv))) {return false;} |
esmiwa | 0:5bf7e3564c3b | 307 | if ((0xAA == recv[0]) && (0x0E == recv[1])) {return true;} |
esmiwa | 0:5bf7e3564c3b | 308 | if ((0xAA == recv[0]) && (0x0F == recv[1])) {return true;} |
esmiwa | 0:5bf7e3564c3b | 309 | return false; |
esmiwa | 0:5bf7e3564c3b | 310 | } |
esmiwa | 0:5bf7e3564c3b | 311 | //Send bytes to camera module. |
esmiwa | 0:5bf7e3564c3b | 312 | //@param buf Pointer to the data buffer. |
esmiwa | 0:5bf7e3564c3b | 313 | //@param len Length of the data buffer. |
esmiwa | 0:5bf7e3564c3b | 314 | bool CameraC1098::sendBytes(char *buf, size_t len, int timeout_us) |
esmiwa | 0:5bf7e3564c3b | 315 | { |
esmiwa | 0:5bf7e3564c3b | 316 | for (uint32_t i = 0; i < (uint32_t)len; i++) { |
esmiwa | 0:5bf7e3564c3b | 317 | int cnt = 0; |
esmiwa | 0:5bf7e3564c3b | 318 | while (!serial.writeable()) { |
esmiwa | 0:5bf7e3564c3b | 319 | wait_us(1); |
esmiwa | 0:5bf7e3564c3b | 320 | cnt++; |
esmiwa | 0:5bf7e3564c3b | 321 | if (timeout_us < cnt) { |
esmiwa | 0:5bf7e3564c3b | 322 | return false; |
esmiwa | 0:5bf7e3564c3b | 323 | } |
esmiwa | 0:5bf7e3564c3b | 324 | } |
esmiwa | 0:5bf7e3564c3b | 325 | serial.putc(buf[i]); |
esmiwa | 0:5bf7e3564c3b | 326 | } |
esmiwa | 0:5bf7e3564c3b | 327 | return true; |
esmiwa | 0:5bf7e3564c3b | 328 | } |
esmiwa | 0:5bf7e3564c3b | 329 | //Receive bytes from camera module. |
esmiwa | 0:5bf7e3564c3b | 330 | //@param buf Pointer to the data buffer. |
esmiwa | 0:5bf7e3564c3b | 331 | //@param len Length of the data buffer. |
esmiwa | 0:5bf7e3564c3b | 332 | bool CameraC1098::recvBytes(char *buf, size_t len, int timeout_us) |
esmiwa | 0:5bf7e3564c3b | 333 | { |
esmiwa | 0:5bf7e3564c3b | 334 | for (uint32_t i = 0; i < (uint32_t)len; i++) { |
esmiwa | 0:5bf7e3564c3b | 335 | int cnt = 0; |
esmiwa | 0:5bf7e3564c3b | 336 | while (!serial.readable()) { |
esmiwa | 0:5bf7e3564c3b | 337 | wait_us(1); |
esmiwa | 0:5bf7e3564c3b | 338 | cnt++; |
esmiwa | 0:5bf7e3564c3b | 339 | if (timeout_us < cnt) { |
esmiwa | 0:5bf7e3564c3b | 340 | return false; |
esmiwa | 0:5bf7e3564c3b | 341 | } |
esmiwa | 0:5bf7e3564c3b | 342 | } |
esmiwa | 0:5bf7e3564c3b | 343 | buf[i] = serial.getc(); |
esmiwa | 0:5bf7e3564c3b | 344 | } |
esmiwa | 0:5bf7e3564c3b | 345 | return true; |
esmiwa | 0:5bf7e3564c3b | 346 | } |
esmiwa | 0:5bf7e3564c3b | 347 | //Wait received. |
esmiwa | 0:5bf7e3564c3b | 348 | bool CameraC1098::waitRecv() |
esmiwa | 0:5bf7e3564c3b | 349 | { |
esmiwa | 0:5bf7e3564c3b | 350 | while (!serial.readable()) { |
esmiwa | 0:5bf7e3564c3b | 351 | } |
esmiwa | 0:5bf7e3564c3b | 352 | return true; |
esmiwa | 0:5bf7e3564c3b | 353 | } |
esmiwa | 0:5bf7e3564c3b | 354 | //Wait idle state. |
esmiwa | 0:5bf7e3564c3b | 355 | bool CameraC1098::waitIdle() |
esmiwa | 0:5bf7e3564c3b | 356 | { |
esmiwa | 0:5bf7e3564c3b | 357 | while (serial.readable()) { |
esmiwa | 0:5bf7e3564c3b | 358 | serial.getc(); |
esmiwa | 0:5bf7e3564c3b | 359 | } |
esmiwa | 0:5bf7e3564c3b | 360 | return true; |
esmiwa | 0:5bf7e3564c3b | 361 | } |
esmiwa | 0:5bf7e3564c3b | 362 | //NEW change baud rate 14400 -> 115200bps |
esmiwa | 0:5bf7e3564c3b | 363 | bool CameraC1098::newbaud() |
esmiwa | 0:5bf7e3564c3b | 364 | { |
esmiwa | 0:5bf7e3564c3b | 365 | Baud baud=CameraC1098::Baud115200; |
esmiwa | 0:5bf7e3564c3b | 366 | serial.baud((int)baud); |
esmiwa | 0:5bf7e3564c3b | 367 | return true; |
esmiwa | 0:5bf7e3564c3b | 368 | } |