1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-23 08:06:13 +01:00

Merge branch 'feature/config' of https://github.com/DCC-EX/CommandStation-EX into feature/config

This commit is contained in:
Asbelos 2020-09-26 08:10:55 +01:00
commit f76fb8d6c5
8 changed files with 731 additions and 613 deletions

View File

@ -29,16 +29,23 @@
int ramLowWatermark = 32767; // This figure gets overwritten dynamically in loop()
#endif
#if defined(ARDUINO_ARCH_MEGAAVR)
#include <Arduino.h>
#endif
////////////////////////////////////////////////////////////////
//
// Enables an I2C 2x24 or 4x24 LCD Screen
#ifdef ENABLE_LCD
#if ENABLE_LCD
bool lcdEnabled = false;
#if defined(LIB_TYPE_PCF8574)
LiquidCrystal_PCF8574 lcdDisplay(LCD_ADDRESS);
#elif defined(LIB_TYPE_I2C)
LiquidCrystal_I2C lcdDisplay = LiquidCrystal_I2C(LCD_ADDRESS, LCD_COLUMNS, LCD_LINES);
#endif
#if defined(LIB_TYPE_PCF8574)
LiquidCrystal_PCF8574 lcdDisplay(LCD_ADDRESS);
#elif defined(LIB_TYPE_I2C)
LiquidCrystal_I2C lcdDisplay = LiquidCrystal_I2C(LCD_ADDRESS, LCD_COLUMNS, LCD_LINES);
#endif
#endif
// this code is here to demonstrate use of the DCC API and other techniques
@ -109,15 +116,16 @@ DCCEXParser serialParser;
void setup()
{
////////////////////////////////////////////
//
// More display stuff. Need to put this in a .h file and make
// it a class
#ifdef ENABLE_LCD
////////////////////////////////////////////
//
// More display stuff. Need to put this in a .h file and make
// it a class
#if ENABLE_LCD
Wire.begin();
// Check that we can find the LCD by its address before attempting to use it.
Wire.beginTransmission(LCD_ADDRESS);
if(Wire.endTransmission() == 0) {
if (Wire.endTransmission() == 0)
{
lcdEnabled = true;
lcdDisplay.begin(LCD_COLUMNS, LCD_LINES);
lcdDisplay.setBacklight(255);
@ -126,15 +134,15 @@ void setup()
lcdDisplay.print("DCC++ EX v");
lcdDisplay.print(VERSION);
lcdDisplay.setCursor(0, 1);
#if COMM_INTERFACE >= 1
#if COMM_INTERFACE >= 1
lcdDisplay.print("IP: PENDING");
#else
#else
lcdDisplay.print("SERIAL: READY");
#endif
#if LCD_LINES > 2
lcdDisplay.setCursor(0, 3);
lcdDisplay.print("TRACK POWER: OFF");
#endif
#endif
#if LCD_LINES > 2
lcdDisplay.setCursor(0, 3);
lcdDisplay.print("TRACK POWER: OFF");
#endif
}
#endif

179
DCC.h
View File

@ -24,89 +24,91 @@
typedef void (*ACK_CALLBACK)(int result);
enum ackOp { // Program opcodes for the ack Manager
BASELINE, // ensure enough resets sent before starting and obtain baseline current
W0,W1, // issue write bit (0..1) packet
WB, // issue write byte packet
VB, // Issue validate Byte packet
V0, // Issue validate bit=0 packet
V1, // issue validate bit=1 packlet
WACK, // wait for ack (or absence of ack)
ITC1, // If True Callback(1) (if prevous WACK got an ACK)
ITC0, // If True callback(0);
ITCB, // If True callback(byte)
NAKFAIL, // if false callback(-1)
FAIL, // callback(-1)
STARTMERGE, // Clear bit and byte settings ready for merge pass
MERGE, // Merge previous wack response with byte value and decrement bit number (use for readimng CV bytes)
SETBIT, // sets bit number to next prog byte
SETCV, // sets cv number to next prog byte
STASHLOCOID, // keeps current byte value for later
COMBINELOCOID, // combines current value with stashed value and returns it
ITSKIP, // skip to SKIPTARGET if ack true
SKIPTARGET=0xFF // jump to target
enum ackOp
{ // Program opcodes for the ack Manager
BASELINE, // ensure enough resets sent before starting and obtain baseline current
W0,
W1, // issue write bit (0..1) packet
WB, // issue write byte packet
VB, // Issue validate Byte packet
V0, // Issue validate bit=0 packet
V1, // issue validate bit=1 packlet
WACK, // wait for ack (or absence of ack)
ITC1, // If True Callback(1) (if prevous WACK got an ACK)
ITC0, // If True callback(0);
ITCB, // If True callback(byte)
NAKFAIL, // if false callback(-1)
FAIL, // callback(-1)
STARTMERGE, // Clear bit and byte settings ready for merge pass
MERGE, // Merge previous wack response with byte value and decrement bit number (use for readimng CV bytes)
SETBIT, // sets bit number to next prog byte
SETCV, // sets cv number to next prog byte
STASHLOCOID, // keeps current byte value for later
COMBINELOCOID, // combines current value with stashed value and returns it
ITSKIP, // skip to SKIPTARGET if ack true
SKIPTARGET = 0xFF // jump to target
};
// Allocations with memory implications..!
// Base system takes approx 900 bytes + 8 per loco. Turnouts, Sensors etc are dynamically created
#ifdef ARDUINO_AVR_UNO
const byte MAX_LOCOS=20;
#else
const byte MAX_LOCOS=50;
#endif
#ifdef ARDUINO_AVR_UNO
const byte MAX_LOCOS = 20;
#else
const byte MAX_LOCOS = 50;
#endif
class DCC {
public:
static void begin(const __FlashStringHelper* motorShieldName, MotorDriver * mainDriver, MotorDriver * progDriver, byte timerNumber=1);
class DCC
{
public:
static void begin(const __FlashStringHelper *motorShieldName, MotorDriver *mainDriver, MotorDriver *progDriver, byte timerNumber = 1);
static void loop();
// Public DCC API functions
static void setThrottle( uint16_t cab, uint8_t tSpeed, bool tDirection);
static void setThrottle(uint16_t cab, uint8_t tSpeed, bool tDirection);
static uint8_t getThrottleSpeed(int cab);
static bool getThrottleDirection(int cab);
static void writeCVByteMain(int cab, int cv, byte bValue);
static void writeCVBitMain(int cab, int cv, byte bNum, bool bValue);
static void setFunction( int cab, byte fByte, byte eByte);
static void setFn( int cab, byte functionNumber, bool on);
static int changeFn( int cab, byte functionNumber, bool pressed);
static void updateGroupflags(byte & flags, int functionNumber);
static void setAccessory(int aAdd, byte aNum, bool activate) ;
static bool writeTextPacket( byte *b, int nBytes);
static void setFunction(int cab, byte fByte, byte eByte);
static void setFn(int cab, byte functionNumber, bool on);
static int changeFn(int cab, byte functionNumber, bool pressed);
static void updateGroupflags(byte &flags, int functionNumber);
static void setAccessory(int aAdd, byte aNum, bool activate);
static bool writeTextPacket(byte *b, int nBytes);
static void setProgTrackSyncMain(bool on); // when true, prog track becomes driveable
// ACKable progtrack calls bitresults callback 0,0 or -1, cv returns value or -1
static void readCV(int cv, ACK_CALLBACK callback, bool blocking=false);
static void readCVBit(int cv, byte bitNum, ACK_CALLBACK callback, bool blocking=false); // -1 for error
static void writeCVByte(int cv, byte byteValue, ACK_CALLBACK callback, bool blocking=false) ;
static void writeCVBit(int cv, byte bitNum, bool bitValue, ACK_CALLBACK callback, bool blocking=false);
static void verifyCVByte(int cv, byte byteValue, ACK_CALLBACK callback, bool blocking=false) ;
static void verifyCVBit(int cv, byte bitNum, bool bitValue, ACK_CALLBACK callback, bool blocking=false);
static void readCV(int cv, ACK_CALLBACK callback, bool blocking = false);
static void readCVBit(int cv, byte bitNum, ACK_CALLBACK callback, bool blocking = false); // -1 for error
static void writeCVByte(int cv, byte byteValue, ACK_CALLBACK callback, bool blocking = false);
static void writeCVBit(int cv, byte bitNum, bool bitValue, ACK_CALLBACK callback, bool blocking = false);
static void verifyCVByte(int cv, byte byteValue, ACK_CALLBACK callback, bool blocking = false);
static void verifyCVBit(int cv, byte bitNum, bool bitValue, ACK_CALLBACK callback, bool blocking = false);
static void getLocoId(ACK_CALLBACK callback, bool blocking=false);
static void getLocoId(ACK_CALLBACK callback, bool blocking = false);
// Enhanced API functions
static void forgetLoco(int cab); // removes any speed reminders for this loco
static void forgetAllLocos(); // removes all speed reminders
static void displayCabList(Print * stream);
static void forgetLoco(int cab); // removes any speed reminders for this loco
static void forgetAllLocos(); // removes all speed reminders
static void displayCabList(Print *stream);
static __FlashStringHelper* getMotorShieldName();
static __FlashStringHelper *getMotorShieldName();
private:
struct LOCO {
int loco;
byte speedCode;
byte groupFlags;
unsigned long functions;
struct LOCO
{
int loco;
byte speedCode;
byte groupFlags;
unsigned long functions;
};
static byte loopStatus;
static void setThrottle2( uint16_t cab, uint8_t speedCode);
static void setThrottle2(uint16_t cab, uint8_t speedCode);
static void updateLocoReminder(int loco, byte speedCode);
static void setFunctionInternal( int cab, byte fByte, byte eByte);
static void setFunctionInternal(int cab, byte fByte, byte eByte);
static bool issueReminder(int reg);
static int nextLoco;
static __FlashStringHelper* shieldName;
static __FlashStringHelper *shieldName;
static LOCO speedTable[MAX_LOCOS];
static byte cv1(byte opcode, int cv);
@ -115,58 +117,57 @@ private:
static void issueReminders();
static void callback(int value);
// ACK MANAGER
static ackOp const * ackManagerProg;
static byte ackManagerByte;
static byte ackManagerBitNum;
static int ackManagerCv;
static byte ackManagerStash;
// ACK MANAGER
static ackOp const *ackManagerProg;
static byte ackManagerByte;
static byte ackManagerBitNum;
static int ackManagerCv;
static byte ackManagerStash;
static bool ackReceived;
static ACK_CALLBACK ackManagerCallback;
static ACK_CALLBACK ackManagerCallback;
static void ackManagerSetup(int cv, byte bitNumOrbyteValue, ackOp const program[], ACK_CALLBACK callback, bool blocking);
static void ackManagerLoop(bool blocking);
static bool checkResets(bool blocking, uint8_t numResets);
static const int PROG_REPEATS=8; // repeats of programming commands (some decoders need at least 8 to be reliable)
static const int PROG_REPEATS = 8; // repeats of programming commands (some decoders need at least 8 to be reliable)
// NMRA codes #
static const byte SET_SPEED=0x3f;
static const byte SET_SPEED = 0x3f;
static const byte WRITE_BYTE_MAIN = 0xEC;
static const byte WRITE_BIT_MAIN = 0xE8;
static const byte WRITE_BYTE = 0x7C;
static const byte VERIFY_BYTE= 0x74;
static const byte BIT_MANIPULATE=0x78;
static const byte WRITE_BIT=0xF0;
static const byte VERIFY_BIT=0xE0;
static const byte BIT_ON=0x08;
static const byte BIT_OFF=0x00;
static const byte VERIFY_BYTE = 0x74;
static const byte BIT_MANIPULATE = 0x78;
static const byte WRITE_BIT = 0xF0;
static const byte VERIFY_BIT = 0xE0;
static const byte BIT_ON = 0x08;
static const byte BIT_OFF = 0x00;
};
#ifdef ARDUINO_AVR_MEGA // is using Mega 1280, define as Mega 2560 (pinouts and functionality are identical)
#define ARDUINO_AVR_MEGA2560
#ifdef ARDUINO_AVR_MEGA // is using Mega 1280, define as Mega 2560 (pinouts and functionality are identical)
#define ARDUINO_AVR_MEGA2560
#endif
#if defined(ARDUINO_AVR_UNO)
#define ARDUINO_TYPE "UNO"
#define ARDUINO_TYPE "UNO"
#elif defined(ARDUINO_AVR_NANO)
#define ARDUINO_TYPE "NANO"
#define ARDUINO_TYPE "NANO"
#elif defined(ARDUINO_AVR_MEGA2560)
#define ARDUINO_TYPE "MEGA"
#define ARDUINO_TYPE "MEGA"
#elif defined(ARDUINO_ARCH_MEGAAVR)
#define ARDUINO_TYPE "UNOWIFIR2"
#else
#error CANNOT COMPILE - DCC++ EX ONLY WORKS WITH AN ARDUINO UNO, NANO 328, OR ARDUINO MEGA 1280/2560
#error CANNOT COMPILE - DCC++ EX ONLY WORKS WITH AN ARDUINO UNO, NANO 328, OR ARDUINO MEGA 1280/2560
#endif
#ifdef ENABLE_LCD
#if ENABLE_LCD
#include <Wire.h>
#if defined(LIB_TYPE_PCF8574)
#include <LiquidCrystal_PCF8574.h>
extern LiquidCrystal_PCF8574 lcdDisplay;
#elif defined(LIB_TYPE_I2C)
#include <LiquidCrystal_I2C.h>
extern LiquidCrystal_I2C lcdDisplay;
#endif
#if defined(LIB_TYPE_PCF8574)
#include <LiquidCrystal_PCF8574.h>
extern LiquidCrystal_PCF8574 lcdDisplay;
#elif defined(LIB_TYPE_I2C)
#include <LiquidCrystal_I2C.h>
extern LiquidCrystal_I2C lcdDisplay;
#endif
extern bool lcdEnabled;
#endif

File diff suppressed because it is too large Load Diff

View File

@ -24,34 +24,43 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#define MemStream_h
#include <inttypes.h>
#if defined(ARDUINO_ARCH_MEGAAVR)
#include <Arduino.h>
#else
#include <Stream.h>
#endif
#include <avr/pgmspace.h>
class MemStream : public Stream
{
private:
uint8_t * _buffer;
const uint16_t _len;
bool _buffer_overflow;
uint16_t _pos_read;
uint16_t _pos_write;
bool _allowWrite;
uint8_t *_buffer;
const uint16_t _len;
bool _buffer_overflow;
uint16_t _pos_read;
uint16_t _pos_write;
bool _allowWrite;
public:
// public methods
MemStream(uint8_t *buffer, const uint16_t len, uint16_t content_len = 0, bool allowWrite=true);
MemStream(uint8_t *buffer, const uint16_t len, uint16_t content_len = 0, bool allowWrite = true);
~MemStream() {}
operator const uint8_t *() const { return _buffer; }
operator const char *() const { return (const char*)_buffer; }
operator const char *() const { return (const char *)_buffer; }
uint16_t current_length() const { return _pos_write; }
bool listen() { return true; }
void end() {}
bool isListening() { return true; }
bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
bool overflow()
{
bool ret = _buffer_overflow;
_buffer_overflow = false;
return ret;
}
int peek();
virtual size_t write(uint8_t byte);

View File

@ -1,5 +1,8 @@
#ifndef MotorDrivers_h
#define MotorDrivers_h
#if defined(ARDUINO_ARCH_MEGAAVR)
#include <Arduino.h>
#endif
// *** PLEASE NOTE *** THIS FILE IS **NOT** INTENDED TO BE EDITED WHEN CONFIGURING A SYSTEM.
// It will be overwritten if the library is updated.
@ -9,35 +12,34 @@
// A custom hardware setup will require your sketch to create MotorDriver instances
// similar to those defined here, WITHOUT editing this file.
const byte UNUSED_PIN = 255;
// MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, byte brake_pin, byte current_pin,
// float senseFactor, unsigned int tripMilliamps, byte faultPin);
// Arduino standard Motor Shield
#define STANDARD_MOTOR_SHIELD F("STANDARD_MOTOR_SHIELD"), \
new MotorDriver(3 , 12, UNUSED_PIN, UNUSED_PIN, A0, 2.99, 2000, UNUSED_PIN), \
new MotorDriver(11, 13, UNUSED_PIN, UNUSED_PIN, A1, 2.99, 250 , UNUSED_PIN)
#define STANDARD_MOTOR_SHIELD F("STANDARD_MOTOR_SHIELD"), \
new MotorDriver(3, 12, UNUSED_PIN, UNUSED_PIN, A0, 2.99, 2000, UNUSED_PIN), \
new MotorDriver(11, 13, UNUSED_PIN, UNUSED_PIN, A1, 2.99, TRIP_CURRENT_PROG, UNUSED_PIN)
// Pololu Motor Shield
#define POLOLU_MOTOR_SHIELD F("POLOLU_MOTOR_SHIELD"), \
new MotorDriver(4, 7, UNUSED_PIN, 9 , A0, 18, 2000, 12), \
new MotorDriver(2, 8, UNUSED_PIN, 10, A1, 18, 250 , UNUSED_PIN)
#define POLOLU_MOTOR_SHIELD F("POLOLU_MOTOR_SHIELD"), \
new MotorDriver(4, 7, UNUSED_PIN, 9, A0, 18, 2000, 12), \
new MotorDriver(2, 8, UNUSED_PIN, 10, A1, 18, TRIP_CURRENT_PROG, UNUSED_PIN)
// Firebox Mk1
#define FIREBOX_MK1 F("FIREBOX_MK1"), \
new MotorDriver(3, 6, 7, UNUSED_PIN, A5, 9.766, 5500, UNUSED_PIN), \
new MotorDriver(4, 8, 9, UNUSED_PIN, A1, 5.00, 250 , UNUSED_PIN)
#define FIREBOX_MK1 F("FIREBOX_MK1"), \
new MotorDriver(3, 6, 7, UNUSED_PIN, A5, 9.766, 5500, UNUSED_PIN), \
new MotorDriver(4, 8, 9, UNUSED_PIN, A1, 5.00, TRIP_CURRENT_PROG, UNUSED_PIN)
// Firebox Mk1S
#define FIREBOX_MK1S F("FIREBOX_MK1A"), \
new MotorDriver(24, 21, 22, 25, 23, 9.766, 5500, UNUSED_PIN), \
new MotorDriver(30, 27, 28, 31, 29, 5.00, 250 , UNUSED_PIN)
#define FIREBOX_MK1S F("FIREBOX_MK1A"), \
new MotorDriver(24, 21, 22, 25, 23, 9.766, 5500, UNUSED_PIN), \
new MotorDriver(30, 27, 28, 31, 29, 5.00, TRIP_CURRENT_PROG, UNUSED_PIN)
// FunduMoto Motor Shield
#define FUNDUMOTO_SHIELD F("FUNDUMOTO_SHIELD"), \
new MotorDriver(10 , 12, UNUSED_PIN, 9, A0, 2.99, 2000, UNUSED_PIN), \
new MotorDriver(11, 13, UNUSED_PIN, UNUSED_PIN, A1, 2.99, 250 , UNUSED_PIN)
#define FUNDUMOTO_SHIELD F("FUNDUMOTO_SHIELD"), \
new MotorDriver(10, 12, UNUSED_PIN, 9, A0, 2.99, 2000, UNUSED_PIN), \
new MotorDriver(11, 13, UNUSED_PIN, UNUSED_PIN, A1, 2.99, TRIP_CURRENT_PROG, UNUSED_PIN)
#endif

View File

@ -85,10 +85,15 @@ ISR(TIMER2_OVF_vect)
#include "ATMEGA4809/Timer.h"
Timer TimerA(0);
Timer TimerA(1);
Timer TimerB(2);
ISR(TCA0_OVF_vect) {
ISR(TIMER1_OVF_vect) {
TimerA.isrCallback();
}
ISR(TIMER2_OVF_vect) {
TimerB.isrCallback();
}
#endif

View File

@ -22,6 +22,8 @@
#include "DIAG.h"
#include "StringFormatter.h"
#include "WiThrottle.h"
const char PROGMEM READY_SEARCH[] = "\r\nready\r\n";
const char PROGMEM OK_SEARCH[] = "\r\nOK\r\n";
const char PROGMEM END_DETAIL_SEARCH[] = "@ 1000";

View File

@ -21,37 +21,39 @@
#define WifiInterface_h
#include "DCCEXParser.h"
#include "MemStream.h"
#include <Arduino.h>
#include <avr/pgmspace.h>
typedef void (*HTTP_CALLBACK)(Print * stream, byte * cmd);
typedef void (*HTTP_CALLBACK)(Print *stream, byte *cmd);
class WifiInterface {
class WifiInterface
{
public:
static bool setup(Stream & setupStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, int port);
static void loop();
static void ATCommand(const byte * command);
static void setHTTPCallback(HTTP_CALLBACK callback);
public:
static bool setup(Stream &setupStream, const __FlashStringHelper *SSSid, const __FlashStringHelper *password,
const __FlashStringHelper *hostname, int port);
static void loop();
static void ATCommand(const byte *command);
static void setHTTPCallback(HTTP_CALLBACK callback);
private:
static Stream * wifiStream;
static DCCEXParser parser;
static bool setup2( const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, int port);
static bool checkForOK(const unsigned int timeout, const char* waitfor, bool echo, bool escapeEcho=true);
static bool isHTTP();
static HTTP_CALLBACK httpCallback;
static bool connected;
static bool closeAfter;
static byte loopstate;
static int datalength;
static int connectionId;
static unsigned long loopTimeoutStart;
static const byte MAX_WIFI_BUFFER=250;
static byte buffer[MAX_WIFI_BUFFER+1];
static MemStream streamer;
private:
static Stream *wifiStream;
static DCCEXParser parser;
static bool setup2(const __FlashStringHelper *SSSid, const __FlashStringHelper *password,
const __FlashStringHelper *hostname, int port);
static bool checkForOK(const unsigned int timeout, const char *waitfor, bool echo, bool escapeEcho = true);
static bool isHTTP();
static HTTP_CALLBACK httpCallback;
static bool connected;
static bool closeAfter;
static byte loopstate;
static int datalength;
static int connectionId;
static unsigned long loopTimeoutStart;
static const byte MAX_WIFI_BUFFER = 250;
static byte buffer[MAX_WIFI_BUFFER + 1];
static MemStream streamer;
};
#endif