-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkipknop.ino
209 lines (174 loc) · 4.77 KB
/
Skipknop.ino
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "Arduino.h"
#include <espnow.h>
#include "ESP8266WiFi.h"
// ==================================================================
// Skipbutton things
const uint8_t numButtons = 3;
const uint8_t buttonPins[] = { D5, D1, D2 }; // Skip, volup, voldown
bool buttonStates[] = { false, false, false };
bool noButtonsPressed = true;
// Skip: revspace/button/skip - jump_fwd
// Stop: revspace/button/stop
#define MQTT_TOPIC_BASE "#Urevspace/button/"
#define TOPIC_SKIP "skip"
#define TOPIC_VOL_UP "vol_up"
#define TOPIC_VOL_DN "vol_down"
#define TOPIC_SHUF "shuffle"
#define TOPIC_STOP "stop"
#define MQTT_MSG_SKIP "jump_fwd"
#define MQTT_MSG "hackalot"
typedef enum {
ACTION_NOTHING,
ACTION_SKIP,
ACTION_VOL_UP,
ACTION_VOL_DOWN,
ACTION_SHUFFLE,
ACTION_STOP
} action_t;
static action_t action = ACTION_NOTHING;
typedef enum {
E_SEND,
E_DONE,
E_SLEEP
} skip_mode_t;
static skip_mode_t mode = E_SEND;
// ==================================================================
// ESP-Now Things
#define ESP_NOW_CHANNEL 9
uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
// ==================================================================
// Cut here ✂️------------------------------------------------------
bool buttonHeld(uint8_t butt, unsigned long timeout = 1250UL) {
unsigned long startMillis = millis();
while (!digitalRead(buttonPins[butt])) {
if (millis() - startMillis >= timeout) {
return true;
}
delay(5);
}
return false;
}
void doShutdown() {
// Disable power
digitalWrite(D0, LOW);
while (true) {
delay(10);
}
}
void setup() {
// Keep power enabled
pinMode(D0, OUTPUT);
digitalWrite(D0, HIGH);
// initialise pins
for (uint8_t i = 0; i < numButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
// Debounce
delay(5);
// Read pins
for (uint8_t i = 0; i < numButtons; i++) {
bool state = !digitalRead(buttonPins[i]);
buttonStates[i] = state;
if (state)
noButtonsPressed = false;
}
Serial.begin(115200);
Serial.println("\nESPNOW-SKIP");
// Do nothing if no buttons were pressed
if (noButtonsPressed) {
Serial.println("Nothing pressed, shutting down");
doShutdown();
}
// Initialize esp-now on right channel
WiFi.forceSleepWake();
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin("aapnoot", "miesmies", ESP_NOW_CHANNEL, NULL, false);
delay(10);
WiFi.disconnect();
wifi_set_channel(ESP_NOW_CHANNEL);
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
doShutdown();
}
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, ESP_NOW_CHANNEL, NULL, 0);
if (buttonStates[0]) { // Main dome button
if (!buttonHeld(0))
action = ACTION_SKIP;
else
action = ACTION_STOP;
return;
}
if (buttonStates[1]) { // Top button
action = ACTION_VOL_UP;
return;
}
if (buttonStates[2]) { // Bottom button
action = ACTION_VOL_DOWN;
return;
}
}
void loop() {
switch (mode) {
case E_SEND:
switch (action) {
case ACTION_SKIP:
send_topic_text(MQTT_TOPIC_BASE TOPIC_SKIP, MQTT_MSG_SKIP); // Different message for backwards-compatibility
break;
case ACTION_VOL_UP:
send_topic_text(MQTT_TOPIC_BASE TOPIC_VOL_UP, MQTT_MSG);
break;
case ACTION_VOL_DOWN:
send_topic_text(MQTT_TOPIC_BASE TOPIC_VOL_DN, MQTT_MSG);
break;
case ACTION_SHUFFLE:
send_topic_text(MQTT_TOPIC_BASE TOPIC_SHUF, MQTT_MSG);
break;
case ACTION_STOP:
send_topic_text(MQTT_TOPIC_BASE TOPIC_STOP, MQTT_MSG);
break;
default:
send_topic_text(MQTT_TOPIC_BASE TOPIC_SKIP, MQTT_MSG_SKIP);
break;
}
// Move to next section to check for held buttons
delay(10);
mode = E_DONE;
break;
case E_DONE:
// First send done
mode = E_SLEEP;
switch (action) {
case ACTION_VOL_UP:
if (buttonHeld(1, 900))
mode = E_SEND;
break;
case ACTION_VOL_DOWN:
if (buttonHeld(2, 900))
mode = E_SEND;
break;
case ACTION_STOP:
if (buttonHeld(0, 2500)) {
action = ACTION_SHUFFLE;
mode = E_SEND;
}
break;
}
break;
case E_SLEEP:
default:
delay(5);
Serial.print("Shutting down...");
doShutdown();
break;
}
}
// ==================================================================
// ESP-Now Functions
bool send_topic_text(const char *topic, const char *text) {
uint8_t *mac = broadcastAddress;
char buf[250];
int n = snprintf(buf, sizeof(buf), "%s %s", topic, text);
return esp_now_send(mac, (uint8_t *)buf, n) == 0;
}