Skip to content

Commit 301b3e7

Browse files
committed
Add clock sprite
1 parent c36be77 commit 301b3e7

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.vscode/c_cpp_properties.json
44
.vscode/launch.json
55
.vscode/ipch
6+
*_cpp

platformio.ini

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,4 @@ lib_deps =
5454
bodmer/TJpg_Decoder@~1.0.8
5555
bodmer/JSON_Decoder@~0.0.7
5656
https://github.com/Bodmer/OpenFontRender#96a29bc ; no tags or releases to reference :( -> pin to Git revision
57-
; squix78/WeatherStation@~2.1.0
58-
https://github.com/ThingPulse/esp8266-weather-station#v2.2.0
57+
thingpulse/ESP8266 Weather Station@~2.2.0

src/main.cpp

+28-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
OpenFontRender ofr;
2828
FT6236 ts = FT6236(TFT_HEIGHT, TFT_WIDTH);
2929
TFT_eSPI tft = TFT_eSPI();
30+
TFT_eSprite timeSprite = TFT_eSprite(&tft);
3031
GfxUi ui = GfxUi(&tft, &ofr);
3132

3233
int lastMinute = -1;
@@ -45,12 +46,13 @@ OpenWeatherMapForecastData forecasts[MAX_FORECASTS];
4546
// Function prototypes (declarations)
4647
// ----------------------------------------------------------------------------
4748
void drawProgress(const char *text, int8_t percentage);
49+
void drawTime();
4850
void initJpegDecoder();
4951
void initOpenFontRender();
5052
bool pushImageToTft(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t *bitmap);
5153
void syncTime();
5254
void update();
53-
void updateData();
55+
void updateData(boolean updateProgressBar);
5456

5557

5658

@@ -67,6 +69,7 @@ void setup(void) {
6769
initJpegDecoder();
6870
initTouchScreen(&ts);
6971
initTft(&tft);
72+
timeSprite.createSprite(timeSpritePos.width, timeSpritePos.height);
7073
logDisplayDebugInfo(&tft);
7174

7275
initFileSystem();
@@ -81,7 +84,10 @@ void loop(void) {
8184
lastUpdateMillis == 0 ||
8285
(millis() - lastUpdateMillis) > updateIntervalMillis) {
8386
update();
87+
} else {
88+
drawTime();
8489
}
90+
delay(1000);
8591
}
8692

8793

@@ -102,6 +108,16 @@ void drawProgress(const char *text, int8_t percentage) {
102108
ui.drawProgressBar(pbX, pbY, pbWidth, 15, percentage, TFT_WHITE, TFT_TP_BLUE);
103109
}
104110

