Advertisement
igendel

TTGO IMU Starfield demo

Apr 4th, 2021 (edited)
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // by Ido Gendel, 2021
  2. // Using the Arduino IDE with ESP32 board support installed, and the TTGO TWatch libraries
  3. // (See https://github.com/Xinyuan-LilyGO/TTGO_TWatch_Library)
  4. // Share and enjoy!
  5.  
  6. #define LILYGO_WATCH_2020_V1
  7. #include <LilyGoWatch.h>
  8.  
  9. #define STARS 120U
  10. #define STAR_SIZE 3U
  11.  
  12. TTGOClass *watch;
  13.  
  14. struct tStar {
  15.  
  16.   int16_t x, y;
  17.   uint16_t z;
  18.   int32_t ix, iy;  
  19.   int32_t shade;
  20.  
  21. } star[STARS];
  22.  
  23. //=========================================================
  24. void initStars(void) {
  25.  
  26.   uint32_t n;
  27.  
  28.   for (n = 0; n < STARS; n++) {
  29.    
  30.     star[n].x = random(65536);
  31.     star[n].y = random(65536);
  32.     star[n].z = random(65536);
  33.     star[n].shade = random(2);
  34.  
  35.   }
  36.  
  37. }
  38.  
  39. //=========================================================
  40. void moveStars(void) {
  41.  
  42.   uint32_t n, sBase;
  43.   int32_t ax, ay, az;
  44.   float factor;
  45.  
  46.   Accel acc;
  47.  
  48.   watch->bma->getAccel(acc);
  49.   ax = acc.x >> 2;
  50.   ay = acc.y >> 2;
  51.   az = acc.z >> 3;
  52.  
  53.   // Erase and draw each one in turn, to avoid flicker
  54.   for (n = 0; n < STARS; n++) {
  55.  
  56.     watch->tft->fillRect(star[n].ix, star[n].iy, STAR_SIZE, STAR_SIZE, 0);
  57.    
  58.     // 5-bit brightness value; screen uses 565 color scheme
  59.     sBase = (256 - (star[n].z >> 8)) >> 3;
  60.     star[n].x += ax;
  61.     star[n].y += ay;
  62.     star[n].z -= az;    
  63.     // Avoid potential division-by-zero
  64.     if (0 == star[n].z) star[n].z = 1;
  65.  
  66.     factor = 240.0 / (float)star[n].z;
  67.     star[n].ix = int((float)(star[n].x) * factor) + 120;
  68.     star[n].iy = int((float)(star[n].y) * factor) + 120;    
  69.  
  70.     watch->tft->fillRect(star[n].ix, star[n].iy, STAR_SIZE, STAR_SIZE,
  71.                          star[n].shade ? sBase : (sBase * 2113));    
  72.    
  73.   }
  74.  
  75. }
  76.  
  77. //=========================================================
  78. void setup() {
  79.    
  80.     watch = TTGOClass::getWatch();
  81.     watch->begin();    
  82.     watch->openBL();
  83.     watch->bma->begin();
  84.     watch->bma->enableAccel();
  85.  
  86.     initStars();
  87.  
  88. }
  89.  
  90. //=========================================================
  91. void loop() {
  92.   moveStars();
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement