From 06945bb114fcfe009de039c0fa5ee4215d347873 Mon Sep 17 00:00:00 2001 From: peteGSX Date: Sat, 10 Dec 2022 08:23:46 +1000 Subject: [PATCH] Try to add pin map classes --- EX-IOExpanderPinMaps.h | 38 +++++++++++++++++++++++++++ IO_EXIOExpander.h | 54 ++++++++++++++++++++++++++++++++------- myEX-IOExpander.example.h | 23 +++++++++++++++++ myPinMap.example.h | 28 -------------------- 4 files changed, 106 insertions(+), 37 deletions(-) create mode 100644 EX-IOExpanderPinMaps.h create mode 100644 myEX-IOExpander.example.h delete mode 100644 myPinMap.example.h diff --git a/EX-IOExpanderPinMaps.h b/EX-IOExpanderPinMaps.h new file mode 100644 index 0000000..b09dca3 --- /dev/null +++ b/EX-IOExpanderPinMaps.h @@ -0,0 +1,38 @@ +/* + * © 2022 Peter Cole + * + * This file is part of EX-CommandStation + * + * This is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * It is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CommandStation. If not, see . + */ + +/* +* This file defines default pin maps for the various architectures supported +* by default by EX-IOExpander. +* +* Custom user defined pinmaps should be defined in myEX-IOExpander.h. +* +* Any modifications to this file will be overwritten by future software updates. +*/ + +#define DEFAULT_NANO_DIGITAL_PINMAP new EXIODigitalPinMap(12,2,3,4,5,6,7,8,9,10,11,12,13,A0,A1,A2,A3,A6,A7) +#define DEFAULT_NANO_ANALOGUE_PINMAP new EXIOAnaloguePinMap(6,A0,A1,A2,A3,A6,A7) + +#define DEFAULT_UNO F("DEFAULT_UNO"), \ + new EXIODigitalPinMap(12,2,3,4,5,6,7,8,9,10,11,12,13,A0,A1,A2,A3,A6,A7) \ + new EXIOAnaloguePinMap(4,A0,A1,A2,A3) + +#define DEFAULT_MEGA F("DEFAULT_MEGA"), \ + new EXIODigitalPinMap(12,2,3,4,5,6,7,8,9,10,11,12,13,A0,A1,A2,A3,A6,A7) \ + new EXIOAnaloguePinMap(4,A0,A1,A2,A3) \ No newline at end of file diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h index c979176..052ad72 100644 --- a/IO_EXIOExpander.h +++ b/IO_EXIOExpander.h @@ -28,31 +28,65 @@ #include "IO_GPIOBase.h" #include "FSH.h" +#include "EX-IOExpanderPinMaps.h" -#if __has_include ("myPinMap.h") - #include "myPinMap.h" -#else - #warning myPinMap.h not found. Using defaults from myPinMap.example.h - #include "myPinMap.example.h" +// Include user defined pin maps in myEX-IOExpander if defined +#if __has_include ("myEX-IOExpander.h") + #include "myEX-IOExpander.h" #endif +///////////////////////////////////////////////////////////////////////////////////////////////////// +/* + * EXIODigitalPinMap class for EX-IOExpander. + */ +class EXIODigitalPinMap { + public: + EXIODigitalPinMap(uint8_t numDigitalPins, uint8_t...); + EXIODigitalPinMap() = default; + + private: + EXIODigitalPinMap(uint8_t numDigitalPins, ...) { + _numDigitalPins = numDigitalPins; + uint8_t _digitalPinMap[_numDigitalPins]; + va_list _pinList; + va_start(_pinList, _numDigitalPins); + for (uint8_t pin = 0; pin < _numDigitalPins; pin++) { + _digitalPinMap[pin] = va_arg(_pinList, int); + } + va_end(_pinList); + } + + uint8_t _numDigitalPins; +}; + +///////////////////////////////////////////////////////////////////////////////////////////////////// +/* + * EXIOAnaloguePinMap class for EX-IOExpander. + */ +class EXIOAnaloguePinMap { + public: + EXIOAnaloguePinMap(uint8_t numAnaloguePins, ...); + EXIOAnaloguePinMap() = default; +}; + ///////////////////////////////////////////////////////////////////////////////////////////////////// /* * IODevice subclass for EX-IOExpander. */ - class EXIOExpander : public IODevice { public: - static void create(VPIN vpin, int nPins, uint8_t i2cAddress) { - if (checkNoOverlap(vpin, nPins, i2cAddress)) new EXIOExpander(vpin, nPins, i2cAddress); + static void create(VPIN vpin, int nPins, uint8_t i2cAddress, EXIODigitalPinMap digitalPinMap, EXIOAnaloguePinMap analoguePinMap) { + if (checkNoOverlap(vpin, nPins, i2cAddress)) new EXIOExpander(vpin, nPins, i2cAddress, digitalPinMap, analoguePinMap); } private: // Constructor - EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress) { + EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress, EXIODigitalPinMap digitalPinMap, EXIOAnaloguePinMap analoguePinMap) { _firstVpin = firstVpin; _nPins = nPins; _i2cAddress = i2cAddress; + _digitalPinMap = digitalPinMap; + _analoguePinMap = analoguePinMap; addDevice(this); } @@ -74,6 +108,8 @@ private: } uint8_t _i2cAddress; + EXIODigitalPinMap _digitalPinMap; + EXIOAnaloguePinMap _analoguePinMap; }; #endif \ No newline at end of file diff --git a/myEX-IOExpander.example.h b/myEX-IOExpander.example.h new file mode 100644 index 0000000..3402c92 --- /dev/null +++ b/myEX-IOExpander.example.h @@ -0,0 +1,23 @@ +/* + * © 2022 Peter Cole. All rights reserved. + * + * This is the example configuration file for a custom EX-IOExpander pin map file. + * + * It is highly recommended to copy this to "myEX-IOExpander.h" and modify to suit your specific + * requirements. + * + * If you are simply using a default definition for a defined microcontroller, then you don't + * need to use this file, and instead can use one of the existing definitions. + * + * Refer to https://dcc-ex.com for the full documentation. + * + * NOTE: Modifications to this file will be overwritten by future software updates. + */ +#ifndef MYEX_IOEXPANDER_H +#define MYEX_IOEXPANDER_H + +#define MY_CUSTOM_NANO F("MY_NANO_PINMAP"), \ + new EXIODigitalPinMap(12,2,3,4,5,6,7,8,9,10,11,12,13) \ + new EXIOAnaloguePinMap(4,A0,A1,A2,A3) + +#endif \ No newline at end of file diff --git a/myPinMap.example.h b/myPinMap.example.h deleted file mode 100644 index 3afebc1..0000000 --- a/myPinMap.example.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * © 2022 Peter Cole. All rights reserved. - * - * This is the example configuration file for the EX-IOExpander pin map file. - * - * It is highly recommended to copy this to "myPinMap.h" and modify to suit your specific - * requirements. - * - * NOTE: Modifications to this file will be overwritten by future software updates. - */ -#ifndef MYPINMAP_H -#define MYPINMAP_H - -///////////////////////////////////////////////////////////////////////////////////// -// Define the number of I/O pins to be configured on the EX-IOExpander device -// -#define NUMBER_OF_DIGITAL_PINS 12 -#define NUMBER_OF_ANALOGUE_PINS 4 - -///////////////////////////////////////////////////////////////////////////////////// -// Define the pin map -// -// You must define the correct number of pins as per NUMBER_OF_PINS above -// -static uint8_t digitalPinMap[NUMBER_OF_DIGITAL_PINS] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; -static uint8_t analoguePinMap[NUMBER_OF_ANALOGUE_PINS] = {A0, A1, A2, A3}; - -#endif \ No newline at end of file