Skip to content

Commit

Permalink
Merge pull request #65 from ninezerozeronine/eeprom_programmer
Browse files Browse the repository at this point in the history
Eeprom programmer
  • Loading branch information
ninezerozeronine committed Aug 13, 2019
2 parents 4ccac58 + b7e030e commit f62ae7f
Show file tree
Hide file tree
Showing 18 changed files with 9,497 additions and 34 deletions.
87 changes: 87 additions & 0 deletions arduino/eeprom-programmer/button.cpp
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;
}
}
}
37 changes: 37 additions & 0 deletions arduino/eeprom-programmer/button.h
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

0 comments on commit f62ae7f

Please sign in to comment.