Sakura IoT Platform b
Fork of SakuraIO by
SakuraIO.cpp@3:c54a1eba22c4, 2016-11-19 (annotated)
- Committer:
- chibiegg
- Date:
- Sat Nov 19 15:19:49 2016 +0000
- Revision:
- 3:c54a1eba22c4
- Parent:
- 0:8d34375d954c
add license
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chibiegg | 3:c54a1eba22c4 | 1 | /* SAKURA Internet IoT Communication Module Library for mbed |
chibiegg | 3:c54a1eba22c4 | 2 | * |
chibiegg | 3:c54a1eba22c4 | 3 | * The MIT License (MIT) |
chibiegg | 3:c54a1eba22c4 | 4 | * |
chibiegg | 3:c54a1eba22c4 | 5 | * Copyright (c) SAKURA Internet Inc. |
chibiegg | 3:c54a1eba22c4 | 6 | * |
chibiegg | 3:c54a1eba22c4 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
chibiegg | 3:c54a1eba22c4 | 8 | * of this software and associated documentation files (the "Software"), |
chibiegg | 3:c54a1eba22c4 | 9 | * to deal in the Software without restriction, including without limitation |
chibiegg | 3:c54a1eba22c4 | 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
chibiegg | 3:c54a1eba22c4 | 11 | * and/or sell copies of the Software, and to permit persons to whom the Software |
chibiegg | 3:c54a1eba22c4 | 12 | * is furnished to do so, subject to the following conditions: |
chibiegg | 3:c54a1eba22c4 | 13 | * |
chibiegg | 3:c54a1eba22c4 | 14 | * The above copyright notice and this permission notice shall be included |
chibiegg | 3:c54a1eba22c4 | 15 | * in all copies or substantial portions of the Software. |
chibiegg | 3:c54a1eba22c4 | 16 | * |
chibiegg | 3:c54a1eba22c4 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
chibiegg | 3:c54a1eba22c4 | 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
chibiegg | 3:c54a1eba22c4 | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
chibiegg | 3:c54a1eba22c4 | 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
chibiegg | 3:c54a1eba22c4 | 21 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
chibiegg | 3:c54a1eba22c4 | 22 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
chibiegg | 3:c54a1eba22c4 | 23 | */ |
chibiegg | 3:c54a1eba22c4 | 24 | |
spiralray | 0:8d34375d954c | 25 | #include "mbed.h" |
spiralray | 0:8d34375d954c | 26 | #include "SakuraIO.h" |
spiralray | 0:8d34375d954c | 27 | #include "SakuraIO/commands.h" |
spiralray | 0:8d34375d954c | 28 | #include "SakuraIO/debug.h" |
spiralray | 0:8d34375d954c | 29 | |
spiralray | 0:8d34375d954c | 30 | uint8_t SakuraIO::executeCommand(uint8_t cmd,uint8_t requestLength, uint8_t *request, uint8_t *responseLength, uint8_t *response) |
spiralray | 0:8d34375d954c | 31 | { |
spiralray | 0:8d34375d954c | 32 | uint8_t parity = 0x00; |
spiralray | 0:8d34375d954c | 33 | uint8_t result = 0x00; |
spiralray | 0:8d34375d954c | 34 | uint8_t tmpResponseLength, tmpResponse; |
spiralray | 0:8d34375d954c | 35 | |
spiralray | 0:8d34375d954c | 36 | dbgln("executeCommand"); |
spiralray | 0:8d34375d954c | 37 | |
spiralray | 0:8d34375d954c | 38 | this->begin(); |
spiralray | 0:8d34375d954c | 39 | |
spiralray | 0:8d34375d954c | 40 | // request |
spiralray | 0:8d34375d954c | 41 | this->sendByte(cmd); |
spiralray | 0:8d34375d954c | 42 | this->sendByte(requestLength); |
spiralray | 0:8d34375d954c | 43 | parity = cmd ^ requestLength; |
spiralray | 0:8d34375d954c | 44 | for(int16_t i=0; i<requestLength; i++){ |
spiralray | 0:8d34375d954c | 45 | parity ^= request[i]; |
spiralray | 0:8d34375d954c | 46 | this->sendByte(request[i]); |
spiralray | 0:8d34375d954c | 47 | } |
spiralray | 0:8d34375d954c | 48 | this->sendByte(parity); |
spiralray | 0:8d34375d954c | 49 | //this->finishSending(); |
spiralray | 0:8d34375d954c | 50 | |
spiralray | 0:8d34375d954c | 51 | tmpResponseLength = 0; |
spiralray | 0:8d34375d954c | 52 | if(responseLength != NULL){ |
spiralray | 0:8d34375d954c | 53 | tmpResponseLength = *responseLength; |
spiralray | 0:8d34375d954c | 54 | } |
spiralray | 0:8d34375d954c | 55 | |
spiralray | 0:8d34375d954c | 56 | wait_ms(10); |
spiralray | 0:8d34375d954c | 57 | |
spiralray | 0:8d34375d954c | 58 | // response |
spiralray | 0:8d34375d954c | 59 | this->startReceive(tmpResponseLength+3); |
spiralray | 0:8d34375d954c | 60 | result = this->receiveByte(); |
spiralray | 0:8d34375d954c | 61 | if(result != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 62 | dbgln("Invalid status"); |
spiralray | 0:8d34375d954c | 63 | this->end(); |
spiralray | 0:8d34375d954c | 64 | return result; |
spiralray | 0:8d34375d954c | 65 | } |
spiralray | 0:8d34375d954c | 66 | tmpResponseLength = this->receiveByte(); |
spiralray | 0:8d34375d954c | 67 | parity = result ^ tmpResponseLength; |
spiralray | 0:8d34375d954c | 68 | if(responseLength != NULL){ |
spiralray | 0:8d34375d954c | 69 | if(*responseLength < tmpResponseLength){ |
spiralray | 0:8d34375d954c | 70 | tmpResponseLength = *responseLength; |
spiralray | 0:8d34375d954c | 71 | } |
spiralray | 0:8d34375d954c | 72 | *responseLength = tmpResponseLength; |
spiralray | 0:8d34375d954c | 73 | } |
spiralray | 0:8d34375d954c | 74 | for(int16_t i=0; i<tmpResponseLength; i++){ |
spiralray | 0:8d34375d954c | 75 | tmpResponse = this->receiveByte(); |
spiralray | 0:8d34375d954c | 76 | parity ^= tmpResponse; |
spiralray | 0:8d34375d954c | 77 | if(response != NULL){ |
spiralray | 0:8d34375d954c | 78 | response[i] = tmpResponse; |
spiralray | 0:8d34375d954c | 79 | } |
spiralray | 0:8d34375d954c | 80 | } |
spiralray | 0:8d34375d954c | 81 | dbgln("Parity"); |
spiralray | 0:8d34375d954c | 82 | uint8_t p = this->receiveByte(true); |
spiralray | 0:8d34375d954c | 83 | parity ^= p; |
spiralray | 0:8d34375d954c | 84 | dbg("Parity="); |
spiralray | 0:8d34375d954c | 85 | dbgln(p); |
spiralray | 0:8d34375d954c | 86 | if(parity != 0x00){ |
spiralray | 0:8d34375d954c | 87 | result = CMD_ERROR_PARITY; |
spiralray | 0:8d34375d954c | 88 | dbgln("Invalid parity"); |
spiralray | 0:8d34375d954c | 89 | }else{ |
spiralray | 0:8d34375d954c | 90 | dbgln("Success"); |
spiralray | 0:8d34375d954c | 91 | } |
spiralray | 0:8d34375d954c | 92 | //this->finishReceiving(); |
spiralray | 0:8d34375d954c | 93 | |
spiralray | 0:8d34375d954c | 94 | this->end(); |
spiralray | 0:8d34375d954c | 95 | return result; |
spiralray | 0:8d34375d954c | 96 | } |
spiralray | 0:8d34375d954c | 97 | |
spiralray | 0:8d34375d954c | 98 | /* Common Commands */ |
spiralray | 0:8d34375d954c | 99 | |
spiralray | 0:8d34375d954c | 100 | uint8_t SakuraIO::getConnectionStatus(){ |
spiralray | 0:8d34375d954c | 101 | uint8_t responseLength = 1; |
spiralray | 0:8d34375d954c | 102 | uint8_t response[1] = {0x00}; |
spiralray | 0:8d34375d954c | 103 | if(executeCommand(CMD_GET_CONNECTION_STATUS, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 104 | return 0x7F; |
spiralray | 0:8d34375d954c | 105 | } |
spiralray | 0:8d34375d954c | 106 | return response[0]; |
spiralray | 0:8d34375d954c | 107 | } |
spiralray | 0:8d34375d954c | 108 | |
spiralray | 0:8d34375d954c | 109 | uint8_t SakuraIO::getSignalQuarity(){ |
spiralray | 0:8d34375d954c | 110 | uint8_t responseLength = 1; |
spiralray | 0:8d34375d954c | 111 | uint8_t response[1] = {0x00}; |
spiralray | 0:8d34375d954c | 112 | |
spiralray | 0:8d34375d954c | 113 | if(executeCommand(CMD_GET_SIGNAL_QUALITY, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 114 | return 0x00; |
spiralray | 0:8d34375d954c | 115 | } |
spiralray | 0:8d34375d954c | 116 | return response[0]; |
spiralray | 0:8d34375d954c | 117 | } |
spiralray | 0:8d34375d954c | 118 | |
spiralray | 0:8d34375d954c | 119 | uint64_t SakuraIO::getUnixtime(){ |
spiralray | 0:8d34375d954c | 120 | uint8_t responseLength = 8; |
spiralray | 0:8d34375d954c | 121 | uint8_t response[8] = {0x00}; |
spiralray | 0:8d34375d954c | 122 | if(executeCommand(CMD_GET_DATETIME, 0, NULL, &responseLength, response) != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 123 | return 0x00; |
spiralray | 0:8d34375d954c | 124 | } |
spiralray | 0:8d34375d954c | 125 | return *((uint64_t *)response); |
spiralray | 0:8d34375d954c | 126 | } |
spiralray | 0:8d34375d954c | 127 | |
spiralray | 0:8d34375d954c | 128 | uint8_t SakuraIO::echoback(uint8_t length, uint8_t *data, uint8_t *response){ |
spiralray | 0:8d34375d954c | 129 | uint8_t responseLength = length; |
spiralray | 0:8d34375d954c | 130 | if(executeCommand(CMD_ECHO_BACK, length, data, &responseLength, response) != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 131 | return 0x00; |
spiralray | 0:8d34375d954c | 132 | } |
spiralray | 0:8d34375d954c | 133 | return responseLength; |
spiralray | 0:8d34375d954c | 134 | } |
spiralray | 0:8d34375d954c | 135 | |
spiralray | 0:8d34375d954c | 136 | /* IO Commands */ |
spiralray | 0:8d34375d954c | 137 | |
spiralray | 0:8d34375d954c | 138 | uint16_t SakuraIO::getADC(uint8_t channel){ |
spiralray | 0:8d34375d954c | 139 | uint8_t request[1] = {channel}; |
spiralray | 0:8d34375d954c | 140 | uint8_t response[2] = {0x00}; |
spiralray | 0:8d34375d954c | 141 | uint8_t responseLength = sizeof(response); |
spiralray | 0:8d34375d954c | 142 | if(executeCommand(CMD_READ_ADC, 1, request, &responseLength, response) != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 143 | return 0xffff; |
spiralray | 0:8d34375d954c | 144 | } |
spiralray | 0:8d34375d954c | 145 | return *((uint16_t *)response); |
spiralray | 0:8d34375d954c | 146 | } |
spiralray | 0:8d34375d954c | 147 | |
spiralray | 0:8d34375d954c | 148 | /* TX Commands */ |
spiralray | 0:8d34375d954c | 149 | uint8_t SakuraIO::enqueueTxRaw(uint8_t ch, uint8_t type, uint8_t length, uint8_t *data, uint64_t offset){ |
spiralray | 0:8d34375d954c | 150 | uint8_t request[18] = {0x00}; |
spiralray | 0:8d34375d954c | 151 | uint8_t requestLength = 10; |
spiralray | 0:8d34375d954c | 152 | request[0] = ch; |
spiralray | 0:8d34375d954c | 153 | request[1] = type; |
spiralray | 0:8d34375d954c | 154 | for(uint8_t i=0;i<length;i++){ |
spiralray | 0:8d34375d954c | 155 | request[2+i] = data[i]; |
spiralray | 0:8d34375d954c | 156 | } |
spiralray | 0:8d34375d954c | 157 | if(offset != 0){ |
spiralray | 0:8d34375d954c | 158 | requestLength = 18; |
spiralray | 0:8d34375d954c | 159 | for(uint8_t i=0;i<8;i++){ |
spiralray | 0:8d34375d954c | 160 | request[10+i] = ((uint8_t *)&offset)[i]; |
spiralray | 0:8d34375d954c | 161 | } |
spiralray | 0:8d34375d954c | 162 | } |
spiralray | 0:8d34375d954c | 163 | return executeCommand(CMD_TX_ENQUEUE, requestLength, request, NULL, NULL); |
spiralray | 0:8d34375d954c | 164 | } |
spiralray | 0:8d34375d954c | 165 | |
spiralray | 0:8d34375d954c | 166 | uint8_t SakuraIO::enqueueTx(uint8_t ch, int32_t value, uint64_t offset){ |
spiralray | 0:8d34375d954c | 167 | return enqueueTxRaw(ch, 'i', 4, (uint8_t *)&value, offset); |
spiralray | 0:8d34375d954c | 168 | } |
spiralray | 0:8d34375d954c | 169 | |
spiralray | 0:8d34375d954c | 170 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint32_t value, uint64_t offset){ |
spiralray | 0:8d34375d954c | 171 | return enqueueTxRaw(ch, 'I', 4, (uint8_t *)&value, offset); |
spiralray | 0:8d34375d954c | 172 | } |
spiralray | 0:8d34375d954c | 173 | |
spiralray | 0:8d34375d954c | 174 | uint8_t SakuraIO::enqueueTx(uint8_t ch, int64_t value, uint64_t offset){ |
spiralray | 0:8d34375d954c | 175 | return enqueueTxRaw(ch, 'l', 8, (uint8_t *)&value, offset); |
spiralray | 0:8d34375d954c | 176 | } |
spiralray | 0:8d34375d954c | 177 | |
spiralray | 0:8d34375d954c | 178 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint64_t value, uint64_t offset){ |
spiralray | 0:8d34375d954c | 179 | return enqueueTxRaw(ch, 'L', 8, (uint8_t *)&value, offset); |
spiralray | 0:8d34375d954c | 180 | } |
spiralray | 0:8d34375d954c | 181 | |
spiralray | 0:8d34375d954c | 182 | uint8_t SakuraIO::enqueueTx(uint8_t ch, float value, uint64_t offset){ |
spiralray | 0:8d34375d954c | 183 | return enqueueTxRaw(ch, 'f', 4, (uint8_t *)&value, offset); |
spiralray | 0:8d34375d954c | 184 | } |
spiralray | 0:8d34375d954c | 185 | |
spiralray | 0:8d34375d954c | 186 | uint8_t SakuraIO::enqueueTx(uint8_t ch, double value, uint64_t offset){ |
spiralray | 0:8d34375d954c | 187 | return enqueueTxRaw(ch, 'd', 8, (uint8_t *)&value, offset); |
spiralray | 0:8d34375d954c | 188 | } |
spiralray | 0:8d34375d954c | 189 | |
spiralray | 0:8d34375d954c | 190 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint8_t value[8], uint64_t offset){ |
spiralray | 0:8d34375d954c | 191 | return enqueueTxRaw(ch, 'b', 8, (uint8_t *)value, offset); |
spiralray | 0:8d34375d954c | 192 | } |
spiralray | 0:8d34375d954c | 193 | |
spiralray | 0:8d34375d954c | 194 | uint8_t SakuraIO::enqueueTx(uint8_t ch, int32_t value){ |
spiralray | 0:8d34375d954c | 195 | return enqueueTx(ch, value, (uint32_t)0); |
spiralray | 0:8d34375d954c | 196 | } |
spiralray | 0:8d34375d954c | 197 | |
spiralray | 0:8d34375d954c | 198 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint32_t value){ |
spiralray | 0:8d34375d954c | 199 | return enqueueTx(ch, value, (uint32_t)0); |
spiralray | 0:8d34375d954c | 200 | } |
spiralray | 0:8d34375d954c | 201 | |
spiralray | 0:8d34375d954c | 202 | uint8_t SakuraIO::enqueueTx(uint8_t ch, int64_t value){ |
spiralray | 0:8d34375d954c | 203 | return enqueueTx(ch, value, (uint32_t)0); |
spiralray | 0:8d34375d954c | 204 | } |
spiralray | 0:8d34375d954c | 205 | |
spiralray | 0:8d34375d954c | 206 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint64_t value){ |
spiralray | 0:8d34375d954c | 207 | return enqueueTx(ch, value, (uint32_t)0); |
spiralray | 0:8d34375d954c | 208 | } |
spiralray | 0:8d34375d954c | 209 | |
spiralray | 0:8d34375d954c | 210 | uint8_t SakuraIO::enqueueTx(uint8_t ch, float value){ |
spiralray | 0:8d34375d954c | 211 | return enqueueTx(ch, value, (uint32_t)0); |
spiralray | 0:8d34375d954c | 212 | } |
spiralray | 0:8d34375d954c | 213 | |
spiralray | 0:8d34375d954c | 214 | uint8_t SakuraIO::enqueueTx(uint8_t ch, double value){ |
spiralray | 0:8d34375d954c | 215 | return enqueueTx(ch, value, (uint32_t)0); |
spiralray | 0:8d34375d954c | 216 | } |
spiralray | 0:8d34375d954c | 217 | |
spiralray | 0:8d34375d954c | 218 | uint8_t SakuraIO::enqueueTx(uint8_t ch, uint8_t value[8]){ |
spiralray | 0:8d34375d954c | 219 | return enqueueTx(ch, value, (uint32_t)0); |
spiralray | 0:8d34375d954c | 220 | } |
spiralray | 0:8d34375d954c | 221 | |
spiralray | 0:8d34375d954c | 222 | uint8_t SakuraIO::getTxQueueLength(uint8_t *available, uint8_t *queued){ |
spiralray | 0:8d34375d954c | 223 | uint8_t response[2] = {0x00}; |
spiralray | 0:8d34375d954c | 224 | uint8_t responseLength = 2; |
spiralray | 0:8d34375d954c | 225 | uint8_t ret = executeCommand(CMD_TX_LENGTH, 0, NULL, &responseLength, response); |
spiralray | 0:8d34375d954c | 226 | *available = response[0]; |
spiralray | 0:8d34375d954c | 227 | *queued = response[1]; |
spiralray | 0:8d34375d954c | 228 | return ret; |
spiralray | 0:8d34375d954c | 229 | } |
spiralray | 0:8d34375d954c | 230 | |
spiralray | 0:8d34375d954c | 231 | uint8_t SakuraIO::clearTx(){ |
spiralray | 0:8d34375d954c | 232 | return executeCommand(CMD_TX_CLEAR, 0, NULL, NULL, NULL); |
spiralray | 0:8d34375d954c | 233 | } |
spiralray | 0:8d34375d954c | 234 | |
spiralray | 0:8d34375d954c | 235 | uint8_t SakuraIO::send(){ |
spiralray | 0:8d34375d954c | 236 | return executeCommand(CMD_TX_SEND, 0, NULL, NULL, NULL); |
spiralray | 0:8d34375d954c | 237 | } |
spiralray | 0:8d34375d954c | 238 | |
spiralray | 0:8d34375d954c | 239 | uint8_t SakuraIO::getTxStatus(uint8_t *queue, uint8_t *immediate){ |
spiralray | 0:8d34375d954c | 240 | uint8_t response[2] = {0x00}; |
spiralray | 0:8d34375d954c | 241 | uint8_t responseLength = 2; |
spiralray | 0:8d34375d954c | 242 | uint8_t ret = executeCommand(CMD_TX_STAT, 0, NULL, &responseLength, response); |
spiralray | 0:8d34375d954c | 243 | *queue = response[0]; |
spiralray | 0:8d34375d954c | 244 | *immediate = response[1]; |
spiralray | 0:8d34375d954c | 245 | return ret; |
spiralray | 0:8d34375d954c | 246 | } |
spiralray | 0:8d34375d954c | 247 | |
spiralray | 0:8d34375d954c | 248 | /* RX Commands */ |
spiralray | 0:8d34375d954c | 249 | |
spiralray | 0:8d34375d954c | 250 | uint8_t SakuraIO::dequeueRx(uint8_t *ch, uint8_t *type, uint8_t *value, int64_t *offset){ |
spiralray | 0:8d34375d954c | 251 | uint8_t response[18] = {0x00}; |
spiralray | 0:8d34375d954c | 252 | uint8_t responseLength = 18; |
spiralray | 0:8d34375d954c | 253 | uint8_t ret = executeCommand(CMD_RX_DEQUEUE, 0, NULL, &responseLength, response); |
spiralray | 0:8d34375d954c | 254 | if(ret != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 255 | return ret; |
spiralray | 0:8d34375d954c | 256 | } |
spiralray | 0:8d34375d954c | 257 | |
spiralray | 0:8d34375d954c | 258 | *ch = response[0]; |
spiralray | 0:8d34375d954c | 259 | *type = response[1]; |
spiralray | 0:8d34375d954c | 260 | for(uint8_t i=0; i<8; i++){ |
spiralray | 0:8d34375d954c | 261 | value[i] = response[2+i]; |
spiralray | 0:8d34375d954c | 262 | } |
spiralray | 0:8d34375d954c | 263 | for(uint8_t i=0; i<8; i++){ |
spiralray | 0:8d34375d954c | 264 | ((uint8_t *)offset)[i] = response[10+i]; |
spiralray | 0:8d34375d954c | 265 | } |
spiralray | 0:8d34375d954c | 266 | |
spiralray | 0:8d34375d954c | 267 | return ret; |
spiralray | 0:8d34375d954c | 268 | } |
spiralray | 0:8d34375d954c | 269 | |
spiralray | 0:8d34375d954c | 270 | uint8_t SakuraIO::peekRx(uint8_t *ch, uint8_t *type, uint8_t *value, int64_t *offset){ |
spiralray | 0:8d34375d954c | 271 | uint8_t response[18] = {0x00}; |
spiralray | 0:8d34375d954c | 272 | uint8_t responseLength = 18; |
spiralray | 0:8d34375d954c | 273 | uint8_t ret = executeCommand(CMD_RX_PEEK, 0, NULL, &responseLength, response); |
spiralray | 0:8d34375d954c | 274 | if(ret != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 275 | return ret; |
spiralray | 0:8d34375d954c | 276 | } |
spiralray | 0:8d34375d954c | 277 | |
spiralray | 0:8d34375d954c | 278 | *ch = response[0]; |
spiralray | 0:8d34375d954c | 279 | *type = response[1]; |
spiralray | 0:8d34375d954c | 280 | for(uint8_t i=0; i<8; i++){ |
spiralray | 0:8d34375d954c | 281 | value[0] = response[2+i]; |
spiralray | 0:8d34375d954c | 282 | } |
spiralray | 0:8d34375d954c | 283 | for(uint8_t i=0; i<8; i++){ |
spiralray | 0:8d34375d954c | 284 | ((uint8_t *)offset)[i] = response[10+i]; |
spiralray | 0:8d34375d954c | 285 | } |
spiralray | 0:8d34375d954c | 286 | |
spiralray | 0:8d34375d954c | 287 | return ret; |
spiralray | 0:8d34375d954c | 288 | } |
spiralray | 0:8d34375d954c | 289 | |
spiralray | 0:8d34375d954c | 290 | uint8_t SakuraIO::getRxQueueLength(uint8_t *available, uint8_t *queued){ |
spiralray | 0:8d34375d954c | 291 | uint8_t response[2] = {0x00}; |
spiralray | 0:8d34375d954c | 292 | uint8_t responseLength = 2; |
spiralray | 0:8d34375d954c | 293 | uint8_t ret = executeCommand(CMD_RX_LENGTH, 0, NULL, &responseLength, response); |
spiralray | 0:8d34375d954c | 294 | *available = response[0]; |
spiralray | 0:8d34375d954c | 295 | *queued = response[1]; |
spiralray | 0:8d34375d954c | 296 | return ret; |
spiralray | 0:8d34375d954c | 297 | } |
spiralray | 0:8d34375d954c | 298 | |
spiralray | 0:8d34375d954c | 299 | uint8_t SakuraIO::clearRx(){ |
spiralray | 0:8d34375d954c | 300 | return executeCommand(CMD_RX_CLEAR, 0, NULL, NULL, NULL); |
spiralray | 0:8d34375d954c | 301 | } |
spiralray | 0:8d34375d954c | 302 | |
spiralray | 0:8d34375d954c | 303 | /* Operation command */ |
spiralray | 0:8d34375d954c | 304 | |
spiralray | 0:8d34375d954c | 305 | uint16_t SakuraIO::getProductID(){ |
spiralray | 0:8d34375d954c | 306 | uint8_t response[2] = {0x00}; |
spiralray | 0:8d34375d954c | 307 | uint8_t responseLength = 2; |
spiralray | 0:8d34375d954c | 308 | uint8_t ret = executeCommand(CMD_GET_PRODUCT_ID, 0, NULL, &responseLength, response); |
spiralray | 0:8d34375d954c | 309 | if(ret != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 310 | return 0x00; |
spiralray | 0:8d34375d954c | 311 | } |
spiralray | 0:8d34375d954c | 312 | return *((uint16_t *)response); |
spiralray | 0:8d34375d954c | 313 | } |
spiralray | 0:8d34375d954c | 314 | |
spiralray | 0:8d34375d954c | 315 | uint8_t SakuraIO::getUniqueID(char *data){ |
spiralray | 0:8d34375d954c | 316 | uint8_t response[11] = {0x00}; |
spiralray | 0:8d34375d954c | 317 | uint8_t responseLength = 10; |
spiralray | 0:8d34375d954c | 318 | uint8_t ret = executeCommand(CMD_GET_UNIQUE_ID, 0, NULL, &responseLength, response); |
spiralray | 0:8d34375d954c | 319 | if(ret != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 320 | return ret; |
spiralray | 0:8d34375d954c | 321 | } |
spiralray | 0:8d34375d954c | 322 | for(uint8_t i=0; i<responseLength; i++){ |
spiralray | 0:8d34375d954c | 323 | data[i] = (char)response[i]; |
spiralray | 0:8d34375d954c | 324 | } |
spiralray | 0:8d34375d954c | 325 | data[responseLength] = 0x00; |
spiralray | 0:8d34375d954c | 326 | return ret; |
spiralray | 0:8d34375d954c | 327 | } |
spiralray | 0:8d34375d954c | 328 | |
spiralray | 0:8d34375d954c | 329 | uint8_t SakuraIO::getFirmwareVersion(char *data){ |
spiralray | 0:8d34375d954c | 330 | uint8_t response[33] = {0x00}; |
spiralray | 0:8d34375d954c | 331 | uint8_t responseLength = 32; |
spiralray | 0:8d34375d954c | 332 | uint8_t ret = executeCommand(CMD_GET_FIRMWARE_VERSION, 0, NULL, &responseLength, response); |
spiralray | 0:8d34375d954c | 333 | if(ret != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 334 | return ret; |
spiralray | 0:8d34375d954c | 335 | } |
spiralray | 0:8d34375d954c | 336 | for(uint8_t i=0; i<responseLength; i++){ |
spiralray | 0:8d34375d954c | 337 | data[i] = (char)response[i]; |
spiralray | 0:8d34375d954c | 338 | } |
spiralray | 0:8d34375d954c | 339 | data[responseLength] = 0x00; |
spiralray | 0:8d34375d954c | 340 | return ret; |
spiralray | 0:8d34375d954c | 341 | } |
spiralray | 0:8d34375d954c | 342 | |
spiralray | 0:8d34375d954c | 343 | uint8_t SakuraIO::unlock(){ |
spiralray | 0:8d34375d954c | 344 | uint8_t request[4] = {0x53, 0x6B, 0x72, 0x61}; |
spiralray | 0:8d34375d954c | 345 | return executeCommand(CMD_UNLOCK, 4, request, NULL, NULL); |
spiralray | 0:8d34375d954c | 346 | } |
spiralray | 0:8d34375d954c | 347 | |
spiralray | 0:8d34375d954c | 348 | uint8_t SakuraIO::updateFirmware(){ |
spiralray | 0:8d34375d954c | 349 | return executeCommand(CMD_UPDATE_FIRMWARE, 0, 0, NULL, NULL); |
spiralray | 0:8d34375d954c | 350 | } |
spiralray | 0:8d34375d954c | 351 | |
spiralray | 0:8d34375d954c | 352 | uint8_t SakuraIO::getFirmwareUpdateStatus(){ |
spiralray | 0:8d34375d954c | 353 | uint8_t response[1] = {0x00}; |
spiralray | 0:8d34375d954c | 354 | uint8_t responseLength = 1; |
spiralray | 0:8d34375d954c | 355 | if(executeCommand(CMD_GET_UPDATE_FIRMWARE_STATUS, 0, 0, &responseLength, response) != CMD_ERROR_NONE){ |
spiralray | 0:8d34375d954c | 356 | return 0xff; |
spiralray | 0:8d34375d954c | 357 | } |
spiralray | 0:8d34375d954c | 358 | return response[0]; |
spiralray | 0:8d34375d954c | 359 | } |
spiralray | 0:8d34375d954c | 360 | |
spiralray | 0:8d34375d954c | 361 | uint8_t SakuraIO::reset(){ |
spiralray | 0:8d34375d954c | 362 | return executeCommand(CMD_SOFTWARE_RESET, 0, 0, NULL, NULL); |
spiralray | 0:8d34375d954c | 363 | } |