16 Commits

Author SHA1 Message Date
Alex Shepherd
4cf529e8c5 bump version to 2.0.11 2022-03-12 21:34:39 +13:00
Alex Shepherd
cea2913e8a Fix example DCCInterface_TurntableControl.ino sketch to work with library changes 2022-03-12 21:30:12 +13:00
Alex Shepherd
ae9afe9c24 added CV_MANUFACTURER_START 2022-02-27 18:35:58 +13:00
Alex Shepherd
afe488d792 bumbump version to 2.0.10 to force update of Arduino Library Manager Indexer 2021-08-30 22:48:44 +12:00
Alex Shepherd
61a730bf82 unified example folder/sketch name that was detected by the arduino-lint utility 2021-08-30 17:55:25 +12:00
Alex Shepherd
b0b27ce0cc bump version to 2.0.9 2021-08-30 14:14:38 +12:00
Alex Shepherd
c3dc28479a corrected Fwd/Rev LED logic in NmraDccMultiFunctionMotorDecoder example
added conditional compilation to use the newer Dcc.pin() method that used digitalPinToInterrupt() to determine the Ext Interrupt Number based on Arduino Pin to reduce confusion
2021-08-30 14:11:54 +12:00
Alex Shepherd
a05c12ce95 tidied up Ardunio Every and Raspberry Pi Pico attachInterrupt() function calls 2021-07-23 21:38:24 +12:00
Alex Shepherd
0a72fc610b reformatted the NmraDcc.h & NmraDcc.cpp files using the astyle rules in the new file: .astyle as per the PR from David Zuhn (davidzuhn) 2021-05-09 16:56:08 +12:00
Alex Shepherd
ad5ce96253 after making a mess of a PR from davidzuhn and not having the git skills to fix it, I've resorted to simply copying the desired files from his PR.
I've excluded his reformatted NmraDcc.h NmraDcc.cpp because there were too many merge conflicts now, so I'll reapply his formatting rules in another commit
2021-05-09 16:51:08 +12:00
Alex Shepherd
1e81b95044 removed incorrect comment about notifyDccAccTurnoutOutput() callback function 2021-05-02 23:06:59 +12:00
Alex Shepherd
84fdf45dce added support for the Arduino AVR Nano Every 2021-05-02 23:02:37 +12:00
Jueff
037070b899 Add support for Raspberry Pico (#56)
* fix ESP32 bug on interrupt reinitialisation

* - add first version of Raspberry Pico support

* - add first version of Raspberry Pico support

* Fix problem with missing type PinStatus
merge changes from master project

* fix encoding issue
2021-04-26 23:33:17 +12:00
Fred
eb15c25491 Update NmraDcc.h (#53)
Fix notifyDccAccTurnoutOutput() comments to show this is used when CV29_OUTPUT_ADDRESS_MODE IS set. Add emphasis to this and notifyDccAccTurnoutBoard() similar routine.
2021-04-26 23:32:58 +12:00
Alex Shepherd
681b362811 Added MACROs CALC_MULTIFUNCTION_EXTENDED_ADDRESS_MSB and CALC_MULTIFUNCTION_EXTENDED_ADDRESS_LSB to correctly compute the DCC Address for CV storage
Corrected bug:  ESP32 version stops working when loosing interupts or signal is bad (#48)
bumped version to 2.0.7
2021-04-04 14:01:35 +12:00
Geoff Bunza
f5d7e9b8c3 Gbidec (#44)
* Create IDEC

* Delete IDEC

* Add files via upload

* Add files via upload
2020-11-20 12:10:19 +13:00
16 changed files with 2727 additions and 2500 deletions

55
.astylerc Normal file
View File

@@ -0,0 +1,55 @@
# http://astyle.sourceforge.net/astyle.html#_style=allman
# use the allman style for braces
--style=allman
# http://astyle.sourceforge.net/astyle.html#_lineend
# newlines for all line endings
--lineend=linux
# http://astyle.sourceforge.net/astyle.html#_indent=spaces
# 4 space indents
--indent=spaces=4
# http://astyle.sourceforge.net/astyle.html#_add-braces
# don't leave any unbraced one-line blocks by changing:
# if (thing)
# statement;
#
# to
# if (thing)
# {
# statement;
# }
#
# --add-braces
# http://astyle.sourceforge.net/astyle.html#_indent-preproc-cond
# line up #if/#endif with the code
#
# statement;
# #if SOMETHING
# statement;
# #endif
#
--indent-preproc-cond
# http://astyle.sourceforge.net/astyle.html#_indent-preproc-block
# indent top level preprocessor block contents
#
# #ifdef __ARDUINO__
# #include "Arduino.h"
# #endif
#
--indent-preproc-block
# normalize spacing around parenthese
# http://astyle.sourceforge.net/astyle.html#_unpad-paren
# first, remove all whitespace on either side of a paren
--unpad-paren
# http://astyle.sourceforge.net/astyle.html#_pad-paren-out
# then add a space on the "outer" side of a paren
--pad-paren-out
# http://astyle.sourceforge.net/astyle.html#_max-code-length
# we've got big monitors these days....
--max-code-length=180

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
.development
*.zip
*.orig
*~

File diff suppressed because it is too large Load Diff

1505
NmraDcc.h

File diff suppressed because it is too large Load Diff

View File

@@ -10,3 +10,14 @@ The library currently supports the AVR ATTiny84/85 & ATMega88/168/328/32u4 and T
extern void notifyDccAccState( uint16_t Addr, uint16_t BoardAddr, uint8_t OutputAddr, uint8_t State )
extern void notifyDccSigState( uint16_t Addr, uint8_t OutputIndex, uint8_t State)
Developers:
Use of the supplied git pre-commit hook is encouraged. This will require installation of the 'astyle' package for formatting source file.
See http://astyle.sourceforge.net for details on this package.
On Linux or Mac development machines, run the following command after you clone the repository:
ln -s support/pre-commit .git/hooks/pre-commit
Reformatting the source code to the preferred style is easy using astyle. Just run 'astyle --options=.astylerc NmraDcc.h NmraDcc.cpp'

View File

@@ -19,6 +19,9 @@
#include <AccelStepper.h>
#include <NmraDcc.h>
// Define the Arduino input Pin number for the DCC Signal
#define DCC_PIN 2
// The lines below define the pins used to connect to the A4988 driver module
#define A4988_STEP_PIN 4
#define A4988_DIRECTION_PIN 5
@@ -29,6 +32,9 @@
#define DISABLE_OUTPUTS_IDLE
#endif
// Uncomment the following line to enable Debug Print of DCC Messages
//#define NOTIFY_DCC_MSG
// By default the stepper motor will move the shortest distance to the desired position.
// If you need the turntable to only move in the Positive/Increasing or Negative/Decreasing step numbers to better handle backlash in the mechanism
// Then uncomment the appropriate line below
@@ -119,16 +125,11 @@ uint16_t lastAddr = 0xFFFF ;
uint8_t lastDirection = 0xFF;
int lastStep = 0;
// This function is called whenever a normal DCC Turnout Packet is received
void notifyDccAccTurnoutOutput( uint16_t Addr, uint8_t Direction, uint8_t OutputPower )
{
Serial.print(F("notifyDccAccTurnoutOutput: "));
Serial.print(Addr,DEC) ;
Serial.print(',');
Serial.print(Direction,DEC) ;
Serial.print(',');
Serial.println(OutputPower, HEX) ;
void processTurnoutCommand(uint16_t Addr, uint8_t Direction, uint8_t OutputPower)
{
Serial.print(F("processTurnoutCommand: "));
for (int i = 0; i < MAX_TURNOUT_POSITIONS ; i++)
{
if ((Addr == turnoutPositions[i].dccAddress) && ((Addr != lastAddr) || (Direction != lastDirection)) && OutputPower)
@@ -186,6 +187,22 @@ void notifyDccAccTurnoutOutput( uint16_t Addr, uint8_t Direction, uint8_t Output
break;
}
}
}
// This function is called from the Library whenever a normal DCC Turnout Packet is received
void notifyDccAccTurnoutBoard (uint16_t BoardAddr, uint8_t OutputPair, uint8_t Direction, uint8_t OutputPower)
{
uint16_t Addr = ((BoardAddr - 1) * 4) + OutputPair + 1;
Serial.print(F("notifyDccAccTurnoutBoard: "));
Serial.print(Addr,DEC) ;
Serial.print(',');
Serial.print(Direction,DEC) ;
Serial.print(',');
Serial.println(OutputPower, HEX) ;
processTurnoutCommand(Addr, Direction, OutputPower);
};
#ifdef DISABLE_OUTPUTS_IDLE
@@ -243,11 +260,17 @@ void setupDCCDecoder()
{
Serial.println(F("Setting up DCC Decorder..."));
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
Dcc.pin(0, 2, 1);
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
// Many Arduino Cores now support the digitalPinToInterrupt() function that makes it easier to figure out the
// Interrupt Number for the Arduino Pin number, which reduces confusion.
#ifdef digitalPinToInterrupt
Dcc.pin(DCC_PIN, 0);
#else
Dcc.pin(0, DCC_PIN, 1);
#endif
// Call the main DCC Init function to enable the DCC Receiver
Dcc.init( MAN_ID_DIY, 10, CV29_ACCESSORY_DECODER | CV29_OUTPUT_ADDRESS_MODE, 0 );
Dcc.init( MAN_ID_DIY, 10, CV29_ACCESSORY_DECODER, 0 );
}
void setup()
@@ -287,7 +310,7 @@ void setup()
setupDCCDecoder();
// Fake a DCC Packet to cause the Turntable to move to Position 1
notifyDccAccTurnoutOutput(POSITION_01_DCC_ADDRESS, 1, 1);
processTurnoutCommand(POSITION_01_DCC_ADDRESS, 1, 1);
}
}
@@ -311,3 +334,16 @@ void loop()
}
#endif
}
#ifdef NOTIFY_DCC_MSG
void notifyDccMsg( DCC_MSG * Msg)
{
Serial.print("notifyDccMsg: ") ;
for(uint8_t i = 0; i < Msg->Size; i++)
{
Serial.print(Msg->Data[i], HEX);
Serial.write(' ');
}
Serial.println();
}
#endif

View File

@@ -7,6 +7,9 @@
NmraDcc Dcc ;
DCC_MSG Packet ;
// Define the Arduino input Pin number for the DCC Signal
#define DCC_PIN 2
struct CVPair
{
uint16_t CV;
@@ -15,8 +18,8 @@ struct CVPair
CVPair FactoryDefaultCVs [] =
{
{CV_ACCESSORY_DECODER_ADDRESS_LSB, 1},
{CV_ACCESSORY_DECODER_ADDRESS_MSB, 0},
{CV_ACCESSORY_DECODER_ADDRESS_LSB, DEFAULT_ACCESSORY_DECODER_ADDRESS & 0xFF},
{CV_ACCESSORY_DECODER_ADDRESS_MSB, DEFAULT_ACCESSORY_DECODER_ADDRESS >> 8},
};
uint8_t FactoryDefaultCVIndex = 0;
@@ -98,8 +101,14 @@ void setup()
Serial.println("NMRA DCC Example 1");
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
Dcc.pin(0, 2, 1);
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
// Many Arduino Cores now support the digitalPinToInterrupt() function that makes it easier to figure out the
// Interrupt Number for the Arduino Pin number, which reduces confusion.
#ifdef digitalPinToInterrupt
Dcc.pin(DCC_PIN, 0);
#else
Dcc.pin(0, DCC_PIN, 1);
#endif
// Call the main DCC Init function to enable the DCC Receiver
Dcc.init( MAN_ID_DIY, 10, CV29_ACCESSORY_DECODER | CV29_OUTPUT_ADDRESS_MODE, 0 );

View File

@@ -18,6 +18,9 @@
// Un-Comment the line below to Enable DCC ACK for Service Mode Programming Read CV Capablilty
//#define ENABLE_DCC_ACK 15 // This is A1 on the Iowa Scaled Engineering ARD-DCCSHIELD DCC Shield
// Define the Arduino input Pin number for the DCC Signal
#define DCC_PIN 2
#define NUM_TURNOUTS 8 // Set Number of Turnouts (Pairs of Pins)
#define ACTIVE_OUTPUT_STATE LOW // Set the ACTIVE State of the output to Drive the Turnout motor electronics HIGH or LOW
@@ -40,8 +43,8 @@ struct CVPair
CVPair FactoryDefaultCVs [] =
{
{CV_ACCESSORY_DECODER_ADDRESS_LSB, 1}, // CV 1 Board Address (lower 6 bits)
{CV_ACCESSORY_DECODER_ADDRESS_MSB, 0}, // CV 9 Board Address (Upper 3 bits)
{CV_ACCESSORY_DECODER_ADDRESS_LSB, DEFAULT_ACCESSORY_DECODER_ADDRESS & 0xFF},
{CV_ACCESSORY_DECODER_ADDRESS_MSB, DEFAULT_ACCESSORY_DECODER_ADDRESS >> 8},
{CV_ACCESSORY_DECODER_OUTPUT_PULSE_TIME, 50}, // x 10mS for the output pulse duration
{CV_ACCESSORY_DECODER_CDU_RECHARGE_TIME, 30}, // x 10mS for the CDU recharge delay time
{CV_ACCESSORY_DECODER_ACTIVE_STATE, ACTIVE_OUTPUT_STATE},
@@ -118,8 +121,14 @@ void setup()
{
Serial.begin(115200);
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
Dcc.pin(0, 2, 1);
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
// Many Arduino Cores now support the digitalPinToInterrupt() function that makes it easier to figure out the
// Interrupt Number for the Arduino Pin number, which reduces confusion.
#ifdef digitalPinToInterrupt
Dcc.pin(DCC_PIN, 0);
#else
Dcc.pin(0, DCC_PIN, 1);
#endif
// Call the main DCC Init function to enable the DCC Receiver
Dcc.init( MAN_ID_DIY, DCC_DECODER_VERSION_NUM, CV29_ACCESSORY_DECODER, 0 );

View File

@@ -1,6 +1,7 @@
#include <NmraDcc.h>
#define This_Decoder_Address 3
// Define the Arduino input Pin number for the DCC Signal
#define DCC_PIN 2
struct CVPair
{
@@ -11,11 +12,11 @@ struct CVPair
CVPair FactoryDefaultCVs [] =
{
// The CV Below defines the Short DCC Address
{CV_MULTIFUNCTION_PRIMARY_ADDRESS, This_Decoder_Address},
{CV_MULTIFUNCTION_PRIMARY_ADDRESS, DEFAULT_MULTIFUNCTION_DECODER_ADDRESS},
// These two CVs define the Long DCC Address
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_MSB, 0},
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_LSB, This_Decoder_Address},
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_MSB, CALC_MULTIFUNCTION_EXTENDED_ADDRESS_MSB(DEFAULT_MULTIFUNCTION_DECODER_ADDRESS)},
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_LSB, CALC_MULTIFUNCTION_EXTENDED_ADDRESS_LSB(DEFAULT_MULTIFUNCTION_DECODER_ADDRESS)},
// ONLY uncomment 1 CV_29_CONFIG line below as approprate
// {CV_29_CONFIG, 0}, // Short Address 14 Speed Steps
@@ -169,8 +170,14 @@ void setup()
pinMode( DccAckPin, OUTPUT );
digitalWrite( DccAckPin, LOW );
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
Dcc.pin(0, 2, 0);
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
// Many Arduino Cores now support the digitalPinToInterrupt() function that makes it easier to figure out the
// Interrupt Number for the Arduino Pin number, which reduces confusion.
#ifdef digitalPinToInterrupt
Dcc.pin(DCC_PIN, 0);
#else
Dcc.pin(0, DCC_PIN, 1);
#endif
// Call the main DCC Init function to enable the DCC Receiver
//Dcc.init( MAN_ID_DIY, 10, CV29_ACCESSORY_DECODER | CV29_OUTPUT_ADDRESS_MODE, 0 );

View File

@@ -32,6 +32,7 @@
// This section defines the Arduino UNO Pins to use
#ifdef __AVR_ATmega328P__
// Define the Arduino input Pin number for the DCC Signal
#define DCC_PIN 2
#define LED_PIN_FWD 5
@@ -42,6 +43,7 @@
// This section defines the Arduino ATTiny85 Pins to use
#elif ARDUINO_AVR_ATTINYX5
// Define the Arduino input Pin number for the DCC Signal
#define DCC_PIN 2
#define LED_PIN_FWD 0
@@ -89,8 +91,8 @@ CVPair FactoryDefaultCVs [] =
{CV_VHIGH, 255},
// These two CVs define the Long DCC Address
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_MSB, 0},
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_LSB, DEFAULT_DECODER_ADDRESS},
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_MSB, CALC_MULTIFUNCTION_EXTENDED_ADDRESS_MSB(DEFAULT_DECODER_ADDRESS)},
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_LSB, CALC_MULTIFUNCTION_EXTENDED_ADDRESS_LSB(DEFAULT_DECODER_ADDRESS)},
// ONLY uncomment 1 CV_29_CONFIG line below as approprate
// {CV_29_CONFIG, 0}, // Short Address 14 Speed Steps
@@ -217,8 +219,14 @@ void setup()
pinMode(MOTOR_PWM_PIN, OUTPUT);
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
// Many Arduino Cores now support the digitalPinToInterrupt() function that makes it easier to figure out the
// Interrupt Number for the Arduino Pin number, which reduces confusion.
#ifdef digitalPinToInterrupt
Dcc.pin(DCC_PIN, 0);
#else
Dcc.pin(0, DCC_PIN, 1);
#endif
Dcc.init( MAN_ID_DIY, 10, FLAGS_MY_ADDRESS_ONLY | FLAGS_AUTO_FACTORY_DEFAULT, 0 );
@@ -292,8 +300,8 @@ void loop()
#ifdef DEBUG_FUNCTIONS
Serial.println("LED On");
#endif
digitalWrite(LED_PIN_FWD, newDirection ? LOW : HIGH);
digitalWrite(LED_PIN_REV, newDirection ? HIGH : LOW);
digitalWrite(LED_PIN_FWD, newDirection ? HIGH : LOW);
digitalWrite(LED_PIN_REV, newDirection ? LOW : HIGH);
}
else
{

View File

@@ -26,6 +26,9 @@
// Un-Comment the line below to Enable DCC ACK for Service Mode Programming Read CV Capablilty
#define ENABLE_DCC_ACK 15 // This is A1 on the Iowa Scaled Engineering ARD-DCCSHIELD DCC Shield
// Define the Arduino input Pin number for the DCC Signal
#define DCC_PIN 2
NmraDcc Dcc ;
struct CVPair
@@ -36,8 +39,8 @@ struct CVPair
CVPair FactoryDefaultCVs [] =
{
{CV_ACCESSORY_DECODER_ADDRESS_LSB, 1},
{CV_ACCESSORY_DECODER_ADDRESS_MSB, 0},
{CV_ACCESSORY_DECODER_ADDRESS_LSB, DEFAULT_ACCESSORY_DECODER_ADDRESS & 0xFF},
{CV_ACCESSORY_DECODER_ADDRESS_MSB, DEFAULT_ACCESSORY_DECODER_ADDRESS >> 8},
};
uint8_t FactoryDefaultCVIndex = 0;
@@ -101,8 +104,14 @@ void setup()
Serial.println("NMRA DCC Iowa Scaled Engineering ARD-DCCSHIELD Example");
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
Dcc.pin(0, 2, 1);
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
// Many Arduino Cores now support the digitalPinToInterrupt() function that makes it easier to figure out the
// Interrupt Number for the Arduino Pin number, which reduces confusion.
#ifdef digitalPinToInterrupt
Dcc.pin(DCC_PIN, 0);
#else
Dcc.pin(0, DCC_PIN, 1);
#endif
// Call the main DCC Init function to enable the DCC Receiver
Dcc.init( MAN_ID_DIY, 10, CV29_ACCESSORY_DECODER, 0 );

View File

@@ -106,9 +106,9 @@ CVPair FactoryDefaultCVs [] =
{CV_MANUF, MANUF_ID }, // Manufacturer ID.
// These two CVs define the Long DCC Address
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_MSB, 0}, // Extended address MSB.
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_LSB, DECODER_ADDR}, // Extended address LSB.
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_MSB, CALC_MULTIFUNCTION_EXTENDED_ADDRESS_MSB(DECODER_ADDR)},
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_LSB, CALC_MULTIFUNCTION_EXTENDED_ADDRESS_LSB(DECODER_ADDR)},
{CV_29_CONFIG, CV29_F0_LOCATION}, // Short Address 28/128 Speed Steps
{CV_MANUF_01, VER_MINOR}, // Minor decoder version.
};

