From 485ca0b8a5443a55a30bcbf91a3e2763e8f0dedd Mon Sep 17 00:00:00 2001 From: rljonesau Date: Sat, 6 Apr 2019 22:06:50 +1100 Subject: [PATCH] Added: Hold input 1 for run mode; Alternate closures input 1 toggle run/stop --- Arduino/BTCDieselHeater/BTCDieselHeater.ino | 2 +- Arduino/BTCDieselHeater/src/Utility/GPIO.cpp | 144 +++++++++++++++++++ Arduino/BTCDieselHeater/src/Utility/GPIO.h | 48 +++++++ 3 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 Arduino/BTCDieselHeater/src/Utility/GPIO.cpp create mode 100644 Arduino/BTCDieselHeater/src/Utility/GPIO.h diff --git a/Arduino/BTCDieselHeater/BTCDieselHeater.ino b/Arduino/BTCDieselHeater/BTCDieselHeater.ino index 042b5f7..a96a3d1 100644 --- a/Arduino/BTCDieselHeater/BTCDieselHeater.ino +++ b/Arduino/BTCDieselHeater/BTCDieselHeater.ino @@ -410,7 +410,7 @@ void setup() { bBTconnected = false; Bluetooth.begin(); - GPIOin.begin(GPIOin1_pin, GPIOin2_pin, GPIOinOn1Off2); + GPIOin.begin(GPIOin1_pin, GPIOin2_pin, GPIOinOn1Off1); } diff --git a/Arduino/BTCDieselHeater/src/Utility/GPIO.cpp b/Arduino/BTCDieselHeater/src/Utility/GPIO.cpp new file mode 100644 index 0000000..db26de0 --- /dev/null +++ b/Arduino/BTCDieselHeater/src/Utility/GPIO.cpp @@ -0,0 +1,144 @@ +/* + * This file is part of the "bluetoothheater" distribution + * (https://gitlab.com/mrjones.id.au/bluetoothheater) + * + * Copyright (C) 2018 Ray Jones + * + * This program 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. + * + * This program 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 this program. If not, see . + * + */ + +#include "GPIO.h" +#include "../Protocol/helpers.h" + +CGPIOin::CGPIOin() +{ + _Mode = GPIOinNone; + _pins[0] = 0; + _pins[1] = 0; + _prevPins = 0; + _lastDebounceTime = 0; + _lastKey = 0; + _debounceDelay = 50; +} + +void +CGPIOin::begin(int pin1, int pin2, GPIOinModes mode) +{ + _pins[0] = pin1; + _pins[1] = pin2; + pinMode(pin1, INPUT_PULLUP); // GPIO input pin #1 + pinMode(pin2, INPUT_PULLUP); // GPIO input pin #1 + + setMode(mode); +} + +void +CGPIOin::manageGPIO() +{ + switch (_Mode) { + case GPIOinNone: + break; + case GPIOinOn1Off2: + _doOn1Off2(); + break; + case GPIOinOnHold1: + _doOnHold1(); + break; + case GPIOinOn1Off1: + _doOn1Off1(); + break; + } +} + +void +CGPIOin::_doOn1Off2() +{ + uint8_t newKey = _scanInputs(); + // determine edge events + uint8_t keyChange = newKey ^ _lastKey; + _lastKey = newKey; + + if(keyChange) { + if(newKey & 0x01) { + requestOn(); + } + if(newKey & 0x02) { + requestOff(); + } + } +} + +// mode where heater runs if input 1 is shorted +// stops when open +void +CGPIOin::_doOnHold1() +{ + uint8_t newKey = _scanInputs(); + // determine edge events + uint8_t keyChange = newKey ^ _lastKey; + _lastKey = newKey; + + if(keyChange) { + if(newKey & 0x01) { + requestOn(); + } + else { + requestOff(); + } + } +} + +// mode where heater runs if input 1 is shorted +// stops when open +void +CGPIOin::_doOn1Off1() +{ + uint8_t newKey = _scanInputs(); + // determine edge events + uint8_t keyChange = newKey ^ _lastKey; + _lastKey = newKey; + + if(keyChange) { + if(newKey & 0x01) { + if(getHeaterInfo().getRunStateEx()) + requestOff(); + else + requestOn(); + } + } +} + + +uint8_t +CGPIOin::_scanInputs() +{ + uint8_t newPins = 0; + if(_pins[0] && digitalRead(_pins[0]) == HIGH) newPins |= 0x01; + if(_pins[1] && digitalRead(_pins[1]) == HIGH) newPins |= 0x02; + + if(newPins != _prevPins) { + _lastDebounceTime = millis(); + _prevPins = newPins; + } + + long elapsed = millis() - _lastDebounceTime; + if (elapsed > _debounceDelay) { + // whatever the reading is at, it's been there for longer than the debounce + // delay, so take it as the actual current state: + _debouncedPins = newPins; + } + + return _debouncedPins; +} diff --git a/Arduino/BTCDieselHeater/src/Utility/GPIO.h b/Arduino/BTCDieselHeater/src/Utility/GPIO.h new file mode 100644 index 0000000..9769f70 --- /dev/null +++ b/Arduino/BTCDieselHeater/src/Utility/GPIO.h @@ -0,0 +1,48 @@ +/* + * This file is part of the "bluetoothheater" distribution + * (https://gitlab.com/mrjones.id.au/bluetoothheater) + * + * Copyright (C) 2018 Ray Jones + * + * This program 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. + * + * This program 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 this program. If not, see . + * + */ + +#include + +enum GPIOinModes { + GPIOinNone, + GPIOinOn1Off2, // input 1 closure, heater starts; input2 closure, heater stops + GPIOinOnHold1, // hold input 1 closure, heater runs; input 1 open, heater stops + GPIOinOn1Off1 // alternate input 1 closures start or stop the heater +}; + +class CGPIOin { + GPIOinModes _Mode; + void _doOn1Off2(); + void _doOnHold1(); + void _doOn1Off1(); + int _pins[2]; + uint8_t _scanInputs(); + uint8_t _prevPins; + uint8_t _debouncedPins; + uint8_t _lastKey; + unsigned long _lastDebounceTime; + unsigned long _debounceDelay; +public: + CGPIOin(); + void setMode(GPIOinModes mode) { _Mode = mode; }; + void begin(int pin1, int pin2, GPIOinModes mode); + void manageGPIO(); +}; \ No newline at end of file