In an array byte, according to Packet[1] i want to:
if ((Packet[1] | 0x7F) == 0x7F)
{for (byte x = 2; x <= y; x++)
{ Packet[x]; Functions(); } //Show me packets from Packet[2] to y
}
else
if((Packet[1] & 0xC0) == 0xC0)
{for (byte x = 3; x <= y; x++)
{ Packet[x]; Functions(); } //Show me packets from Packet[3] to y
}
I have borrow this code. The original works fine, if you want to read the packets.
I am trying to partially extract some infos from the code
Here is the main code :
byte dccPacket[6]; // buffer to hold a packet - shared with A_dccGetPacket.ino
word Address;
word CV = 210;
void setup() {
Serial.begin(115200);
Serial.println("Starting ReadDccPackets.ino");
pinMode(3, OUTPUT); //Function 1
pinMode(4, OUTPUT); //Function 1
pinMode(5, OUTPUT); //Function 1
pinMode(6, OUTPUT); //Function 1
pinMode(7, OUTPUT); //Function 1
pinMode(8, OUTPUT); //Function 1
//PWM :pin 9
//CS: pin 10
//MOSI: pin 11
//MISO: pin 12
//SCK: pin 13
//Reverse : pin A0
//Forward : pin A1
pinMode(A2, OUTPUT); //Function 1
pinMode(A3, OUTPUT); //Function 1
pinMode(A4, OUTPUT); //Function 1
//BEMF : pin A5
beginBitDetection();
}
//====================
void loop() {
getPacket();
showValidPacket();
}
//=====================
void showValidPacket()
{ // this only shows packets with valid check sums
byte pktByteCount = dccPacket[0];
if (pktByteCount == 0) {
return;
}
byte checksum = 0;
for (byte n = 1; n <= pktByteCount; n++) { //Starts from dccPacket[1] and increment till the No of the packets, pktByteCount
checksum ^= dccPacket[n]; //checksum (error detection byte) is the XOR of the dccPackets[1 - n]
}
if (checksum) {
// Serial.println("Invalid Checksum");
return;
}
else
{
//--------------------------Get Address------------------------------------------
if ((dccPacket[1] | 0x7F) == 0x7F) //Bitwise OR to read the Most Significant bit (first left) and if it is 0
{ Address = dccPacket[1];} //then it is Short Address - Baseline CV1
else //else
{ if(((dccPacket[1] & 0xC0) == 0xC0)&&(dccPacket[1]!=255))//Bitwise AND to read if the first two bits (first left) are 11, and the remaining not 111111
{ Address = word(dccPacket[1]-192,dccPacket[2]); } //then it is Extended Address CV17 - CV18
} //εδω μάλλον λειπει η εντολή που θα μας στείλει πάλι στην αρχή. Να δοκιμάσω και το switch - case.
//---------------------------End Address------------------------------------------
if (Address == CV)
//--------------------------Get Data and Functions--------------------------------
{
if ((dccPacket[1] | 0x7F) == 0x7F) //Get Data Byte from dccPacket[2] to dccPacket[n-1]
{for (byte n = 2; n < pktByteCount; n++)
{ Functions(dccPacket[n]); }
}
else
if(((dccPacket[1] & 0xC0) == 0xC0)&&(dccPacket[1]!=255)) //Get Data Byte from dccPacket[3] to dccPacket[n-1]
{for (byte n = 3; n < pktByteCount; n++)
{ Functions(dccPacket[n]); }
}
}
}
}
void Functions(byte n)
{
{Serial.print(dccPacket[n], HEX);
Serial.print(" ");}
Serial.println("=End=");
}
//=====================
Unfortunately i can't make it to work correct.
From the last part -----Get Data and Functions------- i want to read only the data bytes.
If the dccPacket[1] starts from 0 then data bytes are from dccPacket[2] to n-1,
If the dccPacket[1] starts from 11 then data bytes are from dccPacket[3] to n-1,
if ((dccPacket[1] | 0x7F) == 0x7F) //Get Data Byte from dccPacket[2] to dccPacket[n-1]
{for (byte n = 2; n < pktByteCount; n++)
{ Functions(dccPacket[n]); }
Serial.println("=End=");
}
else
if(((dccPacket[1] & 0xC0) == 0xC0)&&(dccPacket[1]!=255)) //Get Data Byte from dccPacket[3] to dccPacket[n-1]
{for (byte n = 3; n < pktByteCount; n++)
{ Functions(dccPacket[n]); }
Serial.println("=End=");
}