Skip to content

Commit b08f0e2

Browse files
Updated the ILI9341 example.
1 parent 690d5a7 commit b08f0e2

File tree

1 file changed

+24
-42
lines changed

1 file changed

+24
-42
lines changed

examples/esp32/ILI9341/ILI9341.ino

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
44
// pics: http://neoprogrammics.com/moon/
55
*/
6-
#include "MoonPhase.h"
7-
#include "WiFi.h"
8-
#include "Adafruit_ILI9341.h"
6+
#include <WiFi.h>
7+
#include <Adafruit_ILI9341.h>
8+
#include <moonPhase.h>
99

1010
#define SPI_TFT_DC_PIN 27 // Goes to TFT DC
1111
#define SPI_SCK_PIN 25 // Goes to TFT SCK/CLK
@@ -42,10 +42,11 @@
4242
#include "phases/phase_345.c"
4343
#include "phases/phase_360.c"
4444

45-
const char* wifi_sid = "..........";
46-
const char* wifi_psk = "..........";
45+
const char* wifi_sid = "network_name";
46+
const char* wifi_psk = "password";
4747

48-
MoonPhase MoonPhase;
48+
moonPhase moonPhase;
49+
struct tm timeinfo;
4950

5051
Adafruit_ILI9341 tft = Adafruit_ILI9341( SPI_TFT_CS_PIN, SPI_TFT_DC_PIN, SPI_TFT_RST_PIN );
5152

@@ -55,7 +56,7 @@ void setup()
5556

5657
SPI.begin( SPI_SCK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN );
5758

58-
tft.begin( 10000000, SPI );
59+
tft.begin( 10000000 );
5960

6061
pinMode( TFT_BACKLIGHT_PIN, OUTPUT );
6162
digitalWrite( TFT_BACKLIGHT_PIN, HIGH );
@@ -66,66 +67,47 @@ void setup()
6667
tft.println( "MoonPhase" );
6768

6869
Serial.printf( "Connecting to '%s' with psk '%s'\n" , wifi_sid, wifi_psk );
70+
tft.println();
71+
tft.printf( "Connecting to '%s'\n" , wifi_sid, wifi_psk );
6972

7073
WiFi.begin( wifi_sid, wifi_psk );
7174

72-
uint32_t timeout = millis() + 5000;
73-
while ( !WiFi.isConnected() && millis() - timeout > 0 )
75+
while ( !WiFi.isConnected() )
7476
{
7577
Serial.print( "." );
7678
delay( 500 );
7779
}
7880
Serial.println();
7981
Serial.println( "Connected." );
82+
tft.println( "Connected." );
83+
configTzTime( "UTC", "0.pool.ntp.org", "1.pool.ntp.org" );
84+
if ( !getLocalTime( &timeinfo, 0 ) )
85+
delay(100);
86+
tft.fillScreen( ILI9341_BLACK );
8087

81-
82-
if ( WiFi.status() != WL_CONNECTED )
83-
{
84-
tft.setCursor( 80, 180 );
85-
tft.println( "WiFi not connected." );
86-
tft.setCursor( 50, 200 );
87-
tft.printf( "SSID: %s PSK: %s", wifi_sid, wifi_psk );
88-
}
89-
else
90-
{
91-
configTzTime( "UTC", "0.pool.ntp.org", "1.pool.ntp.org" );
92-
}
9388
}
9489

9590
void loop()
9691
{
97-
time_t now;
98-
time( &now );
99-
struct tm timeinfo;
100-
gmtime_r( &now, &timeinfo ); /* moon phase is calculated using UTC! */
101-
102-
double hourSecondPrecision = timeinfo.tm_hour + mapFloat( ( timeinfo.tm_min * 60 ) + timeinfo.tm_sec, 1, 3600, 0, 1 );
103-
104-
MoonPhase::moonData moon = MoonPhase.getInfo( 1900 + timeinfo.tm_year,
105-
timeinfo.tm_mon + 1,
106-
timeinfo.tm_mday,
107-
hourSecondPrecision );
92+
moonData_t moon = moonPhase.getPhase();
10893
static int previousAngle;
10994

11095
if ( previousAngle != moon.angle )
11196
{
11297
showMoonTft( moon.angle );
113-
previousAngle = moon.angle;
98+
previousAngle = moon.angle;
11499
}
115-
116-
tft.setCursor( 20, 120 );
117-
tft.printf( "%i%c %s UTC", moon.angle, char(247), asctime( &timeinfo ) );
100+
getLocalTime( &timeinfo );
101+
tft.setCursor( 60, 120 );
102+
char timestr[20];
103+
strftime( timestr, sizeof( timestr), "%c", &timeinfo );
104+
tft.printf( "%s " "UTC", timestr );
118105
tft.setCursor( 120, 105 );
119-
tft.printf( "%.4f%%", moon.percentLit * 100 );
106+
tft.printf( "%i%c %.1f" "%%", moon.angle, char(247), moon.percentLit * 100 );
120107

121108
delay( 1000 );
122109
}
123110

124-
float mapFloat( float x, const float in_min, const float in_max, const float out_min, const float out_max)
125-
{
126-
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
127-
}
128-
129111
void showMoonTft( uint16_t phase )
130112
{
131113
// see example sketch .../Arduino/libraries/AdaFruit_ILI9341/examples/pictureEmbed

0 commit comments

Comments
 (0)