Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)
Dependencies: UniGraphic mbed vt100
18-Jun-2018 外部センサの電源オン・オフ機能は下位互換の為に無効になっていました。 この版で再度有効にしました。
afLib/Command.cpp@1:8d65cfc3a2e2, 2018-06-18 (annotated)
- Committer:
- Rhyme
- Date:
- Mon Jun 18 01:56:00 2018 +0000
- Revision:
- 1:8d65cfc3a2e2
- Parent:
- 0:846e2321c637
External sensor power on/off function enabled. (Previously disabled)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:846e2321c637 | 1 | /** |
Rhyme | 0:846e2321c637 | 2 | * Copyright 2015 Afero, Inc. |
Rhyme | 0:846e2321c637 | 3 | * |
Rhyme | 0:846e2321c637 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Rhyme | 0:846e2321c637 | 5 | * you may not use this file except in compliance with the License. |
Rhyme | 0:846e2321c637 | 6 | * You may obtain a copy of the License at |
Rhyme | 0:846e2321c637 | 7 | * |
Rhyme | 0:846e2321c637 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Rhyme | 0:846e2321c637 | 9 | * |
Rhyme | 0:846e2321c637 | 10 | * Unless required by applicable law or agreed to in writing, software |
Rhyme | 0:846e2321c637 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Rhyme | 0:846e2321c637 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Rhyme | 0:846e2321c637 | 13 | * See the License for the specific language governing permissions and |
Rhyme | 0:846e2321c637 | 14 | * limitations under the License. |
Rhyme | 0:846e2321c637 | 15 | */ |
Rhyme | 0:846e2321c637 | 16 | |
Rhyme | 0:846e2321c637 | 17 | //wsugi #include "Arduino.h" |
Rhyme | 0:846e2321c637 | 18 | #include "mbed.h" |
Rhyme | 0:846e2321c637 | 19 | #include <stdio.h> |
Rhyme | 0:846e2321c637 | 20 | #include "Command.h" |
Rhyme | 0:846e2321c637 | 21 | #include "msg_types.h" |
Rhyme | 0:846e2321c637 | 22 | |
Rhyme | 0:846e2321c637 | 23 | #define SERIAL_PRINT_DBG_ASR_ON 0 |
Rhyme | 0:846e2321c637 | 24 | |
Rhyme | 0:846e2321c637 | 25 | #define CMD_HDR_LEN 4 // 4 byte header on all commands |
Rhyme | 0:846e2321c637 | 26 | #define CMD_VAL_LEN 2 // 2 byte value length for commands that have a value |
Rhyme | 0:846e2321c637 | 27 | |
Rhyme | 0:846e2321c637 | 28 | const char *CMD_NAMES[] = {"SET ", "GET ", "UPDATE"}; |
Rhyme | 0:846e2321c637 | 29 | |
Rhyme | 0:846e2321c637 | 30 | |
Rhyme | 0:846e2321c637 | 31 | Command::Command(uint16_t len, uint8_t *bytes) { |
Rhyme | 0:846e2321c637 | 32 | int index = 0; |
Rhyme | 0:846e2321c637 | 33 | |
Rhyme | 0:846e2321c637 | 34 | _cmd = bytes[index++]; |
Rhyme | 0:846e2321c637 | 35 | _requestId = bytes[index++]; |
Rhyme | 0:846e2321c637 | 36 | _attrId = bytes[index + 0] | bytes[index + 1] << 8; |
Rhyme | 0:846e2321c637 | 37 | index += 2; |
Rhyme | 0:846e2321c637 | 38 | |
Rhyme | 0:846e2321c637 | 39 | if (_cmd == MSG_TYPE_GET) { |
Rhyme | 0:846e2321c637 | 40 | return; |
Rhyme | 0:846e2321c637 | 41 | } |
Rhyme | 0:846e2321c637 | 42 | if (_cmd == MSG_TYPE_UPDATE) { |
Rhyme | 0:846e2321c637 | 43 | _status = bytes[index++]; |
Rhyme | 0:846e2321c637 | 44 | _reason = bytes[index++]; |
Rhyme | 0:846e2321c637 | 45 | } |
Rhyme | 0:846e2321c637 | 46 | |
Rhyme | 0:846e2321c637 | 47 | _valueLen = bytes[index + 0] | bytes[index + 1] << 8; |
Rhyme | 0:846e2321c637 | 48 | index += 2; |
Rhyme | 0:846e2321c637 | 49 | _value = new uint8_t[_valueLen]; |
Rhyme | 0:846e2321c637 | 50 | for (int i = 0; i < _valueLen; i++) { |
Rhyme | 0:846e2321c637 | 51 | _value[i] = bytes[index + i]; |
Rhyme | 0:846e2321c637 | 52 | } |
Rhyme | 0:846e2321c637 | 53 | } |
Rhyme | 0:846e2321c637 | 54 | |
Rhyme | 0:846e2321c637 | 55 | Command::Command(uint8_t requestId, const char *str) { |
Rhyme | 0:846e2321c637 | 56 | _requestId = requestId & 0xff; |
Rhyme | 0:846e2321c637 | 57 | |
Rhyme | 0:846e2321c637 | 58 | char *cp; //wsugi = strdup(str); |
Rhyme | 0:846e2321c637 | 59 | //wsugi 00 start |
Rhyme | 0:846e2321c637 | 60 | { |
Rhyme | 0:846e2321c637 | 61 | int length = strlen(str)+1; |
Rhyme | 0:846e2321c637 | 62 | cp = (char*)malloc(length); |
Rhyme | 0:846e2321c637 | 63 | strcpy(cp,str); |
Rhyme | 0:846e2321c637 | 64 | } |
Rhyme | 0:846e2321c637 | 65 | //wsugi 00 end |
Rhyme | 0:846e2321c637 | 66 | char *tok = strtok(cp, " "); |
Rhyme | 0:846e2321c637 | 67 | _cmd = strToCmd(tok); |
Rhyme | 0:846e2321c637 | 68 | |
Rhyme | 0:846e2321c637 | 69 | tok = strtok(NULL, " "); |
Rhyme | 0:846e2321c637 | 70 | _attrId = strToAttrId(tok); |
Rhyme | 0:846e2321c637 | 71 | |
Rhyme | 0:846e2321c637 | 72 | if (_cmd == MSG_TYPE_GET) { |
Rhyme | 0:846e2321c637 | 73 | _valueLen = 0; |
Rhyme | 0:846e2321c637 | 74 | _value = NULL; |
Rhyme | 0:846e2321c637 | 75 | } else { |
Rhyme | 0:846e2321c637 | 76 | tok = strtok(NULL, " "); |
Rhyme | 0:846e2321c637 | 77 | _valueLen = strlen(tok) / 2; |
Rhyme | 0:846e2321c637 | 78 | _value = new uint8_t[_valueLen]; |
Rhyme | 0:846e2321c637 | 79 | strToValue(tok, _value); |
Rhyme | 0:846e2321c637 | 80 | } |
Rhyme | 0:846e2321c637 | 81 | |
Rhyme | 0:846e2321c637 | 82 | free(cp); |
Rhyme | 0:846e2321c637 | 83 | } |
Rhyme | 0:846e2321c637 | 84 | |
Rhyme | 0:846e2321c637 | 85 | Command::Command(uint8_t requestId, uint8_t cmd, uint16_t attrId) { |
Rhyme | 0:846e2321c637 | 86 | _requestId = requestId; |
Rhyme | 0:846e2321c637 | 87 | _cmd = cmd; |
Rhyme | 0:846e2321c637 | 88 | _attrId = attrId; |
Rhyme | 0:846e2321c637 | 89 | _valueLen = 0; |
Rhyme | 0:846e2321c637 | 90 | _value = NULL; |
Rhyme | 0:846e2321c637 | 91 | } |
Rhyme | 0:846e2321c637 | 92 | |
Rhyme | 0:846e2321c637 | 93 | Command::Command(uint8_t requestId, uint8_t cmd, uint16_t attrId, uint16_t valueLen, uint8_t *value) { |
Rhyme | 0:846e2321c637 | 94 | _requestId = requestId; |
Rhyme | 0:846e2321c637 | 95 | _cmd = cmd; |
Rhyme | 0:846e2321c637 | 96 | _attrId = attrId; |
Rhyme | 0:846e2321c637 | 97 | _valueLen = valueLen; |
Rhyme | 0:846e2321c637 | 98 | _value = new uint8_t[_valueLen]; |
Rhyme | 0:846e2321c637 | 99 | memcpy(_value, value, valueLen); |
Rhyme | 0:846e2321c637 | 100 | } |
Rhyme | 0:846e2321c637 | 101 | |
Rhyme | 0:846e2321c637 | 102 | Command::Command(uint8_t requestId, uint8_t cmd, uint16_t attrId, uint8_t status, uint8_t reason, uint16_t valueLen, |
Rhyme | 0:846e2321c637 | 103 | uint8_t *value) { |
Rhyme | 0:846e2321c637 | 104 | _requestId = requestId; |
Rhyme | 0:846e2321c637 | 105 | _cmd = cmd; |
Rhyme | 0:846e2321c637 | 106 | _attrId = attrId; |
Rhyme | 0:846e2321c637 | 107 | _status = status; |
Rhyme | 0:846e2321c637 | 108 | _reason = reason; |
Rhyme | 0:846e2321c637 | 109 | _valueLen = valueLen; |
Rhyme | 0:846e2321c637 | 110 | _value = new uint8_t[_valueLen]; |
Rhyme | 0:846e2321c637 | 111 | memcpy(_value, value, valueLen); |
Rhyme | 0:846e2321c637 | 112 | } |
Rhyme | 0:846e2321c637 | 113 | |
Rhyme | 0:846e2321c637 | 114 | Command::Command() { |
Rhyme | 0:846e2321c637 | 115 | } |
Rhyme | 0:846e2321c637 | 116 | |
Rhyme | 0:846e2321c637 | 117 | Command::~Command() { |
Rhyme | 0:846e2321c637 | 118 | if (_value != NULL) { |
Rhyme | 0:846e2321c637 | 119 | delete[] _value; //wsugi delete (_value); |
Rhyme | 0:846e2321c637 | 120 | } |
Rhyme | 0:846e2321c637 | 121 | } |
Rhyme | 0:846e2321c637 | 122 | |
Rhyme | 0:846e2321c637 | 123 | int Command::strToValue(char *valueStr, uint8_t *value) { |
Rhyme | 0:846e2321c637 | 124 | for (int i = 0; i < (int) (strlen(valueStr) / 2); i++) { |
Rhyme | 0:846e2321c637 | 125 | int j = i * 2; |
Rhyme | 0:846e2321c637 | 126 | value[i] = getVal(valueStr[j + 1]) + (getVal(valueStr[j]) << 4); |
Rhyme | 0:846e2321c637 | 127 | } |
Rhyme | 0:846e2321c637 | 128 | |
Rhyme | 0:846e2321c637 | 129 | return 0; |
Rhyme | 0:846e2321c637 | 130 | } |
Rhyme | 0:846e2321c637 | 131 | |
Rhyme | 0:846e2321c637 | 132 | uint16_t Command::strToAttrId(char *attrIdStr) { |
Rhyme | 0:846e2321c637 | 133 | return atoi(attrIdStr); |
Rhyme | 0:846e2321c637 | 134 | //return String(attrIdStr).toInt(); |
Rhyme | 0:846e2321c637 | 135 | } |
Rhyme | 0:846e2321c637 | 136 | |
Rhyme | 0:846e2321c637 | 137 | uint8_t Command::strToCmd(char *cmdStr) { |
Rhyme | 0:846e2321c637 | 138 | char c = cmdStr[0]; |
Rhyme | 0:846e2321c637 | 139 | if (c == 'g' || c == 'G') { |
Rhyme | 0:846e2321c637 | 140 | return MSG_TYPE_GET; |
Rhyme | 0:846e2321c637 | 141 | } else if (c == 's' || c == 'S') { |
Rhyme | 0:846e2321c637 | 142 | return MSG_TYPE_SET; |
Rhyme | 0:846e2321c637 | 143 | } else if (c == 'u' || c == 'U') { |
Rhyme | 0:846e2321c637 | 144 | return MSG_TYPE_UPDATE; |
Rhyme | 0:846e2321c637 | 145 | } |
Rhyme | 0:846e2321c637 | 146 | |
Rhyme | 0:846e2321c637 | 147 | return 0xFF ; |
Rhyme | 0:846e2321c637 | 148 | } |
Rhyme | 0:846e2321c637 | 149 | |
Rhyme | 0:846e2321c637 | 150 | uint8_t Command::getCommand() { |
Rhyme | 0:846e2321c637 | 151 | return _cmd; |
Rhyme | 0:846e2321c637 | 152 | } |
Rhyme | 0:846e2321c637 | 153 | |
Rhyme | 0:846e2321c637 | 154 | uint8_t Command::getReqId() { |
Rhyme | 0:846e2321c637 | 155 | return _requestId; |
Rhyme | 0:846e2321c637 | 156 | } |
Rhyme | 0:846e2321c637 | 157 | |
Rhyme | 0:846e2321c637 | 158 | uint16_t Command::getAttrId() { |
Rhyme | 0:846e2321c637 | 159 | return _attrId; |
Rhyme | 0:846e2321c637 | 160 | } |
Rhyme | 0:846e2321c637 | 161 | |
Rhyme | 0:846e2321c637 | 162 | uint16_t Command::getValueLen() { |
Rhyme | 0:846e2321c637 | 163 | return _valueLen; |
Rhyme | 0:846e2321c637 | 164 | } |
Rhyme | 0:846e2321c637 | 165 | |
Rhyme | 0:846e2321c637 | 166 | void Command::getValue(uint8_t *value) { |
Rhyme | 0:846e2321c637 | 167 | for (int i = 0; i < _valueLen; i++) { |
Rhyme | 0:846e2321c637 | 168 | value[i] = _value[i]; |
Rhyme | 0:846e2321c637 | 169 | } |
Rhyme | 0:846e2321c637 | 170 | } |
Rhyme | 0:846e2321c637 | 171 | |
Rhyme | 0:846e2321c637 | 172 | uint8_t *Command::getValueP() { |
Rhyme | 0:846e2321c637 | 173 | return _value; |
Rhyme | 0:846e2321c637 | 174 | } |
Rhyme | 0:846e2321c637 | 175 | |
Rhyme | 0:846e2321c637 | 176 | uint16_t Command::getSize() { |
Rhyme | 0:846e2321c637 | 177 | uint16_t len = CMD_HDR_LEN; |
Rhyme | 0:846e2321c637 | 178 | |
Rhyme | 0:846e2321c637 | 179 | if (_cmd != MSG_TYPE_GET) { |
Rhyme | 0:846e2321c637 | 180 | len += CMD_VAL_LEN + _valueLen; |
Rhyme | 0:846e2321c637 | 181 | } |
Rhyme | 0:846e2321c637 | 182 | |
Rhyme | 0:846e2321c637 | 183 | if (_cmd == MSG_TYPE_UPDATE) { |
Rhyme | 0:846e2321c637 | 184 | len += 2; // status byte + reason byte |
Rhyme | 0:846e2321c637 | 185 | } |
Rhyme | 0:846e2321c637 | 186 | |
Rhyme | 0:846e2321c637 | 187 | return len; |
Rhyme | 0:846e2321c637 | 188 | } |
Rhyme | 0:846e2321c637 | 189 | |
Rhyme | 0:846e2321c637 | 190 | uint16_t Command::getBytes(uint8_t *bytes) { |
Rhyme | 0:846e2321c637 | 191 | uint16_t len = getSize(); |
Rhyme | 0:846e2321c637 | 192 | |
Rhyme | 0:846e2321c637 | 193 | int index = 0; |
Rhyme | 0:846e2321c637 | 194 | |
Rhyme | 0:846e2321c637 | 195 | bytes[index++] = (_cmd); |
Rhyme | 0:846e2321c637 | 196 | |
Rhyme | 0:846e2321c637 | 197 | bytes[index++] = (_requestId); |
Rhyme | 0:846e2321c637 | 198 | |
Rhyme | 0:846e2321c637 | 199 | bytes[index++] = (_attrId & 0xff); |
Rhyme | 0:846e2321c637 | 200 | bytes[index++] = ((_attrId >> 8) & 0xff); |
Rhyme | 0:846e2321c637 | 201 | |
Rhyme | 0:846e2321c637 | 202 | if (_cmd == MSG_TYPE_GET) { |
Rhyme | 0:846e2321c637 | 203 | return len; |
Rhyme | 0:846e2321c637 | 204 | } |
Rhyme | 0:846e2321c637 | 205 | |
Rhyme | 0:846e2321c637 | 206 | if (_cmd == MSG_TYPE_UPDATE) { |
Rhyme | 0:846e2321c637 | 207 | bytes[index++] = (_status); |
Rhyme | 0:846e2321c637 | 208 | bytes[index++] = (_reason); |
Rhyme | 0:846e2321c637 | 209 | } |
Rhyme | 0:846e2321c637 | 210 | |
Rhyme | 0:846e2321c637 | 211 | bytes[index++] = (_valueLen & 0xff); |
Rhyme | 0:846e2321c637 | 212 | bytes[index++] = ((_valueLen >> 8) & 0xff); |
Rhyme | 0:846e2321c637 | 213 | |
Rhyme | 0:846e2321c637 | 214 | for (int i = 0; i < _valueLen; i++) { |
Rhyme | 0:846e2321c637 | 215 | bytes[index++] = (_value[i]); |
Rhyme | 0:846e2321c637 | 216 | } |
Rhyme | 0:846e2321c637 | 217 | |
Rhyme | 0:846e2321c637 | 218 | return len; |
Rhyme | 0:846e2321c637 | 219 | } |
Rhyme | 0:846e2321c637 | 220 | |
Rhyme | 0:846e2321c637 | 221 | bool Command::isValid() { |
Rhyme | 0:846e2321c637 | 222 | return (_cmd == MSG_TYPE_SET) || (_cmd == MSG_TYPE_GET) || (_cmd == MSG_TYPE_UPDATE); |
Rhyme | 0:846e2321c637 | 223 | } |
Rhyme | 0:846e2321c637 | 224 | |
Rhyme | 0:846e2321c637 | 225 | void Command::dumpBytes() { |
Rhyme | 0:846e2321c637 | 226 | #if SERIAL_PRINT_DBG_ASR_ON |
Rhyme | 0:846e2321c637 | 227 | uint16_t len = getSize(); |
Rhyme | 0:846e2321c637 | 228 | uint8_t bytes[len]; |
Rhyme | 0:846e2321c637 | 229 | getBytes(bytes); |
Rhyme | 0:846e2321c637 | 230 | |
Rhyme | 0:846e2321c637 | 231 | _printBuf[0] = 0; |
Rhyme | 0:846e2321c637 | 232 | sprintf(_printBuf, "len: %d value: ", len); |
Rhyme | 0:846e2321c637 | 233 | for (int i = 0; i < len; i++) { |
Rhyme | 0:846e2321c637 | 234 | int b = bytes[i] & 0xff; |
Rhyme | 0:846e2321c637 | 235 | sprintf(&_printBuf[strlen(_printBuf)], "%02x", b); |
Rhyme | 0:846e2321c637 | 236 | } |
Rhyme | 0:846e2321c637 | 237 | printf("%s\n",_printBuf); |
Rhyme | 0:846e2321c637 | 238 | #endif |
Rhyme | 0:846e2321c637 | 239 | } |
Rhyme | 0:846e2321c637 | 240 | |
Rhyme | 0:846e2321c637 | 241 | void Command::dump() { |
Rhyme | 0:846e2321c637 | 242 | #if SERIAL_PRINT_DBG_ASR_ON |
Rhyme | 0:846e2321c637 | 243 | _printBuf[0] = 0; |
Rhyme | 0:846e2321c637 | 244 | sprintf(_printBuf, "cmd: %s attr: %d value: ", |
Rhyme | 0:846e2321c637 | 245 | CMD_NAMES[_cmd - MESSAGE_CHANNEL_BASE - 1], |
Rhyme | 0:846e2321c637 | 246 | _attrId |
Rhyme | 0:846e2321c637 | 247 | ); |
Rhyme | 0:846e2321c637 | 248 | if (_cmd != MSG_TYPE_GET) { |
Rhyme | 0:846e2321c637 | 249 | for (int i = 0; i < _valueLen; i++) { |
Rhyme | 0:846e2321c637 | 250 | int b = _value[i] & 0xff; |
Rhyme | 0:846e2321c637 | 251 | sprintf(&_printBuf[strlen(_printBuf)], "%02x", b); |
Rhyme | 0:846e2321c637 | 252 | } |
Rhyme | 0:846e2321c637 | 253 | } |
Rhyme | 0:846e2321c637 | 254 | printf("%s\n",_printBuf); |
Rhyme | 0:846e2321c637 | 255 | #endif |
Rhyme | 0:846e2321c637 | 256 | } |
Rhyme | 0:846e2321c637 | 257 | |
Rhyme | 0:846e2321c637 | 258 | uint8_t Command::getVal(char c) { |
Rhyme | 0:846e2321c637 | 259 | if (c >= '0' && c <= '9') |
Rhyme | 0:846e2321c637 | 260 | return (uint8_t)(c - '0'); |
Rhyme | 0:846e2321c637 | 261 | else if (c >= 'A' && c <= 'F') |
Rhyme | 0:846e2321c637 | 262 | return (uint8_t)(c - 'A' + 10); |
Rhyme | 0:846e2321c637 | 263 | else if (c >= 'a' && c <= 'f') |
Rhyme | 0:846e2321c637 | 264 | return (uint8_t)(c - 'a' + 10); |
Rhyme | 0:846e2321c637 | 265 | |
Rhyme | 0:846e2321c637 | 266 | printf("bad hex char: %c\n",c); |
Rhyme | 0:846e2321c637 | 267 | |
Rhyme | 0:846e2321c637 | 268 | return 0; |
Rhyme | 0:846e2321c637 | 269 | } |