DCCpp
This is the library version of a program for Arduino to control railroading DCC devices.
DCCpp.hpp
1 //-------------------------------------------------------------------
2 #ifndef __DCCpp_hpp__
3 #define __DCCpp_hpp__
4 //-------------------------------------------------------------------
5 
6 #include "DCCpp.h"
7 
14 {
15  private:
18  byte activeFlags[4];
19 
23  byte activeFlagsSent[4];
24 
25  inline byte byteNumber(byte inFunctionNumber) { return inFunctionNumber / 8; }
26  inline byte bitNumber(byte inFunctionNumber) { return inFunctionNumber % 8; }
27 
28  public:
32 
35  void clear();
39  void activate(byte inFunctionNumber);
43  void inactivate(byte inFunctionNumber);
48  bool isActivated(byte inFunctionNumber);
50  void statesSent();
56  bool isActivationChanged(byte inFunctionNumber);
57 
58 #ifdef DCCPP_DEBUG_MODE
59 
62  void printActivated();
63 #endif
64 };
65 
70 class DCCpp
71 {
72  private:
73  static bool programMode;
74  static bool panicStopped;
75 
76  static bool setThrottle(volatile RegisterList *inReg, int nReg, int inLocoId, int inStepsNumber, int inNewSpeed, bool inForward);
77  //static int readCv(volatile RegisterList *inReg, byte inCvId, int callBack = 100, int callBackSub = 200) { return inReg->readCV(inCvId, callBack, callBackSub); }
78  static void writeCv(volatile RegisterList *inReg, int inCvId, byte inCvValue, int callBack = 100, int callBackSub = 200);
79  static int identifyLocoId(volatile RegisterList *inReg);
80  static void setFunctions(volatile RegisterList *inReg, int nReg, int inLocoId, FunctionsState &inStates);
81 
82  public:
83  static byte ackThreshold;
84  static volatile RegisterList mainRegs, progRegs;
85  static CurrentMonitor mainMonitor;
86  static CurrentMonitor progMonitor;
87 
88  public:
89  // begins
92  static void begin();
99  static void beginMain(uint8_t inOptionalDirectionMotor, uint8_t inSignalPin, uint8_t inSignalEnablePin, uint8_t inCurrentMonitor);
106  static void beginProg(uint8_t inOptionalDirectionMotor, uint8_t inSignalPin, uint8_t inSignalEnablePin, uint8_t inCurrentMonitor);
107 
110  static inline void beginMainMotorShield() { beginMain(MOTOR_SHIELD_DIRECTION_MOTOR_CHANNEL_PIN_A, DCC_SIGNAL_PIN_MAIN, MOTOR_SHIELD_SIGNAL_ENABLE_PIN_MAIN, MOTOR_SHIELD_CURRENT_MONITOR_PIN_MAIN); }
113  static inline void beginProgMotorShield() { beginProg(MOTOR_SHIELD_DIRECTION_MOTOR_CHANNEL_PIN_B, DCC_SIGNAL_PIN_PROG, MOTOR_SHIELD_SIGNAL_ENABLE_PIN_PROG, MOTOR_SHIELD_CURRENT_MONITOR_PIN_PROG); }
114 
117  static inline void beginMainPololu() { beginMain(POLOLU_DIRECTION_MOTOR_CHANNEL_PIN_A, DCC_SIGNAL_PIN_MAIN, POLOLU_SIGNAL_ENABLE_PIN_MAIN, POLOLU_CURRENT_MONITOR_PIN_MAIN); }
120  static inline void beginProgPololu() { beginProg(POLOLU_DIRECTION_MOTOR_CHANNEL_PIN_B, DCC_SIGNAL_PIN_PROG, POLOLU_SIGNAL_ENABLE_PIN_PROG, POLOLU_CURRENT_MONITOR_PIN_PROG); }
121 #ifdef USE_ETHERNET
122 
127  static void beginEthernet(uint8_t *inMac, uint8_t *inIp, EthernetProtocol inProtocol = EthernetProtocol::TCP);
128 #endif
129 
130  // DCCpp global functions
131 
136  static bool IsMainTrack(volatile RegisterList *apRegs) { return apRegs == &mainRegs; }
137 
140  static void loop();
141 
145  static void panicStop(bool inStop);
146 
151  static void powerOn(bool inMain = true, bool inProg = true);
152 
157  static void powerOff(bool inMain = true, bool inProg = true);
158 
163  static byte setAckThreshold(byte inNewValue);
164 
168  static inline void setCurrentSampleMaxMain(float inMax) { mainMonitor.currentSampleMax = inMax; }
169 
173  static inline void setCurrentSampleMaxProg(float inMax) { progMonitor.currentSampleMax = inMax; }
174 
178  static inline float getCurrentMain() { return mainMonitor.pin == UNDEFINED_PIN ? 0 : mainMonitor.current; }
179 
183  static inline float getCurrentProg() { return progMonitor.pin == UNDEFINED_PIN ? 0 : progMonitor.current; }
184 
185  // Main track functions
186 
194  static inline bool setSpeedMain(int nReg, int inLocoId, int inStepsNumber, int inNewSpeed, bool inForward) { return setThrottle(&(mainRegs), nReg, inLocoId, inStepsNumber, inNewSpeed, inForward); }
195 
199  static inline int identifyLocoIdMain() { return identifyLocoId(&(mainRegs)); }
200 
208  static inline int readCvMain(int inCvId, int callBack = 100, int callBackSub = 200) { return mainRegs.readCVmain(inCvId, callBack, callBackSub); }
209 
217  static inline void writeCvMain(int inCvId, byte inValue, int callBack = 100, int callBackSub = 200) { writeCv(&(mainRegs), inCvId, inValue, callBack, callBackSub); }
218 
224  static inline void setFunctionsMain(int nReg, int inLocoId, FunctionsState &inStates) { setFunctions(&(mainRegs), nReg, inLocoId, inStates); }
225 
226  // Programming track functions
227 
235  static inline bool setSpeedProg(int nReg, int inLocoId, int inStepsNumber, int inNewSpeed, bool inForward) { return setThrottle(&(progRegs), nReg, inLocoId, inStepsNumber, inNewSpeed, inForward); }
236 
240  static inline int identifyLocoIdProg() { return identifyLocoId(&(progRegs)); }
241 
249  static inline int readCvProg(int inCvId, int callBack = 100, int callBackSub = 200) { return progRegs.readCV(inCvId, callBack, callBackSub); }
250 
257  static inline void writeCvProg(int inCvId, byte inValue, int callBack = 100, int callBackSub = 200) { writeCv(&(progRegs), inCvId, inValue, callBack, callBackSub); }
258 
264  static inline void setFunctionsProg(int nReg, int inLocoId, FunctionsState &inStates) { setFunctions(&(progRegs), nReg, inLocoId, inStates); }
265 
266  // Accessories
267 
273  static void setAccessory(int inAddress, byte inSubAddress, byte inActivate);
274 
275 public:
276 #ifdef DCCPP_PRINT_DCCPP
277 
278  #define PRINT_DCCPP DCCpp::showConfiguration();
279 
282  static void showConfiguration();
283 #else
284  #define PRINT_DCCPP
285 #endif
286 };
287 
288 //-------------------------------------------------------------------
289 #endif
290 //-------------------------------------------------------------------
static void setAccessory(int inAddress, byte inSubAddress, byte inActivate)
Definition: DCCpp.cpp:770
static void setFunctionsProg(int nReg, int inLocoId, FunctionsState &inStates)
Definition: DCCpp.hpp:264
static void writeCvProg(int inCvId, byte inValue, int callBack = 100, int callBackSub = 200)
Definition: DCCpp.hpp:257
static int readCvProg(int inCvId, int callBack = 100, int callBackSub = 200)
Definition: DCCpp.hpp:249
static int identifyLocoIdProg()
Definition: DCCpp.hpp:240
static void setFunctionsMain(int nReg, int inLocoId, FunctionsState &inStates)
Definition: DCCpp.hpp:224
static int readCvMain(int inCvId, int callBack = 100, int callBackSub = 200)
Definition: DCCpp.hpp:208
static int identifyLocoIdMain()
Definition: DCCpp.hpp:199
static float getCurrentMain()
Definition: DCCpp.hpp:178
static void setCurrentSampleMaxMain(float inMax)
Definition: DCCpp.hpp:168
static void powerOff(bool inMain = true, bool inProg = true)
Definition: DCCpp.cpp:568
FunctionsState()
Definition: DCCpp.cpp:25
bool isActivated(byte inFunctionNumber)
Definition: DCCpp.cpp:51
static void setCurrentSampleMaxProg(float inMax)
Definition: DCCpp.hpp:173
void clear()
Definition: DCCpp.cpp:30
static void begin()
Definition: DCCpp.cpp:299
void activate(byte inFunctionNumber)
Definition: DCCpp.cpp:41
void statesSent()
Definition: DCCpp.cpp:61
static void beginMain(uint8_t inOptionalDirectionMotor, uint8_t inSignalPin, uint8_t inSignalEnablePin, uint8_t inCurrentMonitor)
Definition: DCCpp.cpp:118
void inactivate(byte inFunctionNumber)
Definition: DCCpp.cpp:46
static float getCurrentProg()
Definition: DCCpp.hpp:183
static void beginMainMotorShield()
Definition: DCCpp.hpp:110
bool isActivationChanged(byte inFunctionNumber)
Definition: DCCpp.cpp:56
static void writeCvMain(int inCvId, byte inValue, int callBack = 100, int callBackSub = 200)
Definition: DCCpp.hpp:217
void printActivated()
Definition: DCCpp.cpp:68
static void beginProgPololu()
Definition: DCCpp.hpp:120
static void beginProg(uint8_t inOptionalDirectionMotor, uint8_t inSignalPin, uint8_t inSignalEnablePin, uint8_t inCurrentMonitor)
Definition: DCCpp.cpp:185
static bool setSpeedMain(int nReg, int inLocoId, int inStepsNumber, int inNewSpeed, bool inForward)
Definition: DCCpp.hpp:194
static void beginProgMotorShield()
Definition: DCCpp.hpp:113
static byte setAckThreshold(byte inNewValue)
Definition: DCCpp.cpp:580
Definition: DCCpp.hpp:70
static void beginMainPololu()
Definition: DCCpp.hpp:117
static bool IsMainTrack(volatile RegisterList *apRegs)
Definition: DCCpp.hpp:136
static void loop()
Definition: DCCpp.cpp:93
static void panicStop(bool inStop)
Definition: DCCpp.cpp:538
static bool setSpeedProg(int nReg, int inLocoId, int inStepsNumber, int inNewSpeed, bool inForward)
Definition: DCCpp.hpp:235
static void powerOn(bool inMain = true, bool inProg = true)
Definition: DCCpp.cpp:555