|
@@ -9,19 +9,25 @@
|
9
|
9
|
#include "together.h"
|
10
|
10
|
#include "talis.h"
|
11
|
11
|
#include "metta.h"
|
|
12
|
+#include "loving.h"
|
|
13
|
+#include "kindness.h"
|
12
|
14
|
|
13
|
|
-#define DEBUG false
|
|
15
|
+#define DEBUG true
|
14
|
16
|
|
15
|
17
|
#define BUTTON1PIN ((gpio_num_t)35)
|
16
|
18
|
#define BUTTON2PIN ((gpio_num_t)0)
|
17
|
19
|
|
18
|
|
-#define INTRO_START_MILLIS 4000
|
19
|
|
-#define INTRO_END_MILLIS 6000 // should be > INTRO_START_MILLIS
|
20
|
|
-#define PING_START_MILLIS 8000 // should be > INTRO_END_MILLIS
|
|
20
|
+#define INTRO_FREEZE_MILLIS 4000
|
|
21
|
+#define INTRO_SCROLL_MILLIS 2000
|
21
|
22
|
|
22
|
|
-#define OUTRO_DURATION_MILLIS 2000
|
23
|
|
-#define POST_OUTRO_MILLIS 1000
|
24
|
|
-unsigned long outro_start = 0;
|
|
23
|
+#define PING_START_MILLIS 8000 // should be > INTRO_FREEZE_MILLIS + INTRO_SCROLL_MILLIS
|
|
24
|
+
|
|
25
|
+#define METTA_FREEZE_MILLIS 1000
|
|
26
|
+#define METTA_SCROLL_MILLIS 2000
|
|
27
|
+
|
|
28
|
+#define OUTRO_SCROLL_MILLIS 2000
|
|
29
|
+#define OUTRO_FREEZE_MILLIS 1000
|
|
30
|
+unsigned long outro_start;
|
25
|
31
|
|
26
|
32
|
|
27
|
33
|
#define SEND_INTERVAL 3000
|
|
@@ -30,6 +36,13 @@ unsigned long next_send = 0;
|
30
|
36
|
|
31
|
37
|
String my_mac;
|
32
|
38
|
bool is_salam;
|
|
39
|
+bool metta_from_you;
|
|
40
|
+unsigned long metta_from_me_start = 0;
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+#define MANTRA_I "May I be filled with loving kindness"
|
|
44
|
+#define MANTRA_WE "May we be filled with loving kindness"
|
|
45
|
+
|
33
|
46
|
|
34
|
47
|
uint8_t peer_mac_addr[6];
|
35
|
48
|
esp_now_peer_info_t peerInfo;
|
|
@@ -76,8 +89,8 @@ unsigned long last_flip = 0;
|
76
|
89
|
|
77
|
90
|
TFT_eSPI tft = TFT_eSPI();
|
78
|
91
|
TFT_eSprite background = TFT_eSprite(&tft);
|
79
|
|
-TFT_eSprite talisSprite = TFT_eSprite(&tft);
|
80
|
|
-TFT_eSprite mettaSprite = TFT_eSprite(&tft);
|
|
92
|
+TFT_eSprite leftSprite = TFT_eSprite(&tft);
|
|
93
|
+TFT_eSprite rightSprite = TFT_eSprite(&tft);
|
81
|
94
|
|
82
|
95
|
|
83
|
96
|
// lifted from MacAddress.c
|
|
@@ -99,11 +112,32 @@ bool str2mac(char *buf, uint8_t *mac) {
|
99
|
112
|
|
100
|
113
|
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
|
101
|
114
|
nearby = (status == ESP_NOW_SEND_SUCCESS);
|
|
115
|
+ if (!nearby) {
|
|
116
|
+ metta_from_you = false;
|
|
117
|
+ }
|
102
|
118
|
if (DEBUG) {
|
103
|
119
|
Serial.println(nearby ? "Nearby" : "Not nearby");
|
104
|
120
|
}
|
105
|
121
|
}
|
106
|
122
|
|
|
123
|
+void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
|
|
124
|
+ metta_from_you = (data_len == strlen(MANTRA_WE)) && !strncmp((const char *)data, MANTRA_WE, data_len);
|
|
125
|
+ if (DEBUG) {
|
|
126
|
+ // char macStr[18];
|
|
127
|
+ // snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
128
|
+ // mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
|
|
129
|
+ // Serial.print("Packet Recv from: "); Serial.println(macStr);
|
|
130
|
+ //String msg = "";
|
|
131
|
+ //for (int i = 0 ; i < data_len; i++) {
|
|
132
|
+ // msg.concat(char(data[i]));
|
|
133
|
+ //}
|
|
134
|
+ //Serial.print("Packet Recv message: "); Serial.println(msg);
|
|
135
|
+ Serial.print("Packet Recv message length: "); Serial.println(data_len);
|
|
136
|
+ Serial.print("Metta from other: "); Serial.println(metta_from_you);
|
|
137
|
+ }
|
|
138
|
+}
|
|
139
|
+
|
|
140
|
+
|
107
|
141
|
void setup() {
|
108
|
142
|
pinMode(BUTTON1PIN, INPUT_PULLUP);
|
109
|
143
|
pinMode(BUTTON2PIN, INPUT_PULLUP);
|
|
@@ -129,6 +163,8 @@ void setup() {
|
129
|
163
|
str2mac(SHALOM, peer_mac_addr);
|
130
|
164
|
}
|
131
|
165
|
outro_start = 0;
|
|
166
|
+ metta_from_me_start = 0;
|
|
167
|
+ metta_from_you = false;
|
132
|
168
|
|
133
|
169
|
WiFi.mode(WIFI_STA);
|
134
|
170
|
esp_now_init();
|
|
@@ -137,6 +173,7 @@ void setup() {
|
137
|
173
|
peerInfo.encrypt = false;
|
138
|
174
|
esp_now_add_peer(&peerInfo);
|
139
|
175
|
esp_now_register_send_cb(OnDataSent);
|
|
176
|
+ esp_now_register_recv_cb(OnDataRecv);
|
140
|
177
|
|
141
|
178
|
tft.init();
|
142
|
179
|
tft.setRotation(3); // was 1
|
|
@@ -146,13 +183,34 @@ void setup() {
|
146
|
183
|
background.createSprite(240, 135);
|
147
|
184
|
background.setSwapBytes(true);
|
148
|
185
|
|
149
|
|
- talisSprite.createSprite(120, 135);
|
150
|
|
- talisSprite.setSwapBytes(true);
|
151
|
|
- talisSprite.pushImage(0, 0, 120, 135, talis);
|
|
186
|
+ leftSprite.createSprite(120, 135);
|
|
187
|
+ leftSprite.setSwapBytes(true);
|
|
188
|
+ leftSprite.pushImage(0, 0, 120, 135, talis);
|
152
|
189
|
|
153
|
|
- mettaSprite.createSprite(120, 135);
|
154
|
|
- mettaSprite.setSwapBytes(true);
|
155
|
|
- mettaSprite.pushImage(0, 0, 120, 135, metta);
|
|
190
|
+ rightSprite.createSprite(120, 135);
|
|
191
|
+ rightSprite.setSwapBytes(true);
|
|
192
|
+ rightSprite.pushImage(0, 0, 120, 135, metta);
|
|
193
|
+}
|
|
194
|
+
|
|
195
|
+void do_scroll(const unsigned short *left_image, const unsigned short *right_image, unsigned long start_time, unsigned long freeze_duration, unsigned long scroll_duration, bool is_reverse) {
|
|
196
|
+ unsigned long nowmillis = millis();
|
|
197
|
+ if (nowmillis >= start_time && nowmillis < start_time + freeze_duration + scroll_duration) {
|
|
198
|
+ unsigned long freeze_start = is_reverse ? start_time + scroll_duration : start_time;
|
|
199
|
+ unsigned long scroll_start = is_reverse ? start_time : start_time + freeze_duration;
|
|
200
|
+ leftSprite.pushImage(0, 0, 120, 135, left_image);
|
|
201
|
+ rightSprite.pushImage(0, 0, 120, 135, right_image);
|
|
202
|
+ if (nowmillis >= freeze_start && nowmillis < freeze_start + freeze_duration) {
|
|
203
|
+ leftSprite.pushToSprite(&background, 0, 0);
|
|
204
|
+ rightSprite.pushToSprite(&background, 120, 0);
|
|
205
|
+ } else {
|
|
206
|
+ long scroll_pixels = 123 * (nowmillis - scroll_start) / scroll_duration;
|
|
207
|
+ if (is_reverse) {
|
|
208
|
+ scroll_pixels = 123 - scroll_pixels;
|
|
209
|
+ }
|
|
210
|
+ leftSprite.pushToSprite(&background, -scroll_pixels, 0);
|
|
211
|
+ rightSprite.pushToSprite(&background, 120 + scroll_pixels, 0);
|
|
212
|
+ }
|
|
213
|
+ }
|
156
|
214
|
}
|
157
|
215
|
|
158
|
216
|
void loop() {
|
|
@@ -160,13 +218,21 @@ void loop() {
|
160
|
218
|
if (digitalRead(BUTTON1PIN) == LOW) {
|
161
|
219
|
outro_start = nowmillis;
|
162
|
220
|
}
|
|
221
|
+ if (digitalRead(BUTTON2PIN) == LOW) {
|
|
222
|
+ metta_from_me_start = nowmillis;
|
|
223
|
+ }
|
163
|
224
|
if (nowmillis > next_send) {
|
164
|
225
|
if (DEBUG) {
|
165
|
|
- Serial.println("Pinging...");
|
166
|
|
- } esp_now_send(peerInfo.peer_addr, (const uint8_t *)"<3", 2);
|
|
226
|
+ Serial.print("Sending: ");
|
|
227
|
+ Serial.println(metta_from_me_start ? MANTRA_WE : MANTRA_I);
|
|
228
|
+ }
|
|
229
|
+ esp_now_send(
|
|
230
|
+ peerInfo.peer_addr,
|
|
231
|
+ metta_from_me_start ? (const uint8_t *)MANTRA_WE : (const uint8_t *)MANTRA_I,
|
|
232
|
+ metta_from_me_start ? strlen(MANTRA_WE) : strlen(MANTRA_I));
|
167
|
233
|
next_send = nowmillis + SEND_INTERVAL;
|
168
|
234
|
}
|
169
|
|
- if ((nowmillis > PING_START_MILLIS) && nearby) {
|
|
235
|
+ if (nowmillis > PING_START_MILLIS) {
|
170
|
236
|
if ((nowmillis - last_flip) > DURATION || nowmillis < last_flip) {
|
171
|
237
|
current_frame = (current_frame + 1) % NUM_FRAMES;
|
172
|
238
|
last_flip = nowmillis;
|
|
@@ -175,29 +241,24 @@ void loop() {
|
175
|
241
|
} else {
|
176
|
242
|
current_frame = 0;
|
177
|
243
|
}
|
178
|
|
- // FRAMEW_ME or morph
|
179
|
|
- if (nowmillis >= INTRO_START_MILLIS && (!outro_start || nowmillis < outro_start + OUTRO_DURATION_MILLIS)) {
|
180
|
|
- background.pushImage(0, 0, 240, 135, frame2image(frames[current_frame], is_salam));
|
181
|
|
- }
|
|
244
|
+
|
|
245
|
+ // FRAME_ME or animation frame
|
|
246
|
+ background.pushImage(
|
|
247
|
+ 0, 0, 240, 135,
|
|
248
|
+ frame2image((metta_from_me_start && metta_from_you) ? frames[current_frame] : FRAME_ME, is_salam));
|
|
249
|
+
|
|
250
|
+ // Scroll overlays
|
182
|
251
|
if (outro_start) { // Outro scroll overlay
|
183
|
|
- if (nowmillis - outro_start > OUTRO_DURATION_MILLIS + POST_OUTRO_MILLIS) { // outro over. shut down
|
|
252
|
+ if (nowmillis - outro_start > OUTRO_FREEZE_MILLIS + OUTRO_SCROLL_MILLIS) { // outro over. shut down
|
184
|
253
|
esp_sleep_enable_ext0_wakeup(BUTTON1PIN, LOW);
|
185
|
254
|
esp_deep_sleep_start();
|
186
|
|
- } else if (nowmillis - outro_start > OUTRO_DURATION_MILLIS) {
|
187
|
|
- talisSprite.pushToSprite(&background, 0, 0);
|
188
|
|
- mettaSprite.pushToSprite(&background, 120, 0);
|
189
|
|
- } else { // do outro
|
190
|
|
- long scrollPixels = 123 - (123 * (nowmillis - outro_start) / OUTRO_DURATION_MILLIS);
|
191
|
|
- talisSprite.pushToSprite(&background, -scrollPixels, 0);
|
192
|
|
- mettaSprite.pushToSprite(&background, 120 + scrollPixels, 0);
|
|
255
|
+ } else { // do outro reverse scroll and freeze
|
|
256
|
+ do_scroll(talis, metta, outro_start, OUTRO_FREEZE_MILLIS, OUTRO_SCROLL_MILLIS, true);
|
193
|
257
|
}
|
194
|
|
- } else if (nowmillis < INTRO_START_MILLIS) { // Intro scroll overlay
|
195
|
|
- talisSprite.pushToSprite(&background, 0, 0);
|
196
|
|
- mettaSprite.pushToSprite(&background, 120, 0);
|
197
|
|
- } else if (nowmillis < INTRO_END_MILLIS) {
|
198
|
|
- long scrollPixels = 123 * (nowmillis - INTRO_START_MILLIS) / (INTRO_END_MILLIS - INTRO_START_MILLIS);
|
199
|
|
- talisSprite.pushToSprite(&background, -scrollPixels, 0);
|
200
|
|
- mettaSprite.pushToSprite(&background, 120 + scrollPixels, 0);
|
|
258
|
+ } else if (nowmillis < INTRO_FREEZE_MILLIS + INTRO_SCROLL_MILLIS) { // Intro scroll overlay
|
|
259
|
+ do_scroll(talis, metta, 0, INTRO_FREEZE_MILLIS, INTRO_SCROLL_MILLIS, false);
|
|
260
|
+ } else if (metta_from_me_start && nowmillis < metta_from_me_start + METTA_FREEZE_MILLIS + METTA_SCROLL_MILLIS) {
|
|
261
|
+ do_scroll(loving, kindness, metta_from_me_start, METTA_FREEZE_MILLIS, METTA_SCROLL_MILLIS, false);
|
201
|
262
|
}
|
202
|
263
|
background.pushSprite(0, 0);
|
203
|
264
|
|