111+
void drawTime() {
112+
timeSprite.fillSprite(TFT_BLACK);
113+
ofr.setFontSize(48);
114+
ofr.setDrawer(timeSprite);
115+
ofr.drawString(getCurrentTimestamp(UI_TIME_FORMAT).c_str(), timePosX, 0);
116+
timeSprite.pushSprite(timeSpritePos.x, timeSpritePos.y);
117+
// set the drawer back since we temporarily changed it to the time sprite above
118+
ofr.setDrawer(tft);
119+
}
120+
105121
void initJpegDecoder() {
106122
// The JPEG image can be scaled by a factor of 1, 2, 4, or 8 (default: 0)
107123
TJpgDec.setJpgScale(1);
@@ -140,6 +156,7 @@ void syncTime() {
140156
}
141157

142158
void update() {
159+
tft.fillScreen(TFT_BLACK);
143160
ui.drawLogo();
144161

145162
ofr.setFontSize(16);
@@ -154,14 +171,20 @@ void update() {
154171
drawProgress("Synchronizing time...", 30);
155172
syncTime();
156173

157-
updateData();
174+
updateData(true);
158175

159176
drawProgress("Ready", 100);
160177
lastUpdateMillis = millis();
178+
179+
tft.fillScreen(TFT_BLACK);
180+
181+
ofr.setFontSize(16);
182+
ofr.cdrawString(String("Last weather update: " + getCurrentTimestamp(UI_TIME_FORMAT_NO_SECONDS)).c_str(), tft.width() / 2, 10);
183+
drawTime();
161184
}
162185

163-
void updateData() {
164-
drawProgress("Updating weather...", 70);
186+
void updateData(boolean updateProgressBar) {
187+
if(updateProgressBar) drawProgress("Updating weather...", 70);
165188
OpenWeatherMapCurrent *currentWeatherClient = new OpenWeatherMapCurrent();
166189
currentWeatherClient->setMetric(IS_METRIC);
167190
currentWeatherClient->setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
@@ -170,7 +193,7 @@ void updateData() {
170193
currentWeatherClient = nullptr;
171194
log_i("Current weather in %s: %s, %.1fC°", currentWeather.cityName, currentWeather.description.c_str(), currentWeather.feelsLike);
172195

173-
drawProgress("Updating forecasts...", 90);
196+
if(updateProgressBar) drawProgress("Updating forecast...", 90);
174197
OpenWeatherMapForecast *forecastClient = new OpenWeatherMapForecast();
175198
forecastClient->setMetric(IS_METRIC);
176199
forecastClient->setLanguage(OPEN_WEATHER_MAP_LANGUAGE);

src/settings.h

+29-6
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,24 @@ const char *WIFI_PWD = "yourpassw0rd";
1313
// timezone Europe/Zurich as per https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
1414
#define TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3"
1515

16-
// format specifiers: https://cplusplus.com/reference/ctime/strftime/
17-
#define USER_DATE_FORMAT "%d.%m.%Y"
18-
#define USER_TIMESTAMP_FORMAT "%d.%m.%Y %H:%M:%S"
16+
// uncomment to get "08/23/2022 02:55:02 pm" instead of "23.08.2022 14:55:02"
17+
// #define DATE_TIME_FORMAT_US
1918

2019
// values in metric or imperial system?
2120
bool IS_METRIC = true;
2221

2322
// OpenWeatherMap Settings
2423
// Sign up here to get an API key: https://docs.thingpulse.com/how-tos/openweathermap-key/
25-
String OPEN_WEATHER_MAP_API_KEY = "";
24+
const String OPEN_WEATHER_MAP_API_KEY = "";
2625

2726
/*
2827
Go to https://openweathermap.org/find?q= and search for a location. Go through the
2928
result set and select the entry closest to the actual location you want to display
3029
data for. It'll be a URL like https://openweathermap.org/city/2657896. The number
3130
at the end is what you assign to the constant below.
3231
*/
33-
String OPEN_WEATHER_MAP_LOCATION_ID = "2657896";
34-
String DISPLAYED_LOCATION_NAME = "Zurich";
32+
const String OPEN_WEATHER_MAP_LOCATION_ID = "2657896";
33+
const String DISPLAYED_LOCATION_NAME = "Zurich";
3534
//String OPEN_WEATHER_MAP_LOCATION_ID = "3833367";
3635
//String DISPLAYED_LOCATION_NAME = "Ushuaia";
3736
//String OPEN_WEATHER_MAP_LOCATION_ID = "2147714";
@@ -54,6 +53,15 @@ const String OPEN_WEATHER_MAP_LANGUAGE = "en";
5453
// ****************************************************************************
5554
// System settings - do not modify unless you understand what you are doing!
5655
// ****************************************************************************
56+
typedef struct RectangleDef {
57+
int x;
58+
int y;
59+
int width;
60+
int height;
61+
} RectangleDef;
62+
63+
RectangleDef timeSpritePos = {0, 30, 320, 55};
64+
5765
// 2: portrait, on/off switch right side -> 0/0 top left
5866
// 3: landscape, on/off switch at the top -> 0/0 top left
5967
#define TFT_ROTATION 2
@@ -73,6 +81,21 @@ const String OPEN_WEATHER_MAP_LANGUAGE = "en";
7381
// the medium blue in the TP logo is 0x0067B0 which converts to 0x0336 in 16bit RGB565
7482
#define TFT_TP_BLUE 0x0336
7583

84+
// format specifiers: https://cplusplus.com/reference/ctime/strftime/
85+
#ifdef DATE_TIME_FORMAT_US
86+
int timePosX = 29;
87+
#define UI_DATE_FORMAT "%m/%d/%Y"
88+
#define UI_TIME_FORMAT "%H:%M:%S %P"
89+
#define UI_TIME_FORMAT_NO_SECONDS "%I:%M %P"
90+
#define UI_TIMESTAMP_FORMAT (UI_DATE_FORMAT + " " + UI_TIME_FORMAT)
91+
#else
92+
int timePosX = 68;
93+
#define UI_DATE_FORMAT "%d.%m.%Y"
94+
#define UI_TIME_FORMAT "%H:%M:%S"
95+
#define UI_TIME_FORMAT_NO_SECONDS "%H:%M"
96+
#define UI_TIMESTAMP_FORMAT (UI_DATE_FORMAT + " " + UI_TIME_FORMAT)
97+
#endif
98+
7699
#define SYSTEM_TIMESTAMP_FORMAT "%Y-%m-%d %H:%M:%S"
77100

78101
#define MAX_FORECASTS 12

0 commit comments

Comments
 (0)