Skip to content

Commit

Permalink
Make Raicom code compile on ESP32 by moving out class Pinpair
Browse files Browse the repository at this point in the history
  • Loading branch information
habazut committed Sep 28, 2024
1 parent 77de250 commit cbec612
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 47 deletions.
2 changes: 1 addition & 1 deletion DCCEXParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ bool DCCEXParser::parseC(Print *stream, int16_t params, int16_t p[]) {
DCC::setGlobalSpeedsteps(128);
DIAG(F("128 Speedsteps"));
return true;
#if defined(HAS_ENOUGH_MEMORY) && !defined(ARDUINO_ARCH_UNO)
#if defined(HAS_ENOUGH_MEMORY) && !defined(ARDUINO_ARCH_UNO) && !defined(ARDUINO_ARCH_ESP32)
case "RAILCOM"_hk:
{ // <C RAILCOM ON|OFF|DEBUG >
if (params<2) return false;
Expand Down
4 changes: 2 additions & 2 deletions DCCRMT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void IRAM_ATTR interrupt(rmt_channel_t channel, void *t) {
DCCTimer::updateMinimumFreeMemoryISR(0);
}

RMTChannel::RMTChannel(pinpair pins, bool isMain) {
RMTChannel::RMTChannel(Pinpair pins, bool isMain) {
byte ch;
byte plen;

Expand Down Expand Up @@ -253,7 +253,7 @@ bool RMTChannel::addPin(byte pin, bool inverted) {
if (err != ESP_OK) return false;
return true;
}
bool RMTChannel::addPin(pinpair pins) {
bool RMTChannel::addPin(Pinpair pins) {
return addPin(pins.pin) && addPin(pins.invpin, true);
}
#endif //ESP32
6 changes: 3 additions & 3 deletions DCCRMT.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "driver/rmt.h"
#include "soc/rmt_reg.h"
#include "soc/rmt_struct.h"
#include "MotorDriver.h" // for class pinpair
#include "Pinpair.h" // for class Pinpair

// make calculations easy and set up for microseconds
#define RMT_CLOCK_DIVIDER 80
Expand All @@ -32,9 +32,9 @@

class RMTChannel {
public:
RMTChannel(pinpair pins, bool isMain);
RMTChannel(Pinpair pins, bool isMain);
bool addPin(byte pin, bool inverted=0);
bool addPin(pinpair pins);
bool addPin(Pinpair pins);
void IRAM_ATTR RMTinterrupt();
void RMTprefill();
//int RMTfillData(dccPacket packet);
Expand Down
28 changes: 8 additions & 20 deletions DCCWaveform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,6 @@ volatile bool DCCWaveform::railcomActive=false; // switched on by user
volatile bool DCCWaveform::railcomDebug=false; // switched on by user
volatile bool DCCWaveform::railcomSampleWindow=false; // true during packet transmit

bool DCCWaveform::isRailcom() {
return railcomActive;
}

bool DCCWaveform::isRailcomSampleWindow() {
return railcomSampleWindow;
}
bool DCCWaveform::isRailcomPossible() {
return railcomPossible;
}

void DCCWaveform::setRailcomPossible(bool yes) {
railcomPossible=yes;
if (!yes) setRailcom(false,false);
}

bool DCCWaveform::setRailcom(bool on, bool debug) {
if (on && railcomPossible) {
// TODO check possible
Expand Down Expand Up @@ -266,11 +250,13 @@ void DCCWaveform::promotePendingPacket() {
transmitRepeats = 0;
if (getResets() < 250) sentResetsSincePacket++; // only place to increment (private!)
}
#endif
#endif //not ARDUINO_ARCH_ESP32

#ifdef ARDUINO_ARCH_ESP32
#include "DCCWaveform.h"
#include "TrackManager.h"
#include "DCCACK.h"
#include "Pinpair.h"

DCCWaveform DCCWaveform::mainTrack(PREAMBLE_BITS_MAIN, true);
DCCWaveform DCCWaveform::progTrack(PREAMBLE_BITS_PROG, false);
Expand All @@ -283,7 +269,7 @@ DCCWaveform::DCCWaveform(byte preambleBits, bool isMain) {
}
void DCCWaveform::begin() {
for(const auto& md: TrackManager::getMainDrivers()) {
pinpair p = md->getSignalPin();
Pinpair p = md->getSignalPin();
if(rmtMainChannel) {
//DIAG(F("added pins %d %d to MAIN channel"), p.pin, p.invpin);
rmtMainChannel->addPin(p); // add pin to existing main channel
Expand All @@ -294,7 +280,7 @@ void DCCWaveform::begin() {
}
MotorDriver *md = TrackManager::getProgDriver();
if (md) {
pinpair p = md->getSignalPin();
Pinpair p = md->getSignalPin();
if (rmtProgChannel) {
//DIAG(F("added pins %d %d to PROG channel"), p.pin, p.invpin);
rmtProgChannel->addPin(p); // add pin to existing prog channel
Expand Down Expand Up @@ -352,7 +338,9 @@ void IRAM_ATTR DCCWaveform::loop() {
}

bool DCCWaveform::setRailcom(bool on, bool debug) {
// TODO... ESP32 railcom waveform
// todo
(void)on;
(void)debug;
return false;
}

Expand Down
19 changes: 14 additions & 5 deletions DCCWaveform.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#define DCCWaveform_h
#ifdef ARDUINO_ARCH_ESP32
#include "DCCRMT.h"
#include "TrackManager.h"
//#include "TrackManager.h"
#endif


Expand Down Expand Up @@ -84,10 +84,19 @@ class DCCWaveform {
bool isReminderWindowOpen();
void promotePendingPacket();
static bool setRailcom(bool on, bool debug);
static bool isRailcom();
static bool isRailcomSampleWindow();
static bool isRailcomPossible();
static void setRailcomPossible(bool yes);
inline static bool isRailcom() {
return railcomActive;
};
inline static bool isRailcomSampleWindow() {
return railcomSampleWindow;
};
inline static bool isRailcomPossible() {
return railcomPossible;
};
inline static void setRailcomPossible(bool yes) {
railcomPossible=yes;
if (!yes) setRailcom(false,false);
};
private:
#ifndef ARDUINO_ARCH_ESP32
volatile bool packetPending;
Expand Down
18 changes: 3 additions & 15 deletions MotorDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,9 @@ enum TRACK_MODE : byte {TRACK_MODE_NONE = 1, TRACK_MODE_MAIN = 2, TRACK_MODE_PRO

// Virtualised Motor shield 1-track hardware Interface

#ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h
#define UNUSED_PIN 255 // inside uint8_t
#endif
#include "Pinpair.h" // for class Pinpair
#define MAX_PIN 254

class pinpair {
public:
pinpair(byte p1, byte p2) {
pin = p1;
invpin = p2;
};
byte pin = UNUSED_PIN;
byte invpin = UNUSED_PIN;
};

#if defined(__IMXRT1062__) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32)
typedef uint32_t portreg_t;
#else
Expand Down Expand Up @@ -209,7 +197,7 @@ class MotorDriver {
pinMode(signalPin2, INPUT);
}
};
inline pinpair getSignalPin() { return pinpair(signalPin,signalPin2); };
inline Pinpair getSignalPin() { return Pinpair(signalPin,signalPin2); };
inline int8_t getBrakePinSigned() { return invertBrake ? -brakePin : brakePin; };
void setDCSignal(byte speedByte, uint8_t frequency=0);
void throttleInrush(bool on);
Expand Down Expand Up @@ -285,7 +273,7 @@ class MotorDriver {
else
invertPhase = 0;
#if defined(ARDUINO_ARCH_ESP32)
pinpair p = getSignalPin();
Pinpair p = getSignalPin();
uint32_t *outreg = (uint32_t *)(GPIO_FUNC0_OUT_SEL_CFG_REG + 4*p.pin);
if (invertPhase) // set or clear the invert bit in the gpio out register
*outreg |= ((uint32_t)0x1 << GPIO_FUNC0_OUT_INV_SEL_S);
Expand Down
14 changes: 14 additions & 0 deletions Pinpair.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h
#define UNUSED_PIN 255 // inside uint8_t
#endif
class Pinpair {
public:
Pinpair(byte p1, byte p2) {
pin = p1;
invpin = p2;
};
byte pin = UNUSED_PIN;
byte invpin = UNUSED_PIN;
};

2 changes: 1 addition & 1 deletion TrackManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr

#ifdef ARDUINO_ARCH_ESP32
// remove pin from MUX matrix and turn it off
pinpair p = track[trackToSet]->getSignalPin();
Pinpair p = track[trackToSet]->getSignalPin();
//DIAG(F("Track=%c remove pin %d"),trackToSet+'A', p.pin);
gpio_reset_pin((gpio_num_t)p.pin);
if (p.invpin != UNUSED_PIN) {
Expand Down

0 comments on commit cbec612

Please sign in to comment.