1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-30 11:36:13 +01:00
CommandStation-EX/mySetup.cpp_example.txt
2021-11-11 13:31:59 +00:00

304 lines
14 KiB
Plaintext

// Sample mySetup.cpp file.
//
// To use this file, copy it to mySetup.cpp and uncomment the directives and/or
// edit them to satisfy your requirements.
// Note that if the file has a .cpp extension it WILL be compiled into the build
// and the mySetup() function WILL be invoked.
//
// To prevent this, temporarily rename it to mySetup.txt or similar.
//
// Only the #include directives relating to the devices in use need be included here.
#include "IODevice.h"
#include "Turnouts.h"
#include "Sensors.h"
#include "IO_HCSR04.h"
#include "IO_VL53L0X.h"
#include "DFPlayer.h"
#include "IO_Network.h"
#include "Net_RF24"
#include "Net_ENC28J60"
#include "Net_Ethernet"
// Examples of statically defined HAL directives (alternative to the create() call).
// These have to be outside of the mySetup() function.
//=======================================================================
// The following directive defines a PCA9685 PWM Servo driver module.
//=======================================================================
// The parameters are:
// First Vpin=100
// Number of VPINs=16 (numbered 100-115)
// I2C address of module=0x40
//PCA9685 pwmModule1(100, 16, 0x40);
//=======================================================================
// The following directive defines an MCP23017 16-port I2C GPIO Extender module.
//=======================================================================
// The parameters are:
// First Vpin=196
// Number of VPINs=16 (numbered 196-211)
// I2C address of module=0x22
//MCP23017 gpioModule2(196, 16, 0x22);
// Alternative form, which allows the INT pin of the module to request a scan
// by pulling Arduino pin 40 to ground. Means that the I2C isn't being polled
// all the time, only when a change takes place. Multiple modules' INT pins
// may be connected to the same Arduino pin.
//MCP23017 gpioModule2(196, 16, 0x22, 40);
//=======================================================================
// The following directive defines an MCP23008 8-port I2C GPIO Extender module.
//=======================================================================
// The parameters are:
// First Vpin=300
// Number of VPINs=8 (numbered 300-307)
// I2C address of module=0x22
//MCP23008 gpioModule3(300, 8, 0x22);
//=======================================================================
// The following directive defines a PCF8574 8-port I2C GPIO Extender module.
//=======================================================================
// The parameters are:
// First Vpin=200
// Number of VPINs=8 (numbered 200-207)
// I2C address of module=0x23
//PCF8574 gpioModule4(200, 8, 0x23);
// Alternative form using INT pin (see above)
//PCF8574 gpioModule4(200, 8, 0x23, 40);
//=======================================================================
// The function mySetup() is invoked from CS if it exists within the build.
// It is called just before mysetup.h is executed, so things set up within here can be
// referenced by commands in mySetup.h.
//=======================================================================
void mySetup() {
// Alternative way of creating a module driver, which has to be within the mySetup() function
// The other devices can also be created in this way. The parameter lists for the
// create() function are identical to the parameter lists for the declarations.
//MCP23017::create(196, 16, 0x22);
//=======================================================================
// Creating a Turnout
//=======================================================================
// Parameters: same as <T> command for Servo turnouts
// ID and VPIN are 100, sonar moves between positions 102 and 490 with slow profile.
// Profile may be Instant, Fast, Medium, Slow or Bounce.
//ServoTurnout::create(100, 100, 490, 102, PCA9685::Slow);
//=======================================================================
// DCC Accessory turnout
//=======================================================================
// Parameters: same as <T> command for DCC Accessory turnouts
// ID=3000
// Decoder address=23
// Decoder subaddress = 1
//DCCTurnout::create(3000, 23, 1);
//=======================================================================
// Creating a Sensor
//=======================================================================
// Parameters: As for the <S> command,
// id = 164,
// Vpin = 164 (configured above as pin 0 of an MCP23017)
// Pullup enable = 1 (enabled)
//Sensor::create(164, 164, 1);
//=======================================================================
// Way of creating lots of identical sensors in a range
//=======================================================================
//for (int i=165; i<180; i++)
// Sensor::create(i, i, 1);
//=======================================================================
// The following directive defines an HCSR04 ultrasonic ranging module.
//=======================================================================
// The parameters are:
// Vpin=2000 (only one VPIN per directive)
// Number of VPINs=1
// Arduino pin connected to TRIG=30
// Arduino pin connected to ECHO=31
// Minimum trigger range=20cm (VPIN goes to 1 when <20cm)
// Maximum trigger range=25cm (VPIN goes to 0 when >25cm)
// Note: Multiple devices can be configured by using a different ECHO pin
// for each one. The TRIG pin can be shared between multiple devices.
// Be aware that the 'ping' of one device may be received by another
// device and position them accordingly!
//HCSR04 sonarModule1(2000, 30, 31, 20, 25);
//HCSR04 sonarModule2(2001, 30, 32, 20, 25);
//=======================================================================
// VL53L0X Time-of-Flight range sensor.
//=======================================================================
// The following directive defines a single VL53L0X Time-of-Flight range sensor.
// The parameters are:
// VPIN=5000
// Number of VPINs=1
// I2C address=0x29 (default for this chip)
// Minimum trigger range=200mm (VPIN goes to 1 when <20cm)
// Maximum trigger range=250mm (VPIN goes to 0 when >25cm)
//VL53L0X::create(5000, 1, 0x29, 200, 250);
// For multiple VL53L0X modules, add another parameter which is a VPIN connected to the
// module's XSHUT pin. This allows the modules to be configured, at start,
// with distinct I2C addresses. In this case, the address 0x29 is only used during
// initialisation to configure each device in turn with the desired unique I2C address.
// The examples below have the modules' XSHUT pins connected to the first two pins of
// the first MCP23017 module (164 and 165), but Arduino pins may be used instead.
// The first module here is given I2C address 0x30 and the second is 0x31.
//VL53L0X::create(5000, 1, 0x30, 200, 250, 164);
//VL53L0X::create(5001, 1, 0x31, 200, 250, 165);
//=======================================================================
// Play mp3 files from a Micro-SD card, using a DFPlayer MP3 Module.
//=======================================================================
// Parameters:
// 10000 = first VPIN allocated.
// 10 = number of VPINs allocated.
// Serial1 = name of serial port (usually Serial1 or Serial2).
// With these parameters, up to 10 files may be played on pins 10000-10009.
// Play is started from EX-RAIL with SET(10000) for first mp3 file, SET(10001)
// for second file, etc. Play may also be initiated by writing an analogue
// value to the first pin, e.g. SERVO(10000,23,0) will play the 23rd mp3 file.
// SERVO(10000,23,30) will do the same thing, as well as setting the volume to
// 30 (maximum value).
// Play is stopped by RESET(10000) (or any other allocated VPIN).
// Volume may also be set by writing an analogue value to the second pin for the player,
// e.g. SERVO(10001,30,0) sets volume to maximum (30).
// The EX-RAIL script may check for completion of play by calling WAITFOR(pin), which will only proceed to the
// following line when the player is no longer busy.
// E.g.
// SEQUENCE(1)
// AT(164) // Wait for sensor attached to pin 164 to activate
// SET(10003) // Play fourth MP3 file
// LCD(4, "Playing") // Display message on LCD/OLED
// WAITFOR(10003) // Wait for playing to finish
// LCD(4, " ") // Clear LCD/OLED line
// FOLLOW(1) // Go back to start
// DFPlayer::create(10000, 10, Serial1);
//=======================================================================
// Remote (networked) VPIN Configuration
//=======================================================================
// Define remote pins to be used. The range of remote pins is like a common data area shared
// between all nodes.
// For outputs, a write to a remote VPIN causes a message to be sent to another node, which then performs
// the write operation on the device VPIN that is local to that node.
// For inputs, the state of remote input VPIN is read on the node where it is connected, and then
// sent to other nodes in the system where the state is saved and processed. Updates are sent on change, and
// also periodically if no changes.
//
// Currently, analogue inputs are not enabled for remote access.
//
// Each definition is a triple of remote node, remote pin, type, indexed by relative pin.
// Each pin may be an input whose value is to be read before being transmitted (RPIN_IN),
// an output which is to be triggered when written to (RPIN_OUT) or both (RPIN_INPUT),
// such as a servo or DFPlayer device pin. Unused pins (e.g. spares to reserve contiguous
// pin sequences) should be defined sd VPIN_NONE with 0 as the type.
// Where possible, align the input and inout pins on an offset which is a multiple of 8
// from the start of the remote pins, as in the example below.
//
// There is a limit of 224 in the number of pin states per node that are sent
// over the network (to keep the number of sent messages to 1 x 32 bytes). This restriction
// may be lifted in future.
//
// In the example below with two nodes 30 and 31,
// a turnout object set to operate pin 4004 will operate the servo connected to
// VPIN 100 on node 30;
// a sensor object monitoring pin 4024 will trigger if pin D24 on node
// 31 activates;
// an output object associated with pin 4002 will, when set on, activate
// pin 166 (MCP23017 pin 3) on node 30.
//
// All nodes on the same network should use the same REMOTEPINS setup, and the
// node number of each node should be set to a different number.
//
// REMOTEPINS rpins[] = {
// {30,164,RPIN_IN} , //4000 Node 30, first MCP23017 pin, input
// {30,165,RPIN_IN}, //4001 Node 30, second MCP23017 pin, input
// {30,166,RPIN_OUT}, //4002 Node 30, third MCP23017 pin, output
// {30,166,RPIN_OUT}, //4003 Node 30, fourth MCP23017 pin, output
// {30,100,RPIN_INOUT}, //4004 Node 30, first PCA9685 servo pin
// {30,101,RPIN_INOUT}, //4005 Node 30, second PCA9685 servo pin
// {30,102,RPIN_INOUT}, //4006 Node 30, third PCA9685 servo pin
// {30,103,RPIN_INOUT}, //4007 Node 30, fourth PCA9685 servo pin
// {30,24,RPIN_IN}, //4008 Node 30, Arduino pin D24
// {30,25,RPIN_IN}, //4009 Node 30, Arduino pin D25
// {30,26,RPIN_IN}, //4010 Node 30, Arduino pin D26
// {30,27,RPIN_IN}, //4011 Node 30, Arduino pin D27
// {30,VPIN_NONE,0}, //4012 Node 30, spare
// {30,VPIN_NONE,0}, //4013 Node 30, spare
// {30,VPIN_NONE,0}, //4014 Node 30, spare
// {30,VPIN_NONE,0}, //4015 Node 30, spare
//
// {31,164,RPIN_IN} , //4016 Node 31, first MCP23017 pin, input
// {31,165,RPIN_IN}, //4017 Node 31, second MCP23017 pin, input
// {31,166,RPIN_OUT}, //4018 Node 31, third MCP23017 pin, output
// {31,166,RPIN_OUT}, //4019 Node 31, fourth MCP23017 pin, output
// {31,100,RPIN_INOUT}, //4020 Node 31, first PCA9685 servo pin
// {31,101,RPIN_INOUT}, //4021 Node 31, second PCA9685 servo pin
// {31,102,RPIN_INOUT}, //4022 Node 31, third PCA9685 servo pin
// {31,103,RPIN_INOUT}, //4023 Node 31, fourth PCA9685 servo pin
// {31,24,RPIN_IN}, //4024 Node 31, Arduino pin D24
// {31,25,RPIN_IN}, //4025 Node 31, Arduino pin D25
// {31,26,RPIN_IN}, //4026 Node 31, Arduino pin D26
// {31,27,RPIN_IN}, //4027 Node 31, Arduino pin D27
// {31,VPIN_NONE,0}, //4028 Node 31, spare
// {31,VPIN_NONE,0}, //4029 Node 31, spare
// {31,VPIN_NONE,0}, //4030 Node 31, spare
// {31,VPIN_NONE,0} //4031 Node 31, spare
// };
// Network using RF24 wireless module, with CE on pin 48 and CS on pin 49 and node=30
// Net_RF24 *rf24Driver = new Net_RF24(48, 49);
// Network<Net_RF24>::create(4000, NUMREMOTEPINS(rpins), 30, rpins, rf24Driver);
// Network using ENC28J60 ethernet module, with CS on pin 49 and node=30
// Net_ENC28J60 *encDriver = new Net_ENC28J60(49);
// Network<Net_ENC28J60>::create(4000, NUMREMOTEPINS(rpins), 30, rpins, encDriver);
// Network using Arduino Ethernet library, with CS on default pin (10) and node=30
// This would be the option selected if you already use ethernet for DCC++EX
// commands.
// Net_Ethernet *etherDriver = new Net_Ethernet();
// Network<Net_Ethernet>::create(4000, NUMREMOTEPINS(rpins), 30, rpins, etherDriver);
}