From 8cbcf5df32f9435825a4b4d135eeffafc603432a Mon Sep 17 00:00:00 2001 From: peteGSX Date: Thu, 8 Dec 2022 14:21:01 +1000 Subject: [PATCH] Basic shell of device driver started --- IO_EXIOExpander.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++ myPinMap.example.h | 28 ++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 IO_EXIOExpander.h create mode 100644 myPinMap.example.h diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h new file mode 100644 index 0000000..c979176 --- /dev/null +++ b/IO_EXIOExpander.h @@ -0,0 +1,79 @@ +/* + * © 2021, Peter Cole. All rights reserved. + * + * 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 . +*/ + +/* +* The IO_EX-IOExpander.h device driver integrates with one or more EX-IOExpander devices. +* This device driver will configure the device and all I/O ports on startup, along with +* interacting with the device for all input/output duties. +*/ + +#ifndef IO_EX_IOEXPANDER_H +#define IO_EX_IOEXPANDER_H + +#include "IO_GPIOBase.h" +#include "FSH.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" +#endif + +///////////////////////////////////////////////////////////////////////////////////////////////////// +/* + * 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); + } + +private: + // Constructor + EXIOExpander(VPIN firstVpin, int nPins, uint8_t i2cAddress) { + _firstVpin = firstVpin; + _nPins = nPins; + _i2cAddress = i2cAddress; + addDevice(this); + } + + void _begin() { + // Initialise EX-IOExander device + if (I2CManager.exists(_i2cAddress)) { +#ifdef DIAG_IO + _display(); +#endif + } else { + DIAG(F("EX-IOExpander device not found, I2C:%x"), _i2cAddress); + _deviceState = DEVSTATE_FAILED; + } + } + + void _display() override { + DIAG(F("EX-IOExpander I2C:x%x Configured on Vpins:%d-%d %S"), _i2cAddress, _firstVpin, _firstVpin+_nPins-1, + _deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F("")); + } + + uint8_t _i2cAddress; +}; + +#endif \ No newline at end of file diff --git a/myPinMap.example.h b/myPinMap.example.h new file mode 100644 index 0000000..3afebc1 --- /dev/null +++ b/myPinMap.example.h @@ -0,0 +1,28 @@ +/* + * © 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