Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
StatusCommand.cpp@0:6f371c791202, 2018-03-20 (annotated)
- Committer:
- Rhyme
- Date:
- Tue Mar 20 06:47:25 2018 +0000
- Revision:
- 0:6f371c791202
SPI mode started working
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Rhyme | 0:6f371c791202 | 1 | /** |
| Rhyme | 0:6f371c791202 | 2 | * Copyright 2015 Afero, Inc. |
| Rhyme | 0:6f371c791202 | 3 | * |
| Rhyme | 0:6f371c791202 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| Rhyme | 0:6f371c791202 | 5 | * you may not use this file except in compliance with the License. |
| Rhyme | 0:6f371c791202 | 6 | * You may obtain a copy of the License at |
| Rhyme | 0:6f371c791202 | 7 | * |
| Rhyme | 0:6f371c791202 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| Rhyme | 0:6f371c791202 | 9 | * |
| Rhyme | 0:6f371c791202 | 10 | * Unless required by applicable law or agreed to in writing, software |
| Rhyme | 0:6f371c791202 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| Rhyme | 0:6f371c791202 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| Rhyme | 0:6f371c791202 | 13 | * See the License for the specific language governing permissions and |
| Rhyme | 0:6f371c791202 | 14 | * limitations under the License. |
| Rhyme | 0:6f371c791202 | 15 | */ |
| Rhyme | 0:6f371c791202 | 16 | |
| Rhyme | 0:6f371c791202 | 17 | #include "mbed.h" |
| Rhyme | 0:6f371c791202 | 18 | #include "StatusCommand.h" |
| Rhyme | 0:6f371c791202 | 19 | |
| Rhyme | 0:6f371c791202 | 20 | StatusCommand::StatusCommand(Stream *serial,uint16_t bytesToSend) { |
| Rhyme | 0:6f371c791202 | 21 | _serial = serial; |
| Rhyme | 0:6f371c791202 | 22 | _cmd = 0x30; |
| Rhyme | 0:6f371c791202 | 23 | _bytesToSend = bytesToSend; |
| Rhyme | 0:6f371c791202 | 24 | _bytesToRecv = 0; |
| Rhyme | 0:6f371c791202 | 25 | } |
| Rhyme | 0:6f371c791202 | 26 | |
| Rhyme | 0:6f371c791202 | 27 | StatusCommand::StatusCommand(Stream *serial) { |
| Rhyme | 0:6f371c791202 | 28 | _serial = serial; |
| Rhyme | 0:6f371c791202 | 29 | _cmd = 0x30; |
| Rhyme | 0:6f371c791202 | 30 | _bytesToSend = 0; |
| Rhyme | 0:6f371c791202 | 31 | _bytesToRecv = 0; |
| Rhyme | 0:6f371c791202 | 32 | } |
| Rhyme | 0:6f371c791202 | 33 | |
| Rhyme | 0:6f371c791202 | 34 | StatusCommand::~StatusCommand() { |
| Rhyme | 0:6f371c791202 | 35 | } |
| Rhyme | 0:6f371c791202 | 36 | |
| Rhyme | 0:6f371c791202 | 37 | uint16_t StatusCommand::getSize() { |
| Rhyme | 0:6f371c791202 | 38 | return sizeof(_cmd) + sizeof(_bytesToSend) + sizeof(_bytesToRecv); |
| Rhyme | 0:6f371c791202 | 39 | } |
| Rhyme | 0:6f371c791202 | 40 | |
| Rhyme | 0:6f371c791202 | 41 | uint16_t StatusCommand::getBytes(int *bytes) { |
| Rhyme | 0:6f371c791202 | 42 | int index = 0; |
| Rhyme | 0:6f371c791202 | 43 | |
| Rhyme | 0:6f371c791202 | 44 | bytes[index++] = (_cmd); |
| Rhyme | 0:6f371c791202 | 45 | bytes[index++] = (_bytesToSend & 0xff); |
| Rhyme | 0:6f371c791202 | 46 | bytes[index++] = ((_bytesToSend >> 8) & 0xff); |
| Rhyme | 0:6f371c791202 | 47 | bytes[index++] = (_bytesToRecv & 0xff); |
| Rhyme | 0:6f371c791202 | 48 | bytes[index++] = ((_bytesToRecv >> 8) & 0xff); |
| Rhyme | 0:6f371c791202 | 49 | |
| Rhyme | 0:6f371c791202 | 50 | return index; |
| Rhyme | 0:6f371c791202 | 51 | } |
| Rhyme | 0:6f371c791202 | 52 | |
| Rhyme | 0:6f371c791202 | 53 | uint8_t StatusCommand::calcChecksum() { |
| Rhyme | 0:6f371c791202 | 54 | uint8_t result = 0; |
| Rhyme | 0:6f371c791202 | 55 | |
| Rhyme | 0:6f371c791202 | 56 | result += (_cmd); |
| Rhyme | 0:6f371c791202 | 57 | result += (_bytesToSend & 0xff); |
| Rhyme | 0:6f371c791202 | 58 | result += ((_bytesToSend >> 8) & 0xff); |
| Rhyme | 0:6f371c791202 | 59 | result += (_bytesToRecv & 0xff); |
| Rhyme | 0:6f371c791202 | 60 | result += ((_bytesToRecv >> 8) & 0xff); |
| Rhyme | 0:6f371c791202 | 61 | |
| Rhyme | 0:6f371c791202 | 62 | return result; |
| Rhyme | 0:6f371c791202 | 63 | } |
| Rhyme | 0:6f371c791202 | 64 | |
| Rhyme | 0:6f371c791202 | 65 | void StatusCommand::setChecksum(uint8_t checksum) { |
| Rhyme | 0:6f371c791202 | 66 | _checksum = checksum; |
| Rhyme | 0:6f371c791202 | 67 | } |
| Rhyme | 0:6f371c791202 | 68 | |
| Rhyme | 0:6f371c791202 | 69 | uint8_t StatusCommand::getChecksum() { |
| Rhyme | 0:6f371c791202 | 70 | uint8_t result = 0; |
| Rhyme | 0:6f371c791202 | 71 | |
| Rhyme | 0:6f371c791202 | 72 | result += (_cmd); |
| Rhyme | 0:6f371c791202 | 73 | result += (_bytesToSend & 0xff); |
| Rhyme | 0:6f371c791202 | 74 | result += ((_bytesToSend >> 8) & 0xff); |
| Rhyme | 0:6f371c791202 | 75 | result += (_bytesToRecv & 0xff); |
| Rhyme | 0:6f371c791202 | 76 | result += ((_bytesToRecv >> 8) & 0xff); |
| Rhyme | 0:6f371c791202 | 77 | |
| Rhyme | 0:6f371c791202 | 78 | return result; |
| Rhyme | 0:6f371c791202 | 79 | } |
| Rhyme | 0:6f371c791202 | 80 | |
| Rhyme | 0:6f371c791202 | 81 | void StatusCommand::setAck(bool ack) { |
| Rhyme | 0:6f371c791202 | 82 | _cmd = ack ? 0x31 : 0x30; |
| Rhyme | 0:6f371c791202 | 83 | } |
| Rhyme | 0:6f371c791202 | 84 | |
| Rhyme | 0:6f371c791202 | 85 | void StatusCommand::setBytesToSend(uint16_t bytesToSend) { |
| Rhyme | 0:6f371c791202 | 86 | _bytesToSend = bytesToSend; |
| Rhyme | 0:6f371c791202 | 87 | } |
| Rhyme | 0:6f371c791202 | 88 | |
| Rhyme | 0:6f371c791202 | 89 | uint16_t StatusCommand::getBytesToSend() { |
| Rhyme | 0:6f371c791202 | 90 | return _bytesToSend; |
| Rhyme | 0:6f371c791202 | 91 | } |
| Rhyme | 0:6f371c791202 | 92 | |
| Rhyme | 0:6f371c791202 | 93 | void StatusCommand::setBytesToRecv(uint16_t bytesToRecv) { |
| Rhyme | 0:6f371c791202 | 94 | _bytesToRecv = bytesToRecv; |
| Rhyme | 0:6f371c791202 | 95 | } |
| Rhyme | 0:6f371c791202 | 96 | |
| Rhyme | 0:6f371c791202 | 97 | uint16_t StatusCommand::getBytesToRecv() { |
| Rhyme | 0:6f371c791202 | 98 | return _bytesToRecv; |
| Rhyme | 0:6f371c791202 | 99 | } |
| Rhyme | 0:6f371c791202 | 100 | |
| Rhyme | 0:6f371c791202 | 101 | bool StatusCommand::equals(StatusCommand *statusCommand) { |
| Rhyme | 0:6f371c791202 | 102 | return (_cmd == statusCommand->_cmd && _bytesToSend == statusCommand->_bytesToSend && |
| Rhyme | 0:6f371c791202 | 103 | _bytesToRecv == statusCommand->_bytesToRecv); |
| Rhyme | 0:6f371c791202 | 104 | } |
| Rhyme | 0:6f371c791202 | 105 | |
| Rhyme | 0:6f371c791202 | 106 | bool StatusCommand::isValid() { |
| Rhyme | 0:6f371c791202 | 107 | return (_checksum == calcChecksum()) && (_cmd == 0x30 || _cmd == 0x31); |
| Rhyme | 0:6f371c791202 | 108 | } |
| Rhyme | 0:6f371c791202 | 109 | |
| Rhyme | 0:6f371c791202 | 110 | void StatusCommand::dumpBytes() { |
| Rhyme | 0:6f371c791202 | 111 | int len = getSize(); |
| Rhyme | 0:6f371c791202 | 112 | int bytes[len]; |
| Rhyme | 0:6f371c791202 | 113 | getBytes(bytes); |
| Rhyme | 0:6f371c791202 | 114 | |
| Rhyme | 0:6f371c791202 | 115 | _serial->printf("len : %d\n", len); |
| Rhyme | 0:6f371c791202 | 116 | _serial->printf("data : "); |
| Rhyme | 0:6f371c791202 | 117 | for (int i = 0; i < len; i++) { |
| Rhyme | 0:6f371c791202 | 118 | if (i > 0) { |
| Rhyme | 0:6f371c791202 | 119 | _serial->printf(", "); |
| Rhyme | 0:6f371c791202 | 120 | } |
| Rhyme | 0:6f371c791202 | 121 | int b = bytes[i] & 0xff; |
| Rhyme | 0:6f371c791202 | 122 | _serial->printf("0x%02X", b) ; |
| Rhyme | 0:6f371c791202 | 123 | } |
| Rhyme | 0:6f371c791202 | 124 | _serial->printf("\n"); |
| Rhyme | 0:6f371c791202 | 125 | } |
| Rhyme | 0:6f371c791202 | 126 | |
| Rhyme | 0:6f371c791202 | 127 | void StatusCommand::dump() { |
| Rhyme | 0:6f371c791202 | 128 | _serial->printf("cmd : %s\n", _cmd == 0x30 ? "STATUS" : "STATUS_ACK"); |
| Rhyme | 0:6f371c791202 | 129 | _serial->printf("bytes to send : %d\n", _bytesToSend); |
| Rhyme | 0:6f371c791202 | 130 | _serial->printf("bytes to receive : %d\n", _bytesToRecv); |
| Rhyme | 0:6f371c791202 | 131 | } |
| Rhyme | 0:6f371c791202 | 132 | |
| Rhyme | 0:6f371c791202 | 133 |