Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)
Dependencies: UniGraphic mbed vt100
18-Jun-2018 外部センサの電源オン・オフ機能は下位互換の為に無効になっていました。 この版で再度有効にしました。
afLib/StatusCommand.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 | #include "StatusCommand.h" |
Rhyme | 0:846e2321c637 | 18 | |
Rhyme | 0:846e2321c637 | 19 | #define SERIAL_PRINT_DBG_ASR_ON 0 |
Rhyme | 0:846e2321c637 | 20 | |
Rhyme | 0:846e2321c637 | 21 | StatusCommand::StatusCommand(uint16_t bytesToSend) { |
Rhyme | 0:846e2321c637 | 22 | _cmd = 0x30; |
Rhyme | 0:846e2321c637 | 23 | _bytesToSend = bytesToSend; |
Rhyme | 0:846e2321c637 | 24 | _bytesToRecv = 0; |
Rhyme | 0:846e2321c637 | 25 | } |
Rhyme | 0:846e2321c637 | 26 | |
Rhyme | 0:846e2321c637 | 27 | StatusCommand::StatusCommand() { |
Rhyme | 0:846e2321c637 | 28 | _cmd = 0x30; |
Rhyme | 0:846e2321c637 | 29 | _bytesToSend = 0; |
Rhyme | 0:846e2321c637 | 30 | _bytesToRecv = 0; |
Rhyme | 0:846e2321c637 | 31 | } |
Rhyme | 0:846e2321c637 | 32 | |
Rhyme | 0:846e2321c637 | 33 | StatusCommand::~StatusCommand() { |
Rhyme | 0:846e2321c637 | 34 | } |
Rhyme | 0:846e2321c637 | 35 | |
Rhyme | 0:846e2321c637 | 36 | uint16_t StatusCommand::getSize() { |
Rhyme | 0:846e2321c637 | 37 | return sizeof(_cmd) + sizeof(_bytesToSend) + sizeof(_bytesToRecv); |
Rhyme | 0:846e2321c637 | 38 | } |
Rhyme | 0:846e2321c637 | 39 | |
Rhyme | 0:846e2321c637 | 40 | uint16_t StatusCommand::getBytes(int *bytes) { |
Rhyme | 0:846e2321c637 | 41 | int index = 0; |
Rhyme | 0:846e2321c637 | 42 | |
Rhyme | 0:846e2321c637 | 43 | bytes[index++] = (_cmd); |
Rhyme | 0:846e2321c637 | 44 | bytes[index++] = (_bytesToSend & 0xff); |
Rhyme | 0:846e2321c637 | 45 | bytes[index++] = ((_bytesToSend >> 8) & 0xff); |
Rhyme | 0:846e2321c637 | 46 | bytes[index++] = (_bytesToRecv & 0xff); |
Rhyme | 0:846e2321c637 | 47 | bytes[index++] = ((_bytesToRecv >> 8) & 0xff); |
Rhyme | 0:846e2321c637 | 48 | |
Rhyme | 0:846e2321c637 | 49 | return index; |
Rhyme | 0:846e2321c637 | 50 | } |
Rhyme | 0:846e2321c637 | 51 | |
Rhyme | 0:846e2321c637 | 52 | uint8_t StatusCommand::calcChecksum() { |
Rhyme | 0:846e2321c637 | 53 | uint8_t result = 0; |
Rhyme | 0:846e2321c637 | 54 | |
Rhyme | 0:846e2321c637 | 55 | result += (_cmd); |
Rhyme | 0:846e2321c637 | 56 | result += (_bytesToSend & 0xff); |
Rhyme | 0:846e2321c637 | 57 | result += ((_bytesToSend >> 8) & 0xff); |
Rhyme | 0:846e2321c637 | 58 | result += (_bytesToRecv & 0xff); |
Rhyme | 0:846e2321c637 | 59 | result += ((_bytesToRecv >> 8) & 0xff); |
Rhyme | 0:846e2321c637 | 60 | |
Rhyme | 0:846e2321c637 | 61 | return result; |
Rhyme | 0:846e2321c637 | 62 | } |
Rhyme | 0:846e2321c637 | 63 | |
Rhyme | 0:846e2321c637 | 64 | void StatusCommand::setChecksum(uint8_t checksum) { |
Rhyme | 0:846e2321c637 | 65 | _checksum = checksum; |
Rhyme | 0:846e2321c637 | 66 | } |
Rhyme | 0:846e2321c637 | 67 | |
Rhyme | 0:846e2321c637 | 68 | uint8_t StatusCommand::getChecksum() { |
Rhyme | 0:846e2321c637 | 69 | uint8_t result = 0; |
Rhyme | 0:846e2321c637 | 70 | |
Rhyme | 0:846e2321c637 | 71 | result += (_cmd); |
Rhyme | 0:846e2321c637 | 72 | result += (_bytesToSend & 0xff); |
Rhyme | 0:846e2321c637 | 73 | result += ((_bytesToSend >> 8) & 0xff); |
Rhyme | 0:846e2321c637 | 74 | result += (_bytesToRecv & 0xff); |
Rhyme | 0:846e2321c637 | 75 | result += ((_bytesToRecv >> 8) & 0xff); |
Rhyme | 0:846e2321c637 | 76 | |
Rhyme | 0:846e2321c637 | 77 | return result; |
Rhyme | 0:846e2321c637 | 78 | } |
Rhyme | 0:846e2321c637 | 79 | |
Rhyme | 0:846e2321c637 | 80 | void StatusCommand::setAck(bool ack) { |
Rhyme | 0:846e2321c637 | 81 | _cmd = ack ? 0x31 : 0x30; |
Rhyme | 0:846e2321c637 | 82 | } |
Rhyme | 0:846e2321c637 | 83 | |
Rhyme | 0:846e2321c637 | 84 | void StatusCommand::setBytesToSend(uint16_t bytesToSend) { |
Rhyme | 0:846e2321c637 | 85 | _bytesToSend = bytesToSend; |
Rhyme | 0:846e2321c637 | 86 | } |
Rhyme | 0:846e2321c637 | 87 | |
Rhyme | 0:846e2321c637 | 88 | uint16_t StatusCommand::getBytesToSend() { |
Rhyme | 0:846e2321c637 | 89 | return _bytesToSend; |
Rhyme | 0:846e2321c637 | 90 | } |
Rhyme | 0:846e2321c637 | 91 | |
Rhyme | 0:846e2321c637 | 92 | void StatusCommand::setBytesToRecv(uint16_t bytesToRecv) { |
Rhyme | 0:846e2321c637 | 93 | _bytesToRecv = bytesToRecv; |
Rhyme | 0:846e2321c637 | 94 | } |
Rhyme | 0:846e2321c637 | 95 | |
Rhyme | 0:846e2321c637 | 96 | uint16_t StatusCommand::getBytesToRecv() { |
Rhyme | 0:846e2321c637 | 97 | return _bytesToRecv; |
Rhyme | 0:846e2321c637 | 98 | } |
Rhyme | 0:846e2321c637 | 99 | |
Rhyme | 0:846e2321c637 | 100 | bool StatusCommand::equals(StatusCommand *statusCommand) { |
Rhyme | 0:846e2321c637 | 101 | return (_cmd == statusCommand->_cmd && _bytesToSend == statusCommand->_bytesToSend && |
Rhyme | 0:846e2321c637 | 102 | _bytesToRecv == statusCommand->_bytesToRecv); |
Rhyme | 0:846e2321c637 | 103 | } |
Rhyme | 0:846e2321c637 | 104 | |
Rhyme | 0:846e2321c637 | 105 | bool StatusCommand::isValid() { |
Rhyme | 0:846e2321c637 | 106 | return (_checksum == calcChecksum()) && (_cmd == 0x30 || _cmd == 0x31); |
Rhyme | 0:846e2321c637 | 107 | } |
Rhyme | 0:846e2321c637 | 108 | |
Rhyme | 0:846e2321c637 | 109 | void StatusCommand::dumpBytes() { |
Rhyme | 0:846e2321c637 | 110 | #if SERIAL_PRINT_DBG_ASR_ON |
Rhyme | 0:846e2321c637 | 111 | int len = getSize(); |
Rhyme | 0:846e2321c637 | 112 | int bytes[len]; |
Rhyme | 0:846e2321c637 | 113 | getBytes(bytes); |
Rhyme | 0:846e2321c637 | 114 | |
Rhyme | 0:846e2321c637 | 115 | printf("len : %d\n",len); |
Rhyme | 0:846e2321c637 | 116 | printf("data : "); |
Rhyme | 0:846e2321c637 | 117 | for (int i = 0; i < len; i++) { |
Rhyme | 0:846e2321c637 | 118 | if (i > 0) { |
Rhyme | 0:846e2321c637 | 119 | printf(", "); |
Rhyme | 0:846e2321c637 | 120 | } |
Rhyme | 0:846e2321c637 | 121 | int b = bytes[i] & 0xff; |
Rhyme | 0:846e2321c637 | 122 | printf("0x%02x", b) ; |
Rhyme | 0:846e2321c637 | 123 | } |
Rhyme | 0:846e2321c637 | 124 | printf("\n") ; |
Rhyme | 0:846e2321c637 | 125 | #endif |
Rhyme | 0:846e2321c637 | 126 | } |
Rhyme | 0:846e2321c637 | 127 | |
Rhyme | 0:846e2321c637 | 128 | void StatusCommand::dump() { |
Rhyme | 0:846e2321c637 | 129 | #if SERIAL_PRINT_DBG_ASR_ON |
Rhyme | 0:846e2321c637 | 130 | printf("cmd : %s\n",_cmd == 0x30 ? "STATUS" : "STATUS_ACK"); |
Rhyme | 0:846e2321c637 | 131 | printf("bytes to send : %d\n",_bytesToSend); |
Rhyme | 0:846e2321c637 | 132 | printf("bytes to receive : %d\n",_bytesToRecv); |
Rhyme | 0:846e2321c637 | 133 | #endif |
Rhyme | 0:846e2321c637 | 134 | } |
Rhyme | 0:846e2321c637 | 135 |