طاليس-مِتّة | TALIS-METTA | טליס-מטא
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

talismetta.ino 7.7KB

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