طاليس-مِتّة | TALIS-METTA | טליס-מטא
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

talismetta.ino 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include <WiFi.h>
  2. #include <esp_now.h>
  3. #include <TFT_eSPI.h>
  4. #include "config.h"
  5. #include "shalom.h"
  6. #include "shalombg.h"
  7. #include "salam.h"
  8. #include "salambg.h"
  9. #include "togetherbg.h"
  10. #include "talis.h"
  11. #include "metta.h"
  12. #include "loving.h"
  13. #include "kindness.h"
  14. #include "mybird.h"
  15. #include "yourbird.h"
  16. #define DEBUG false // note: it's easier to read the MAC address if DEBUG is false
  17. #define BUTTON1PIN ((gpio_num_t)35)
  18. #define BUTTON2PIN ((gpio_num_t)0)
  19. #define INTRO_FREEZE_MILLIS 4000
  20. #define INTRO_SCROLL_MILLIS 2000
  21. #define PING_START_MILLIS 8000 // should be > INTRO_FREEZE_MILLIS + INTRO_SCROLL_MILLIS
  22. #define METTA_FREEZE_MILLIS 1000
  23. #define METTA_SCROLL_MILLIS 2000
  24. #define OUTRO_SCROLL_MILLIS 2000
  25. #define OUTRO_FREEZE_MILLIS 1000
  26. unsigned long outro_start;
  27. #define MYBIRD_WIDTH 30
  28. #define MYBIRD_HEIGHT 36
  29. #define MYBIRD_X 20
  30. #define MYBIRD_Y 10
  31. #define YOURBIRD_WIDTH 32
  32. #define YOURBIRD_HEIGHT 33
  33. #define YOURBIRD_X 53
  34. #define YOURBIRD_Y 5
  35. #define SEND_INTERVAL 3000
  36. unsigned long next_send = 0;
  37. String my_mac;
  38. bool is_salam;
  39. bool metta_from_you;
  40. unsigned long metta_from_me_since = 0;
  41. #define MANTRA_I "May I be filled with loving kindness"
  42. #define MANTRA_YOU "May you be filled with loving kindness"
  43. uint8_t peer_mac_addr[6];
  44. esp_now_peer_info_t peerInfo;
  45. bool nearby = false;
  46. #define NUM_FRAMES 9
  47. enum frameType {
  48. FRAME_ME,
  49. FRAME_US,
  50. FRAME_U
  51. };
  52. frameType frames[NUM_FRAMES] = {
  53. FRAME_ME, FRAME_US, FRAME_U,
  54. FRAME_U, FRAME_US, FRAME_U,
  55. FRAME_U, FRAME_US, FRAME_ME
  56. };
  57. const unsigned short *frame2image(frameType frame, bool is_salam, bool metta_from_me, bool metta_from_you) {
  58. if (!metta_from_you) {
  59. if (metta_from_me) {
  60. return is_salam ? salambg : shalombg;
  61. } else {
  62. return is_salam ? salam : shalom;
  63. }
  64. } else { // metta_from_you
  65. switch (frame) {
  66. case FRAME_ME:
  67. return metta_from_me ? (is_salam ? salambg : shalombg) : (is_salam ? salam : shalom);
  68. case FRAME_US:
  69. return metta_from_me ? togetherbg : (is_salam ? salam : shalom);
  70. case FRAME_U:
  71. return (metta_from_me) ? (is_salam ? shalombg : salambg) : (is_salam ? salam : shalom);
  72. }
  73. }
  74. }
  75. int current_frame = 0;
  76. unsigned long last_flip = 0;
  77. #define DURATION 250
  78. TFT_eSPI tft = TFT_eSPI();
  79. TFT_eSprite background = TFT_eSprite(&tft);
  80. TFT_eSprite leftSprite = TFT_eSprite(&tft);
  81. TFT_eSprite rightSprite = TFT_eSprite(&tft);
  82. TFT_eSprite mybirdSprite = TFT_eSprite(&tft);
  83. TFT_eSprite yourbirdSprite = TFT_eSprite(&tft);
  84. // lifted from MacAddress.c
  85. bool str2mac(char *buf, uint8_t *mac) {
  86. char cs[18];
  87. char *token;
  88. char *next; //Unused but required
  89. int i;
  90. strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
  91. for (i = 0; i < 6; i++) {
  92. token = strtok((i == 0) ? cs : NULL, ":"); //Find first or next token
  93. if (!token) { //No more tokens found
  94. return false;
  95. }
  96. mac[i] = strtol(token, &next, 16);
  97. }
  98. return true;
  99. }
  100. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  101. nearby = (status == ESP_NOW_SEND_SUCCESS);
  102. if (!nearby) {
  103. metta_from_you = false;
  104. }
  105. if (DEBUG) {
  106. Serial.println(nearby ? "||" : "|");
  107. }
  108. }
  109. void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
  110. metta_from_you = (data_len == strlen(MANTRA_YOU)) && !strncmp((const char *)data, MANTRA_YOU, data_len);
  111. if (DEBUG) {
  112. // char macStr[18];
  113. // snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
  114. // mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  115. // Serial.print("Packet Recv from: "); Serial.println(macStr);
  116. char msg[64];
  117. int msglen = min(data_len, 63);
  118. strncpy(msg, (const char *)data, msglen);
  119. msg[msglen] = '\0';
  120. Serial.print("<<< "); Serial.println(msg);
  121. Serial.print(metta_from_me_since ? "_/" : "__");
  122. Serial.println(metta_from_you ? "\\_" : "__");
  123. }
  124. }
  125. void setup() {
  126. pinMode(BUTTON1PIN, INPUT_PULLUP);
  127. pinMode(BUTTON2PIN, INPUT_PULLUP);
  128. Serial.begin(115200);
  129. delay(1000);
  130. Serial.println("=== Talis-Metta ===");
  131. // determine peer mac address
  132. my_mac = WiFi.macAddress();
  133. Serial.print("My MAC: ");
  134. Serial.println(my_mac);
  135. if (my_mac.equals(SHALOM)) {
  136. if (DEBUG) {
  137. Serial.println("Shalom");
  138. }
  139. is_salam = false;
  140. str2mac(SALAM, peer_mac_addr);
  141. } else {
  142. if (DEBUG) {
  143. Serial.println("Salam");
  144. }
  145. is_salam = true;
  146. str2mac(SHALOM, peer_mac_addr);
  147. }
  148. outro_start = 0;
  149. metta_from_me_since = 0;
  150. metta_from_you = false;
  151. WiFi.mode(WIFI_STA);
  152. esp_now_init();
  153. memcpy(peerInfo.peer_addr, peer_mac_addr, 6);
  154. peerInfo.channel = 0;
  155. peerInfo.encrypt = false;
  156. esp_now_add_peer(&peerInfo);
  157. esp_now_register_send_cb(OnDataSent);
  158. esp_now_register_recv_cb(OnDataRecv);
  159. tft.init();
  160. tft.setRotation(3); // was 1
  161. tft.setSwapBytes(true);
  162. tft.fillScreen(TFT_BLACK);
  163. background.createSprite(240, 135);
  164. background.setSwapBytes(true);
  165. leftSprite.createSprite(120, 135);
  166. leftSprite.setSwapBytes(true);
  167. rightSprite.createSprite(120, 135);
  168. rightSprite.setSwapBytes(true);
  169. mybirdSprite.createSprite(MYBIRD_WIDTH, MYBIRD_HEIGHT);
  170. mybirdSprite.setSwapBytes(true);
  171. mybirdSprite.pushImage(0, 0, MYBIRD_WIDTH, MYBIRD_HEIGHT, mybird);
  172. yourbirdSprite.createSprite(YOURBIRD_WIDTH, YOURBIRD_HEIGHT);
  173. yourbirdSprite.setSwapBytes(true);
  174. yourbirdSprite.pushImage(0, 0, YOURBIRD_WIDTH, YOURBIRD_HEIGHT, yourbird);
  175. }
  176. 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) {
  177. unsigned long nowmillis = millis();
  178. if (nowmillis >= start_time && nowmillis < start_time + freeze_duration + scroll_duration) {
  179. unsigned long freeze_start = is_reverse ? start_time + scroll_duration : start_time;
  180. unsigned long scroll_start = is_reverse ? start_time : start_time + freeze_duration;
  181. leftSprite.pushImage(0, 0, 120, 135, left_image);
  182. rightSprite.pushImage(0, 0, 120, 135, right_image);
  183. if (nowmillis >= freeze_start && nowmillis < freeze_start + freeze_duration) {
  184. leftSprite.pushToSprite(&background, 0, 0);
  185. rightSprite.pushToSprite(&background, 120, 0);
  186. } else {
  187. long scroll_pixels = 123 * (nowmillis - scroll_start) / scroll_duration;
  188. if (is_reverse) {
  189. scroll_pixels = 123 - scroll_pixels;
  190. }
  191. leftSprite.pushToSprite(&background, -scroll_pixels, 0);
  192. rightSprite.pushToSprite(&background, 120 + scroll_pixels, 0);
  193. }
  194. }
  195. }
  196. void loop() {
  197. unsigned long nowmillis = millis();
  198. if (digitalRead(BUTTON1PIN) == LOW) {
  199. outro_start = nowmillis;
  200. }
  201. if (digitalRead(BUTTON2PIN) == LOW) {
  202. metta_from_me_since = nowmillis;
  203. }
  204. if (nowmillis > next_send) {
  205. if (DEBUG) {
  206. Serial.print(">>> ");
  207. Serial.println(metta_from_me_since ? MANTRA_YOU : MANTRA_I);
  208. }
  209. esp_now_send(
  210. peerInfo.peer_addr,
  211. metta_from_me_since ? (const uint8_t *)MANTRA_YOU : (const uint8_t *)MANTRA_I,
  212. metta_from_me_since ? strlen(MANTRA_YOU) : strlen(MANTRA_I));
  213. next_send = nowmillis + SEND_INTERVAL;
  214. }
  215. if (nowmillis > PING_START_MILLIS) {
  216. if ((nowmillis - last_flip) > DURATION || nowmillis < last_flip) {
  217. current_frame = (current_frame + 1) % NUM_FRAMES;
  218. last_flip = nowmillis;
  219. background.pushSprite(0, 0);
  220. }
  221. } else {
  222. current_frame = 0;
  223. }
  224. // FRAME_ME or animation frame
  225. background.pushImage(
  226. 0, 0, 240, 135,
  227. frame2image(frames[current_frame], is_salam, metta_from_me_since, metta_from_you));
  228. // mybird (steady, pulse if metta_from_me_Since)
  229. if (!metta_from_me_since || frames[current_frame] == FRAME_ME || frames[current_frame] == FRAME_US) {
  230. mybirdSprite.pushToSprite(&background, MYBIRD_X, MYBIRD_Y, TFT_BLACK);
  231. }
  232. // yourbird (steady if nearby, heart beat if metta_from_you)
  233. if (nearby && (!metta_from_you || frames[current_frame] == FRAME_U || frames[current_frame] == FRAME_US)) {
  234. yourbirdSprite.pushToSprite(&background, YOURBIRD_X, YOURBIRD_Y, TFT_BLACK);
  235. }
  236. // Scroll overlays
  237. if (outro_start) { // Outro scroll overlay
  238. if (nowmillis - outro_start > OUTRO_FREEZE_MILLIS + OUTRO_SCROLL_MILLIS) { // outro over. shut down
  239. esp_sleep_enable_ext0_wakeup(BUTTON1PIN, LOW);
  240. esp_deep_sleep_start();
  241. } else { // do outro reverse scroll and freeze
  242. do_scroll(talis, metta, outro_start, OUTRO_FREEZE_MILLIS, OUTRO_SCROLL_MILLIS, true);
  243. }
  244. } else if (nowmillis < INTRO_FREEZE_MILLIS + INTRO_SCROLL_MILLIS) { // Intro scroll overlay
  245. do_scroll(talis, metta, 0, INTRO_FREEZE_MILLIS, INTRO_SCROLL_MILLIS, false);
  246. } else if (metta_from_me_since && nowmillis < metta_from_me_since + METTA_FREEZE_MILLIS + METTA_SCROLL_MILLIS) {
  247. do_scroll(loving, kindness, metta_from_me_since, METTA_FREEZE_MILLIS, METTA_SCROLL_MILLIS, false);
  248. }
  249. background.pushSprite(0, 0);
  250. }