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