Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/button/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,4 +31,4 @@ void setup() {

void loop() {
automaton.run();
}
}
2 changes: 1 addition & 1 deletion examples/frere_jacques/frere_jacques.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/knight_rider2/knight_rider2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void setup() {
.speed( 50 )
.trigger( sweep.EVT_START );

button.begin( 2 )
button.begin( 2 , LOW)
.onPress( sweep, sweep.EVT_TOGGLE );

}
Expand Down
4 changes: 2 additions & 2 deletions examples/led_test/led_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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 )
Expand Down
13 changes: 9 additions & 4 deletions src/Atm_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}

Expand All @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Atm_button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/atm_timer_millis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 atm_timer_millis::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
Expand Down
1 change: 1 addition & 0 deletions src/atm_timer_millis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
};