igendel icon

TTGO IMU Starfield demo

igendel | PRO | 04/04/21 09:20:54 PM UTC (Edited) | 0 ⭐ | 1436 👁️ | Never ⏰ | []
Arduino |

2.04 KB

|

None

|

0 👍

/

0 👎

// by Ido Gendel, 2021
// Using the Arduino IDE with ESP32 board support installed, and the TTGO TWatch libraries
// (See https://github.com/Xinyuan-LilyGO/TTGO_TWatch_Library)
// Share and enjoy!
 
#define LILYGO_WATCH_2020_V1
#include <LilyGoWatch.h>
 
#define STARS 120U
#define STAR_SIZE 3U
 
TTGOClass *watch;
 
struct tStar {
 
  int16_t x, y;
  uint16_t z;
  int32_t ix, iy;  
  int32_t shade;
  
} star[STARS];
 
//=========================================================
void initStars(void) {
 
  uint32_t n;
 
  for (n = 0; n < STARS; n++) {
    
    star[n].x = random(65536);
    star[n].y = random(65536);
    star[n].z = random(65536);
    star[n].shade = random(2);
 
  }
  
}
 
//=========================================================
void moveStars(void) {
 
  uint32_t n, sBase;
  int32_t ax, ay, az;
  float factor;
 
  Accel acc;
 
  watch->bma->getAccel(acc);
  ax = acc.x >> 2;
  ay = acc.y >> 2;
  az = acc.z >> 3;
  
  // Erase and draw each one in turn, to avoid flicker
  for (n = 0; n < STARS; n++) {
 
    watch->tft->fillRect(star[n].ix, star[n].iy, STAR_SIZE, STAR_SIZE, 0);
    
    // 5-bit brightness value; screen uses 565 color scheme
    sBase = (256 - (star[n].z >> 8)) >> 3; 
    star[n].x += ax;
    star[n].y += ay;
    star[n].z -= az;    
    // Avoid potential division-by-zero
    if (0 == star[n].z) star[n].z = 1;
 
    factor = 240.0 / (float)star[n].z; 
    star[n].ix = int((float)(star[n].x) * factor) + 120;
    star[n].iy = int((float)(star[n].y) * factor) + 120;    
 
    watch->tft->fillRect(star[n].ix, star[n].iy, STAR_SIZE, STAR_SIZE, 
                         star[n].shade ? sBase : (sBase * 2113));    
    
  }
  
}
 
//=========================================================
void setup() {
    
    watch = TTGOClass::getWatch();
    watch->begin();     
    watch->openBL();
    watch->bma->begin();
    watch->bma->enableAccel();
 
    initStars();
 
}
 
//=========================================================
void loop() {
  moveStars();
}
 

Comments