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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 BRIGHTNESS 31 // 0..255
  20. #define INTRO_FREEZE_MILLIS 4000
  21. #define INTRO_SCROLL_MILLIS 2000
  22. #define PING_START_MILLIS 8000 // should be > INTRO_FREEZE_MILLIS + INTRO_SCROLL_MILLIS
  23. #define METTA_FREEZE_MILLIS 1000
  24. #define METTA_SCROLL_MILLIS 2000
  25. #define OUTRO_SCROLL_MILLIS 2000
  26. #define OUTRO_FREEZE_MILLIS 1000
  27. unsigned long outro_start;
  28. #define MYBIRD_WIDTH 30
  29. #define MYBIRD_HEIGHT 36
  30. #define MYBIRD_X 20
  31. #define MYBIRD_Y 10
  32. #define YOURBIRD_WIDTH 32
  33. #define YOURBIRD_HEIGHT 33
  34. #define YOURBIRD_X 53
  35. #define YOURBIRD_Y 5
  36. #define STATUS_SEND_MILLIS 1000
  37. #define STATUS_RECEIVE_MILLIS 1058 // 2X23X23 to make things resonate
  38. #define STATUS_COLOR TFT_SKYBLUE
  39. #define SEND_INTERVAL 3000
  40. unsigned long next_send = 0;
  41. String my_mac;
  42. bool is_salam;
  43. bool metta_from_you;
  44. unsigned long metta_from_me_since = 0;
  45. #define MANTRA_I "May I be filled with loving kindness"
  46. #define MANTRA_YOU "May you be filled with loving kindness"
  47. uint8_t peer_mac_addr[6];
  48. esp_now_peer_info_t peerInfo;
  49. bool nearby = false;
  50. #define NUM_FRAMES 9
  51. enum frameType {
  52. FRAME_ME,
  53. FRAME_US,
  54. FRAME_U
  55. };
  56. frameType frames[NUM_FRAMES] = {
  57. FRAME_ME, FRAME_ME, FRAME_US,
  58. FRAME_U, FRAME_U, FRAME_US,
  59. FRAME_US, FRAME_US, FRAME_US
  60. };
  61. const unsigned short *frame2image(frameType frame, bool is_salam, bool metta_from_me, bool metta_from_you) {
  62. if (!metta_from_me || !metta_from_you) {
  63. return is_salam ? salambg : shalombg;
  64. }
  65. switch (frame) {
  66. case FRAME_ME:
  67. return is_salam ? salambg : shalombg;
  68. case FRAME_US:
  69. return togetherbg;
  70. case FRAME_U:
  71. return is_salam ? shalombg : salambg;
  72. }
  73. }
  74. int current_frame = 0;
  75. unsigned long last_flip = 0;
  76. #define DURATION 200
  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. TFT_eSprite statusSprite = TFT_eSprite(&tft);
  84. // lifted from MacAddress.c
  85. bool str2mac(char *buf, uint8_t *mac) {
  86. char cs[18];
  87. char *token;
  88. char *next; //Unused but required
  89. int i;
  90. strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
  91. for (i = 0; i < 6; i++) {
  92. token = strtok((i == 0) ? cs : NULL, ":"); //Find first or next token
  93. if (!token) { //No more tokens found
  94. return false;
  95. }
  96. mac[i] = strtol(token, &next, 16);
  97. }
  98. return true;
  99. }
  100. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  101. nearby = (status == ESP_NOW_SEND_SUCCESS);
  102. if (!nearby) {
  103. metta_from_you = false;
  104. }
  105. if (DEBUG) {
  106. Serial.println(nearby ? "||" : "|");
  107. }
  108. }
  109. void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
  110. metta_from_you = (data_len == strlen(MANTRA_YOU)) && !strncmp((const char *)data, MANTRA_YOU, data_len);
  111. if (DEBUG) {
  112. // char macStr[18];
  113. // snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
  114. // mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  115. // Serial.print("Packet Recv from: "); Serial.println(macStr);
  116. char msg[64];
  117. int msglen = min(data_len, 63);
  118. strncpy(msg, (const char *)data, msglen);
  119. msg[msglen] = '\0';
  120. Serial.print("<<< "); Serial.println(msg);
  121. Serial.print(metta_from_me_since ? "_/" : "__");
  122. Serial.println(metta_from_you ? "\\_" : "__");
  123. }
  124. }
  125. void setup() {
  126. pinMode(BUTTON1PIN, INPUT_PULLUP);
  127. pinMode(BUTTON2PIN, INPUT_PULLUP);
  128. Serial.begin(115200);
  129. delay(1000);
  130. Serial.println("=== Talis-Metta ===");
  131. // determine peer mac address
  132. my_mac = WiFi.macAddress();
  133. Serial.print("My MAC: ");
  134. Serial.println(my_mac);
  135. if (my_mac.equals(SHALOM)) {
  136. if (DEBUG) {
  137. Serial.println("Shalom");
  138. }
  139. is_salam = false;
  140. str2mac(SALAM, peer_mac_addr);
  141. } else {
  142. if (DEBUG) {
  143. Serial.println("Salam");
  144. }
  145. is_salam = true;
  146. str2mac(SHALOM, peer_mac_addr);
  147. }
  148. outro_start = 0;
  149. metta_from_me_since = 0;
  150. metta_from_you = false;
  151. WiFi.mode(WIFI_STA);
  152. esp_now_init();
  153. memcpy(peerInfo.peer_addr, peer_mac_addr, 6);
  154. peerInfo.channel = 0;
  155. peerInfo.encrypt = false;
  156. esp_now_add_peer(&peerInfo);
  157. esp_now_register_send_cb(OnDataSent);
  158. esp_now_register_recv_cb(OnDataRecv);
  159. tft.init();
  160. tft.setRotation(3); // was 1
  161. tft.setSwapBytes(true);
  162. analogWrite(TFT_BL,BRIGHTNESS);
  163. tft.fillScreen(TFT_BLACK);
  164. background.createSprite(240, 135);
  165. background.setSwapBytes(true);
  166. leftSprite.createSprite(120, 135);
  167. leftSprite.setSwapBytes(true);
  168. rightSprite.createSprite(120, 135);
  169. rightSprite.setSwapBytes(true);
  170. mybirdSprite.createSprite(MYBIRD_WIDTH, MYBIRD_HEIGHT);
  171. mybirdSprite.setSwapBytes(true);
  172. mybirdSprite.pushImage(0, 0, MYBIRD_WIDTH, MYBIRD_HEIGHT, mybird);
  173. yourbirdSprite.createSprite(YOURBIRD_WIDTH, YOURBIRD_HEIGHT);
  174. yourbirdSprite.setSwapBytes(true);
  175. yourbirdSprite.pushImage(0, 0, YOURBIRD_WIDTH, YOURBIRD_HEIGHT, yourbird);
  176. statusSprite.createSprite(240, 18);
  177. statusSprite.setSwapBytes(true);
  178. }
  179. 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) {
  180. unsigned long nowmillis = millis();
  181. if (nowmillis >= start_time && nowmillis < start_time + freeze_duration + scroll_duration) {
  182. unsigned long freeze_start = is_reverse ? start_time + scroll_duration : start_time;
  183. unsigned long scroll_start = is_reverse ? start_time : start_time + freeze_duration;
  184. leftSprite.pushImage(0, 0, 120, 135, left_image);
  185. rightSprite.pushImage(0, 0, 120, 135, right_image);
  186. if (nowmillis >= freeze_start && nowmillis < freeze_start + freeze_duration) {
  187. leftSprite.pushToSprite(&background, 0, 0);
  188. rightSprite.pushToSprite(&background, 120, 0);
  189. } else {
  190. long scroll_pixels = 123 * (nowmillis - scroll_start) / scroll_duration;
  191. if (is_reverse) {
  192. scroll_pixels = 123 - scroll_pixels;
  193. }
  194. leftSprite.pushToSprite(&background, -scroll_pixels, 0);
  195. rightSprite.pushToSprite(&background, 120 + scroll_pixels, 0);
  196. }
  197. }
  198. }
  199. void do_status(bool is_send) {
  200. unsigned long cycle = is_send ? STATUS_SEND_MILLIS : STATUS_RECEIVE_MILLIS;
  201. float quotient = float(millis() % cycle) / cycle;
  202. if (!is_send) {
  203. quotient = 1.0 - quotient;
  204. }
  205. int32_t r_in = 130 * quotient; // a bit larger than 240/2
  206. int32_t r_out = 130 * pow(quotient, 0.75); // grows faster
  207. statusSprite.fillSprite(TFT_BLACK);
  208. statusSprite.fillEllipse(120, 9, r_out, 10, STATUS_COLOR);
  209. statusSprite.fillEllipse(120, 9, r_in, 10, TFT_BLACK);
  210. statusSprite.pushToSprite(&background, 0, 116, TFT_BLACK);
  211. }
  212. void loop() {
  213. unsigned long nowmillis = millis();
  214. if (digitalRead(BUTTON1PIN) == LOW) {
  215. outro_start = nowmillis;
  216. }
  217. if (digitalRead(BUTTON2PIN) == LOW) {
  218. metta_from_me_since = nowmillis;
  219. }
  220. if (nowmillis > next_send) {
  221. if (DEBUG) {
  222. Serial.print(">>> ");
  223. Serial.println(metta_from_me_since ? MANTRA_YOU : MANTRA_I);
  224. }
  225. esp_now_send(
  226. peerInfo.peer_addr,
  227. metta_from_me_since ? (const uint8_t *)MANTRA_YOU : (const uint8_t *)MANTRA_I,
  228. metta_from_me_since ? strlen(MANTRA_YOU) : strlen(MANTRA_I));
  229. next_send = nowmillis + SEND_INTERVAL;
  230. }
  231. if (nowmillis > PING_START_MILLIS) {
  232. if ((nowmillis - last_flip) > DURATION || nowmillis < last_flip) {
  233. current_frame = (current_frame + 1) % NUM_FRAMES;
  234. last_flip = nowmillis;
  235. background.pushSprite(0, 0);
  236. }
  237. } else {
  238. current_frame = 0;
  239. }
  240. // FRAME_ME or animation frame
  241. background.pushImage(
  242. 0, 0, 240, 135,
  243. frame2image(frames[current_frame], is_salam, metta_from_me_since, metta_from_you));
  244. // mybird (always on)
  245. if (true /* !metta_from_me_since || frames[current_frame] == FRAME_ME || frames[current_frame] == FRAME_US */) {
  246. mybirdSprite.pushToSprite(&background, MYBIRD_X, MYBIRD_Y, TFT_BLACK);
  247. }
  248. // yourbird (if nearby)
  249. if (nearby /* && (!metta_from_you || frames[current_frame] == FRAME_U || frames[current_frame] == FRAME_US) */) {
  250. yourbirdSprite.pushToSprite(&background, YOURBIRD_X, YOURBIRD_Y, TFT_BLACK);
  251. }
  252. // "send" status
  253. if (metta_from_me_since) {
  254. do_status(true);
  255. }
  256. // "receive" status
  257. if (metta_from_you) {
  258. do_status(false);
  259. }
  260. // Scroll overlays
  261. if (outro_start) { // Outro scroll overlay
  262. if (nowmillis - outro_start > OUTRO_FREEZE_MILLIS + OUTRO_SCROLL_MILLIS) { // outro over. shut down
  263. esp_sleep_enable_ext0_wakeup(BUTTON1PIN, LOW);
  264. esp_deep_sleep_start();
  265. } else { // do outro reverse scroll and freeze
  266. do_scroll(talis, metta, outro_start, OUTRO_FREEZE_MILLIS, OUTRO_SCROLL_MILLIS, true);
  267. }
  268. } else if (nowmillis < INTRO_FREEZE_MILLIS + INTRO_SCROLL_MILLIS) { // Intro scroll overlay
  269. do_scroll(talis, metta, 0, INTRO_FREEZE_MILLIS, INTRO_SCROLL_MILLIS, false);
  270. } else if (metta_from_me_since && nowmillis < metta_from_me_since + METTA_FREEZE_MILLIS + METTA_SCROLL_MILLIS) {
  271. do_scroll(loving, kindness, metta_from_me_since, METTA_FREEZE_MILLIS, METTA_SCROLL_MILLIS, false);
  272. }
  273. background.pushSprite(0, 0);
  274. }