summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2013-01-16 21:43:28 +0100
committerFlorian Pritz <bluewind@xinu.at>2013-01-16 21:43:28 +0100
commitf6c2485f49613617605474ba7ef2a78471ba1fd0 (patch)
tree6cc1da1fdedbf0eb0e79ff6882c5c26157577fa6 /src
downloadmpd-box-f6c2485f49613617605474ba7ef2a78471ba1fd0.tar.gz
mpd-box-f6c2485f49613617605474ba7ef2a78471ba1fd0.tar.xz
initial commit
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'src')
-rw-r--r--src/Bounce.cpp90
-rw-r--r--src/Bounce.h71
-rw-r--r--src/main.cpp65
3 files changed, 226 insertions, 0 deletions
diff --git a/src/Bounce.cpp b/src/Bounce.cpp
new file mode 100644
index 0000000..40709e4
--- /dev/null
+++ b/src/Bounce.cpp
@@ -0,0 +1,90 @@
+
+// Please read Bounce.h for information about the liscence and authors
+
+#include <Arduino.h>
+#include "Bounce.h"
+
+Bounce::Bounce() {
+}
+
+Bounce::Bounce(uint8_t pin,unsigned long interval_millis)
+{
+ interval(interval_millis);
+ previous_millis = millis();
+ state = digitalRead(pin);
+ this->pin = pin;
+}
+
+
+void Bounce::write(int new_state)
+ {
+ this->state = new_state;
+ digitalWrite(pin,state);
+ }
+
+
+void Bounce::interval(unsigned long interval_millis)
+{
+ this->interval_millis = interval_millis;
+ this->rebounce_millis = 0;
+}
+
+void Bounce::rebounce(unsigned long interval)
+{
+ this->rebounce_millis = interval;
+}
+
+
+
+int Bounce::update()
+{
+ if ( debounce() ) {
+ rebounce(0);
+ return stateChanged = 1;
+ }
+
+ // We need to rebounce, so simulate a state change
+
+ if ( rebounce_millis && (millis() - previous_millis >= rebounce_millis) ) {
+ previous_millis = millis();
+ rebounce(0);
+ return stateChanged = 1;
+ }
+
+ return stateChanged = 0;
+}
+
+
+unsigned long Bounce::duration()
+{
+ return millis() - previous_millis;
+}
+
+
+int Bounce::read()
+{
+ return (int)state;
+}
+
+
+// Protected: debounces the pin
+int Bounce::debounce() {
+
+ uint8_t newState = digitalRead(pin);
+ if (state != newState ) {
+ if (millis() - previous_millis >= interval_millis) {
+ previous_millis = millis();
+ state = newState;
+ return 1;
+ }
+ }
+
+ return 0;
+
+}
+
+// The risingEdge method is true for one scan after the de-bounced input goes from off-to-on.
+bool Bounce::risingEdge() { return stateChanged && state; }
+// The fallingEdge method it true for one scan after the de-bounced input goes from on-to-off.
+bool Bounce::fallingEdge() { return stateChanged && !state; }
+
diff --git a/src/Bounce.h b/src/Bounce.h
new file mode 100644
index 0000000..68cdbd8
--- /dev/null
+++ b/src/Bounce.h
@@ -0,0 +1,71 @@
+
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ Main code by Thomas O Fredericks
+ Rebounce and duration functions contributed by Eric Lowry
+ Write function contributed by Jim Schimpf
+ risingEdge and fallingEdge contributed by Tom Harkaway
+* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
+#ifndef Bounce_h
+#define Bounce_h
+
+#include <inttypes.h>
+
+class Bounce
+{
+
+public:
+ // Initialize
+ Bounce();
+ Bounce(uint8_t pin, unsigned long interval_millis );
+ // Sets the debounce interval
+ void interval(unsigned long interval_millis);
+ // Updates the pin
+ // Returns 1 if the state changed
+ // Returns 0 if the state did not change
+ int update();
+ // Forces the pin to signal a change (through update()) in X milliseconds
+ // even if the state does not actually change
+ // Example: press and hold a button and have it repeat every X milliseconds
+ void rebounce(unsigned long interval);
+ // Returns the updated pin state
+ int read();
+ // Sets the stored pin state
+ void write(int new_state);
+ // Returns the number of milliseconds the pin has been in the current state
+ unsigned long duration();
+ // The risingEdge method is true for one scan after the de-bounced input goes from off-to-on.
+ bool risingEdge();
+ // The fallingEdge method it true for one scan after the de-bounced input goes from on-to-off.
+ bool fallingEdge();
+
+protected:
+ int debounce();
+ unsigned long previous_millis, interval_millis, rebounce_millis;
+ uint8_t state;
+ uint8_t pin;
+ uint8_t stateChanged;
+};
+
+#endif
+
+
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..30f2b87
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,65 @@
+#include <Arduino.h>
+#include <LiquidCrystal.h>
+#include "Bounce.h"
+
+void setup();
+void loop();
+
+#define BUFFER_SIZE 64
+char buf[BUFFER_SIZE];
+
+// initialize the library with the numbers of the interface pins
+LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
+
+// A0 = prev; A1 = pause; A2 = next
+byte buttons[] = {A0, A1, A2};
+#define NUMBUTTONS sizeof(buttons)
+#define DEBOUNCE 10
+Bounce bounce_buttons[NUMBUTTONS];
+
+void setup() {
+ byte i;
+ lcd.begin(16, 2);
+ Serial.begin(9600);
+ Serial.setTimeout(5000);
+
+ for (i = 0; i < NUMBUTTONS; i++) {
+ pinMode(buttons[i], INPUT);
+ digitalWrite(buttons[i], HIGH);
+ bounce_buttons[i] = Bounce(buttons[i], DEBOUNCE);
+ }
+
+ Serial.println("ready");
+}
+
+bool button_just_pressed(int i) {
+ bounce_buttons[i].update();
+ return bounce_buttons[i].fallingEdge();
+}
+
+void loop() {
+ if (button_just_pressed(0)) {
+ Serial.println("previous");
+ }
+
+ if (button_just_pressed(1)) {
+ Serial.println("pause");
+ }
+
+ if (button_just_pressed(2)) {
+ Serial.println("next");
+ }
+
+ // output what we got over serial
+ if (Serial.available() >= 32) {
+ lcd.setCursor(0, 0);
+ Serial.readBytes(buf, 32);
+ for (int i = 0; i < 32; i++) {
+ if (i == 16) {
+ lcd.setCursor(0, 1);
+ }
+ lcd.write(buf[i]);
+ }
+ }
+}
+