طاليس-مِتّة | TALIS-METTA | טליס-מטא
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

talismetta.ino 8.4KB

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