1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-07-28 18:03:45 +02:00

JMRI_SENSOR_NOPULLUP

and associated dump for <S>
This commit is contained in:
Asbelos
2025-07-25 09:29:57 +01:00
parent d84e354142
commit c699eb3492
7 changed files with 68 additions and 28 deletions

View File

@@ -1135,12 +1135,7 @@ bool DCCEXParser::parseS(Print *stream, int16_t params, int16_t p[])
return true; return true;
case 0: // <S> list sensor definitions case 0: // <S> list sensor definitions
if (Sensor::firstSensor == NULL) Sensor::dumpAll(stream);
return false;
for (Sensor *tt = Sensor::firstSensor; tt != NULL; tt = tt->nextSensor)
{
StringFormatter::send(stream, F("<Q %d %d %d>\n"), tt->data.snum, tt->data.pin, tt->data.pullUp);
}
return true; return true;
default: // invalid number of arguments default: // invalid number of arguments

View File

@@ -98,6 +98,7 @@
#undef IFBITMAP_ANY #undef IFBITMAP_ANY
#undef INVERT_DIRECTION #undef INVERT_DIRECTION
#undef JMRI_SENSOR #undef JMRI_SENSOR
#undef JMRI_SENSOR_NOPULLUP
#undef JOIN #undef JOIN
#undef KILLALL #undef KILLALL
#undef LATCH #undef LATCH
@@ -684,11 +685,18 @@
#define INVERT_DIRECTION #define INVERT_DIRECTION
/** /**
* @def JMRI_SENSOR(vpin,count...) * @def JMRI_SENSOR(vpin,count...)
* @brief Defines multiple JMRI <s> type sensor feedback definitions each with id matching vpin * @brief Defines multiple JMRI <s> type sensor feedback definitions each with id matching vpin and INPUT_PULLUP
* @param vpin first vpin number * @param vpin first vpin number
* @param count... Number of consecutine VPINS for which to create JMRI sensor feedbacks. Default 1. * @param count... Number of consecutine VPINS for which to create JMRI sensor feedbacks. Default 1.
*/ */
#define JMRI_SENSOR(vpin,count...) #define JMRI_SENSOR(vpin,count...)
/**
* @def JMRI_SENSOR_NOPULLUP(vpin,count...)
* @brief Defines multiple JMRI <s> type sensor feedback definitions each with id matching vpin
* @param vpin first vpin number
* @param count... Number of consecutine VPINS for which to create JMRI sensor feedbacks. Default 1.
*/
#define JMRI_SENSOR_NOPULLUP(vpin,count...)
/** /**
* @def JOIN * @def JOIN
* @brief Switches PROG track to receive MAIN track DCC packets. (Drive on PROG track) * @brief Switches PROG track to receive MAIN track DCC packets. (Drive on PROG track)

View File

@@ -117,12 +117,19 @@ bool exrailHalSetup1() {
{ \ { \
const int npins=#count[0]? count+0:1; \ const int npins=#count[0]? count+0:1; \
static byte state_map[(npins+7)/8]; \ static byte state_map[(npins+7)/8]; \
SensorGroup::doSensorGroup(vpin,npins,state_map,action,&USB_SERIAL); \ SensorGroup::doSensorGroup(vpin,npins,state_map,action,&USB_SERIAL,true); \
}
#undef JMRI_SENSOR_NOPULLUP
#define JMRI_SENSOR_NOPULLUP(vpin,count...) \
{ \
const int npins=#count[0]? count+0:1; \
static byte state_map[(npins+7)/8]; \
SensorGroup::doSensorGroup(vpin,npins,state_map,action,&USB_SERIAL,false); \
} }
void SensorGroup::doExrailSensorGroup(GroupProcess action, Print * stream) { void SensorGroup::doExrailSensorGroup(GroupProcess action, Print * stream) {
(void) action; // suppress unused warning (void) action; // suppress unused warnings if no groups
(void) stream; // suppress unused warning (void) stream;
#include "myAutomation.h" #include "myAutomation.h"
} }
@@ -135,7 +142,7 @@ void SensorGroup::doExrailSensorGroup(GroupProcess action, Print * stream) {
void exrailHalSetup2() { void exrailHalSetup2() {
#include "myAutomation.h" #include "myAutomation.h"
// pullup any group sensors // pullup any group sensors
SensorGroup::pullupAll(); SensorGroup::prepareAll();
} }
// Pass 1c detect compile time featurtes // Pass 1c detect compile time featurtes
@@ -513,6 +520,7 @@ int RMFT2::onLCCLookup[RMFT2::countLCCLookup];
#define IFBITMAP_ANY(vpin,mask) OPCODE_IFBITMAP_ANY,V(vpin),OPCODE_PAD,V(mask), #define IFBITMAP_ANY(vpin,mask) OPCODE_IFBITMAP_ANY,V(vpin),OPCODE_PAD,V(mask),
#define INVERT_DIRECTION OPCODE_INVERT_DIRECTION,0,0, #define INVERT_DIRECTION OPCODE_INVERT_DIRECTION,0,0,
#define JMRI_SENSOR(vpin,count...) #define JMRI_SENSOR(vpin,count...)
#define JMRI_SENSOR_NOPULLUP(vpin,count...)
#define JOIN OPCODE_JOIN,0,0, #define JOIN OPCODE_JOIN,0,0,
#define KILLALL OPCODE_KILLALL,0,0, #define KILLALL OPCODE_KILLALL,0,0,
#define LATCH(sensor_id) OPCODE_LATCH,V(sensor_id), #define LATCH(sensor_id) OPCODE_LATCH,V(sensor_id),

View File

@@ -1,29 +1,36 @@
#include "SensorGroup.h" #include "SensorGroup.h"
#ifdef EXRAIL_ACTIVE
// called in loop to check sensors // called in loop to check sensors
void SensorGroup::checkAll() { void SensorGroup::checkAll() {
#ifdef EXRAIL_ACTIVE
doExrailSensorGroup(GroupProcess::check, & USB_SERIAL); doExrailSensorGroup(GroupProcess::check, & USB_SERIAL);
#endif
} }
// called by command to get sensor list // called by command to get sensor list
void SensorGroup::printAll(Print * serial) { void SensorGroup::printAll(Print * serial) {
(void)serial; // suppress unused warning (void)serial; // suppress unused warning
#ifdef EXRAIL_ACTIVE
doExrailSensorGroup(GroupProcess::print,serial); doExrailSensorGroup(GroupProcess::print,serial);
#endif
} }
void SensorGroup::pullupAll() { void SensorGroup::prepareAll() {
#ifdef EXRAIL_ACTIVE doExrailSensorGroup(GroupProcess::prepare, & USB_SERIAL);
doExrailSensorGroup(GroupProcess::pullup, & USB_SERIAL);
#endif
} }
void SensorGroup::dumpAll(Print * stream) {
doExrailSensorGroup(GroupProcess::dump, stream);
}
#else
// if EXRAIL is not active, these functions are empty
void SensorGroup::checkAll() {}
void SensorGroup::printAll(Print * serial) {(void)serial;}
void SensorGroup::prepareAll() {}
#endif
// called by EXRAIL constructed doExrailSensorGroup for each group // called by EXRAIL constructed doExrailSensorGroup for each group
void SensorGroup::doSensorGroup(VPIN firstVpin, int nPins, byte* statebits, void SensorGroup::doSensorGroup(VPIN firstVpin, int nPins, byte* statebits,
GroupProcess action, Print * serial) { GroupProcess action, Print * serial, bool pullup) {
// Loop through the pins in the group // Loop through the pins in the group
for (auto i=0;i<nPins;i++) { for (auto i=0;i<nPins;i++) {
@@ -32,8 +39,8 @@ void SensorGroup::doSensorGroup(VPIN firstVpin, int nPins, byte* statebits,
byte stateMask=1<<(i%8); byte stateMask=1<<(i%8);
VPIN vpin= firstVpin+i; VPIN vpin= firstVpin+i;
switch(action) { switch(action) {
case GroupProcess::pullup: case GroupProcess::prepare:
IODevice::configureInput(vpin, true); IODevice::configureInput(vpin, pullup);
__attribute__ ((fallthrough)); // to check the current state __attribute__ ((fallthrough)); // to check the current state
case GroupProcess::check: case GroupProcess::check:
@@ -41,13 +48,18 @@ void SensorGroup::doSensorGroup(VPIN firstVpin, int nPins, byte* statebits,
if ((bool)(statebits[stateByte]&stateMask) ==IODevice::read(vpin)) break; // no change if ((bool)(statebits[stateByte]&stateMask) ==IODevice::read(vpin)) break; // no change
// flip state bit // flip state bit
statebits[stateByte]^=stateMask; statebits[stateByte]^=stateMask;
if (action==GroupProcess::pullup) break; if (action==GroupProcess::prepare) break;
// fall through to print the changed value // fall through to print the changed value
__attribute__ ((fallthrough)); __attribute__ ((fallthrough));
case GroupProcess::print: case GroupProcess::print:
StringFormatter::send(serial, F("<%c %d>\n"), StringFormatter::send(serial, F("<%c %d>\n"),
(statebits[stateByte]&stateMask)?'Q':'q', firstVpin+i); (statebits[stateByte]&stateMask)?'Q':'q', vpin);
break;
case GroupProcess::dump:
StringFormatter::send(serial, F("<Q %d %d %c>\n"),
vpin, vpin, pullup?'1':'0');
break; break;
} }
} }

View File

@@ -9,15 +9,19 @@
// reference to the optional exrail built function which contains the // reference to the optional exrail built function which contains the
// calls to SensorGroup::doSensorGroup // calls to SensorGroup::doSensorGroup
enum GroupProcess:byte {pullup,print,check}; enum GroupProcess:byte {prepare,print,check,dump};
class SensorGroup { class SensorGroup {
public: public:
static void checkAll(); static void checkAll();
static void printAll(Print * serial); static void printAll(Print * serial);
static void pullupAll(); static void prepareAll();
static void dumpAll(Print* serial);
// doSensorGroup is called from the automatically
// built doExrailSensorGroup, once for each user defined group.
static void doSensorGroup(VPIN vpin, int nPins, byte* statebits, static void doSensorGroup(VPIN vpin, int nPins, byte* statebits,
GroupProcess action, Print * serial); GroupProcess action, Print * serial, bool pullup=false);
private: private:
static void doExrailSensorGroup(GroupProcess action, Print * stream); static void doExrailSensorGroup(GroupProcess action, Print * stream);
}; };

View File

@@ -191,7 +191,18 @@ void Sensor::printAll(Print *stream){
for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){ for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
StringFormatter::send(stream, F("<%c %d>\n"), tt->active ? 'Q' : 'q', tt->data.snum); StringFormatter::send(stream, F("<%c %d>\n"), tt->active ? 'Q' : 'q', tt->data.snum);
} }
} // Sensor::printAll }
void Sensor::dumpAll(Print *stream){
if (stream == NULL) return; // Nothing to do
SensorGroup::dumpAll(stream);
for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
StringFormatter::send(stream, F("<Q %d %d %d>\n"),
tt->data.snum, tt->data.pin,tt->data.pullUp);
}
} // Sensor::dumpAll
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Static Function to create/find Sensor object. // Static Function to create/find Sensor object.

View File

@@ -82,6 +82,8 @@ public:
static bool remove(int id); static bool remove(int id);
static void checkAll(); static void checkAll();
static void printAll(Print *stream); static void printAll(Print *stream);
static void dumpAll(Print* stream);
static unsigned long lastReadCycle; // value of micros at start of last read cycle static unsigned long lastReadCycle; // value of micros at start of last read cycle
static const unsigned int cycleInterval = 10000; // min time between consecutive reads of each sensor in microsecs. static const unsigned int cycleInterval = 10000; // min time between consecutive reads of each sensor in microsecs.
// should not be less than device scan cycle time. // should not be less than device scan cycle time.