-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from ninezerozeronine/eeprom_programmer
Eeprom programmer
- Loading branch information
Showing
18 changed files
with
9,497 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Convenience class to use a button with Arduino | ||
// | ||
// To use a button, have it ground the pin it's connected to when set to "on". | ||
|
||
#include "button.h" | ||
|
||
Button::Button() { | ||
constructor_defaults(); | ||
} | ||
|
||
// Constructor | ||
// | ||
// pin - the pin the button is connected to | ||
// debounce_time - the number of miliseconds for the button to be in it's new state before considering it pressed | ||
// low_to_high_callback - the callback to call when the button goes from unpressed to pressed | ||
// high_to_low_callback - the callback to call when the button goes from pressed to unpressed | ||
Button::Button(byte pin_, byte debounce_time_) { | ||
constructor_defaults(); | ||
set_pin(pin_); | ||
set_debounce_time(debounce_time_); | ||
} | ||
|
||
void Button::constructor_defaults() { | ||
pin = 0; | ||
debounce_time = 0; | ||
stable_state = LOW; | ||
last_read_state = LOW; | ||
last_state_change = 0; | ||
} | ||
|
||
void Button::set_pin(byte pin_){ | ||
pin = pin_; | ||
} | ||
|
||
void Button::set_debounce_time(byte debounce_time_){ | ||
debounce_time = debounce_time_; | ||
} | ||
|
||
byte Button::get_state(){ | ||
return stable_state; | ||
} | ||
|
||
// Set up button ready for use. | ||
// Note that we're using INPUT_PULLUP to save on having to add a resistor | ||
void Button::init() { | ||
pinMode(pin, INPUT_PULLUP); | ||
stable_state, last_read_state = !digitalRead(pin); | ||
last_state_change = millis(); | ||
} | ||
|
||
// Update the button each time round the main loop | ||
void Button::update(void (*low_to_high_callback)(), void (*high_to_low_callback)()) { | ||
// Read state of the button (invert because we're using INPUT_PULLUP) | ||
byte current_state = !digitalRead(pin); | ||
|
||
// If the button is in a different state to the stable state | ||
if (current_state != stable_state) { | ||
|
||
// Get current time | ||
unsigned long current_time = millis(); | ||
|
||
// If the button is in the same state as it was last time | ||
if (current_state == last_read_state) { | ||
|
||
// If it's been in this new state for longer that the debounce time, flip the state | ||
// and call the callbacks | ||
if (current_time - last_state_change > debounce_time) { | ||
stable_state = current_state; | ||
if (current_state == HIGH) { | ||
if (low_to_high_callback != NULL) { | ||
low_to_high_callback(); | ||
} | ||
} else { | ||
if (high_to_low_callback != NULL) { | ||
high_to_low_callback(); | ||
} | ||
} | ||
} | ||
|
||
// Else the button has just changed state | ||
} else { | ||
// Set the last state and record the time | ||
last_read_state = current_state; | ||
last_state_change = current_time; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Convenience class to use a button with Arduino | ||
|
||
#ifndef BUTTON_H | ||
#define BUTTON_H | ||
|
||
#include "Arduino.h" | ||
|
||
class Button { | ||
public: | ||
Button(); | ||
Button(byte pin_, byte debounce_time_=10); | ||
void init(); | ||
void set_pin(byte pin_); | ||
void set_debounce_time(byte debounce_time); | ||
byte get_state(); | ||
void update(void (*low_to_high_callback)()=NULL, void (*high_to_low_callback)()=NULL); | ||
|
||
private: | ||
void constructor_defaults(); | ||
|
||
// The pin this button is connected to | ||
byte pin; | ||
|
||
// The time in milliseconds for the state to be held before considering it to be on | ||
byte debounce_time; | ||
|
||
// Whether the button is pressed or not | ||
byte stable_state; | ||
|
||
// The state of the pin read at the last update | ||
byte last_read_state; | ||
|
||
// When the button last changes state (as directly read) | ||
unsigned long last_state_change; | ||
}; | ||
|
||
#endif |
Oops, something went wrong.