DCCpp
This is the library version of a program for Arduino to control railroading DCC devices.
PacketRegister.h
1 /**********************************************************************
2 
3 PacketRegister.h
4 COPYRIGHT (c) 2013-2016 Gregg E. Berman
5 
6 Part of DCC++ BASE STATION for the Arduino
7 
8 **********************************************************************/
9 
10 #ifndef PacketRegister_h
11 #define PacketRegister_h
12 
13 #include "Arduino.h"
14 
15 // Define constants used for reading CVs from the Programming Track
16 
17 #define ACK_BASE_COUNT 100 // number of analogRead samples to take before each CV verify to establish a baseline current
18 #define ACK_SAMPLE_COUNT 500 // number of analogRead samples to take when monitoring current after a CV verify (bit or byte) has been sent
19 #define ACK_SAMPLE_SMOOTHING 0.2 // exponential smoothing to use in processing the analogRead samples after a CV verify (bit or byte) has been sent
20 #define ACK_SAMPLE_THRESHOLD 30 // the threshold that the exponentially-smoothed analogRead samples (after subtracting the baseline current) must cross to establish ACKNOWLEDGEMENT
21 
22 // Define a series of registers that can be sequentially accessed over a loop to generate a repeating series of DCC Packets
23 
24 struct Packet{
25  byte buf[10];
26  byte nBits;
27 }; // Packet
28 
29 struct Register{
30  Packet packet[2];
31  Packet *activePacket;
32  Packet *updatePacket;
33  void initPackets();
34 }; // Register
35 
36 struct RegisterList{
37  int maxNumRegs;
38  Register *reg;
39  Register **regMap;
40  Register *currentReg;
41  Register *maxLoadedReg;
42  Register *nextReg;
43  Packet *tempPacket;
44  byte currentBit;
45  byte nRepeat;
46  int *speedTable;
47  static byte idlePacket[];
48  static byte resetPacket[];
49  static byte bitMask[];
50  RegisterList(int);
51  void loadPacket(int, byte *, int, int, int=0) volatile;
52 
53  void setThrottle(char *) volatile;
54  void setFunction(char *) volatile;
55  void setAccessory(char *) volatile;
56  void writeTextPacket(char *) volatile;
57 
58  int readCVraw(int cv, int callBack, int callBackSub, bool FromProg) volatile;
59 
60  void readCV(char *) volatile;
61  void writeCVByte(char *) volatile;
62  void writeCVBit(char *) volatile;
63 
64  int readCVmain(char *) volatile;
65  void writeCVByteMain(char *) volatile;
66  void writeCVBitMain(char *s) volatile;
67 
68  void setThrottle(int nReg, int cab, int tSpeed, int tDirection) volatile;
69  void setFunction(int nReg, int cab, int fByte, int eByte) volatile;
70  void setAccessory(int aAdd, int aNum, int activate) volatile;
71  void writeTextPacket(int nReg, byte *b, int nBytes) volatile;
72  void readCV(int cv, int callBack, int callBackSub) volatile;
73  int readCVmain(int cv, int callBack, int callBackSub) volatile;
74  void writeCVByte(int cv, int bValue, int callBack, int callBackSub) volatile;
75  void writeCVBit(int cv, int bNum, int bValue, int callBack, int callBackSub) volatile;
76  void writeCVByteMain(int cab, int cv, int bvalue) volatile;
77  void writeCVBitMain(int cab, int cv, int bNum, int bValue) volatile;
78 
79 #ifdef DCCPP_DEBUG_MODE
80  void printPacket(int, byte *, int, int) volatile;
81 #endif
82 };
83 
84 #endif