From ca90fa471a7e216c4482bd72a3e5ab004979059a Mon Sep 17 00:00:00 2001 From: Steve Jernigan Date: Tue, 6 Feb 2024 11:31:41 -0500 Subject: [PATCH 1/3] Feature Addition: Buttons can be active high or low, examples changed to comply --- examples/button/button.ino | 4 ++-- examples/frere_jacques/frere_jacques.ino | 2 +- examples/knight_rider2/knight_rider2.ino | 2 +- examples/led_test/led_test.ino | 4 ++-- .../nuclear_missile_launcher.ino | 4 ++-- src/Atm_button.cpp | 13 +++++++++---- src/Atm_button.hpp | 3 ++- 7 files changed, 19 insertions(+), 13 deletions(-) diff --git a/examples/button/button.ino b/examples/button/button.ino index 57d8571..14a1031 100644 --- a/examples/button/button.ino +++ b/examples/button/button.ino @@ -19,7 +19,7 @@ void setup() { .onInput( led3, led3.EVT_TOGGLE_BLINK ); // Button triggers the fan - btn.begin( 2 ) + btn.begin( 2 , LOW) .onPress( fan, fan.EVT_INPUT ); // Start the blinking @@ -31,4 +31,4 @@ void setup() { void loop() { automaton.run(); -} +} diff --git a/examples/frere_jacques/frere_jacques.ino b/examples/frere_jacques/frere_jacques.ino index 6856a9a..e23c0df 100644 --- a/examples/frere_jacques/frere_jacques.ino +++ b/examples/frere_jacques/frere_jacques.ino @@ -23,7 +23,7 @@ void setup() { .play( pattern, sizeof( pattern ) ) .repeat( -1 ); - button.begin( 2 ) // A button on pin 2 toggles playback on and off + button.begin( 2 , LOW) // A button on pin 2 toggles playback on and off .onPress( player, player.EVT_TOGGLE ); speed.begin( A0 ) // An analog pot on pin A0 controls playback speed diff --git a/examples/knight_rider2/knight_rider2.ino b/examples/knight_rider2/knight_rider2.ino index 3a7a511..f71f6ad 100644 --- a/examples/knight_rider2/knight_rider2.ino +++ b/examples/knight_rider2/knight_rider2.ino @@ -10,7 +10,7 @@ void setup() { .speed( 50 ) .trigger( sweep.EVT_START ); - button.begin( 2 ) + button.begin( 2 , LOW) .onPress( sweep, sweep.EVT_TOGGLE ); } diff --git a/examples/led_test/led_test.ino b/examples/led_test/led_test.ino index ae2e8fe..81abe98 100644 --- a/examples/led_test/led_test.ino +++ b/examples/led_test/led_test.ino @@ -16,10 +16,10 @@ void setup() { led.begin( ledPin ) .blink( 1000 ); - button.begin( buttonPin ) + button.begin( buttonPin , LOW) .onPress( led, led.EVT_TOGGLE ); } void loop() { automaton.run(); -} +} diff --git a/examples/nuclear_missile_launcher/nuclear_missile_launcher.ino b/examples/nuclear_missile_launcher/nuclear_missile_launcher.ino index ad3583c..f0d3f58 100644 --- a/examples/nuclear_missile_launcher/nuclear_missile_launcher.ino +++ b/examples/nuclear_missile_launcher/nuclear_missile_launcher.ino @@ -19,7 +19,7 @@ const int countdownFlashOff = 900; void setup() { // Self resetting button 1 - button1.begin( pinButton1 ) + button1.begin( pinButton1 , LOW) .onPress( bit1, bit1.EVT_ON ); timer1.begin( buttonIntervalMax ) @@ -29,7 +29,7 @@ void setup() { .onChange( true, timer1, timer1.EVT_START ); // Self resetting button 2 - button2.begin( pinButton2 ) + button2.begin( pinButton2 , LOW) .onPress( bit2, bit2.EVT_ON ); timer2.begin( buttonIntervalMax ) diff --git a/src/Atm_button.cpp b/src/Atm_button.cpp index f7faa4f..8c5297a 100644 --- a/src/Atm_button.cpp +++ b/src/Atm_button.cpp @@ -2,7 +2,7 @@ // Add option for button press callback (for reading i2c buttons etc) -Atm_button& Atm_button::begin( int attached_pin ) { +Atm_button& Atm_button::begin( int attached_pin, int _active_high ) { // clang-format off const static state_t state_table[] PROGMEM = { /* Standard Mode: press/repeat */ @@ -23,12 +23,17 @@ Atm_button& Atm_button::begin( int attached_pin ) { // clang-format on Machine::begin( state_table, ELSE ); pin = attached_pin; + active_high = _active_high; counter_longpress.set( 0 ); timer_debounce.set( DEBOUNCE ); timer_delay.set( ATM_TIMER_OFF ); timer_repeat.set( ATM_TIMER_OFF ); timer_auto.set( ATM_TIMER_OFF ); - pinMode( pin, INPUT_PULLUP ); + if (active_high) { + pinMode( pin, INPUT_PULLUP ); + } else { + pinMode( pin, INPUT_PULLDOWN ); + } return *this; } @@ -45,9 +50,9 @@ int Atm_button::event( int id ) { case EVT_AUTO: return timer_auto.expired( this ); case EVT_PRESS: - return !digitalRead( pin ); + return digitalRead( pin ) == active_high; case EVT_RELEASE: - return digitalRead( pin ); + return digitalRead( pin ) != active_high; case EVT_COUNTER: return counter_longpress.expired(); } diff --git a/src/Atm_button.hpp b/src/Atm_button.hpp index 2e5f4cd..1239a6a 100644 --- a/src/Atm_button.hpp +++ b/src/Atm_button.hpp @@ -9,7 +9,7 @@ class Atm_button : public Machine { enum { BTN_PASS4 = -4, BTN_PASS3 = -3, BTN_PASS2 = -2, BTN_PASS1 = -1, BTN_RELEASE = 0, BTN_PRESS1 = 1, BTN_PRESS2 = 2, BTN_PRESS3 = 3, BTN_PRESS4 = 4 }; Atm_button( void ) : Machine(){}; - Atm_button& begin( int attached_pin ); + Atm_button& begin( int attached_pin, int _active_high ); Atm_button& trace( Stream& stream ); Atm_button& onPress( atm_cb_push_t callback, int idx = 0 ); Atm_button& onPress( Machine& machine, int event = 0 ); @@ -28,6 +28,7 @@ class Atm_button : public Machine { atm_connector onpress, onrelease; atm_connector longpress[2]; short pin; + short active_high; atm_timer_millis timer_debounce, timer_delay, timer_repeat, timer_auto; atm_counter counter_longpress; int longpress_max; From ef5dea922591360b36308da96a0fa3d37ca71c7f Mon Sep 17 00:00:00 2001 From: Steve Jernigan Date: Thu, 8 Feb 2024 22:50:38 -0500 Subject: [PATCH 2/3] added elapsed gettor to millis timer --- src/atm_timer_millis.cpp | 9 +++++++++ src/atm_timer_millis.hpp | 1 + 2 files changed, 10 insertions(+) diff --git a/src/atm_timer_millis.cpp b/src/atm_timer_millis.cpp index 83d0d97..1d5493b 100644 --- a/src/atm_timer_millis.cpp +++ b/src/atm_timer_millis.cpp @@ -18,6 +18,15 @@ void atm_timer_millis::setFromNow( Machine* machine, uint32_t v ) { value = millis() - machine->state_millis + v; } +/* + * atm_timer_millis::elapsed( this ) - returns the elapsed time so far + * This is a rollover-safe 32 bit unsigned integer + * + */ +uint32_t elapsed( Machine* machine ) { + return value == ATM_TIMER_OFF ? 0 : millis() - machine->state_millis +} + /* * atm_timer_millis::expired( this ) - Checks a millis timer for expiry (== 0) * This is a rollover-safe 32 bit unsigned integer comparison diff --git a/src/atm_timer_millis.hpp b/src/atm_timer_millis.hpp index 6412bf7..590c08a 100644 --- a/src/atm_timer_millis.hpp +++ b/src/atm_timer_millis.hpp @@ -12,5 +12,6 @@ class atm_timer_millis { uint32_t value; void set( uint32_t v ); void setFromNow( Machine* machine, uint32_t v ); + uint32_t elapsed( Machine* machine ); int expired( Machine* machine ); }; From 1df40c61474b455bfa8b87b54e20809154b69d8c Mon Sep 17 00:00:00 2001 From: Steve Jernigan Date: Thu, 8 Feb 2024 23:24:33 -0500 Subject: [PATCH 3/3] added elapsed gettor to millis timer, fix compilation error --- src/atm_timer_millis.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/atm_timer_millis.cpp b/src/atm_timer_millis.cpp index 1d5493b..6bcedf9 100644 --- a/src/atm_timer_millis.cpp +++ b/src/atm_timer_millis.cpp @@ -23,8 +23,8 @@ void atm_timer_millis::setFromNow( Machine* machine, uint32_t v ) { * This is a rollover-safe 32 bit unsigned integer * */ -uint32_t elapsed( Machine* machine ) { - return value == ATM_TIMER_OFF ? 0 : millis() - machine->state_millis +uint32_t atm_timer_millis::elapsed( Machine* machine ) { + return value == ATM_TIMER_OFF ? 0 : millis() - machine->state_millis; } /*