FastLED 3.9.15
Loading...
Searching...
No Matches
buttons.h
Go to the documentation of this file.
1#pragma once
2
3#include <Arduino.h>
4#include "fl/ui/ui.h"
5#include "fl/log/log.h"
6#include "fl/system/delay.h"
7
8
9// Done by hand. Old school.
11 public:
12 ToggleButton(int pin) : mPin(pin), mOn(false), mDebounceTimestamp(0), mChanged(false) {
13 pinMode(mPin, OUTPUT);
14 digitalWrite(mPin, LOW);
15 fl::delay(1);
16 }
17
18 // true - button is pressed.
19 bool Read() {
21 return mChanged;
22 }
23
24 void Update(uint32_t time_now) {
25 if ((time_now - mDebounceTimestamp) < 150) {
26 mChanged = false;
27 return;
28 }
29
30 int val = Read_Internal();
31 mChanged = mOn != val;
32
33 if (mChanged) { // Has the toggle switch changed?
34 mOn = 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 - mDebounceTimestamp) > 150) {
38 //++curr_val_; // ... and increment the value.
39 mDebounceTimestamp = time_now;
40 }
41 }
42 }
43
44 private:
46 // Toggle the pin back to INPUT and take a reading.
47 pinMode(mPin, INPUT);
48 bool on = (digitalRead(mPin) == HIGH);
49 // Switch the pin back to output so that we can enable the
50 // pulldown resister.
51 pinMode(mPin, OUTPUT);
52 digitalWrite(mPin, LOW);
53 return on;
54 }
55
56
57 int mPin;
58 bool mOn;
61};
62
63// This is the new type that is built into the midi shield.
65 public:
66 MidiShieldButton(int pin) : mPin(pin) {
67 pinMode(mPin, INPUT_PULLUP);
68 fl::delay(1);
69 }
70
71 bool Read() {
72 // Toggle the pin back to INPUT and take a reading.
73 int val = digitalRead(mPin) == LOW;
74
75 return val;
76 }
77 private:
78 int mPin;
79};
80
82 public:
83 Potentiometer(int sensor_pin) : mSensorPin(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(mSensorPin);
90 }
91 avg = avg / 8.0f;
92 return avg;
93 }
94 private:
96};
97
99
100
102 public:
103 explicit CountingButton(int but_pin) : mButton(but_pin), mCurrVal(0), mUIButton("Counting fl::UIButton") {
105 mOn = Read();
106 }
107
108 void Update(uint32_t time_now) {
109 bool clicked = mUIButton.clicked();
110 bool val = Read() || mUIButton.clicked();
111 bool changed = val != mOn;
112
113 if (clicked) {
114 ++mCurrVal;
115 mDebounceTimestamp = time_now;
116 return;
117 }
118
119 if (changed) { // Has the toggle switch changed?
120 mOn = val; // Set the new toggle switch value.
121 // Protect against debouncing artifacts by putting a 200ms delay from the
122 // last time the value changed.
123 if ((time_now - mDebounceTimestamp) > 16) {
124 if (mOn) {
125 ++mCurrVal; // ... and increment the value.
126 }
127 mDebounceTimestamp = time_now;
128 }
129 }
130 }
131
132 int curr_val() const { return mCurrVal; }
133
134 private:
135 bool Read() {
136 return mButton.Read();
137 }
138
140 bool mOn;
142 unsigned long mDebounceTimestamp;
144};
145
147 public:
148 ColorSelector(int sensor_pin) : mBut(sensor_pin) {}
149
150 void Update() {
151 mBut.Update(fl::millis());
152 }
153
154 int curr_val() const { return mBut.curr_val() % 7; }
155 private:
157};
MidiShieldButton DigitalButton
Definition buttons.h:98
int curr_val() const
Definition buttons.h:154
ColorSelector(int sensor_pin)
Definition buttons.h:148
CountingButton mBut
Definition buttons.h:156
void Update()
Definition buttons.h:150
int curr_val() const
Definition buttons.h:132
fl::UIButton mUIButton
Definition buttons.h:143
bool Read()
Definition buttons.h:135
void Update(uint32_t time_now)
Definition buttons.h:108
CountingButton(int but_pin)
Definition buttons.h:103
unsigned long mDebounceTimestamp
Definition buttons.h:142
DigitalButton mButton
Definition buttons.h:139
MidiShieldButton(int pin)
Definition buttons.h:66
Potentiometer(int sensor_pin)
Definition buttons.h:83
int mSensorPin
Definition buttons.h:95
float Read()
Definition buttons.h:84
bool mChanged
Definition buttons.h:60
ToggleButton(int pin)
Definition buttons.h:12
uint32_t mDebounceTimestamp
Definition buttons.h:59
bool Read_Internal()
Definition buttons.h:45
bool Read()
Definition buttons.h:19
void Update(uint32_t time_now)
Definition buttons.h:24
Delay utilities for FastLED Includes nanosecond-precision delays, cycle counting, and microsecond del...
Centralized logging categories for FastLED hardware interfaces and subsystems.
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
void delay(u32 ms, bool run_async=true) FL_NOEXCEPT
Public delay wrapper that keeps bare Arduino delay() preferred after using fl::delay; while still all...
Definition delay.h:98
Aggregator header for the fl/ui/ family of per-element UI types.