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 
9 {
10  private:
11  // Four bytes : 32 bits : from function 0 to 28, maximum for Dcc.
12  // A bit at true is an activated function.
13  byte activeFlags[4];
14 
15  inline byte byteNumber(byte inFunctionNumber) { return inFunctionNumber / 8; }
16  inline byte bitNumber(byte inFunctionNumber) { return inFunctionNumber % 8; }
17 
18  public:
20 
21  void clear();
22  void activate(byte inFunctionNumber);
23  void inactivate(byte inFunctionNumber);
24  bool isActivated(byte inFunctionNumber);
25 
26 #ifdef DCCPP_DEBUG_MODE
27  void printActivated();
28 #endif
29 };
30 
32 {
33  private:
34  bool programMode;
35  bool panicStopped;
36 
37  bool setThrottle(volatile RegisterList *inReg, int nReg, int inLocoId, int inStepsNumber, int inNewSpeed, bool inToLeft);
38  int readCv(volatile RegisterList *inReg, int inLocoId, byte inCvId);
39  void writeCv(volatile RegisterList *inReg, int inLocoId, int inCvId, byte inCvValue);
40  void setFunctions(volatile RegisterList *inReg, int nReg, int inLocoId, FunctionsState inStates);
41 
42  public:
43  static volatile RegisterList mainRegs, progRegs;
44  static CurrentMonitor mainMonitor;
45  static CurrentMonitor progMonitor;
46 
47  DCCppClass();
48 
49  public:
50  // begins
51  void begin();
52  void beginMain(uint8_t OptionalDirectionMotor, uint8_t Dummy, uint8_t SignalEnablePin, uint8_t CurrentMonitor);
53  void beginProg(uint8_t OptionalDirectionMotor, uint8_t Dummy, uint8_t SignalEnablePin, uint8_t CurrentMonitor);
54 
55  inline void beginMainMotorShield() { this->beginMain(MOTOR_SHIELD_DIRECTION_MOTOR_CHANNEL_PIN_A, DCC_SIGNAL_PIN_MAIN, MOTOR_SHIELD_SIGNAL_ENABLE_PIN_MAIN, MOTOR_SHIELD_CURRENT_MONITOR_PIN_MAIN); }
56  inline void beginProgMotorShield() { this->beginMain(MOTOR_SHIELD_DIRECTION_MOTOR_CHANNEL_PIN_B, DCC_SIGNAL_PIN_PROG, MOTOR_SHIELD_SIGNAL_ENABLE_PIN_PROG, MOTOR_SHIELD_CURRENT_MONITOR_PIN_PROG); }
57 
58  inline void beginMainPololu() { this->beginMain(POLOLU_DIRECTION_MOTOR_CHANNEL_PIN_A, DCC_SIGNAL_PIN_MAIN, POLOLU_SIGNAL_ENABLE_PIN_MAIN, POLOLU_CURRENT_MONITOR_PIN_MAIN); }
59  inline void beginProgPololu() { this->beginMain(POLOLU_DIRECTION_MOTOR_CHANNEL_PIN_B, DCC_SIGNAL_PIN_PROG, POLOLU_SIGNAL_ENABLE_PIN_PROG, POLOLU_CURRENT_MONITOR_PIN_PROG); }
60 #ifdef USE_ETHERNET
61  void beginEthernet(uint8_t *inMac, uint8_t *inIp, EthernetProtocol inProtocol = EthernetProtocol::TCP);
62 #endif
63 
64  // DCCpp global functions
65  void loop();
66  void panicStop(bool inStop);
67  void powerOn();
68  void powerOff();
69  inline void setCurrentSampleMaxMain(float inMax) { this->mainMonitor.currentSampleMax = inMax; }
70  inline void setCurrentSampleMaxProg(float inMax) { this->progMonitor.currentSampleMax = inMax; }
71  inline float getCurrentMain() { return this->mainMonitor.pin == UNDEFINED_PIN ? 0 : mainMonitor.current; }
72  inline float getCurrentProg() { return this->progMonitor.pin == UNDEFINED_PIN ? 0 : progMonitor.current; }
73 
74  // Main driving functions
75  inline bool setSpeedMain(int nReg, int inLocoId, int inStepsNumber, int inNewSpeed, bool inToLeft) { return this->setThrottle(&(this->mainRegs), nReg, inLocoId, inStepsNumber, inNewSpeed, inToLeft); }
76  inline int readCvMain(int inLocoId, byte inCvId) { return this->readCv(&(this->mainRegs), inLocoId, inCvId); }
77  inline void writeCvMain(int inLocoId, int inCvId, byte inValue) { this->writeCv(&(this->mainRegs), inLocoId, inCvId, inValue); }
78  inline void setFunctionsMain(int nReg, int inLocoId, FunctionsState inStates) { this->setFunctions(&(this->mainRegs), nReg, inLocoId, inStates); }
79 
80  // Prog driving functions
81  inline bool setSpeedProg(int nReg, int inLocoId, int inStepsNumber, int inNewSpeed, bool inToLeft) { return this->setThrottle(&(this->progRegs), nReg, inLocoId, inStepsNumber, inNewSpeed, inToLeft); }
82  inline int readCvProg(int inLocoId, byte inCvId) { return this->readCv(&(this->progRegs), inLocoId, inCvId); }
83  inline void writeCvProg(int inLocoId, int inCvId, byte inValue) { this->writeCv(&(this->progRegs), inLocoId, inCvId, inValue); }
84  inline void setFunctionsProg(int nReg, int inLocoId, FunctionsState inStates) { this->setFunctions(&(this->progRegs), nReg, inLocoId, inStates); }
85 
86  // Accessories
87  void setAccessory(int inAddress, byte inSubAddress, byte inActivate);
88 
89 #ifdef DCCPP_PRINT_DCCPP
90  static void showConfiguration();
91 #endif
92 
93  static DCCppClass DCCppInstance;
94 };
95 
96 #define DCCpp DCCppClass::DCCppInstance
97 
98 //-------------------------------------------------------------------
99 #endif
100 //-------------------------------------------------------------------