View File

@@ -1,9 +1,9 @@
name=NmraDcc
version=2.0.6
author=Alex Shepherd, Wolfgang Kuffer, Geoff Bunza, Martin Pischky, Franz-Peter Müller, Sven (littleyoda), Hans Tanner
maintainer=Alex Shepherd <kiwi64ajs@gmail.com>
sentence=Enables NMRA DCC Communication
paragraph=This library allows you to interface to a NMRA DCC track signal and receive DCC commands. The library has been tested on AVR ATTiny84/85 & ATMega88/168/328/32u4, ESP8266 and Teensy 3.x using the INT0/1 Hardware Interrupt and micros() ONLY and no longer uses Timer0 Compare Match B, which makes it much more portable to other platforms.
category=Communication
url=https://github.com/mrrwa/NmraDcc
architectures=*
name=NmraDcc
version=2.0.11
author=Alex Shepherd, Wolfgang Kuffer, Geoff Bunza, Martin Pischky, Franz-Peter Müller, Sven (littleyoda), Hans Tanner, bugfixes by Jueff
maintainer=Alex Shepherd <kiwi64ajs@gmail.com>
sentence=Enables NMRA DCC Communication
paragraph=This library allows you to interface to a NMRA DCC track signal and receive DCC commands. The library has been tested on AVR ATTiny84/85 & ATMega88/168/328/32u4, ESP8266 and Teensy 3.x using the INT0/1 Hardware Interrupt and micros() ONLY and no longer uses Timer0 Compare Match B, which makes it much more portable to other platforms.
category=Communication
url=https://github.com/mrrwa/NmraDcc
architectures=*

20
support/pre-commit Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/sh
FORMAT_SOURCES="NmraDcc.h NmraDcc.cpp"
error=0
for file in ${FORMAT_SOURCES}
do
output=$(mktemp -t stylecheckXXXXXXX)
astyle --options=.astylerc < $file > $output
if ! cmp -s $file $output
then
echo "Formatting on $file doesn't match expectations"
diff -u $file $output
error=1
fi
[ -f $output ] && rm $output
done
exit $error