Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Railstars committed Dec 18, 2010
0 parents commit 96f3ef8
Show file tree
Hide file tree
Showing 8 changed files with 782 additions and 0 deletions.
60 changes: 60 additions & 0 deletions DCCPacket.cpp
@@ -0,0 +1,60 @@
#include "DCCPacket.h"


DCCPacket::DCCPacket(unsigned int decoder_address) : address(decoder_address), size(1), kind(idle_packet_kind), repeat(0)
{
data[0] = 0xFF; //is this really the idle packet? TODO
}

byte DCCPacket::getBitstream(byte rawbytes[]) //returns size of array.
{
int total_size = 1; //minimum size
//Serial.print("Address: ");
if(address < 127) //TODO IS THIS RIGHT?
{
rawbytes[0] = (byte)address;
//Serial.println(rawbytes[0],BIN);
}
else //ELSE WHAT?
{
rawbytes[0] = (byte)address >> 8;
rawbytes[1] = (byte)(address & 0xFF);
//Serial.print(rawbytes[0],BIN);
//Serial.print(" ");
//Serial.println(rawbytes[1],BIN);
++total_size;
}
int i;
//Serial.print("Payload:");
for(i = 0; i < size; ++i,++total_size)
{
rawbytes[total_size] = data[i];
//Serial.print(" ");
//Serial.print(rawbytes[total_size],BIN);
}
//Serial.println("");
//Serial.print("XOR: ");
//Serial.print(total_size);
//Serial.print(" ");
byte XOR = 0;
for(i = 0; i < total_size; ++i)
{
XOR ^= rawbytes[i];
}
rawbytes[total_size] = XOR;
//Serial.println(rawbytes[total_size],BIN);

return total_size+1;
}

byte DCCPacket::getSize(void)
{
return size;
}

byte DCCPacket::addData(byte data[], byte size) //insert freeform data.
{
for(int i = 0; i < size; ++i)
data[i] = data[i];
size = size;
}
42 changes: 42 additions & 0 deletions DCCPacket.h
@@ -0,0 +1,42 @@
#ifndef __DCCPACKET_H__
#define __DCCPACKET_H__

#include "WProgram.h"

//Packet kinds
enum packet_kind_t {
idle_packet_kind,
e_stop_packet_kind,
speed_packet_kind,
function_packet_kind,
accessory_packet_kind,
reset_packet_kind,
other_packe_kind
};

class DCCPacket
{
//A DCC packet is at most 6 bytes: 2 of address, three of data, one of XOR
unsigned int address;// = 0;
byte data[3];// = {0,0,0};
byte size;// = 3; //of data only
// byte XOR;// = 0;
byte repeat;
packet_kind_t kind;

// void computeXOR(void);
public:
DCCPacket(unsigned int address=0);

byte getBitstream(byte rawbytes[]); //returns size of array.
byte getSize(void);
inline unsigned int getAddress(void) { return address; }
inline void setAddress(unsigned int new_address) { address = new_address; }
byte addData(byte data[], byte size); //insert freeform data.
inline void setKind(packet_kind_t new_kind) { kind = new_kind; }
inline packet_kind_t getKind(void) { return kind; }
inline void setRepeat(byte new_repeat) { repeat = new_repeat; }
inline byte getRepeat(void) { return repeat; }
};

#endif //__DCCPACKET_H__

0 comments on commit 96f3ef8

Please sign in to comment.