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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 SCROLL_START_MILLIS 4000
  16. #define SCROLL_END_MILLIS 6000 // should be > SCROLL_START_MILLIS
  17. #define PING_START_MILLIS 8000 // should be > SCROLL_END_MILLIS
  18. #define SEND_INTERVAL 3000
  19. unsigned long next_send = 0;
  20. String my_mac;
  21. bool is_salam;
  22. uint8_t peer_mac_addr[6];
  23. esp_now_peer_info_t peerInfo;
  24. bool nearby = false;
  25. #define NUM_FRAMES 21
  26. enum frameType {
  27. FRAME_ME,
  28. FRAME_ME2US,
  29. FRAME_US,
  30. FRAME_US2U,
  31. FRAME_U
  32. };
  33. frameType frames[NUM_FRAMES] = {
  34. FRAME_ME, FRAME_ME2US, FRAME_US, FRAME_US2U,
  35. FRAME_U, FRAME_U, FRAME_U,
  36. FRAME_US2U,
  37. FRAME_US, FRAME_US,
  38. FRAME_US2U,
  39. FRAME_U, FRAME_U, FRAME_U,
  40. FRAME_US2U, FRAME_US, FRAME_ME2US,
  41. FRAME_ME, FRAME_ME, FRAME_ME, FRAME_ME
  42. };
  43. const unsigned short *frame2image(frameType frame, bool is_salam) {
  44. switch (frame) {
  45. case FRAME_ME:
  46. return is_salam ? salam : shalom;
  47. case FRAME_ME2US:
  48. return is_salam ? salamorph : shalomorph;
  49. case FRAME_US:
  50. return together;
  51. case FRAME_US2U:
  52. return is_salam ? shalomorph : salamorph;
  53. case FRAME_U:
  54. return is_salam ? shalom : salam;
  55. }
  56. }
  57. int current_frame = 0;
  58. unsigned long last_flip = 0;
  59. #define DURATION 100
  60. TFT_eSPI tft = TFT_eSPI();
  61. TFT_eSprite background = TFT_eSprite(&tft);
  62. TFT_eSprite talisSprite = TFT_eSprite(&tft);
  63. TFT_eSprite mettaSprite = TFT_eSprite(&tft);
  64. // lifted from MacAddress.c
  65. bool str2mac(char *buf, uint8_t *mac) {
  66. char cs[18];
  67. char *token;
  68. char *next; //Unused but required
  69. int i;
  70. strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
  71. for (i = 0; i < 6; i++) {
  72. token = strtok((i == 0) ? cs : NULL, ":"); //Find first or next token
  73. if (!token) { //No more tokens found
  74. return false;
  75. }
  76. mac[i] = strtol(token, &next, 16);
  77. }
  78. return true;
  79. }
  80. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  81. nearby = (status == ESP_NOW_SEND_SUCCESS);
  82. if (DEBUG) {
  83. Serial.println(nearby ? "Nearby" : "Not nearby");
  84. }
  85. }
  86. void setup() {
  87. pinMode(BUTTON1PIN, INPUT_PULLUP);
  88. pinMode(BUTTON2PIN, INPUT_PULLUP);
  89. Serial.begin(115200);
  90. delay(1000);
  91. Serial.println("=== Talis-Metta ===");
  92. // determine peer mac address
  93. my_mac = WiFi.macAddress();
  94. Serial.println("My MAC:");
  95. Serial.println(my_mac);
  96. if (my_mac.equals(SHALOM)) {
  97. if (DEBUG) {
  98. Serial.println("Shalom");
  99. }
  100. is_salam = false;
  101. str2mac(SALAM, peer_mac_addr);
  102. } else {
  103. if (DEBUG) {
  104. Serial.println("Salam");
  105. }
  106. is_salam = true;
  107. str2mac(SHALOM, peer_mac_addr);
  108. }
  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. background.createSprite(240, 135);
  120. background.setSwapBytes(true);
  121. talisSprite.createSprite(120, 135);
  122. talisSprite.setSwapBytes(true);
  123. talisSprite.pushImage(0, 0, 120, 135, talis);
  124. mettaSprite.createSprite(120, 135);
  125. mettaSprite.setSwapBytes(true);
  126. mettaSprite.pushImage(0, 0, 120, 135, metta);
  127. }
  128. void loop() {
  129. if (digitalRead(BUTTON1PIN) == LOW) {
  130. esp_sleep_enable_ext0_wakeup(BUTTON2PIN, LOW);
  131. esp_deep_sleep_start();
  132. }
  133. unsigned long nowmillis = millis();
  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. }
  145. } else {
  146. current_frame = 0;
  147. }
  148. background.pushImage(0, 0, 240, 135, frame2image(frames[current_frame], is_salam));
  149. if (nowmillis < SCROLL_START_MILLIS) {
  150. talisSprite.pushToSprite(&background, 0, 0);
  151. mettaSprite.pushToSprite(&background, 120, 0);
  152. } else if (nowmillis < SCROLL_END_MILLIS) {
  153. long scrollPixels = 123 * (nowmillis - SCROLL_START_MILLIS) / (SCROLL_END_MILLIS - SCROLL_START_MILLIS);
  154. talisSprite.pushToSprite(&background, -scrollPixels, 0);
  155. mettaSprite.pushToSprite(&background, 120 + scrollPixels, 0);
  156. }
  157. background.pushSprite(0, 0);
  158. }