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