// H/W: Wemos D1 Mini
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <ILI9341_SPI.h>
#include <MiniGrafx.h>
RTC_DS1307 RTC; //3231 OR 1307
// defines the colors usable in the paletted 16 color frame buffer
uint16_t palette[] = {ILI9341_BLACK, // 0
ILI9341_WHITE, // 1
ILI9341_LIGHTGREY, // 2
ILI9341_DARKGREY, // 3
ILI9341_DARKGREEN, // 4
ILI9341_MAROON, // 5
ILI9341_PURPLE, // 6
ILI9341_OLIVE, // 7
ILI9341_NAVY, // 8
ILI9341_DARKCYAN, // 9
ILI9341_BLUE, // 10
ILI9341_GREEN, // 11
ILI9341_CYAN, // 12
ILI9341_RED, // 13
ILI9341_MAGENTA, // 14
ILI9341_YELLOW}; // 15
int SCREEN_WIDTH = 320;
int SCREEN_HEIGHT = 240;
int BITS_PER_PIXEL = 4; // 4^4 = 16 colors
#define TFT_DC D0
#define TFT_CS D3
#define TFT_LED D8
ILI9341_SPI tft = ILI9341_SPI(TFT_CS, TFT_DC);
MiniGrafx gfx = MiniGrafx(&tft, BITS_PER_PIXEL, palette);
uint16_t counter = 0; // Used for fps measuring
long startMillis = millis();
uint16_t interval = 20;
//int color = 1;
String fps = "0fps";
String nfm = ""; // days to next full moon
//int moon_phase = mp;
void drawAbout();
void drawMoon();
void setup() {
Wire.begin(D2,D1);
Serial.begin(115200);
RTC.begin();
Serial.println(TFT_LED);
pinMode(TFT_LED, OUTPUT);
digitalWrite(TFT_LED, HIGH); // HIGH to Turn on;
// create grey scale palette for greay image
for (int i = 0; i < 16; i++) {
palette[i] = ((i * 31 / 16) << 11) | ((i * 63 / 16) << 5) | ((i * 31 / 16));
}
gfx.init();
gfx.fillBuffer(0);
gfx.commit();
//SPIFFS.format();
SPIFFS.begin();
startMillis = millis();
gfx.setRotation (3);
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
//}
// following line sets the RTC to the date & time this sketch was compiled
// un REM the line below to set clock, then re REM it
// and upload this sketch again
//RTC.adjust(DateTime(__DATE__, __TIME__));
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! RTC.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
}
}
void loop() {
DateTime now = RTC.now();
Serial.println ("");
Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
Serial.print ("Moon Phase ");
Serial.println (moon_phase());
Serial.print ("Next Full Moon ");
Serial.println (nfm);
//drawAbout();
drawMoon();
delay(15000);
}
void drawMoon(void) {
int mp = moon_phase();
gfx.fillBuffer(0);
//gfx.setColor(1);
if (mp == 1) {
gfx.setColor(1);
gfx.drawBmpFromFile("/1_WnG.bmp", 95, 55);
gfx.setColor(15);
gfx.setTextAlignment(TEXT_ALIGN_CENTER);
gfx.setFont(Open_Sans_SemiBold_20);
gfx.drawString (160, 200, "Wanning Gibbous");}
if (mp == 2) {
gfx.drawBmpFromFile("/2_Last.bmp", 95, 55);
gfx.drawString (160, 200, "Last Quarter");}
if (mp == 3) {
gfx.drawBmpFromFile("/3_WnC.bmp", 95, 55);
gfx.drawString (160, 200, "Wanning Crescent");}
if (mp == 4) {
gfx.drawBmpFromFile("/4_New.bmp", 95, 55);
gfx.drawString (160, 200, "New Moon");}
if (mp == 5) {
gfx.drawBmpFromFile("/5_WxC.bmp", 95, 55);
gfx.drawString (160, 200, "Waxing Crescent");}
if (mp == 6) {
gfx.drawBmpFromFile("/6_First.bmp", 95, 55);
gfx.drawString (160, 200, "First Quarter");}
if (mp == 7) {
gfx.drawBmpFromFile("/7_WxG.bmp", 95, 55);
gfx.drawString (160, 200, "Waxing Gibbous");}
if (mp == 0) {
gfx.drawBmpFromFile("/0_Full.bmp", 95, 55);
gfx.setColor(11);
gfx.setTextAlignment(TEXT_ALIGN_CENTER);
gfx.setFont(Open_Sans_SemiBold_20);
gfx.drawString (160, 200, "Full Moon");}
gfx.setColor(15);
gfx.setFont(Open_Sans_SemiBold_20);
gfx.setTextAlignment(TEXT_ALIGN_LEFT);
gfx.drawString (5,10, "NFM:");
gfx.drawString (5,28, nfm);
gfx.drawString (5,28, " Days");
gfx.commit();
const char* newNfm = (const char*) nfm.c_str();
}
int moon_phase(){ // calculates the age of the moon phase(0 to 7) there are eight stages
DateTime now = RTC.now();
double jd = 0; // Julian Date
double ed = 0; //days elapsed since start of full moon
int b= 0;
jd = julianDate(now.year(), now.month(), now.day());
//jd = julianDate(1972,1,1); // used to debug this is a new moon
jd = int(jd - 2244116.75); // start at Jan 1 1972
jd /= 29.53; // divide by the moon cycle
b = jd;
jd -= b; // leaves the fractional part of jd
ed = jd * 29.53; // days elapsed this month
nfm = String((int(29.53 - ed))); // days to next full moon
b = jd*8 +0.5;
b = b & 7;
return b;
}
double julianDate(int y, int m, int d){
// convert a date to a Julian Date}
int mm,yy;
double k1, k2, k3;
double j;
yy = y- int((12-m)/10);
mm = m+9;
if(mm >= 12) {
mm = mm-12;
}
k1 = 365.25 *(yy +4172);
k2 = int((30.6001 * mm) + 0.5);
k3 = int((((yy/100) + 4) * 0.75) -38);
j = k1 +k2 + d + 59;
j = j-k3; // j is the Julian date at 12h UT (Universal Time)
return j;
}