FastLED 3.9.7
Loading...
Searching...
No Matches
buttons.h
1#pragma once
2
3#include <Arduino.h>
4#include "fl/ui.h"
5#include "fl/dbg.h"
6
7using namespace fl;
8
9// Done by hand. Old school.
11 public:
12 ToggleButton(int pin) : pin_(pin), on_(false), debounce_timestamp_(0), changed_(false) {
13 pinMode(pin_, OUTPUT);
14 digitalWrite(pin_, LOW);
15 delay(1);
16 }
17
18 // true - button is pressed.
19 bool Read() {
20 Update(millis());
21 return changed_;
22 }
23
24 void Update(uint32_t time_now) {
25 if ((time_now - debounce_timestamp_) < 150) {
26 changed_ = false;
27 return;
28 }
29
30 int val = Read_Internal();
31 changed_ = on_ != val;
32
33 if (changed_) { // Has the toggle switch changed?
34 on_ = val; // Set the new toggle switch value.
35 // Protect against debouncing artifacts by putting a 200ms delay from the
36 // last time the value changed.
37 if ((time_now - debounce_timestamp_) > 150) {
38 //++curr_val_; // ... and increment the value.
39 debounce_timestamp_ = time_now;
40 }
41 }
42 }
43
44 private:
45 bool Read_Internal() {
46 // Toggle the pin back to INPUT and take a reading.
47 pinMode(pin_, INPUT);
48 bool on = (digitalRead(pin_) == HIGH);
49 // Switch the pin back to output so that we can enable the
50 // pulldown resister.
51 pinMode(pin_, OUTPUT);
52 digitalWrite(pin_, LOW);
53 return on;
54 }
55
56
57 int pin_;
58 bool on_;
59 uint32_t debounce_timestamp_;
60 bool changed_;
61};
62
63// This is the new type that is built into the midi shield.
65 public:
66 MidiShieldButton(int pin) : pin_(pin) {
67 pinMode(pin_, INPUT_PULLUP);
68 delay(1);
69 }
70
71 bool Read() {
72 // Toggle the pin back to INPUT and take a reading.
73 int val = digitalRead(pin_) == LOW;
74
75 return val;
76 }
77 private:
78 int pin_;
79};
80
82 public:
83 Potentiometer(int sensor_pin) : sensor_pin_(sensor_pin) {}
84 float Read() {
85 float avg = 0.0;
86 // Filter by reading the value multiple times and taking
87 // the average.
88 for (int i = 0; i < 8; ++i) {
89 avg += analogRead(sensor_pin_);
90 }
91 avg = avg / 8.0f;
92 return avg;
93 }
94 private:
95 int sensor_pin_;
96};
97
99
100
102 public:
103 explicit CountingButton(int but_pin) : button_(but_pin), curr_val_(0) {
104 debounce_timestamp_ = millis();
105 on_ = Read();
106 }
107
108 void Update(uint32_t time_now) {
109 bool clicked = mButton.clicked();
110 FASTLED_DBG("clicked: " << clicked);
111 bool val = Read() || mButton.clicked();
112 bool changed = val != on_;
113
114 if (clicked) {
115 ++curr_val_;
116 debounce_timestamp_ = time_now;
117 return;
118 }
119
120 if (changed) { // Has the toggle switch changed?
121 on_ = val; // Set the new toggle switch value.
122 // Protect against debouncing artifacts by putting a 200ms delay from the
123 // last time the value changed.
124 if ((time_now - debounce_timestamp_) > 16) {
125 if (on_) {
126 ++curr_val_; // ... and increment the value.
127 }
128 debounce_timestamp_ = time_now;
129 }
130 }
131 }
132
133 int curr_val() const { return curr_val_; }
134
135 private:
136 bool Read() {
137 return button_.Read();
138 }
139
140 DigitalButton button_;
141 bool on_;
142 int curr_val_;
143 unsigned long debounce_timestamp_;
144 Button mButton = Button("Counting Button");
145};
146
148 public:
149 ColorSelector(int sensor_pin) : but_(sensor_pin) {}
150
151 void Update() {
152 but_.Update(millis());
153 }
154
155 int curr_val() const { return but_.curr_val() % 7; }
156 private:
157 CountingButton but_;
158};
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16