طاليس-مِتّة | TALIS-METTA | טליס-מטא
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

talismetta.ino 8.5KB

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