//DCC_Debug.ino --application or client code#include "DCC_Debug.h"DCC_Debug DCC; void setup() { Serial.begin(115200); DCC.begin();}void loop() { //DCC.DCC_Dump(); DCC.DCC_Decode( );}-----------------------------------
//DCC_Debug.cpp Implementation file
#include "DCC_Debug.h"DCC_Debug::DCC_Debug( ) { };
void DCC_Debug::begin( ) { DCC_Probe::begin();}
void do_speed(int vel ){ if (vel <128) { Serial.print(" Rev: "); Serial.print(vel); } else { Serial.print(" Fwd: "); Serial.print(vel-128); }}void do_fns(int fn){ if (fn >= 192) Serial.print(" Feature Expansion Instruction"); else { if (fn<160){ Serial.print(" Light "); if (fn & 16) Serial.print("On"); else Serial.print("Off"); } Serial.print(" Function(s) "); if (fn >= 176) { if (fn&1) Serial.print("9 "); if (fn&2) Serial.print("10 "); if (fn&4) Serial.print("11 "); if (fn&8) Serial.print("12 "); } else if (fn >= 160) { if (fn&1) Serial.print("5 "); if (fn&2) Serial.print("6 "); if (fn&4) Serial.print("7 "); if (fn&8) Serial.print("8 "); }else { if (fn&1) Serial.print("1 "); if (fn&2) Serial.print("2 "); if (fn&4) Serial.print("3 "); if (fn&8) Serial.print("4 "); } if (fn&15) Serial.print("Active"); else { if (fn<160) Serial.print("1-4 "); else if (fn <176) Serial.print("5-8 "); else Serial.print("9-12 "); Serial.print("All inactive"); } } }
void do_locos(int loco,int op1,int op2) { Serial.print("Loco "); Serial.print(loco); if (op1 < 128) do_speed(op2); else do_fns(op1);}
void long_loco(int cmd1, int cmd2, int cmd3, int cmd4 ){ int addr = cmd1&0x3F; addr *= 256; addr += cmd2; //long address if (cmd3 <64) Serial.print("NA"); else if ((cmd3&0xF) ==1 ){ Serial.print("EMERGENCY"); } else { cmd3 = cmd3&0x3F; if (cmd3 >= 64) cmd3 = 128 + (cmd3 & 0x1F); //move sign do_locos(addr,cmd3,cmd4); }}void do_access(int op1, int op2){ unsigned int accessory; Serial.print("Access: "); accessory = ((~op2)&(0x70))<<2 ; accessory += op1 &0x3F; accessory -= 1; accessory *= 4; accessory += (op2&0x06)>>1; accessory += 1; Serial.print(accessory); if (op2&1) Serial.print(" on"); else Serial.print(" off");}#define TEST_ACC 0
void DCC_Debug :: DCC_Decode( ){ if (message_done( )) { _len = get_result(0); _opcode = get_result(1); _operand1 = get_result(2); _operand2 = get_result(3); _operand3 = get_result(4); #if TEST_ACC //testing - address 1411 //force everything to be this command _opcode = 161; _operand1 = 173; //173 accessory ON 172 accessory OFF #endif clear_message_done( ); if (_opcode == 255)return; //ignore idle commands if (_opcode == 0) Serial.print("Special:"); else if (_opcode <128) do_locos(_opcode,_operand1,_operand2); else if (_opcode <192) do_access(_opcode,_operand1 ); else if (_opcode <= 199) long_loco(_opcode,_operand1,_operand2,_operand3); else if ((_opcode <= 241)&&(_opcode >= 234))long_loco(_opcode,_operand1,_operand2,_operand3); else Serial.print("Opcode not implemented"); Serial.println(); } //message_done } //DCC_Decode
-----------------------------------------------------//DCC_Debug.h header file#ifndef DCC_DEBUG_H#define DCC_DEBUG_H#include <DCC_Probe.h>class DCC_Debug : public DCC_Probe { public : DCC_Debug( ); void begin( ); void DCC_Decode( ); private : byte _len; byte _opcode, _operand1, _operand2,_operand3; };#endif