طاليس-مِتّة | TALIS-METTA | טליס-מטא
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

talismetta.ino 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 STATUS_SEND_MILLIS 1000
  36. #define STATUS_RECEIVE_MILLIS 1058 // 2X23X23 to make things resonate
  37. #define STATUS_COLOR TFT_SKYBLUE
  38. #define SEND_INTERVAL 3000
  39. unsigned long next_send = 0;
  40. String my_mac;
  41. bool is_salam;
  42. bool metta_from_you;
  43. unsigned long metta_from_me_since = 0;
  44. #define MANTRA_I "May I be filled with loving kindness"
  45. #define MANTRA_YOU "May you be filled with loving kindness"
  46. uint8_t peer_mac_addr[6];
  47. esp_now_peer_info_t peerInfo;
  48. bool nearby = false;
  49. #define NUM_FRAMES 9
  50. enum frameType {
  51. FRAME_ME,
  52. FRAME_US,
  53. FRAME_U
  54. };
  55. frameType frames[NUM_FRAMES] = {
  56. FRAME_ME, FRAME_ME, FRAME_US,
  57. FRAME_U, FRAME_U, FRAME_US,
  58. FRAME_US, FRAME_US, FRAME_US
  59. };
  60. const unsigned short *frame2image(frameType frame, bool is_salam, bool metta_from_me, bool metta_from_you) {
  61. if (!metta_from_me || !metta_from_you) {
  62. return is_salam ? salambg : shalombg;
  63. }
  64. switch (frame) {
  65. case FRAME_ME:
  66. return is_salam ? salambg : shalombg;
  67. case FRAME_US:
  68. return togetherbg;
  69. case FRAME_U:
  70. return is_salam ? shalombg : salambg;
  71. }
  72. }
  73. int current_frame = 0;
  74. unsigned long last_flip = 0;
  75. #define DURATION 250
  76. TFT_eSPI tft = TFT_eSPI();
  77. TFT_eSprite background = TFT_eSprite(&tft);
  78. TFT_eSprite leftSprite = TFT_eSprite(&tft);
  79. TFT_eSprite rightSprite = TFT_eSprite(&tft);
  80. TFT_eSprite mybirdSprite = TFT_eSprite(&tft);
  81. TFT_eSprite yourbirdSprite = TFT_eSprite(&tft);
  82. TFT_eSprite statusSprite = 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. statusSprite.createSprite(240, 18);
  175. statusSprite.setSwapBytes(true);
  176. }
  177. 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) {
  178. unsigned long nowmillis = millis();
  179. if (nowmillis >= start_time && nowmillis < start_time + freeze_duration + scroll_duration) {
  180. unsigned long freeze_start = is_reverse ? start_time + scroll_duration : start_time;
  181. unsigned long scroll_start = is_reverse ? start_time : start_time + freeze_duration;
  182. leftSprite.pushImage(0, 0, 120, 135, left_image);
  183. rightSprite.pushImage(0, 0, 120, 135, right_image);
  184. if (nowmillis >= freeze_start && nowmillis < freeze_start + freeze_duration) {
  185. leftSprite.pushToSprite(&background, 0, 0);
  186. rightSprite.pushToSprite(&background, 120, 0);
  187. } else {
  188. long scroll_pixels = 123 * (nowmillis - scroll_start) / scroll_duration;
  189. if (is_reverse) {
  190. scroll_pixels = 123 - scroll_pixels;
  191. }
  192. leftSprite.pushToSprite(&background, -scroll_pixels, 0);
  193. rightSprite.pushToSprite(&background, 120 + scroll_pixels, 0);
  194. }
  195. }
  196. }
  197. void do_status(bool is_send) {
  198. unsigned long cycle = is_send ? STATUS_SEND_MILLIS : STATUS_RECEIVE_MILLIS;
  199. float quotient = float(millis() % cycle) / cycle;
  200. if (!is_send) {
  201. quotient = 1.0 - quotient;
  202. }
  203. int32_t r_in = 130 * quotient; // a bit larger than 240/2
  204. int32_t r_out = 130 * pow(quotient, 0.75); // grows faster
  205. statusSprite.fillSprite(TFT_BLACK);
  206. statusSprite.fillEllipse(120, 9, r_out, 10, STATUS_COLOR);
  207. statusSprite.fillEllipse(120, 9, r_in, 10, TFT_BLACK);
  208. statusSprite.pushToSprite(&background, 0, 116, TFT_BLACK);
  209. }
  210. void loop() {
  211. unsigned long nowmillis = millis();
  212. if (digitalRead(BUTTON1PIN) == LOW) {
  213. outro_start = nowmillis;
  214. }
  215. if (digitalRead(BUTTON2PIN) == LOW) {
  216. metta_from_me_since = nowmillis;
  217. }
  218. if (nowmillis > next_send) {
  219. if (DEBUG) {
  220. Serial.print(">>> ");
  221. Serial.println(metta_from_me_since ? MANTRA_YOU : MANTRA_I);
  222. }
  223. esp_now_send(
  224. peerInfo.peer_addr,
  225. metta_from_me_since ? (const uint8_t *)MANTRA_YOU : (const uint8_t *)MANTRA_I,
  226. metta_from_me_since ? strlen(MANTRA_YOU) : strlen(MANTRA_I));
  227. next_send = nowmillis + SEND_INTERVAL;
  228. }
  229. if (nowmillis > PING_START_MILLIS) {
  230. if ((nowmillis - last_flip) > DURATION || nowmillis < last_flip) {
  231. current_frame = (current_frame + 1) % NUM_FRAMES;
  232. last_flip = nowmillis;
  233. background.pushSprite(0, 0);
  234. }
  235. } else {
  236. current_frame = 0;
  237. }
  238. // FRAME_ME or animation frame
  239. background.pushImage(
  240. 0, 0, 240, 135,
  241. frame2image(frames[current_frame], is_salam, metta_from_me_since, metta_from_you));
  242. // mybird (always on)
  243. if (true /* !metta_from_me_since || frames[current_frame] == FRAME_ME || frames[current_frame] == FRAME_US */) {
  244. mybirdSprite.pushToSprite(&background, MYBIRD_X, MYBIRD_Y, TFT_BLACK);
  245. }
  246. // yourbird (if nearby)
  247. if (nearby /* && (!metta_from_you || frames[current_frame] == FRAME_U || frames[current_frame] == FRAME_US) */) {
  248. yourbirdSprite.pushToSprite(&background, YOURBIRD_X, YOURBIRD_Y, TFT_BLACK);
  249. }
  250. // "send" status
  251. if (metta_from_me_since) {
  252. do_status(true);
  253. }
  254. // "receive" status
  255. if (metta_from_you) {
  256. do_status(false);
  257. }
  258. // Scroll overlays
  259. if (outro_start) { // Outro scroll overlay
  260. if (nowmillis - outro_start > OUTRO_FREEZE_MILLIS + OUTRO_SCROLL_MILLIS) { // outro over. shut down
  261. esp_sleep_enable_ext0_wakeup(BUTTON1PIN, LOW);
  262. esp_deep_sleep_start();
  263. } else { // do outro reverse scroll and freeze
  264. do_scroll(talis, metta, outro_start, OUTRO_FREEZE_MILLIS, OUTRO_SCROLL_MILLIS, true);
  265. }
  266. } else if (nowmillis < INTRO_FREEZE_MILLIS + INTRO_SCROLL_MILLIS) { // Intro scroll overlay
  267. do_scroll(talis, metta, 0, INTRO_FREEZE_MILLIS, INTRO_SCROLL_MILLIS, false);
  268. } else if (metta_from_me_since && nowmillis < metta_from_me_since + METTA_FREEZE_MILLIS + METTA_SCROLL_MILLIS) {
  269. do_scroll(loving, kindness, metta_from_me_since, METTA_FREEZE_MILLIS, METTA_SCROLL_MILLIS, false);
  270. }
  271. background.pushSprite(0, 0);
  272. }