طاليس-مِتّة | 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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #define DEBUG false
  13. #define BUTTON1PIN ((gpio_num_t)35)
  14. #define BUTTON2PIN ((gpio_num_t)0)
  15. #define INTRO_START_MILLIS 4000
  16. #define INTRO_END_MILLIS 6000 // should be > INTRO_START_MILLIS
  17. #define PING_START_MILLIS 8000 // should be > INTRO_END_MILLIS
  18. #define OUTRO_DURATION_MILLIS 2000
  19. #define POST_OUTRO_MILLIS 1000
  20. unsigned long outro_start = 0;
  21. #define SEND_INTERVAL 3000
  22. unsigned long next_send = 0;
  23. String my_mac;
  24. bool is_salam;
  25. uint8_t peer_mac_addr[6];
  26. esp_now_peer_info_t peerInfo;
  27. bool nearby = false;
  28. #define NUM_FRAMES 23
  29. enum frameType {
  30. FRAME_ME,
  31. FRAME_ME2US,
  32. FRAME_US,
  33. FRAME_US2U,
  34. FRAME_U
  35. };
  36. frameType frames[NUM_FRAMES] = {
  37. FRAME_ME, FRAME_ME2US, FRAME_US, FRAME_US2U, FRAME_U, FRAME_U, FRAME_U,
  38. FRAME_US2U, FRAME_US, FRAME_US, FRAME_US2U, FRAME_U, FRAME_U, FRAME_U,
  39. FRAME_US2U, FRAME_US, FRAME_ME2US, FRAME_ME, FRAME_ME, FRAME_ME,
  40. FRAME_ME, FRAME_ME, FRAME_ME
  41. };
  42. const unsigned short *frame2image(frameType frame, bool is_salam) {
  43. switch (frame) {
  44. case FRAME_ME:
  45. return is_salam ? salam : shalom;
  46. case FRAME_ME2US:
  47. return is_salam ? salamorph : shalomorph;
  48. case FRAME_US:
  49. return together;
  50. case FRAME_US2U:
  51. return is_salam ? shalomorph : salamorph;
  52. case FRAME_U:
  53. return is_salam ? shalom : salam;
  54. }
  55. }
  56. int current_frame = 0;
  57. unsigned long last_flip = 0;
  58. #define DURATION 100
  59. TFT_eSPI tft = TFT_eSPI();
  60. TFT_eSprite background = TFT_eSprite(&tft);
  61. TFT_eSprite talisSprite = TFT_eSprite(&tft);
  62. TFT_eSprite mettaSprite = TFT_eSprite(&tft);
  63. // lifted from MacAddress.c
  64. bool str2mac(char *buf, uint8_t *mac) {
  65. char cs[18];
  66. char *token;
  67. char *next; //Unused but required
  68. int i;
  69. strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
  70. for (i = 0; i < 6; i++) {
  71. token = strtok((i == 0) ? cs : NULL, ":"); //Find first or next token
  72. if (!token) { //No more tokens found
  73. return false;
  74. }
  75. mac[i] = strtol(token, &next, 16);
  76. }
  77. return true;
  78. }
  79. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  80. nearby = (status == ESP_NOW_SEND_SUCCESS);
  81. if (DEBUG) {
  82. Serial.println(nearby ? "Nearby" : "Not nearby");
  83. }
  84. }
  85. void setup() {
  86. pinMode(BUTTON1PIN, INPUT_PULLUP);
  87. pinMode(BUTTON2PIN, INPUT_PULLUP);
  88. Serial.begin(115200);
  89. delay(1000);
  90. Serial.println("=== Talis-Metta ===");
  91. // determine peer mac address
  92. my_mac = WiFi.macAddress();
  93. Serial.println("My MAC:");
  94. Serial.println(my_mac);
  95. if (my_mac.equals(SHALOM)) {
  96. if (DEBUG) {
  97. Serial.println("Shalom");
  98. }
  99. is_salam = false;
  100. str2mac(SALAM, peer_mac_addr);
  101. } else {
  102. if (DEBUG) {
  103. Serial.println("Salam");
  104. }
  105. is_salam = true;
  106. str2mac(SHALOM, peer_mac_addr);
  107. }
  108. outro_start = 0;
  109. WiFi.mode(WIFI_STA);
  110. esp_now_init();
  111. memcpy(peerInfo.peer_addr, peer_mac_addr, 6);
  112. peerInfo.channel = 0;
  113. peerInfo.encrypt = false;
  114. esp_now_add_peer(&peerInfo);
  115. esp_now_register_send_cb(OnDataSent);
  116. tft.init();
  117. tft.setRotation(3); // was 1
  118. tft.setSwapBytes(true);
  119. tft.fillScreen(TFT_BLACK);
  120. background.createSprite(240, 135);
  121. background.setSwapBytes(true);
  122. talisSprite.createSprite(120, 135);
  123. talisSprite.setSwapBytes(true);
  124. talisSprite.pushImage(0, 0, 120, 135, talis);
  125. mettaSprite.createSprite(120, 135);
  126. mettaSprite.setSwapBytes(true);
  127. mettaSprite.pushImage(0, 0, 120, 135, metta);
  128. }
  129. void loop() {
  130. unsigned long nowmillis = millis();
  131. if (digitalRead(BUTTON1PIN) == LOW) {
  132. outro_start = nowmillis;
  133. }
  134. if (nowmillis > next_send) {
  135. if (DEBUG) {
  136. Serial.println("Pinging...");
  137. } esp_now_send(peerInfo.peer_addr, (const uint8_t *)"<3", 2);
  138. next_send = nowmillis + SEND_INTERVAL;
  139. }
  140. if ((nowmillis > PING_START_MILLIS) && nearby) {
  141. if ((nowmillis - last_flip) > DURATION || nowmillis < last_flip) {
  142. current_frame = (current_frame + 1) % NUM_FRAMES;
  143. last_flip = nowmillis;
  144. background.pushSprite(0, 0);
  145. }
  146. } else {
  147. current_frame = 0;
  148. }
  149. // FRAMEW_ME or morph
  150. if (nowmillis >= INTRO_START_MILLIS && (!outro_start || nowmillis < outro_start + OUTRO_DURATION_MILLIS)) {
  151. background.pushImage(0, 0, 240, 135, frame2image(frames[current_frame], is_salam));
  152. }
  153. if (outro_start) { // Outro scroll overlay
  154. if (nowmillis - outro_start > OUTRO_DURATION_MILLIS + POST_OUTRO_MILLIS) { // outro over. shut down
  155. esp_sleep_enable_ext0_wakeup(BUTTON1PIN, LOW);
  156. esp_deep_sleep_start();
  157. } else if (nowmillis - outro_start > OUTRO_DURATION_MILLIS) {
  158. talisSprite.pushToSprite(&background, 0, 0);
  159. mettaSprite.pushToSprite(&background, 120, 0);
  160. } else { // do outro
  161. long scrollPixels = 123 - (123 * (nowmillis - outro_start) / OUTRO_DURATION_MILLIS);
  162. talisSprite.pushToSprite(&background, -scrollPixels, 0);
  163. mettaSprite.pushToSprite(&background, 120 + scrollPixels, 0);
  164. }
  165. } else if (nowmillis < INTRO_START_MILLIS) { // Intro scroll overlay
  166. talisSprite.pushToSprite(&background, 0, 0);
  167. mettaSprite.pushToSprite(&background, 120, 0);
  168. } else if (nowmillis < INTRO_END_MILLIS) {
  169. long scrollPixels = 123 * (nowmillis - INTRO_START_MILLIS) / (INTRO_END_MILLIS - INTRO_START_MILLIS);
  170. talisSprite.pushToSprite(&background, -scrollPixels, 0);
  171. mettaSprite.pushToSprite(&background, 120 + scrollPixels, 0);
  172. }
  173. background.pushSprite(0, 0);
  174. }