main changed for arduino nano debug

This commit is contained in:
Lukas Loidold 2020-04-21 18:38:38 +02:00
parent 2c1925d18f
commit 866944fa26

View File

@ -16,6 +16,7 @@
#define RING_BUFFER_SIZE 100
#define MAX_PACKET_LENGTH 100
#define SERIAL_RX_BUFFER_SIZE 256
static const uint8_t COMMAND_TRANSFER_SPI = 1;
@ -60,8 +61,11 @@ void handlePacket(uint8_t *packet, size_t packetLen) {
// 8 bit command | 8 bit address | 16bit length | <length> byte data | 16 bit crc
uint16_t crc = Calculate_CRC(packet, packetLen);
if (crc != 0) {
Serial.println("invalid Checksum");
return;
@ -77,11 +81,18 @@ void handlePacket(uint8_t *packet, size_t packetLen) {
uint8_t command = packet[0];
uint8_t address = packet[1];
switch (command) {
case COMMAND_TRANSFER_SPI:
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()
//check reply:
Serial.println("Data response check: ");
for(size_t i =0; i< packetLen; i++){
Serial.print("packet nr ");Serial.print(i);Serial.print(" ");Serial.println(packet[i]);
}
sendData(packet, packetLen);
break;
default:
@ -102,11 +113,13 @@ void handleNewData() {
size_t firstSTXinRawData = 0;
while ((firstSTXinRawData < rawDataSize)
&& (rawData[firstSTXinRawData] != DleEncoder::STX)) {
Serial.println(rawData[firstSTXinRawData]);
firstSTXinRawData++;
}
if (rawData[firstSTXinRawData] != DleEncoder::STX) {
//there is no STX in our data, throw it away...
Serial.println("NO STX");
ringBuffer.deleteData(rawDataSize);
return;
}
@ -136,7 +149,9 @@ void handleNewData() {
//TODO check if this is thread safe by arduino
void serialEvent() {
//Serial.println(ringBuffer.availableWriteSpace());
while (Serial.available()) {
uint8_t i = 0;
while (Serial.available()>0) {
uint8_t byte = Serial.read();
ringBuffer.writeData(&byte, 1);
}
@ -151,6 +166,7 @@ void setup() {
}
void loop() {
;
handleNewData();
delay(100);
delay(1000);
}