DCCpp
This is the library version of a program for Arduino to control railroading DCC devices.
DCCpp.cpp
1 /*************************************************************
2 project: <DCCpp library>
3 author: <Thierry PARIS>
4 description: <DCCpp class>
5 *************************************************************/
6 
7 #include "DCCpp.h"
8 #include "arduino.h"
9 
10 // NEXT DECLARE GLOBAL OBJECTS TO PROCESS AND STORE DCC PACKETS AND MONITOR TRACK CURRENTS.
11 // NOTE REGISTER LISTS MUST BE DECLARED WITH "VOLATILE" QUALIFIER TO ENSURE THEY ARE PROPERLY UPDATED BY INTERRUPT ROUTINES
12 
13 volatile RegisterList DCCpp::mainRegs(MAX_MAIN_REGISTERS); // create list of registers for MAX_MAIN_REGISTER Main Track Packets
14 volatile RegisterList DCCpp::progRegs(3); // create a shorter list of only two registers for Program Track Packets
15 
16 CurrentMonitor DCCpp::mainMonitor; // create monitor for current on Main Track
17 CurrentMonitor DCCpp::progMonitor; // create monitor for current on Program Track
18 
19 bool DCCpp::programMode;
20 bool DCCpp::panicStopped;
21 
22 // *********************************************************** FunctionsState
23 
25 {
26  this->clear();
27 }
28 
30 {
31  // Clear all functions
32  this->activeFlags[0] = 0;
33  this->activeFlags[1] = 0;
34  this->activeFlags[2] = 0;
35  this->activeFlags[3] = 0;
36 
37  this->statesSent();
38 }
39 
40 void FunctionsState::activate(byte inFunctionNumber)
41 {
42  bitSet(this->activeFlags[inFunctionNumber / 8], inFunctionNumber % 8);
43 }
44 
45 void FunctionsState::inactivate(byte inFunctionNumber)
46 {
47  bitClear(this->activeFlags[inFunctionNumber / 8], inFunctionNumber % 8);
48 }
49 
50 bool FunctionsState::isActivated(byte inFunctionNumber)
51 {
52  return bitRead(this->activeFlags[inFunctionNumber / 8], inFunctionNumber % 8);
53 }
54 
55 bool FunctionsState::isActivationChanged(byte inFunctionNumber)
56 {
57  return bitRead(this->activeFlagsSent[inFunctionNumber / 8], inFunctionNumber % 8) != isActivated(inFunctionNumber);
58 }
59 
61 {
62  for (int i = 0; i < 4; i++)
63  this->activeFlagsSent[i] = this->activeFlags[i];
64 }
65 
66 #ifdef DCCPP_DEBUG_MODE
67 void FunctionsState::printActivated()
68 {
69  for (int i = 0; i < 32; i++)
70  {
71  if (this->isActivated(i))
72  {
73  Serial.print(i);
74  Serial.print(" ");
75  }
76  }
77 
78  Serial.println("");
79 }
80 #endif
81 
82 // *********************************************************** end of FunctionsState
83 
84 // *********************************************************** DCCpp class
85 
86 static bool first = true;
87 
89 // MAIN ARDUINO LOOP
91 
93 {
94 #ifdef USE_TEXTCOMMAND
95  TextCommand::process(); // check for, and process, and new serial commands
96 #endif
97 
98  if (first)
99  {
100  first = false;
101 #if defined(DCCPP_DEBUG_MODE) && defined(DCCPP_PRINT_DCCPP)
102  showConfiguration();
103 #endif
104  }
105 
107  { // if sufficient time has elapsed since last update, check current draw on Main and Program Tracks
108  mainMonitor.check();
109  progMonitor.check();
110  }
111 
112 #ifdef USE_SENSOR
113  Sensor::check(); // check sensors for activated or not
114 #endif
115 }
116 
117 void DCCpp::beginMain(uint8_t inOptionalDirectionMotor, uint8_t inSignalPin, uint8_t inSignalEnable, uint8_t inCurrentMonitor)
118 {
119  DCCppConfig::DirectionMotorA = inOptionalDirectionMotor;
120  DCCppConfig::SignalEnablePinMain = inSignalEnable; // PWM
121  DCCppConfig::CurrentMonitorMain = inCurrentMonitor;
122 
123  // If no main line, exit.
124  if (DCCppConfig::SignalEnablePinMain == UNDEFINED_PIN)
125  {
126 #ifdef DCCPP_DEBUG_MODE
127  Serial.println("No main line");
128 #endif
129  return;
130  }
131 
132  mainMonitor.begin(DCCppConfig::CurrentMonitorMain, (char *) "<p2>");
133 
134  // CONFIGURE TIMER_1 TO OUTPUT 50% DUTY CYCLE DCC SIGNALS ON OC1B INTERRUPT PINS
135 
136  // Direction Pin for Motor Shield Channel A - MAIN OPERATIONS TRACK
137  // Controlled by Arduino 16-bit TIMER 1 / OC1B Interrupt Pin
138  // Values for 16-bit OCR1A and OCR1B registers calibrated for 1:1 prescale at 16 MHz clock frequency
139  // Resulting waveforms are 200 microseconds for a ZERO bit and 116 microseconds for a ONE bit with exactly 50% duty cycle
140 
141 #define DCC_ZERO_BIT_TOTAL_DURATION_TIMER1 3199
142 #define DCC_ZERO_BIT_PULSE_DURATION_TIMER1 1599
143 
144 #define DCC_ONE_BIT_TOTAL_DURATION_TIMER1 1855
145 #define DCC_ONE_BIT_PULSE_DURATION_TIMER1 927
146  if (DCCppConfig::DirectionMotorA != UNDEFINED_PIN)
147  {
148  pinMode(DCCppConfig::DirectionMotorA, INPUT); // ensure this pin is not active! Direction will be controlled by DCC SIGNAL instead (below)
149  digitalWrite(DCCppConfig::DirectionMotorA, LOW);
150  }
151 
152  pinMode(inSignalPin, OUTPUT); // FOR SHIELDS, THIS ARDUINO OUPUT PIN MUST BE PHYSICALLY CONNECTED TO THE PIN FOR DIRECTION-A OF MOTOR CHANNEL-A
153 
154  bitSet(TCCR1A, WGM10); // set Timer 1 to FAST PWM, with TOP=OCR1A
155  bitSet(TCCR1A, WGM11);
156  bitSet(TCCR1B, WGM12);
157  bitSet(TCCR1B, WGM13);
158 
159  bitSet(TCCR1A, COM1B1); // set Timer 1, OC1B (pin 10/UNO, pin 12/MEGA) to inverting toggle (actual direction is arbitrary)
160  bitSet(TCCR1A, COM1B0);
161 
162  bitClear(TCCR1B, CS12); // set Timer 1 prescale=1
163  bitClear(TCCR1B, CS11);
164  bitSet(TCCR1B, CS10);
165 
166  OCR1A = DCC_ONE_BIT_TOTAL_DURATION_TIMER1;
167  OCR1B = DCC_ONE_BIT_PULSE_DURATION_TIMER1;
168 
169  pinMode(DCCppConfig::SignalEnablePinMain, OUTPUT); // master enable for motor channel A
170 
171  mainRegs.loadPacket(1, RegisterList::idlePacket, 2, 0); // load idle packet into register 1
172 
173  bitSet(TIMSK1, OCIE1B); // enable interrupt vector for Timer 1 Output Compare B Match (OCR1B)
174  digitalWrite(DCCppConfig::SignalEnablePinMain, LOW);
175 
176 #ifdef DCCPP_DEBUG_MODE
177  Serial.println(F("beginMain achivied"));
178 #endif
179 }
180 
181 void DCCpp::beginProg(uint8_t inOptionalDirectionMotor, uint8_t inSignalPin, uint8_t inSignalEnable, uint8_t inCurrentMonitor)
182 {
183  DCCppConfig::DirectionMotorB = inOptionalDirectionMotor;
184  DCCppConfig::SignalEnablePinProg = inSignalEnable;
185  DCCppConfig::CurrentMonitorProg = inCurrentMonitor;
186 
187  // If no programming line, exit.
188  if (DCCppConfig::SignalEnablePinProg == UNDEFINED_PIN)
189  {
190 #ifdef DCCPP_DEBUG_MODE
191  Serial.println("No prog line");
192 #endif
193  return;
194  }
195 
196  progMonitor.begin(DCCppConfig::CurrentMonitorProg, (char *) "<p3>");
197 
198  // CONFIGURE EITHER TIMER_0 (UNO) OR TIMER_3 (MEGA) TO OUTPUT 50% DUTY CYCLE DCC SIGNALS ON OC0B (UNO) OR OC3B (MEGA) INTERRUPT PINS
199 
200 #if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) // Configuration for UNO
201 
202  // Direction Pin for Motor Shield Channel B - PROGRAMMING TRACK
203  // Controlled by Arduino 8-bit TIMER 0 / OC0B Interrupt Pin
204  // Values for 8-bit OCR0A and OCR0B registers calibrated for 1:64 prescale at 16 MHz clock frequency
205  // Resulting waveforms are 200 microseconds for a ZERO bit and 116 microseconds for a ONE bit with as-close-as-possible to 50% duty cycle
206 
207 #define DCC_ZERO_BIT_TOTAL_DURATION_TIMER0 49
208 #define DCC_ZERO_BIT_PULSE_DURATION_TIMER0 24
209 
210 #define DCC_ONE_BIT_TOTAL_DURATION_TIMER0 28
211 #define DCC_ONE_BIT_PULSE_DURATION_TIMER0 14
212 
213  if (DCCppConfig::DirectionMotorB != UNDEFINED_PIN)
214  {
215  pinMode(DCCppConfig::DirectionMotorB, INPUT); // ensure this pin is not active! Direction will be controlled by DCC SIGNAL instead (below)
216  digitalWrite(DCCppConfig::DirectionMotorB, LOW);
217  }
218 
219  pinMode(inSignalPin, OUTPUT); // THIS ARDUINO OUTPUT PIN MUST BE PHYSICALLY CONNECTED TO THE PIN FOR DIRECTION-B OF MOTOR CHANNEL-B
220 
221  bitSet(TCCR0A, WGM00); // set Timer 0 to FAST PWM, with TOP=OCR0A
222  bitSet(TCCR0A, WGM01);
223  bitSet(TCCR0B, WGM02);
224 
225  bitSet(TCCR0A, COM0B1); // set Timer 0, OC0B (pin 5) to inverting toggle (actual direction is arbitrary)
226  bitSet(TCCR0A, COM0B0);
227 
228  bitClear(TCCR0B, CS02); // set Timer 0 prescale=64
229  bitSet(TCCR0B, CS01);
230  bitSet(TCCR0B, CS00);
231 
232  OCR0A = DCC_ONE_BIT_TOTAL_DURATION_TIMER0;
233  OCR0B = DCC_ONE_BIT_PULSE_DURATION_TIMER0;
234 
235  pinMode(DCCppConfig::SignalEnablePinProg, OUTPUT); // master enable for motor channel B
236 
237  progRegs.loadPacket(1, RegisterList::idlePacket, 2, 0); // load idle packet into register 1
238 
239  bitSet(TIMSK0, OCIE0B); // enable interrupt vector for Timer 0 Output Compare B Match (OCR0B)
240 
241 #else // Configuration for MEGA
242 
243  // Direction Pin for Motor Shield Channel B - PROGRAMMING TRACK
244  // Controlled by Arduino 16-bit TIMER 3 / OC3B Interrupt Pin
245  // Values for 16-bit OCR3A and OCR3B registers calibrated for 1:1 prescale at 16 MHz clock frequency
246  // Resulting waveforms are 200 microseconds for a ZERO bit and 116 microseconds for a ONE bit with exactly 50% duty cycle
247 
248 #define DCC_ZERO_BIT_TOTAL_DURATION_TIMER3 3199
249 #define DCC_ZERO_BIT_PULSE_DURATION_TIMER3 1599
250 
251 #define DCC_ONE_BIT_TOTAL_DURATION_TIMER3 1855
252 #define DCC_ONE_BIT_PULSE_DURATION_TIMER3 927
253 
254  if (DCCppConfig::DirectionMotorB != UNDEFINED_PIN)
255  {
256  pinMode(DCCppConfig::DirectionMotorB, INPUT); // ensure this pin is not active! Direction will be controlled by DCC SIGNAL instead (below)
257  digitalWrite(DCCppConfig::DirectionMotorB, LOW);
258  }
259 
260  pinMode(DCC_SIGNAL_PIN_PROG, OUTPUT); // THIS ARDUINO OUTPUT PIN MUST BE PHYSICALLY CONNECTED TO THE PIN FOR DIRECTION-B OF MOTOR CHANNEL-B
261 
262  bitSet(TCCR3A, WGM30); // set Timer 3 to FAST PWM, with TOP=OCR3A
263  bitSet(TCCR3A, WGM31);
264  bitSet(TCCR3B, WGM32);
265  bitSet(TCCR3B, WGM33);
266 
267  bitSet(TCCR3A, COM3B1); // set Timer 3, OC3B (pin 2) to inverting toggle (actual direction is arbitrary)
268  bitSet(TCCR3A, COM3B0);
269 
270  bitClear(TCCR3B, CS32); // set Timer 3 prescale=1
271  bitClear(TCCR3B, CS31);
272  bitSet(TCCR3B, CS30);
273 
274  OCR3A = DCC_ONE_BIT_TOTAL_DURATION_TIMER3;
275  OCR3B = DCC_ONE_BIT_PULSE_DURATION_TIMER3;
276 
277  pinMode(DCCppConfig::SignalEnablePinProg, OUTPUT); // master enable for motor channel B
278 
279  progRegs.loadPacket(1, RegisterList::idlePacket, 2, 0); // load idle packet into register 1
280 
281  bitSet(TIMSK3, OCIE3B); // enable interrupt vector for Timer 3 Output Compare B Match (OCR3B)
282 
283 #endif
284  digitalWrite(DCCppConfig::SignalEnablePinProg, LOW);
285 
286 #ifdef DCCPP_DEBUG_MODE
287  Serial.println(F("beginProg achivied"));
288 #endif
289 }
290 
292 {
293  programMode = false;
294  panicStopped = false;
295 
296  DCCppConfig::SignalEnablePinMain = UNDEFINED_PIN;
297  DCCppConfig::CurrentMonitorMain = UNDEFINED_PIN;
298 
299  DCCppConfig::SignalEnablePinProg = UNDEFINED_PIN;
300  DCCppConfig::CurrentMonitorProg = UNDEFINED_PIN;
301 
302  DCCppConfig::DirectionMotorA = UNDEFINED_PIN;
303  DCCppConfig::DirectionMotorB = UNDEFINED_PIN;
304 
305  mainMonitor.begin(UNDEFINED_PIN, "");
306  progMonitor.begin(UNDEFINED_PIN, "");
307 
308 #ifdef SDCARD_CS
309  pinMode(SDCARD_CS, OUTPUT);
310  digitalWrite(SDCARD_CS, HIGH); // De-select the SD card
311 #endif
312 
313 #ifdef USE_EEPROM
314  EEStore::init(); // initialize and load Turnout and Sensor definitions stored in EEPROM
316  EEStore::store();
317 #endif
318 
319 #ifdef DCCPP_DEBUG_MODE
320  //pinMode(LED_BUILTIN, OUTPUT);
321  Serial.println(F("begin achieved"));
322 #endif
323 
324 } // begin
325 
326 #ifdef USE_ETHERNET
327 void DCCpp::beginEthernet(uint8_t *inMac, uint8_t *inIp, EthernetProtocol inProtocol)
328 {
329  if (inIp != NULL)
330  for (int i = 0; i < 4; i++)
331  DCCppConfig::EthernetIp[i] = inIp[i];
332 
333  for (int i = 0; i < 6; i++)
334  DCCppConfig::EthernetMac[i] = inMac[i];
335 
336  DCCppConfig::Protocol = inProtocol;
337 
338  if (inIp == NULL)
339  Ethernet.begin(inMac); // Start networking using DHCP to get an IP Address
340  else
341  Ethernet.begin(inMac, inIp); // Start networking using STATIC IP Address
342 
343  INTERFACE.begin();
344 #ifdef DCCPP_DEBUG_MODE
345  //pinMode(LED_BUILTIN, OUTPUT);
346  showConfiguration();
347  Serial.println(F("beginEthernet achieved"));
348 #endif
349 } // beginEthernet
350 #endif
351 
353  // DEFINE THE INTERRUPT LOGIC THAT GENERATES THE DCC SIGNAL
355 
356  // The code below will be called every time an interrupt is triggered on OCNB, where N can be 0 or 1.
357  // It is designed to read the current bit of the current register packet and
358  // updates the OCNA and OCNB counters of Timer-N to values that will either produce
359  // a long (200 microsecond) pulse, or a short (116 microsecond) pulse, which respectively represent
360  // DCC ZERO and DCC ONE bits.
361 
362  // These are hardware-driven interrupts that will be called automatically when triggered regardless of what
363  // DCC++ BASE STATION was otherwise processing. But once inside the interrupt, all other interrupt routines are temporarily disabled.
364  // Since a short pulse only lasts for 116 microseconds, and there are TWO separate interrupts
365  // (one for Main Track Registers and one for the Program Track Registers), the interrupt code must complete
366  // in much less than 58 microseconds, otherwise there would be no time for the rest of the program to run. Worse, if the logic
367  // of the interrupt code ever caused it to run longer than 58 microseconds, an interrupt trigger would be missed, the OCNA and OCNB
368  // registers would not be updated, and the net effect would be a DCC signal that keeps sending the same DCC bit repeatedly until the
369  // interrupt code completes and can be called again.
370 
371  // A significant portion of this entire program is designed to do as much of the heavy processing of creating a properly-formed
372  // DCC bit stream upfront, so that the interrupt code below can be as simple and efficient as possible.
373 
374  // Note that we need to create two very similar copies of the code --- one for the Main Track OC1B interrupt and one for the
375  // Programming Track OCOB interrupt. But rather than create a generic function that incurs additional overhead, we create a macro
376  // that can be invoked with proper parameters for each interrupt. This slightly increases the size of the code base by duplicating
377  // some of the logic for each interrupt, but saves additional time.
378 
379  // As structured, the interrupt code below completes at an average of just under 6 microseconds with a worse-case of just under 11 microseconds
380  // when a new register is loaded and the logic needs to switch active register packet pointers.
381 
382  // THE INTERRUPT CODE MACRO: R=REGISTER LIST (mainRegs or progRegs), and N=TIMER (0 or 1)
383 
384 #define DCC_SIGNAL(R,N)
385  if(R.currentBit==R.currentReg->activePacket->nBits){ /* IF no more bits in this DCC Packet */
386  R.currentBit=0; /* reset current bit pointer and determine which Register and Packet to process next--- */
387  if (R.nRepeat>0 && R.currentReg == R.reg) { /* IF current Register is first Register AND should be repeated */
388  R.nRepeat--; /* decrement repeat count; result is this same Packet will be repeated */
389  }
390  else if (R.nextReg != NULL) { /* ELSE IF another Register has been updated */
391  R.currentReg = R.nextReg; /* update currentReg to nextReg */
392  R.nextReg = NULL; /* reset nextReg to NULL */
393  R.tempPacket = R.currentReg->activePacket; /* flip active and update Packets */
394  R.currentReg->activePacket = R.currentReg->updatePacket;
395  R.currentReg->updatePacket = R.tempPacket;
396  }
397  else { /* ELSE simply move to next Register */
398  if (R.currentReg == R.maxLoadedReg) /* BUT IF this is last Register loaded */
399  R.currentReg = R.reg; /* first reset currentReg to base Register, THEN */
400  R.currentReg++; /* increment current Register (note this logic causes Register[0] to be skipped when simply cycling through all Registers) */
401  } /* END-ELSE */
402  } /* END-IF: currentReg, activePacket, and currentBit should now be properly set to point to next DCC bit */
403 
404  if (R.currentReg->activePacket->buf[R.currentBit / 8] & R.bitMask[R.currentBit % 8]) { /* IF bit is a ONE */
405  OCR ## N ## A = DCC_ONE_BIT_TOTAL_DURATION_TIMER ## N; /* set OCRA for timer N to full cycle duration of DCC ONE bit */
406  OCR ## N ## B=DCC_ONE_BIT_PULSE_DURATION_TIMER ## N; /* set OCRB for timer N to half cycle duration of DCC ONE but */
407  } else{ /* ELSE it is a ZERO */
408  OCR ## N ## A=DCC_ZERO_BIT_TOTAL_DURATION_TIMER ## N; /* set OCRA for timer N to full cycle duration of DCC ZERO bit */
409  OCR ## N ## B=DCC_ZERO_BIT_PULSE_DURATION_TIMER ## N; /* set OCRB for timer N to half cycle duration of DCC ZERO bit */
410  } /* END-ELSE */
411 
412  R.currentBit++; /* point to next bit in current Packet */
413 
415 // NOW USE THE ABOVE MACRO TO CREATE THE CODE FOR EACH INTERRUPT
416 
417 ISR(TIMER1_COMPB_vect) { // set interrupt service for OCR1B of TIMER-1 which flips direction bit of Motor Shield Channel A controlling Main Track
418  DCC_SIGNAL(DCCpp::mainRegs, 1)
419 }
420 
421 #if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) // Configuration for UNO
422 
423 ISR(TIMER0_COMPB_vect) { // set interrupt service for OCR1B of TIMER-0 which flips direction bit of Motor Shield Channel B controlling Programming Track
424  DCC_SIGNAL(DCCpp::progRegs, 0)
425 }
426 
427 #else // Configuration for MEGA
428 
429 ISR(TIMER3_COMPB_vect) { // set interrupt service for OCR3B of TIMER-3 which flips direction bit of Motor Shield Channel B controlling Programming Track
430  DCC_SIGNAL(DCCpp::progRegs, 3)
431 }
432 
433 #endif
434 
435 #ifdef DCCPP_PRINT_DCCPP
436 // PRINT CONFIGURATION INFO TO SERIAL PORT REGARDLESS OF INTERFACE TYPE
438 // - ACTIVATED ON STARTUP IF SHOW_CONFIG_PIN IS TIED HIGH
439 
440 void DCCpp::showConfiguration()
441 {
442  Serial.println(F("*** DCCpp LIBRARY ***"));
443 
444  Serial.print(F("VERSION DCC++: "));
445  Serial.println(VERSION);
446  Serial.println(F("VERSION DCCpp library: 0.8.0"));
447  Serial.print(F("COMPILED: "));
448  Serial.print(__DATE__);
449  Serial.print(F(" "));
450  Serial.println(__TIME__);
451 
452  //Serial.print(F("nARDUINO: "));
453  //Serial.print(ARDUINO_TYPE);
454 
455  //Serial.print(F("nnMOTOR SHIELD: "));
456  //Serial.print(MOTOR_SHIELD_NAME);
457 
458  if (DCCppConfig::SignalEnablePinMain!= UNDEFINED_PIN)
459  {
460  Serial.print(F("nnDCC SIG MAIN(DIR): "));
461  Serial.println(DCC_SIGNAL_PIN_MAIN);
462  Serial.print(F(" DIRECTION: "));
463  Serial.println(DCCppConfig::DirectionMotorA);
464  Serial.print(F(" ENABLE(PWM): "));
465  Serial.println(DCCppConfig::SignalEnablePinMain);
466  Serial.print(F(" CURRENT: "));
467  Serial.println(DCCppConfig::CurrentMonitorMain);
468  }
469 
470  if (DCCppConfig::SignalEnablePinProg!= UNDEFINED_PIN)
471  {
472  Serial.print(F("nnDCC SIG PROG(DIR): "));
473  Serial.println(DCC_SIGNAL_PIN_PROG);
474  Serial.print(F(" DIRECTION: "));
475  Serial.println(DCCppConfig::DirectionMotorB);
476  Serial.print(F(" ENABLE(PWM): "));
477  Serial.println(DCCppConfig::SignalEnablePinProg);
478  Serial.print(F(" CURRENT: "));
479  Serial.println(DCCppConfig::CurrentMonitorProg);
480  }
481 #if defined(USE_EEPROM)
482 #if defined(USE_TURNOUT)
483  Serial.print(F("nnNUM TURNOUTS: "));
484  Serial.println(EEStore::data.nTurnouts);
485 #endif
486 #if defined(USE_SENSOR)
487  Serial.print(F(" SENSORS: "));
488  Serial.println(EEStore::data.nSensors);
489 #endif
490 #if defined(USE_OUTPUT)
491  Serial.print(F(" OUTPUTS: "));
492  Serial.println(EEStore::data.nOutputs);
493 #endif
494 #endif
495 
496 #ifdef USE_TEXTCOMMAND
497  Serial.print(F("nnINTERFACE: "));
498 #ifdef USE_ETHERNET
499  Serial.println(F("ETHERNET "));
500  Serial.print(F("MAC ADDRESS: "));
501  for (int i = 0; i<5; i++) {
502  Serial.print(DCCppConfig::EthernetMac[i], HEX);
503  Serial.print(F(":"));
504  }
505  Serial.println(DCCppConfig::EthernetMac[5], HEX);
506 // Serial.print(F("PORT: "));
507 // Serial.println(DCCppConfig::EthernetPort);
508  Serial.print(F("IP ADDRESS: "));
509  Serial.println(Ethernet.localIP());
510 
511 /*#ifdef IP_ADDRESS
512  Serial.println(F(" (STATIC)"));
513 #else
514  Serial.println(F(" (DHCP)"));
515 #endif*/
516 
517 #else
518  Serial.println(F("SERIAL"));
519 #endif
520 
521 #endif
522 // Serial.print(F("nnPROGRAM HALTED - PLEASE RESTART ARDUINO"));
523 
524 // while (true);
525 // Serial.println("");
526 }
527 #endif
528 
529 void DCCpp::panicStop(bool inStop)
530 {
531  panicStopped = inStop;
532 
533 #ifdef DCCPP_DEBUG_MODE
534  Serial.print(F("DCCpp PanicStop "));
535  Serial.println(inStop ? F("pressed"):F("canceled"));
536 #endif
537 
538  /* activate or not the power on rails */
539 
540  if (inStop)
541  powerOff();
542  else
543  powerOn();
544 }
545 
547 {
548  if (DCCppConfig::SignalEnablePinProg != UNDEFINED_PIN)
549  digitalWrite(DCCppConfig::SignalEnablePinProg, HIGH);
550  if (DCCppConfig::SignalEnablePinMain != UNDEFINED_PIN)
551  digitalWrite(DCCppConfig::SignalEnablePinMain, HIGH);
552  INTERFACE.print("<p1>");
553 #if !defined(USE_ETHERNET)
554  INTERFACE.println("");
555 #endif
556 }
557 
559 {
560  if (DCCppConfig::SignalEnablePinProg != UNDEFINED_PIN)
561  digitalWrite(DCCppConfig::SignalEnablePinProg, LOW);
562  if (DCCppConfig::SignalEnablePinMain != UNDEFINED_PIN)
563  digitalWrite(DCCppConfig::SignalEnablePinMain, LOW);
564  INTERFACE.print("<p0>");
565 #if !defined(USE_ETHERNET)
566  INTERFACE.println("");
567 #endif
568 }
569 
570 /***************************** Driving functions */
571 
572 bool DCCpp::setThrottle(volatile RegisterList *inpRegs, int nReg, int inLocoId, int inStepsNumber, int inNewSpeed, bool inForward)
573 {
574  int val = 0;
575 
576  if (panicStopped)
577  val = 1;
578  else
579  if (inNewSpeed > 0)
580  val = map(inNewSpeed, 0, inStepsNumber, 2, 127);
581 
582 #ifdef DCCPP_DEBUG_MODE
583  Serial.print(F("DCCpp SetSpeed "));
584  Serial.print(inForward?inNewSpeed:-inNewSpeed);
585  Serial.print(F("/"));
586  Serial.print(inStepsNumber);
587  Serial.print(F(" (in Dcc "));
588  Serial.print(val);
589  Serial.println(F(" )"));
590 #endif
591 
592  inpRegs->setThrottle(nReg, inLocoId, val, inForward);
593 
594  return true;
595 }
596 
597 void DCCpp::setFunctions(volatile RegisterList *inpRegs, int nReg, int inLocoId, FunctionsState &inStates)
598 {
599 #ifdef DCCPP_DEBUG_MODE
600  if (inpRegs == &mainRegs)
601  {
602  if (nReg > MAX_MAIN_REGISTERS)
603  Serial.println(F("Invalid register number on main track."));
604  }
605  else
606  {
607  if (nReg > MAX_PROG_REGISTERS)
608  Serial.println(F("Invalid register number on programming track."));
609  }
610 #endif
611  byte flags = 0;
612 
613  byte oneByte1 = 128; // Group one functions F0-F4
614  byte twoByte1 = 176; // Group two F5-F8
615  byte threeByte1 = 160; // Group three F9-F12
616  byte fourByte2 = 0; // Group four F13-F20
617  byte fiveByte2 = 0; // Group five F21-F28
618 
619  for (byte func = 0; func <= 28; func++)
620  {
621  if (func <= 4)
622  {
623  /*
624  * To set functions F0 - F4 on(= 1) or off(= 0) :
625  *
626  * BYTE1 : 128 + F1 * 1 + F2 * 2 + F3 * 4 + F4 * 8 + F0 * 16
627  * BYTE2 : omitted
628  */
629 
630  if (inStates.isActivationChanged(func))
631  flags |= 1;
632  if (inStates.isActivated(func))
633  {
634  if (func == 0)
635  oneByte1 += 16;
636  else
637  oneByte1 += (1 << (func - 1));
638  }
639  }
640  else if (func <= 8)
641  {
642  /*
643  * To set functions F5 - F8 on(= 1) or off(= 0) :
644  *
645  * BYTE1 : 176 + F5 * 1 + F6 * 2 + F7 * 4 + F8 * 8
646  * BYTE2 : omitted
647  */
648 
649  if (inStates.isActivationChanged(func))
650  flags |= 2;
651  if (inStates.isActivated(func))
652  twoByte1 += (1 << (func - 5));
653  }
654  else if (func <= 12)
655  {
656  /*
657  * To set functions F9 - F12 on(= 1) or off(= 0) :
658  *
659  * BYTE1 : 160 + F9 * 1 + F10 * 2 + F11 * 4 + F12 * 8
660  * BYTE2 : omitted
661  */
662 
663  if (inStates.isActivationChanged(func))
664  flags |= 4;
665  if (inStates.isActivated(func))
666  threeByte1 += (1 << (func - 9));
667  }
668  else if (func <= 20)
669  {
670  /*
671  * To set functions F13 - F20 on(= 1) or off(= 0) :
672  *
673  * BYTE1 : 222
674  * BYTE2 : F13 * 1 + F14 * 2 + F15 * 4 + F16 * 8 + F17 * 16 + F18 * 32 + F19 * 64 + F20 * 128
675  */
676 
677  if (inStates.isActivationChanged(func))
678  flags |= 8;
679  if (inStates.isActivated(func))
680  fourByte2 += (1 << (func - 13));
681  }
682  else if (func <= 28)
683  {
684  /*
685  * To set functions F21 - F28 on(= 1) of off(= 0) :
686  *
687  * BYTE1 : 223
688  * BYTE2 : F21 * 1 + F22 * 2 + F23 * 4 + F24 * 8 + F25 * 16 + F26 * 32 + F27 * 64 + F28 * 128
689  */
690 
691  if (inStates.isActivationChanged(func))
692  flags |= 16;
693  if (inStates.isActivated(func))
694  fiveByte2 += (1 << (func - 21));
695  }
696  }
697 
698  if (flags & 1)
699  inpRegs->setFunction(nReg, inLocoId, oneByte1, -1);
700  if (flags & 2)
701  inpRegs->setFunction(nReg, inLocoId, twoByte1, -1);
702  if (flags & 4)
703  inpRegs->setFunction(nReg, inLocoId, threeByte1, -1);
704  if (flags & 8)
705  inpRegs->setFunction(nReg, inLocoId, 222, fourByte2);
706  if (flags & 16)
707  inpRegs->setFunction(nReg, inLocoId, 223, fiveByte2);
708 
709  inStates.statesSent();
710 
711 #ifdef DCCPP_DEBUG_MODE
712  Serial.print(F("DCCpp SetFunctions for loco"));
713  Serial.print(inLocoId);
714  Serial.print(" / Activated : ");
715  inStates.printActivated();
716 #endif
717 }
718 
719 void DCCpp::writeCv(volatile RegisterList *inReg, int inLocoId, int inCv, byte inValue)
720 {
721  inReg->writeCVByte(inCv, inValue, 100, 101);
722 
723 #ifdef DCCPP_DEBUG_MODE
724  Serial.print(F("DCCpp WriteCv "));
725  Serial.print(inCv);
726  Serial.print(F(" : "));
727  Serial.println(inValue);
728 #endif
729 }
730 
731 int DCCpp::readCv(volatile RegisterList *inReg, int inLocoId, byte inCv)
732 {
733  return inReg->readCVmain(1, 100+inCv, 100+inCv);
734 }
735 
736 void DCCpp::setAccessory(int inAddress, byte inSubAddress, byte inActivate)
737 {
738  mainRegs.setAccessory(inAddress, inSubAddress, inActivate);
739 
740 #ifdef DCCPP_DEBUG_MODE
741  Serial.print(F("DCCpp AccessoryOperation "));
742  Serial.print(inAddress);
743  Serial.print(F(" / "));
744  Serial.print(inSubAddress);
745  Serial.print(F(" : "));
746  Serial.println(inActivate);
747 #endif
748 }
749 
static void setAccessory(int inAddress, byte inSubAddress, byte inActivate)
Definition: DCCpp.cpp:736
static void powerOn()
Definition: DCCpp.cpp:546
static void powerOff()
Definition: DCCpp.cpp:558
static void panicStop(bool inStop)
Definition: DCCpp.cpp:529
static EEStoreData data
Definition: EEStore.h:45
static void loop()
Definition: DCCpp.cpp:92
static void begin()
Definition: DCCpp.cpp:291
void statesSent()
Definition: DCCpp.cpp:60
FunctionsState()
Definition: DCCpp.cpp:24
static void check()
Definition: Sensor.cpp:120
void clear()
Definition: DCCpp.cpp:29
void inactivate(byte inFunctionNumber)
Definition: DCCpp.cpp:45
bool isActivationChanged(byte inFunctionNumber)
Definition: DCCpp.cpp:55
static boolean checkTime()
static bool needsRefreshing()
Definition: EEStore.cpp:110
void activate(byte inFunctionNumber)
Definition: DCCpp.cpp:40
static void store()
Definition: EEStore.cpp:90
static void beginMain(uint8_t inOptionalDirectionMotor, uint8_t inSignalPin, uint8_t inSignalEnablePin, uint8_t inCurrentMonitor)
Definition: DCCpp.cpp:117
void begin(int pin, const char *msg, float inSampleMax = 300)
bool isActivated(byte inFunctionNumber)
Definition: DCCpp.cpp:50
static void beginProg(uint8_t inOptionalDirectionMotor, uint8_t inSignalPin, uint8_t inSignalEnablePin, uint8_t inCurrentMonitor)
Definition: DCCpp.cpp:181
static void beginEthernet(uint8_t *inMac, uint8_t *inIp, EthernetProtocol inProtocol = EthernetProtocol::TCP)
Definition: DCCpp.cpp:327
static void init()
Definition: EEStore.cpp:24