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