Skip to content

Commit 8e5fe6a

Browse files
committed
Bugfix.
1 parent e23aeac commit 8e5fe6a

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

src/FPS_GT511C3.cpp

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -222,23 +222,23 @@ Data_Packet::Data_Packet(byte* buffer, bool UseSerialDebug)
222222
CheckParsing(buffer[2], DATA_DEVICE_ID_1, DATA_DEVICE_ID_1, "DATA_DEVICE_ID_1", UseSerialDebug);
223223
CheckParsing(buffer[3], DATA_DEVICE_ID_2, DATA_DEVICE_ID_2, "DATA_DEVICE_ID_2", UseSerialDebug);
224224

225-
Data_Packet.checksum = CalculateChecksum(buffer, 4);
225+
this->checksum = CalculateChecksum(buffer, 4);
226226
}
227227

228228
// Get a data packet (128 bytes), calculate checksum and send it to serial
229-
Data_Packet::GetData(byte* buffer, bool UseSerialDebug)
229+
void Data_Packet::GetData(byte* buffer, bool UseSerialDebug)
230230
{
231-
FPS_GT511C3.SendToSerial(buffer, 128);
232-
Data_Packet.checksum = CalculateChecksum(buffer, 128);
231+
SendToSerial(buffer, 128);
232+
this->checksum = CalculateChecksum(buffer, 128);
233233
}
234234

235235
// Get the last data packet (<=128 bytes), calculate checksum, validate checksum received and send it to serial
236-
Data_Packet::GetLastData(byte* buffer, int length, bool UseSerialDebug)
236+
void Data_Packet::GetLastData(byte* buffer, int length, bool UseSerialDebug)
237237
{
238-
FPS_GT511C3.SendToSerial(buffer, length-2);
239-
Data_Packet.checksum = CalculateChecksum(buffer, length);
240-
byte checksum_low = GetLowByte(Data_Packet.checksum);
241-
byte checksum_high = GetHighByte(Data_Packet.checksum);
238+
SendToSerial(buffer, length-2);
239+
this->checksum = CalculateChecksum(buffer, length);
240+
byte checksum_low = GetLowByte(this->checksum);
241+
byte checksum_high = GetHighByte(this->checksum);
242242
CheckParsing(buffer[length-2], checksum_low, checksum_low, "Checksum_LOW", UseSerialDebug);
243243
CheckParsing(buffer[length-1], checksum_high, checksum_high, "Checksum_HIGH", UseSerialDebug);
244244
}
@@ -264,18 +264,7 @@ bool Data_Packet::CheckParsing(byte b, byte propervalue, byte alternatevalue, co
264264
// calculates the checksum from the bytes in the packet
265265
word Data_Packet::CalculateChecksum(byte* buffer, int length)
266266
{
267-
word checksum = Data_Packet.checksum;
268-
for (int i=0; i<length; i++)
269-
{
270-
checksum +=buffer[i];
271-
}
272-
return checksum;
273-
}
274-
275-
// Returns the high byte from a word// calculates the checksum from the bytes in the packet
276-
word Data_Packet::CalculateChecksum(byte* buffer, int length)
277-
{
278-
word checksum = 0;
267+
word checksum = this->checksum;
279268
for (int i=0; i<length; i++)
280269
{
281270
checksum +=buffer[i];
@@ -294,6 +283,27 @@ byte Data_Packet::GetLowByte(word w)
294283
{
295284
return (byte)w&0x00FF;
296285
}
286+
287+
// sends a byte to the serial debugger in the hex format we want EX "0F"
288+
void Data_Packet::serialPrintHex(byte data)
289+
{
290+
char tmp[16];
291+
sprintf(tmp, "%.2X",data);
292+
Serial.print(tmp);
293+
}
294+
295+
// sends the bye aray to the serial debugger in our hex format EX: "00 AF FF 10 00 13"
296+
void Data_Packet::SendToSerial(byte data[], int length)
297+
{
298+
boolean first=true;
299+
Serial.print("\"");
300+
for(int i=0; i<length; i++)
301+
{
302+
if (first) first=false; else Serial.print(" ");
303+
serialPrintHex(data[i]);
304+
}
305+
Serial.print("\"");
306+
}
297307
#ifndef __GNUC__
298308
#pragma endregion
299309
#endif //__GNUC__
@@ -896,10 +906,10 @@ void FPS_GT511C3::GetData(int length)
896906
while (done == false)
897907
{
898908
firstbyte = (byte)_serial.read();
899-
if (firstbyte == Data_Packet()::DATA_START_CODE_1)
909+
if (firstbyte == Data_Packet::DATA_START_CODE_1)
900910
{
901911
secondbyte = (byte)_serial.read();
902-
if (secondbyte == Data_Packet()::DATA_START_CODE_2)
912+
if (secondbyte == Data_Packet::DATA_START_CODE_2)
903913
{
904914
done = true;
905915
}
@@ -924,7 +934,7 @@ void FPS_GT511C3::GetData(int length)
924934
}
925935
delete firstdata;
926936

927-
numberPacketsNeeded = (length-4) / 128;
937+
int numberPacketsNeeded = (length-4) / 128;
928938
bool smallLastPacket = false;
929939
int lastPacketSize = (length-4) % 128;
930940
if(lastPacketSize != 0)
@@ -941,7 +951,7 @@ void FPS_GT511C3::GetData(int length)
941951
while (_serial.available() == false) delay(10);
942952
data[i]= (byte) _serial.read();
943953
}
944-
dp.GetData(data, UseSerialDebug);
954+
dp->GetData(data, UseSerialDebug);
945955
if(UseSerialDebug)
946956
{
947957
Serial.print("FPS - RECV: ");
@@ -958,7 +968,7 @@ void FPS_GT511C3::GetData(int length)
958968
while (_serial.available() == false) delay(10);
959969
lastdata[i]= (byte) _serial.read();
960970
}
961-
dp.GetLastData(lastdata, lastPacketSize, UseSerialDebug);
971+
dp->GetLastData(lastdata, lastPacketSize, UseSerialDebug);
962972
if(UseSerialDebug)
963973
{
964974
Serial.print("FPS - RECV: ");

src/FPS_GT511C3.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,16 @@ class Data_Packet
156156
static const byte DATA_START_CODE_2 = 0xA5; // Static byte to mark the beginning of a data packet - never changes
157157
static const byte DATA_DEVICE_ID_1 = 0x01; // Device ID Byte 1 (lesser byte) - theoretically never changes
158158
static const byte DATA_DEVICE_ID_2 = 0x00; // Device ID Byte 2 (greater byte)
159-
private:
160-
void GetData(byte* buffer, bool UseSerialDebug);
159+
160+
void GetData(byte* buffer, bool UseSerialDebug);
161161
void GetLastData(byte* buffer, int length, bool UseSerialDebug);
162+
private:
162163
bool CheckParsing(byte b, byte propervalue, byte alternatevalue, const char* varname, bool UseSerialDebug);
163164
word CalculateChecksum(byte* buffer, int length);
164165
byte GetHighByte(word w);
165166
byte GetLowByte(word w);
167+
void serialPrintHex(byte data);
168+
void SendToSerial(byte data[], int length);
166169
};
167170
#ifndef __GNUC__
168171
#pragma endregion

0 commit comments

Comments
 (0)