From 2ddd75debfd909e968d97c9dc43855434f5dc0ff Mon Sep 17 00:00:00 2001 From: DccPlusPlus Date: Sat, 21 Nov 2015 22:00:42 -0500 Subject: [PATCH] Added auto-configure for Uno/Mega as well as Motor Shield Type * board type detected at compile-time * macro to select type of motor shield (Arduino or Pololu MC32926 * new command that clears EEPROM * safety check added to reject throttle command with nReg out-of-bounds (<1 or >MaxNumRegs) --- DCCpp_Uno/Accessories.cpp | 2 +- DCCpp_Uno/DCCpp_Uno.h | 57 +++++++++++++++++++++++++++++------- DCCpp_Uno/EEStore.cpp | 11 +++++++ DCCpp_Uno/EEStore.h | 1 + DCCpp_Uno/PacketRegister.cpp | 3 ++ DCCpp_Uno/Sensor.cpp | 2 +- DCCpp_Uno/SerialCommand.cpp | 21 +++++++++++-- 7 files changed, 82 insertions(+), 15 deletions(-) diff --git a/DCCpp_Uno/Accessories.cpp b/DCCpp_Uno/Accessories.cpp index dbe7581..29d5fbb 100644 --- a/DCCpp_Uno/Accessories.cpp +++ b/DCCpp_Uno/Accessories.cpp @@ -40,7 +40,7 @@ where Once all turnouts have been properly defined, use the command to store their definitions to EEPROM. If you later make edits/additions/deletions to the turnout definitions, you must invoke the command if you want those -new definitions updated in the EEPROM. +new definitions updated in the EEPROM. You can also clear everything stored in the EEPROM by invoking the command. To "throw" turnouts that have been defined use: diff --git a/DCCpp_Uno/DCCpp_Uno.h b/DCCpp_Uno/DCCpp_Uno.h index 0b44e5b..aaee3f0 100644 --- a/DCCpp_Uno/DCCpp_Uno.h +++ b/DCCpp_Uno/DCCpp_Uno.h @@ -10,9 +10,37 @@ Part of DCC++ BASE STATION for the Arduino Uno #ifndef DCCpp_Uno_h #define DCCpp_Uno_h -#define BASE_STATION_VERSION "UNO_1.0" +#if defined ARDUINO_AVR_UNO +#define ARDUINO_TYPE "UNO" -// Define the Arduino Pins that control various functions +#define DCC_SIGNAL_PIN_MAIN 10 // Ardunio Uno +#define DCC_SIGNAL_PIN_PROG 5 // Arduino Uno + +#elif defined ARDUINO_AVR_MEGA2560 +#define ARDUINO_TYPE "MEGA" + +#define DCC_SIGNAL_PIN_MAIN 12 // Arduino Mega +#define DCC_SIGNAL_PIN_PROG 4 // Arduino Mega + +#else + +#error CAN'T COMPILE - DCC++ ONLY WORKS WITH AN ARDUINO UNO OR AN ARDUINO MEGA 2560 +#endif + +///////////////////////////////////////////////////////////////////////////////////// +// +// DEFINE MOTOR SHIELD PINS - SET MOTOR_SHIELD_TYPE ACCORDING TO THE FOLLOWING TABLE: +// +// 0 = ARDUINO MOTOR SHIELD (MAX 18V/2A PER CHANNEL) +// 1 = POLOLU MC33926 MOTOR SHIELD (MAX 28V/3A PER CHANNEL) + +#define MOTOR_SHIELD_TYPE 0 + +///////////////////////////////////////////////////////////////////////////////////// + +#if MOTOR_SHIELD_TYPE == 0 + +#define MOTOR_SHIELD_NAME "ARDUINO MOTOR SHIELD" #define SIGNAL_ENABLE_PIN_MAIN 3 #define SIGNAL_ENABLE_PIN_PROG 11 @@ -20,21 +48,30 @@ Part of DCC++ BASE STATION for the Arduino Uno #define CURRENT_MONITOR_PIN_MAIN A0 #define CURRENT_MONITOR_PIN_PROG A1 -#define DCC_SIGNAL_PIN_MAIN 10 // Ardunio Uno -#define DCC_SIGNAL_PIN_PROG 5 // Arduino Uno - #define DIRECTION_MOTOR_CHANNEL_PIN_A 12 #define DIRECTION_MOTOR_CHANNEL_PIN_B 13 -// Define number of MAIN TRACK Registers +#elif MOTOR_SHIELD_TYPE == 1 -#define MAX_MAIN_REGISTERS 12 +#define MOTOR_SHIELD_NAME "POLOLU MC33926 MOTOR SHIELD" + +#define SIGNAL_ENABLE_PIN_MAIN 9 +#define SIGNAL_ENABLE_PIN_PROG 11 + +#define CURRENT_MONITOR_PIN_MAIN A0 +#define CURRENT_MONITOR_PIN_PROG A1 -enum { EE_NTURNOUTS, EE_NSENSORS }; +#define DIRECTION_MOTOR_CHANNEL_PIN_A 7 +#define DIRECTION_MOTOR_CHANNEL_PIN_B 8 -// Define slots for EE_PROM storage of key variables. EE_TURNOUT must be last since it is starting point for a number of turnouts +#else + +#error CAN'T COMPILE - PLEASE SELECT A PROPER MOTOR SHIELD TYPE +#endif -enum { EE_TURNOUT }; +// Define number of MAIN TRACK Registers + +#define MAX_MAIN_REGISTERS 12 // If SHOW_PACKETS is set to 1, then for select main operations track commands that modify an internal DCC packet register, // if printFlag for that command is also set to 1, DCC++ BASE STATION will additionally return the diff --git a/DCCpp_Uno/EEStore.cpp b/DCCpp_Uno/EEStore.cpp index b33e1af..5696caf 100644 --- a/DCCpp_Uno/EEStore.cpp +++ b/DCCpp_Uno/EEStore.cpp @@ -36,6 +36,17 @@ void EEStore::init(){ /////////////////////////////////////////////////////////////////////////////// +void EEStore::clear(){ + + sprintf(eeStore->data.id,EESTORE_ID); // create blank eeStore structure (no turnouts, no sensors) and save it back to EEPROM + eeStore->data.nTurnouts=0; + eeStore->data.nSensors=0; + EEPROM.put(0,eeStore->data); + +} + +/////////////////////////////////////////////////////////////////////////////// + void EEStore::store(){ reset(); Turnout::store(); diff --git a/DCCpp_Uno/EEStore.h b/DCCpp_Uno/EEStore.h index a010f2c..ccc1fca 100644 --- a/DCCpp_Uno/EEStore.h +++ b/DCCpp_Uno/EEStore.h @@ -27,6 +27,7 @@ struct EEStore{ static int pointer(); static void advance(int); static void store(); + static void clear(); }; #endif diff --git a/DCCpp_Uno/PacketRegister.cpp b/DCCpp_Uno/PacketRegister.cpp index f234f99..8aba28f 100644 --- a/DCCpp_Uno/PacketRegister.cpp +++ b/DCCpp_Uno/PacketRegister.cpp @@ -108,6 +108,9 @@ void RegisterList::setThrottle(char *s) volatile{ if(sscanf(s,"%d %d %d %d",&nReg,&cab,&tSpeed,&tDirection)!=4) return; + if(nReg<1 || nReg>maxNumRegs) + return; + if(cab>127) b[nB++]=highByte(cab) | 0xC0; // convert train number into a two-byte address diff --git a/DCCpp_Uno/Sensor.cpp b/DCCpp_Uno/Sensor.cpp index cf21c61..0cd4cab 100644 --- a/DCCpp_Uno/Sensor.cpp +++ b/DCCpp_Uno/Sensor.cpp @@ -42,7 +42,7 @@ where Once all sensors have been properly defined, use the command to store their definitions to EEPROM. If you later make edits/additions/deletions to the sensor definitions, you must invoke the command if you want those -new definitions updated in the EEPROM. +new definitions updated in the EEPROM. You can also clear everything stored in the EEPROM by invoking the command. All sensors defined as per above are repeatedly and sequentially checked within the main loop of this sketch. If a Sensor Pin is found to have transitioned from one state to another, one of the following serial messages are generated: diff --git a/DCCpp_Uno/SerialCommand.cpp b/DCCpp_Uno/SerialCommand.cpp index 047e85c..e4bda5c 100644 --- a/DCCpp_Uno/SerialCommand.cpp +++ b/DCCpp_Uno/SerialCommand.cpp @@ -318,8 +318,10 @@ void SerialCommand::parse(char *com){ Serial.print(" 0>"); } } - Serial.print(" /* - * stores settings for Turnouts in EEPROM + * stores settings for turnouts and sensors EEPROM * * returns: */ @@ -347,6 +349,19 @@ void SerialCommand::parse(char *com){ Serial.print(">"); break; +/***** CLEAR SETTINGS IN EEPROM ****/ + + case 'e': // +/* + * clears settings for Turnouts in EEPROM + * + * returns: +*/ + + EEStore::clear(); + Serial.print(""); + break; + /***** PRINT CARRIAGE RETURN IN SERIAL MONITOR WINDOW ****/ case ' ': // < >