cosmetics

This commit is contained in:
Ulrich Mohr 2020-04-15 17:50:30 +02:00
parent 93a02c8db2
commit ffc7e7eb7a

View File

@ -4,6 +4,8 @@
#include "helper/DleEncoder.h"
#include "helper/crc_ccitt.h"
#include <avr/io.h>
//SPI is formally a library, so it is not part of the objects compiled
//from the core and we need to include it explicitly
#include "arduino_core/ArduinoCore-avr/libraries/SPI/src/SPI.h"
@ -27,16 +29,17 @@ size_t rawDataSize = 0;
* and will be overwritten with it
*/
void sendData(uint8_t *data, size_t len) {
uint16_t crc = Calculate_CRC(data,len-2);
data[len-2] = crc >> 8;
data[len-1] = crc;
uint16_t crc = Calculate_CRC(data, len - 2);
data[len - 2] = crc >> 8;
data[len - 1] = crc;
//we're being conservative here
//TODO move this to global so other protocols can use it too
uint8_t buffer[2*len +2];
uint8_t buffer[2 * len + 2];
buffer[0] = DleEncoder::STX;
size_t writtenLen;
ReturnValue_t result = DleEncoder::encode(data,len,buffer,sizeof(buffer),&writtenLen, true);
if (result != HasReturnvaluesIF::RETURN_OK){
ReturnValue_t result = DleEncoder::encode(data, len, buffer, sizeof(buffer),
&writtenLen, true);
if (result != HasReturnvaluesIF::RETURN_OK) {
return;
}
Serial.write(buffer, writtenLen);
@ -45,7 +48,7 @@ void sendData(uint8_t *data, size_t len) {
void transferSPI(uint8_t address, uint8_t *data, size_t datalen) {
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
CS_PORT = ~address;
SPI.transfer(data,datalen);
SPI.transfer(data, datalen);
delay(100);
CS_PORT = 0xff;
SPI.endTransaction();
@ -63,10 +66,9 @@ void handlePacket(uint8_t *packet, size_t packetLen) {
return;
}
uint16_t payloadLen = (packet[2] << 8) | packet[3];
if (payloadLen + 6 != packetLen) {
if (payloadLen != packetLen - 6) {
//Serial.println("invalid len");
return;
}
@ -79,7 +81,7 @@ void handlePacket(uint8_t *packet, size_t packetLen) {
transferSPI(address, packet + 4, payloadLen);
//echo the data back, no need to change the header fields, they are the same
//checksum will be written by sendData()
sendData(packet,packetLen);
sendData(packet, packetLen);
break;
default:
//Serial.println("invalid command");
@ -104,7 +106,6 @@ void handleNewData() {
if (rawData[firstSTXinRawData] != DleEncoder::STX) {
//there is no STX in our data, throw it away...
Serial.println(firstSTXinRawData);
ringBuffer.deleteData(rawDataSize);
return;
}
@ -144,7 +145,6 @@ void setup() {
CS_PORT = 0xff;
Serial.begin(9600);
SPI.begin();
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {