Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "TFT_eSPI.h"
- #include "U8g2_for_TFT_eSPI.h"
- #include "SPI.h"
- #include <Ephemeris.h>
- #define fontBig u8g2_font_fub49_tn // 61px
- #define fontMid u8g2_font_crox5tb_tf // 16px
- #define fontSml u8g2_font_helvR14_tr // 15px
- #define fontxSml u8g2_font_6x13_tf // ??px
- #define fontSpecial u8g2_font_gb24st_t_3 // 13px
- #define paddingL 5
- #define paddingR 10
- enum directions { LEFT,
- RIGHT,
- CENTER };
- TFT_eSPI tft = TFT_eSPI();
- U8g2_for_TFT_eSPI u8f;
- TFT_eSprite spr = TFT_eSprite(&tft);
- int hour = 13;
- int minute = 20;
- float sec;
- int totalTime;
- int day = 10;
- int mon = 9;
- int yr = 2023;
- const int totalCalcs = 50;
- int delta;
- int pos[totalCalcs][2];
- String posTime[totalCalcs];
- const byte outerRadius = 119;
- const int screenWidth = 320;
- const int screenHeight = 240;
- void setup() {
- Serial.begin(115200);
- tft.init();
- u8f.begin(spr);
- u8f.setFont(u8g2_font_t0_22_me);
- tft.setTextWrap(false, false);
- tft.setRotation(1);
- spr.createSprite(320, 240);
- Ephemeris::setLocationOnEarth(xx, x, xx, x, x, xx);
- Ephemeris::setAltitude(5);
- // get initial position once to get rise/set
- SolarSystemObject sun = Ephemeris::solarSystemObjectAtDateAndTime(Sun, day, mon, yr, 12, 0, 0);
- Serial.println(String(sun.horiCoordinates.azi) + " - " + String(sun.horiCoordinates.alt));
- if (sun.riseAndSetState == RiseAndSetOk) {
- Ephemeris::floatingHoursToHoursMinutesSeconds(Ephemeris::floatingHoursWithUTCOffset(sun.set, 2), &hour, &minute, &sec);
- Serial.println("Set time: " + String(hour) + ":" + String(minute));
- totalTime = (hour * 60) + minute;
- Ephemeris::floatingHoursToHoursMinutesSeconds(Ephemeris::floatingHoursWithUTCOffset(sun.rise, 2), &hour, &minute, &sec);
- totalTime -= (hour * 60) + minute;
- delta = totalTime / totalCalcs;
- int tempHour = hour;
- int tempMin = minute;
- for (int i = 0; i < totalCalcs; i++) {
- SolarSystemObject sunTemp = Ephemeris::solarSystemObjectAtDateAndTime(Sun, day, mon, yr, tempHour, tempMin, 0);
- pos[i][0] = sunTemp.horiCoordinates.azi;
- pos[i][1] = sunTemp.horiCoordinates.alt;
- posTime[i] = String(tempHour);
- posTime[i] += ":";
- if (tempMin < 10) { posTime[i] += "0"; }
- posTime[i] += String(tempMin);
- Serial.println(posTime[i] + "- Az: " + String(pos[i][0]) + " Alt: " + String(pos[i][1]));
- tempMin += delta;
- if (tempMin > 60) {
- tempMin %= 60;
- tempHour++;
- }
- }
- drawOrbit();
- }
- }
- void loop() {
- }
- void drawOrbit() {
- int circleX = 160;
- int circleY = 120;
- spr.fillSprite(TFT_BLACK);
- // draw background first
- spr.drawSmoothCircle(circleX, circleY, outerRadius, TFT_WHITE, TFT_BLACK);
- spr.drawSmoothCircle(circleX, circleY, outerRadius / 2, TFT_WHITE, TFT_BLACK);
- for (int i = 0; i < 8; i++) {
- int xPos = sin(radians((i * 45) + 90)) * outerRadius;
- int yPos = cos(radians((i * 45) + 90)) * outerRadius;
- spr.drawLine(circleX, circleY, xPos + circleX, yPos + circleY, TFT_WHITE);
- u8f.setFont(fontxSml);
- int markerIndex = i + 2;
- markerIndex %= 8;
- }
- // draw orbit
- for (int i = 0; i < totalCalcs; i++) {
- if (pos[i][1] < 0) { break; }
- int xPos = -sin(radians(pos[i][0] + 180)) * map(pos[i][1], 0, 90, outerRadius, 1);
- int yPos = cos(radians(pos[i][0] + 180)) * map(pos[i][1], 0, 90, outerRadius, 1);
- spr.drawSpot(xPos + circleX, yPos + circleY, 2, TFT_YELLOW);
- }
- spr.pushSprite(0, 0);
- }
- void drawText(String text, int y, directions dir, uint16_t color) {
- u8f.setForegroundColor(color);
- int textW = u8f.getUTF8Width(text.c_str());
- switch (dir) {
- case LEFT:
- drawText(text, 0 + paddingL, y, color);
- break;
- case RIGHT:
- drawText(text, (screenWidth - textW) - paddingR, y, color);
- break;
- case CENTER: // TODO: make offset toggleable
- drawText(text, (screenWidth / 2) - textW / 2, y, color);
- break;
- }
- }
- void drawText(String text, int x, int y, uint16_t color) {
- u8f.setForegroundColor(color);
- u8f.setCursor(x, y);
- int textW = u8f.getUTF8Width(text.c_str());
- int textH = u8f.getFontAscent() - u8f.getFontDescent();
- u8f.print(text);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement