Skip to content

Commit

Permalink
Added auto-configure for Uno/Mega as well as Motor Shield Type
Browse files Browse the repository at this point in the history
* board type detected at compile-time
* macro to select type of motor shield (Arduino or Pololu MC32926
* new <e> command that clears EEPROM
* safety check added to reject throttle command with nReg out-of-bounds
(<1 or >MaxNumRegs)
  • Loading branch information
DccPlusPlus committed Nov 22, 2015
1 parent a9cf8bc commit 2ddd75d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DCCpp_Uno/Accessories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
Once all turnouts have been properly defined, use the <E> command to store their definitions to EEPROM.
If you later make edits/additions/deletions to the turnout definitions, you must invoke the <E> 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 <e> command.
To "throw" turnouts that have been defined use:
Expand Down
57 changes: 47 additions & 10 deletions DCCpp_Uno/DCCpp_Uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,68 @@ 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

#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
Expand Down
11 changes: 11 additions & 0 deletions DCCpp_Uno/EEStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions DCCpp_Uno/EEStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct EEStore{
static int pointer();
static void advance(int);
static void store();
static void clear();
};

#endif
Expand Down
3 changes: 3 additions & 0 deletions DCCpp_Uno/PacketRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion DCCpp_Uno/Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
Once all sensors have been properly defined, use the <E> command to store their definitions to EEPROM.
If you later make edits/additions/deletions to the sensor definitions, you must invoke the <E> 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 <e> 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:
Expand Down
21 changes: 18 additions & 3 deletions DCCpp_Uno/SerialCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,10 @@ void SerialCommand::parse(char *com){
Serial.print(" 0>");
}
}
Serial.print("<iDCC++ BASE STATION v");
Serial.print(BASE_STATION_VERSION);
Serial.print("<iDCC++ BASE STATION FOR ARDUINO ");
Serial.print(ARDUINO_TYPE);
Serial.print(" / ");
Serial.print(MOTOR_SHIELD_NAME);
Serial.print(": BUILD ");
Serial.print(__DATE__);
Serial.print(" ");
Expand All @@ -334,7 +336,7 @@ void SerialCommand::parse(char *com){

case 'E': // <E>
/*
* stores settings for Turnouts in EEPROM
* stores settings for turnouts and sensors EEPROM
*
* returns: <e nTurnouts nSensors>
*/
Expand All @@ -347,6 +349,19 @@ void SerialCommand::parse(char *com){
Serial.print(">");
break;

/***** CLEAR SETTINGS IN EEPROM ****/

case 'e': // <e>
/*
* clears settings for Turnouts in EEPROM
*
* returns: <O>
*/

EEStore::clear();
Serial.print("<O>");
break;

/***** PRINT CARRIAGE RETURN IN SERIAL MONITOR WINDOW ****/

case ' ': // < >
Expand Down

0 comments on commit 2ddd75d

Please sign in to comment.