diff --git a/DCCTimer.h b/DCCTimer.h index a50c669..d1ba700 100644 --- a/DCCTimer.h +++ b/DCCTimer.h @@ -100,7 +100,7 @@ public: // called PRIOR to the start of the waveform. It returns the // current value so that an offset can be initialized. static int init(uint8_t pin); - static int read(uint8_t pin, bool fromISR); + static int read(uint8_t pin, bool fromISR=false); private: static void scan(); static void begin(); diff --git a/EXRAIL2.cpp b/EXRAIL2.cpp index 75336fd..9095de8 100644 --- a/EXRAIL2.cpp +++ b/EXRAIL2.cpp @@ -180,6 +180,15 @@ LookList* RMFT2::LookListLoader(OPCODE op1, OPCODE op2, OPCODE op3) { break; } + case OPCODE_ATGTE: + case OPCODE_ATLT: + case OPCODE_IFGTE: + case OPCODE_IFLT: + case OPCODE_DRIVE: { + IODevice::configureAnalogIn((VPIN)operand); + break; + } + case OPCODE_TURNOUT: { VPIN id=operand; int addr=GET_OPERAND(1); diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index c8cba40..ccc9d87 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "PORTX-HAL-202210042018Z" +#define GITHUB_SHA "PORTX-HAL-202210052105Z" diff --git a/IODevice.cpp b/IODevice.cpp index b292195..812d7ed 100644 --- a/IODevice.cpp +++ b/IODevice.cpp @@ -25,6 +25,7 @@ #include "DIAG.h" #include "FSH.h" #include "IO_MCP23017.h" +#include "DCCTimer.h" #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) #define USE_FAST_IO @@ -195,7 +196,17 @@ int IODevice::readAnalogue(VPIN vpin) { #ifdef DIAG_IO DIAG(F("IODevice::readAnalogue(): Vpin %d not found!"), (int)vpin); #endif - return false; + return -1023; +} +int IODevice::configureAnalogIn(VPIN vpin) { + for (IODevice *dev = _firstDevice; dev != 0; dev = dev->_nextDevice) { + if (dev->owns(vpin)) + return dev->_configureAnalogIn(vpin); + } +#ifdef DIAG_IO + DIAG(F("IODevice::configureAnalogIn(): Vpin %d not found!"), (int)vpin); +#endif + return -1023; } // Write value to virtual pin(s). If multiple devices are allocated the same pin @@ -361,11 +372,10 @@ int IODevice::read(VPIN vpin) { return !digitalRead(vpin); // Return inverted state (5v=0, 0v=1) } int IODevice::readAnalogue(VPIN vpin) { - pinMode(vpin, INPUT); - noInterrupts(); - int value = analogRead(vpin); - interrupts(); - return value; + return ADCee::read(vpin); +} +int IODevice::configureAnalogIn(VPIN vpin) { + return ADCee::init(vpin); } void IODevice::loop() {} void IODevice::DumpAll() { @@ -467,7 +477,18 @@ int ArduinoPins::_read(VPIN vpin) { // Device-specific readAnalogue function (analogue input) int ArduinoPins::_readAnalogue(VPIN vpin) { - int pin = vpin; + if (vpin > 255) return -1023; + uint8_t pin = vpin; + int value = ADCee::read(pin); + + #ifdef DIAG_IO + DIAG(F("Arduino Read Pin:%d Value:%d"), pin, value); + #endif + return value; +} +int ArduinoPins::_configureAnalogIn(VPIN vpin) { + if (vpin > 255) return -1023; + uint8_t pin = vpin; uint8_t mask = 1 << ((pin-_firstVpin) % 8); uint8_t index = (pin-_firstVpin) / 8; if (_pinModes[index] & mask) { @@ -479,22 +500,9 @@ int ArduinoPins::_readAnalogue(VPIN vpin) { else pinMode(pin, INPUT); } - - // Since AnalogRead is also called from interrupt code, disable interrupts - // while we're using it. There's only one ADC shared by all analogue inputs - // on the Arduino, so we don't want interruptions. - //****************************************************************************** - // NOTE: If the HAL is running on a computer without the DCC signal generator, - // then interrupts needn't be disabled. Also, the DCC signal generator puts - // the ADC into fast mode, so if it isn't present, analogueRead calls will be much - // slower!! - //****************************************************************************** - noInterrupts(); - int value = analogRead(pin); - interrupts(); - + int value = ADCee::init(pin); #ifdef DIAG_IO - DIAG(F("Arduino Read Pin:%d Value:%d"), pin, value); + DIAG(F("configureAnalogIn Pin:%d Value:%d"), pin, value); #endif return value; } diff --git a/IODevice.h b/IODevice.h index 2a3437c..ce47267 100644 --- a/IODevice.h +++ b/IODevice.h @@ -143,6 +143,7 @@ public: // read invokes the IODevice instance's _readAnalogue method. static int readAnalogue(VPIN vpin); + static int configureAnalogIn(VPIN vpin); // loop invokes the IODevice instance's _loop method. static void loop(); @@ -201,6 +202,10 @@ protected: (void)vpin; return 0; }; + virtual int _configureAnalogIn(VPIN vpin) { + (void)vpin; + return 0; + }; // Method to perform updates on an ongoing basis (optionally implemented within device class) virtual void _loop(unsigned long currentMicros) { @@ -356,6 +361,7 @@ private: // Device-specific read functions. int _read(VPIN vpin) override; int _readAnalogue(VPIN vpin) override; + int _configureAnalogIn(VPIN vpin) override; void _display() override; @@ -403,4 +409,4 @@ private: #include "IO_MCP23017.h" #include "IO_PCF8574.h" -#endif // iodevice_h \ No newline at end of file +#endif // iodevice_h