Gas Pressure Display Updated Power control for Pressure sensor added
Dependencies: UniGraphic mbed vt100
afLib/StatusCommand.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 | #include "StatusCommand.h" |
Rhyme | 0:37c8ecde13c2 | 18 | |
Rhyme | 0:37c8ecde13c2 | 19 | StatusCommand::StatusCommand(uint16_t bytesToSend) { |
Rhyme | 0:37c8ecde13c2 | 20 | _cmd = 0x30; |
Rhyme | 0:37c8ecde13c2 | 21 | _bytesToSend = bytesToSend; |
Rhyme | 0:37c8ecde13c2 | 22 | _bytesToRecv = 0; |
Rhyme | 0:37c8ecde13c2 | 23 | } |
Rhyme | 0:37c8ecde13c2 | 24 | |
Rhyme | 0:37c8ecde13c2 | 25 | StatusCommand::StatusCommand() { |
Rhyme | 0:37c8ecde13c2 | 26 | _cmd = 0x30; |
Rhyme | 0:37c8ecde13c2 | 27 | _bytesToSend = 0; |
Rhyme | 0:37c8ecde13c2 | 28 | _bytesToRecv = 0; |
Rhyme | 0:37c8ecde13c2 | 29 | } |
Rhyme | 0:37c8ecde13c2 | 30 | |
Rhyme | 0:37c8ecde13c2 | 31 | StatusCommand::~StatusCommand() { |
Rhyme | 0:37c8ecde13c2 | 32 | } |
Rhyme | 0:37c8ecde13c2 | 33 | |
Rhyme | 0:37c8ecde13c2 | 34 | uint16_t StatusCommand::getSize() { |
Rhyme | 0:37c8ecde13c2 | 35 | return sizeof(_cmd) + sizeof(_bytesToSend) + sizeof(_bytesToRecv); |
Rhyme | 0:37c8ecde13c2 | 36 | } |
Rhyme | 0:37c8ecde13c2 | 37 | |
Rhyme | 0:37c8ecde13c2 | 38 | uint16_t StatusCommand::getBytes(int *bytes) { |
Rhyme | 0:37c8ecde13c2 | 39 | int index = 0; |
Rhyme | 0:37c8ecde13c2 | 40 | |
Rhyme | 0:37c8ecde13c2 | 41 | bytes[index++] = (_cmd); |
Rhyme | 0:37c8ecde13c2 | 42 | bytes[index++] = (_bytesToSend & 0xff); |
Rhyme | 0:37c8ecde13c2 | 43 | bytes[index++] = ((_bytesToSend >> 8) & 0xff); |
Rhyme | 0:37c8ecde13c2 | 44 | bytes[index++] = (_bytesToRecv & 0xff); |
Rhyme | 0:37c8ecde13c2 | 45 | bytes[index++] = ((_bytesToRecv >> 8) & 0xff); |
Rhyme | 0:37c8ecde13c2 | 46 | |
Rhyme | 0:37c8ecde13c2 | 47 | return index; |
Rhyme | 0:37c8ecde13c2 | 48 | } |
Rhyme | 0:37c8ecde13c2 | 49 | |
Rhyme | 0:37c8ecde13c2 | 50 | uint8_t StatusCommand::calcChecksum() { |
Rhyme | 0:37c8ecde13c2 | 51 | uint8_t result = 0; |
Rhyme | 0:37c8ecde13c2 | 52 | |
Rhyme | 0:37c8ecde13c2 | 53 | result += (_cmd); |
Rhyme | 0:37c8ecde13c2 | 54 | result += (_bytesToSend & 0xff); |
Rhyme | 0:37c8ecde13c2 | 55 | result += ((_bytesToSend >> 8) & 0xff); |
Rhyme | 0:37c8ecde13c2 | 56 | result += (_bytesToRecv & 0xff); |
Rhyme | 0:37c8ecde13c2 | 57 | result += ((_bytesToRecv >> 8) & 0xff); |
Rhyme | 0:37c8ecde13c2 | 58 | |
Rhyme | 0:37c8ecde13c2 | 59 | return result; |
Rhyme | 0:37c8ecde13c2 | 60 | } |
Rhyme | 0:37c8ecde13c2 | 61 | |
Rhyme | 0:37c8ecde13c2 | 62 | void StatusCommand::setChecksum(uint8_t checksum) { |
Rhyme | 0:37c8ecde13c2 | 63 | _checksum = checksum; |
Rhyme | 0:37c8ecde13c2 | 64 | } |
Rhyme | 0:37c8ecde13c2 | 65 | |
Rhyme | 0:37c8ecde13c2 | 66 | uint8_t StatusCommand::getChecksum() { |
Rhyme | 0:37c8ecde13c2 | 67 | uint8_t result = 0; |
Rhyme | 0:37c8ecde13c2 | 68 | |
Rhyme | 0:37c8ecde13c2 | 69 | result += (_cmd); |
Rhyme | 0:37c8ecde13c2 | 70 | result += (_bytesToSend & 0xff); |
Rhyme | 0:37c8ecde13c2 | 71 | result += ((_bytesToSend >> 8) & 0xff); |
Rhyme | 0:37c8ecde13c2 | 72 | result += (_bytesToRecv & 0xff); |
Rhyme | 0:37c8ecde13c2 | 73 | result += ((_bytesToRecv >> 8) & 0xff); |
Rhyme | 0:37c8ecde13c2 | 74 | |
Rhyme | 0:37c8ecde13c2 | 75 | return result; |
Rhyme | 0:37c8ecde13c2 | 76 | } |
Rhyme | 0:37c8ecde13c2 | 77 | |
Rhyme | 0:37c8ecde13c2 | 78 | void StatusCommand::setAck(bool ack) { |
Rhyme | 0:37c8ecde13c2 | 79 | _cmd = ack ? 0x31 : 0x30; |
Rhyme | 0:37c8ecde13c2 | 80 | } |
Rhyme | 0:37c8ecde13c2 | 81 | |
Rhyme | 0:37c8ecde13c2 | 82 | void StatusCommand::setBytesToSend(uint16_t bytesToSend) { |
Rhyme | 0:37c8ecde13c2 | 83 | _bytesToSend = bytesToSend; |
Rhyme | 0:37c8ecde13c2 | 84 | } |
Rhyme | 0:37c8ecde13c2 | 85 | |
Rhyme | 0:37c8ecde13c2 | 86 | uint16_t StatusCommand::getBytesToSend() { |
Rhyme | 0:37c8ecde13c2 | 87 | return _bytesToSend; |
Rhyme | 0:37c8ecde13c2 | 88 | } |
Rhyme | 0:37c8ecde13c2 | 89 | |
Rhyme | 0:37c8ecde13c2 | 90 | void StatusCommand::setBytesToRecv(uint16_t bytesToRecv) { |
Rhyme | 0:37c8ecde13c2 | 91 | _bytesToRecv = bytesToRecv; |
Rhyme | 0:37c8ecde13c2 | 92 | } |
Rhyme | 0:37c8ecde13c2 | 93 | |
Rhyme | 0:37c8ecde13c2 | 94 | uint16_t StatusCommand::getBytesToRecv() { |
Rhyme | 0:37c8ecde13c2 | 95 | return _bytesToRecv; |
Rhyme | 0:37c8ecde13c2 | 96 | } |
Rhyme | 0:37c8ecde13c2 | 97 | |
Rhyme | 0:37c8ecde13c2 | 98 | bool StatusCommand::equals(StatusCommand *statusCommand) { |
Rhyme | 0:37c8ecde13c2 | 99 | return (_cmd == statusCommand->_cmd && _bytesToSend == statusCommand->_bytesToSend && |
Rhyme | 0:37c8ecde13c2 | 100 | _bytesToRecv == statusCommand->_bytesToRecv); |
Rhyme | 0:37c8ecde13c2 | 101 | } |
Rhyme | 0:37c8ecde13c2 | 102 | |
Rhyme | 0:37c8ecde13c2 | 103 | bool StatusCommand::isValid() { |
Rhyme | 0:37c8ecde13c2 | 104 | return (_checksum == calcChecksum()) && (_cmd == 0x30 || _cmd == 0x31); |
Rhyme | 0:37c8ecde13c2 | 105 | } |
Rhyme | 0:37c8ecde13c2 | 106 | |
Rhyme | 0:37c8ecde13c2 | 107 | void StatusCommand::dumpBytes() { |
Rhyme | 0:37c8ecde13c2 | 108 | int len = getSize(); |
Rhyme | 0:37c8ecde13c2 | 109 | int bytes[len]; |
Rhyme | 0:37c8ecde13c2 | 110 | getBytes(bytes); |
Rhyme | 0:37c8ecde13c2 | 111 | |
Rhyme | 0:37c8ecde13c2 | 112 | SERIAL_PRINT_DBG_ASR("len : %d\n",len); |
Rhyme | 0:37c8ecde13c2 | 113 | SERIAL_PRINT_DBG_ASR("data : "); |
Rhyme | 0:37c8ecde13c2 | 114 | for (int i = 0; i < len; i++) { |
Rhyme | 0:37c8ecde13c2 | 115 | if (i > 0) { |
Rhyme | 0:37c8ecde13c2 | 116 | SERIAL_PRINT_DBG_ASR(", "); |
Rhyme | 0:37c8ecde13c2 | 117 | } |
Rhyme | 0:37c8ecde13c2 | 118 | int b = bytes[i] & 0xff; |
Rhyme | 0:37c8ecde13c2 | 119 | if (b < 0x10) { |
Rhyme | 0:37c8ecde13c2 | 120 | SERIAL_PRINT_DBG_ASR("0x%08x",b); |
Rhyme | 0:37c8ecde13c2 | 121 | } else { |
Rhyme | 0:37c8ecde13c2 | 122 | SERIAL_PRINT_DBG_ASR("0x%08x",b); |
Rhyme | 0:37c8ecde13c2 | 123 | } |
Rhyme | 0:37c8ecde13c2 | 124 | } |
Rhyme | 0:37c8ecde13c2 | 125 | SERIAL_PRINT_DBG_ASR("\n"); |
Rhyme | 0:37c8ecde13c2 | 126 | } |
Rhyme | 0:37c8ecde13c2 | 127 | |
Rhyme | 0:37c8ecde13c2 | 128 | void StatusCommand::dump() { |
Rhyme | 0:37c8ecde13c2 | 129 | SERIAL_PRINT_DBG_ASR("cmd : %s\n",_cmd == 0x30 ? "STATUS" : "STATUS_ACK"); |
Rhyme | 0:37c8ecde13c2 | 130 | SERIAL_PRINT_DBG_ASR("bytes to send : %d\n",_bytesToSend); |
Rhyme | 0:37c8ecde13c2 | 131 | SERIAL_PRINT_DBG_ASR("bytes to receive : %d\n",_bytesToRecv); |
Rhyme | 0:37c8ecde13c2 | 132 | } |
Rhyme | 0:37c8ecde13c2 | 133 |