forked from Juerd/m5atom-matrixflut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm5atom-httpbutton.cpp
119 lines (95 loc) · 2.42 KB
/
m5atom-httpbutton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <FastLED.h>
#include <SPIFFS.h>
#include <WiFi.h>
#include <WiFiSettings.h>
#include <ArduinoOTA.h>
#include <HTTPClient.h>
const int ledpin = 27;
const int numleds = 25;
const int buttonpin = 39;
const int debounce_threshhold = 0;
enum button_state_t {
NOT_PRESSED = 0,
JUST_PRESSED,
HELD,
JUST_RELEASED,
};
bool button_state;
int debounce_rounds;
String url;
CRGB leds[numleds];
WiFiClient wificlient;
void setup_ota()
{
ArduinoOTA.setHostname(WiFiSettings.hostname.c_str());
ArduinoOTA.setPassword(WiFiSettings.password.c_str());
ArduinoOTA.begin();
}
void setup()
{
button_state = false;
debounce_rounds = 0;
FastLED.addLeds<WS2812B, ledpin, GRB>(leds, numleds);
FastLED.setBrightness(20);
Serial.begin(115200);
SPIFFS.begin(true);
pinMode(buttonpin, INPUT);
url = WiFiSettings.string("url", 8, 512, "http://10.10.10.151/cm?cmnd=power1%202");
WiFiSettings.onWaitLoop = []() {
static CHSV color(0, 255, 255);
color.hue += 10;
FastLED.showColor(color);
if (!digitalRead(buttonpin)) WiFiSettings.portal();
return 50;
};
WiFiSettings.onSuccess = []() {
FastLED.showColor(CRGB::Green);
setup_ota();
delay(200);
};
WiFiSettings.onPortal = []() {
setup_ota();
};
WiFiSettings.onPortalWaitLoop = []() {
static CHSV color(0, 255, 255);
color.saturation--;
FastLED.showColor(color);
ArduinoOTA.handle();
};
WiFiSettings.connect();
}
enum button_state_t check_button()
{
bool inst_button_state = !digitalRead(buttonpin);
if (inst_button_state != button_state) {
debounce_rounds++;
} else if (debounce_rounds > 0) {
debounce_rounds--;
}
if (debounce_rounds > debounce_threshhold) {
debounce_rounds = 0;
button_state = inst_button_state;
return button_state ? JUST_PRESSED : JUST_RELEASED;
}
return button_state ? HELD : NOT_PRESSED;
}
void loop()
{
static CHSV color(0, 255, 0);
button_state_t btn = check_button();
if (btn == JUST_PRESSED) {
color.value = 255;
color.hue += 10;
}
FastLED.showColor(color);
if (btn == JUST_PRESSED) {
HTTPClient http;
http.begin(url.c_str());
http.GET();
http.end();
}
if (btn == NOT_PRESSED && color.value > 0) {
color.value -= 1;
}
ArduinoOTA.handle();
}