an old afLib which supports both SPI and UART
Embed:
(wiki syntax)
Show/hide line numbers
StatusCommand.cpp
00001 /** 00002 * Copyright 2015 Afero, Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "StatusCommand.h" 00019 00020 StatusCommand::StatusCommand(Stream *serial,uint16_t bytesToSend) { 00021 _serial = serial; 00022 _cmd = 0x30; 00023 _bytesToSend = bytesToSend; 00024 _bytesToRecv = 0; 00025 } 00026 00027 StatusCommand::StatusCommand(Stream *serial) { 00028 _serial = serial; 00029 _cmd = 0x30; 00030 _bytesToSend = 0; 00031 _bytesToRecv = 0; 00032 } 00033 00034 StatusCommand::~StatusCommand() { 00035 } 00036 00037 uint16_t StatusCommand::getSize() { 00038 return sizeof(_cmd) + sizeof(_bytesToSend) + sizeof(_bytesToRecv); 00039 } 00040 00041 uint16_t StatusCommand::getBytes(int *bytes) { 00042 int index = 0; 00043 00044 bytes[index++] = (_cmd); 00045 bytes[index++] = (_bytesToSend & 0xff); 00046 bytes[index++] = ((_bytesToSend >> 8) & 0xff); 00047 bytes[index++] = (_bytesToRecv & 0xff); 00048 bytes[index++] = ((_bytesToRecv >> 8) & 0xff); 00049 00050 return index; 00051 } 00052 00053 uint8_t StatusCommand::calcChecksum() { 00054 uint8_t result = 0; 00055 00056 result += (_cmd); 00057 result += (_bytesToSend & 0xff); 00058 result += ((_bytesToSend >> 8) & 0xff); 00059 result += (_bytesToRecv & 0xff); 00060 result += ((_bytesToRecv >> 8) & 0xff); 00061 00062 return result; 00063 } 00064 00065 void StatusCommand::setChecksum(uint8_t checksum) { 00066 _checksum = checksum; 00067 } 00068 00069 uint8_t StatusCommand::getChecksum() { 00070 uint8_t result = 0; 00071 00072 result += (_cmd); 00073 result += (_bytesToSend & 0xff); 00074 result += ((_bytesToSend >> 8) & 0xff); 00075 result += (_bytesToRecv & 0xff); 00076 result += ((_bytesToRecv >> 8) & 0xff); 00077 00078 return result; 00079 } 00080 00081 void StatusCommand::setAck(bool ack) { 00082 _cmd = ack ? 0x31 : 0x30; 00083 } 00084 00085 void StatusCommand::setBytesToSend(uint16_t bytesToSend) { 00086 _bytesToSend = bytesToSend; 00087 } 00088 00089 uint16_t StatusCommand::getBytesToSend() { 00090 return _bytesToSend; 00091 } 00092 00093 void StatusCommand::setBytesToRecv(uint16_t bytesToRecv) { 00094 _bytesToRecv = bytesToRecv; 00095 } 00096 00097 uint16_t StatusCommand::getBytesToRecv() { 00098 return _bytesToRecv; 00099 } 00100 00101 bool StatusCommand::equals(StatusCommand *statusCommand) { 00102 return (_cmd == statusCommand->_cmd && _bytesToSend == statusCommand->_bytesToSend && 00103 _bytesToRecv == statusCommand->_bytesToRecv); 00104 } 00105 00106 bool StatusCommand::isValid() { 00107 return (_checksum == calcChecksum()) && (_cmd == 0x30 || _cmd == 0x31); 00108 } 00109 00110 void StatusCommand::dumpBytes() { 00111 int len = getSize(); 00112 int bytes[len]; 00113 getBytes(bytes); 00114 00115 _serial->printf("len : %d\n", len); 00116 _serial->printf("data : "); 00117 for (int i = 0; i < len; i++) { 00118 if (i > 0) { 00119 _serial->printf(", "); 00120 } 00121 int b = bytes[i] & 0xff; 00122 _serial->printf("0x%02X", b) ; 00123 } 00124 _serial->printf("\n"); 00125 } 00126 00127 void StatusCommand::dump() { 00128 _serial->printf("cmd : %s\n", _cmd == 0x30 ? "STATUS" : "STATUS_ACK"); 00129 _serial->printf("bytes to send : %d\n", _bytesToSend); 00130 _serial->printf("bytes to receive : %d\n", _bytesToRecv); 00131 } 00132 00133
Generated on Wed Jul 13 2022 19:00:06 by
1.7.2