Skip to content

Commit

Permalink
Change debouncing to use Bounce2 library
Browse files Browse the repository at this point in the history
  • Loading branch information
alown committed Jul 12, 2014
1 parent 303a130 commit 79a57e2
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 27 deletions.
2 changes: 1 addition & 1 deletion fw/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ P = ..

BOARD_TAG = oscarleo
MONITOR_PORT = /dev/leo.usb*
ARDUINO_LIBS = AdapterBoard Backlight RGBLed EEPROM
ARDUINO_LIBS = AdapterBoard Backlight RGBLed EEPROM Bounce2
USER_LIB_PATH = libs

ARDUINO_DIR = $P/Arduino/
Expand Down
33 changes: 15 additions & 18 deletions fw/libs/AdapterBoard/AdapterBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,31 @@ void AdapterBoard::initSwitches()
pinMode(SW_UP, INPUT);
pinMode(SW_DOWN, INPUT);

prev_swOn = HIGH;
prev_swUp = HIGH;
prev_swDown = HIGH;
switchDelay = 0;
swOn = Bounce();
swOn.attach(SW_ON);
swOn.interval(20);
swUp = Bounce();
swUp.attach(SW_UP);
swUp.interval(20);
swDown = Bounce();
swDown.attach(SW_DOWN);
swDown.interval(20);
}

void AdapterBoard::pollSwitches()
{
//Ignore a few polls
if(switchDelay++ < 1000)
return;
swOn.update();
swUp.update();
swDown.update();

int swOn = digitalRead(SW_ON);
int swUp = digitalRead(SW_UP);
int swDown = digitalRead(SW_DOWN);

if(swOn == LOW && prev_swOn == HIGH)
if(swOn.read() == LOW)
togglePower();

//When both pressed, backlight up button has priority
if(swUp == LOW && prev_swUp == HIGH)
if(swUp.read() == LOW)
backlight.up();
if(swDown == LOW && prev_swDown == HIGH)
if(swDown.read() == LOW)
backlight.down();

prev_swOn = swOn;
prev_swUp = swUp;
prev_swDown = swDown;
}

void AdapterBoard::togglePower()
Expand Down
13 changes: 5 additions & 8 deletions fw/libs/AdapterBoard/AdapterBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <Backlight.h>
#include <RGBLed.h>
#include <Bounce2.h>
#include <usb.h>

#define LED_R 13
Expand All @@ -13,9 +14,9 @@

//When reading the switches, the logical value is inverted from the actual
//due to the board design.
#define SW_ON 4
#define SW_UP 12
#define SW_DOWN 6
#define SW_ON 4 //PD4
#define SW_UP 12 //PD6
#define SW_DOWN 6 //PD7

#define STANDBY_COLOUR 1, 0, 0
#define ON_COLOUR 20, 20, 20
Expand All @@ -38,11 +39,7 @@ class AdapterBoard
Backlight backlight;
USB usb;

unsigned switchDelay;

int prev_swOn;
int prev_swUp;
int prev_swDown;
Bounce swOn, swUp, swDown;
};

#endif
83 changes: 83 additions & 0 deletions fw/libs/Bounce2/Bounce2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

// Please read Bounce2.h for information about the liscence and authors

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Bounce2.h"

#define DEBOUNCED_STATE 0
#define UNSTABLE_STATE 1
#define STATE_CHANGED 3

Bounce::Bounce()
: previous_millis(0)
, interval_millis(10)
, state(0)
, pin(0)
{}

void Bounce::attach(int pin) {
this->pin = pin;
bool read = digitalRead(pin);
state = 0;
if (digitalRead(pin)) {
state = _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE);
}
#ifdef BOUNCE_LOCK-OUT
previous_millis = 0;
#else
previous_millis = millis();
#endif
}

void Bounce::interval(uint16_t interval_millis)
{
this->interval_millis = interval_millis;
}

bool Bounce::update()
{
#ifdef BOUNCE_LOCK-OUT
state &= ~_BV(STATE_CHANGED);
// Ignore everything if we are locked out
if (millis() - previous_millis >= interval_millis) {
bool currentState = digitalRead(pin);
if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) {
previous_millis = millis();
state ^= _BV(DEBOUNCED_STATE);
state |= _BV(STATE_CHANGED);
}
}
return state & _BV(STATE_CHANGED);
#else
// Lire l'etat de l'interrupteur dans une variable temporaire.
bool currentState = digitalRead(pin);
state &= ~_BV(STATE_CHANGED);

// Redemarrer le compteur timeStamp tant et aussi longtemps que
// la lecture ne se stabilise pas.
if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) {
previous_millis = millis();
state ^= _BV(UNSTABLE_STATE);
} else if ( millis() - previous_millis >= interval_millis ) {
// Rendu ici, la lecture est stable
// Est-ce que la lecture est différente de l'etat emmagasine de l'interrupteur?
if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) {
previous_millis = millis();
state ^= _BV(DEBOUNCED_STATE);
state |= _BV(STATE_CHANGED);
}
}

return state & _BV(STATE_CHANGED);
#endif
}

bool Bounce::read()
{
return state & _BV(DEBOUNCED_STATE);
}

68 changes: 68 additions & 0 deletions fw/libs/Bounce2/Bounce2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

/*
The MIT License (MIT)
Copyright (c) 2013 thomasfredericks
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/



/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Main code by Thomas O Fredericks (tof@t-o-f.info)
Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// Uncomment the following line for "LOCK-OUT" debounce method
//#define BOUNCE_LOCK-OUT


#ifndef Bounce2_h
#define Bounce2_h

#include <inttypes.h>

class Bounce
{

public:
// Create an instance of the bounce library
Bounce();
// Attach to a pin (and also sets initial state)
void attach(int pin);
// Sets the debounce interval
void interval(uint16_t interval_millis);
// Updates the pin
// Returns 1 if the state changed
// Returns 0 if the state did not change
bool update();
// Returns the updated pin state
bool read();


protected:
unsigned long previous_millis;
uint16_t interval_millis;
uint8_t state;
uint8_t pin;
};

#endif


20 changes: 20 additions & 0 deletions fw/libs/Bounce2/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013 thomasfredericks

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 comments on commit 79a57e2

Please sign in to comment.