Advertisement
Guest User

Untitled

a guest
Sep 11th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "TFT_eSPI.h"
  2. #include "U8g2_for_TFT_eSPI.h"
  3.  
  4. #include "SPI.h"
  5.  
  6. #include <Ephemeris.h>
  7.  
  8. #define fontBig u8g2_font_fub49_tn        // 61px
  9. #define fontMid u8g2_font_crox5tb_tf      // 16px
  10. #define fontSml u8g2_font_helvR14_tr      // 15px
  11. #define fontxSml u8g2_font_6x13_tf        // ??px
  12. #define fontSpecial u8g2_font_gb24st_t_3  // 13px
  13.  
  14. #define paddingL 5
  15. #define paddingR 10
  16.  
  17. enum directions { LEFT,
  18.                   RIGHT,
  19.                   CENTER };
  20.  
  21. TFT_eSPI tft = TFT_eSPI();
  22. U8g2_for_TFT_eSPI u8f;
  23. TFT_eSprite spr = TFT_eSprite(&tft);
  24.  
  25. int hour = 13;
  26. int minute = 20;
  27. float sec;
  28.  
  29. int totalTime;
  30.  
  31. int day = 10;
  32. int mon = 9;
  33. int yr = 2023;
  34.  
  35. const int totalCalcs = 50;
  36. int delta;
  37.  
  38. int pos[totalCalcs][2];
  39. String posTime[totalCalcs];
  40.  
  41. const byte outerRadius = 119;
  42. const int screenWidth = 320;
  43. const int screenHeight = 240;
  44.  
  45. void setup() {
  46.  
  47.   Serial.begin(115200);
  48.  
  49.   tft.init();
  50.  
  51.   u8f.begin(spr);
  52.   u8f.setFont(u8g2_font_t0_22_me);
  53.   tft.setTextWrap(false, false);
  54.  
  55.   tft.setRotation(1);
  56.   spr.createSprite(320, 240);
  57.  
  58.   Ephemeris::setLocationOnEarth(xx, x, xx, x, x, xx);
  59.   Ephemeris::setAltitude(5);
  60.  
  61.   // get initial position once to get rise/set
  62.   SolarSystemObject sun = Ephemeris::solarSystemObjectAtDateAndTime(Sun, day, mon, yr, 12, 0, 0);
  63.   Serial.println(String(sun.horiCoordinates.azi) + " - " + String(sun.horiCoordinates.alt));
  64.  
  65.   if (sun.riseAndSetState == RiseAndSetOk) {
  66.     Ephemeris::floatingHoursToHoursMinutesSeconds(Ephemeris::floatingHoursWithUTCOffset(sun.set, 2), &hour, &minute, &sec);
  67.     Serial.println("Set time: " + String(hour) + ":" + String(minute));
  68.  
  69.     totalTime = (hour * 60) + minute;
  70.  
  71.     Ephemeris::floatingHoursToHoursMinutesSeconds(Ephemeris::floatingHoursWithUTCOffset(sun.rise, 2), &hour, &minute, &sec);
  72.     totalTime -= (hour * 60) + minute;
  73.    
  74.     delta = totalTime / totalCalcs;
  75.  
  76.     int tempHour = hour;
  77.     int tempMin = minute;
  78.  
  79.      for (int i = 0; i < totalCalcs; i++) {
  80.  
  81.        SolarSystemObject sunTemp = Ephemeris::solarSystemObjectAtDateAndTime(Sun, day, mon, yr, tempHour, tempMin, 0);
  82.        pos[i][0] = sunTemp.horiCoordinates.azi;
  83.        pos[i][1] = sunTemp.horiCoordinates.alt;
  84.  
  85.        posTime[i] = String(tempHour);
  86.        posTime[i] += ":";
  87.        if (tempMin < 10) { posTime[i] += "0"; }
  88.        posTime[i] += String(tempMin);
  89.  
  90.        Serial.println(posTime[i] + "- Az: " + String(pos[i][0]) + " Alt: " + String(pos[i][1]));
  91.  
  92.        tempMin += delta;
  93.  
  94.        if (tempMin > 60) {
  95.          tempMin %= 60;
  96.          tempHour++;
  97.        }
  98.      }
  99.      drawOrbit();
  100.   }
  101. }
  102.  
  103. void loop() {
  104. }
  105.  
  106. void drawOrbit() {
  107.   int circleX = 160;
  108.   int circleY = 120;
  109.  
  110.   spr.fillSprite(TFT_BLACK);
  111.    
  112.  // draw background first
  113.   spr.drawSmoothCircle(circleX, circleY, outerRadius, TFT_WHITE, TFT_BLACK);
  114.   spr.drawSmoothCircle(circleX, circleY, outerRadius / 2, TFT_WHITE, TFT_BLACK);
  115.  
  116.   for (int i = 0; i < 8; i++) {
  117.     int xPos = sin(radians((i * 45) + 90)) * outerRadius;
  118.     int yPos = cos(radians((i * 45) + 90)) * outerRadius;
  119.  
  120.     spr.drawLine(circleX, circleY, xPos + circleX, yPos + circleY, TFT_WHITE);
  121.  
  122.     u8f.setFont(fontxSml);
  123.     int markerIndex = i + 2;
  124.     markerIndex %= 8;
  125.  
  126.   }
  127.  
  128. // draw orbit
  129.   for (int i = 0; i < totalCalcs; i++) {
  130.     if (pos[i][1] < 0) { break; }
  131.  
  132.     int xPos = -sin(radians(pos[i][0] + 180)) * map(pos[i][1], 0, 90, outerRadius, 1);
  133.     int yPos = cos(radians(pos[i][0] + 180)) * map(pos[i][1], 0, 90, outerRadius, 1);
  134.  
  135.     spr.drawSpot(xPos + circleX, yPos + circleY, 2, TFT_YELLOW);
  136.   }
  137.   spr.pushSprite(0, 0);
  138. }
  139.  
  140.  
  141. void drawText(String text, int y, directions dir, uint16_t color) {
  142.   u8f.setForegroundColor(color);
  143.   int textW = u8f.getUTF8Width(text.c_str());
  144.  
  145.   switch (dir) {
  146.  
  147.     case LEFT:
  148.       drawText(text, 0 + paddingL, y, color);
  149.       break;
  150.  
  151.     case RIGHT:
  152.       drawText(text, (screenWidth - textW) - paddingR, y, color);
  153.       break;
  154.  
  155.     case CENTER:  //  TODO: make offset toggleable
  156.       drawText(text, (screenWidth / 2) - textW / 2, y, color);
  157.       break;
  158.   }
  159. }
  160.  
  161. void drawText(String text, int x, int y, uint16_t color) {
  162.   u8f.setForegroundColor(color);
  163.   u8f.setCursor(x, y);
  164.  
  165.   int textW = u8f.getUTF8Width(text.c_str());
  166.   int textH = u8f.getFontAscent() - u8f.getFontDescent();
  167.  
  168.   u8f.print(text);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement