Skip to content

Commit 124c5bc

Browse files
committed
Zero-Index-Array-Loop-Fix
Min/Max DrawGraph-Fix 1 Value Missing from DrawGraph-Fix Humidity 0% instead of 0.0% fix
1 parent 24ff1e7 commit 124c5bc

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

ESP32-e-Paper-Weather-Display.ino

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- Fixed Moon drawing routine to avoid drawing strayed pixels
3131
*/
3232

33+
#include <u8g2_fonts.h>
3334
#define BOX_HEADER 20
3435

3536
#include "owm_credentials.h" // See 'owm_credentials' tab and enter your OWM API key and set the Wifi SSID and PASSWORD
@@ -210,7 +211,6 @@ void drawBox(int x, int y, int w, int h, int cx, String text) {
210211
//#########################################################################################
211212
void DisplayDisplayWindSection(int x, int y, int w, int h, float angle, float windspeed, float gust, int Cradius) {
212213
int cx = x + (w / 2);
213-
// int cy = y + BOX_HEADER + ((h - BOX_HEADER) / 2);
214214
int cy = y + BOX_HEADER + Cradius + 20;
215215

216216
drawBox(x, y, w, h, cx, TXT_WIND_SPEED_DIRECTION);
@@ -237,6 +237,7 @@ void DisplayDisplayWindSection(int x, int y, int w, int h, float angle, float wi
237237
dyi = dyo * 0.9;
238238
display.drawLine(dxo + cx, dyo + cy, dxi + cx, dyi + cy, GxEPD_BLACK);
239239
}
240+
240241
drawString(cx, cy - Cradius - 15, TXT_N, CENTER);
241242
drawString(cx, cy + Cradius + 3, TXT_S, CENTER);
242243
drawString(cx - Cradius - 12, cy - 3, TXT_W, CENTER);
@@ -248,7 +249,7 @@ void DisplayDisplayWindSection(int x, int y, int w, int h, float angle, float wi
248249
drawValue(cx, cy + 5, "("+String(gust, 1)+")", Units == "M" ? "km/h" : "mph", u8g2_font_helvB12_tf, u8g2_font_helvB08_tf, CENTER);
249250

250251
// draw wind forecast
251-
for (uint8_t r = 1; r <= max_readings; r++) {
252+
for (uint8_t r = 0; r < max_readings; r++) {
252253
if (Units == "M") wind_readings[r] = WxForecast[r].Windspeed * 3.6; else wind_readings[r] = WxForecast[r].Windspeed;
253254
if (Units == "M") gust_readings[r] = WxForecast[r].Gust * 3.6; else gust_readings[r] = WxForecast[r].Gust;
254255
}
@@ -258,7 +259,6 @@ void DisplayDisplayWindSection(int x, int y, int w, int h, float angle, float wi
258259
calcMinMaxFromArray(max_readings, gust_readings, y2min, y2max);
259260
DrawGraph(34, y+h-70, 164, 55, _min(y1min, y2min), _max(y1max, y2max), Units == "M" ? TXT_WINDGUST_M : TXT_WINDGUST_I, wind_readings, max_readings, autoscale_off, barchart_off, drawgraph_on);
260261
DrawGraph(34, y+h-70, 164, 55, _min(y1min, y2min), _max(y1max, y2max), Units == "M" ? TXT_WINDGUST_M : TXT_WINDGUST_I, gust_readings, max_readings, autoscale_off, barchart_off, drawgraph_off);
261-
262262
}
263263
//#########################################################################################
264264
String WindDegToDirection(float winddirection) {
@@ -363,7 +363,7 @@ void DisplayAstronomySection(int x, int y) {
363363
DrawMoon(x + 320, y - 2, day_utc, month_utc, year_utc, Hemisphere);
364364

365365
time_t utcOffset = mktime(now_utc) - _now;
366-
m.calculate(_now + utcOffset);
366+
m.calculate(_now + utcOffset + WxConditions[0].Timezone);
367367
mr.calculate(WxConditions[0].lat, WxConditions[0].lon, _now + utcOffset);
368368
time_t moonRiseTime = mr.riseTime - utcOffset;
369369
time_t moonSetTime = mr.setTime - utcOffset;
@@ -484,22 +484,18 @@ String MoonAge(int d, int m, int y, String hemisphere) {
484484
//#########################################################################################
485485
void DisplayForecastSection(int x, int y) {
486486
u8g2Fonts.setFont(u8g2_font_helvB08_tf);
487-
int f = 0;
488-
do {
487+
for (uint8_t f = 0; f <= 7; f++) {
489488
DisplayForecastWeather(x, y, f);
490-
f++;
491-
} while (f <= 7);
489+
};
492490
// Pre-load temporary arrays with with data - because C parses by reference
493-
int r = 1;
494-
do {
491+
for (uint8_t r = 0; r < max_readings; r++) {
495492
if (Units == "I") pressure_readings[r] = WxForecast[r].Pressure * 0.02953; else pressure_readings[r] = WxForecast[r].Pressure;
496493
if (Units == "I") rain_readings[r] = WxForecast[r].Rainfall * 0.0393701; else rain_readings[r] = WxForecast[r].Rainfall;
497494
if (Units == "I") snow_readings[r] = WxForecast[r].Snowfall * 0.0393701; else snow_readings[r] = WxForecast[r].Snowfall;
498495
temperature_readings[r] = WxForecast[r].Temperature;
499496
temperature_feel_readings[r] = WxForecast[r].Feelslike;
500497
humidity_readings[r] = WxForecast[r].Humidity;
501-
r++;
502-
} while (r <= max_readings);
498+
};
503499
int gwidth = 165, gheight = 122;
504500
int gx = (SCREEN_WIDTH - gwidth * 4) / 5 + 5;
505501
int gy = 345;
@@ -521,6 +517,7 @@ void DisplayForecastSection(int x, int y) {
521517
void DisplayConditionsSection(int x, int y, int w, int h, String IconName, bool IconSize) {
522518
int cx = x + (w / 2);
523519
int cy = y + BOX_HEADER + ((h - BOX_HEADER) / 2);
520+
524521
if (IconSize == LargeIcon) {
525522
drawBox(x, y, w, h, cx, TXT_CONDITIONS);
526523
drawValue(cx, y + h - 25, String(WxConditions[0].Humidity, 0) + "%", "RH", u8g2_font_helvB14_tf, u8g2_font_helvB10_tf, CENTER);
@@ -971,21 +968,21 @@ void Nodata(int x, int y, bool IconSize, String IconName) {
971968
If called with Y!_Max value of 500 and the data never goes above 500, then autoscale will retain a 0-500 Y scale, if on, the scale increases/decreases to match the data.
972969
auto_scale_margin, e.g. if set to 1000 then autoscale increments the scale by 1000 steps.
973970
Marani: Added draw_graph: TRUE = Draw complete graph with values, FALSE = Just draw values (to add multiple values in one graph) thin
971+
Marani: Fixed error in MinMax Calculations
972+
Marani: Fixed error in DrawGraph values (Array 0-based index error)
974973
*/
975-
void calcMinMaxFromArray(int readings, float DataArray[], float& Y1Min, float& Y1Max) {
976-
#define auto_scale_margin 0 // Sets the autoscale increment, so axis steps up in units of e.g. 3
974+
void calcMinMaxFromArray(uint8_t readings, float DataArray[], float& Y1Min, float& Y1Max) {
977975
float maxYscale = -10000;
978976
float minYscale = 10000;
979-
for (int i = 1; i < readings; i++) {
980-
if (DataArray[i] >= maxYscale) maxYscale = DataArray[i];
981-
if (DataArray[i] <= minYscale) minYscale = DataArray[i];
977+
for (uint8_t i = 0; i < readings; i++) {
978+
if (DataArray[i] > maxYscale) maxYscale = DataArray[i];
979+
if (DataArray[i] < minYscale) minYscale = DataArray[i];
982980
}
983-
maxYscale = round(maxYscale + auto_scale_margin); // Auto scale the graph and round to the nearest value defined, default was Y1Max
984-
Y1Max = round(maxYscale + 0.5);
985-
if (minYscale != 0) minYscale = round(minYscale - auto_scale_margin); // Auto scale the graph and round to the nearest value defined, default was Y1Min
986-
if (minYscale > 0.5) Y1Min = round(minYscale - 0.5); else Y1Min = round(minYscale);
981+
Y1Max = ceil(maxYscale + 0.5);
982+
if (minYscale > 0.5) Y1Min = floor(minYscale - 0.5); else Y1Min = floor(minYscale);
987983
}
988-
void DrawGraph(int x_pos, int y_pos, int gwidth, int gheight, float Y1Min, float Y1Max, String title, float DataArray[], int readings, boolean auto_scale, boolean barchart_mode, boolean draw_graph) {
984+
985+
void DrawGraph(int x_pos, int y_pos, int gwidth, int gheight, float Y1Min, float Y1Max, String title, float DataArray[], uint8_t readings, boolean auto_scale, boolean barchart_mode, boolean draw_graph) {
989986
#define y_minor_axis 5 // 5 y-axis division markers
990987
int last_x, last_y;
991988
float x2, y2;
@@ -998,8 +995,8 @@ void DrawGraph(int x_pos, int y_pos, int gwidth, int gheight, float Y1Min, float
998995
drawString(x_pos + gwidth / 2, y_pos - 16, title, CENTER);
999996
}
1000997
// Draw the data
1001-
for (int gx = 1; gx < readings; gx++) {
1002-
x2 = x_pos + gx * gwidth / (readings - 1) - 1; // max_readings is the global variable that sets the maximum data that can be plotted
998+
for (uint8_t gx = 0; gx < readings; gx++) {
999+
x2 = x_pos + (gx+1) * gwidth / (readings) - 1; // max_readings is the global variable that sets the maximum data that can be plotted
10031000
y2 = y_pos + (Y1Max - constrain(DataArray[gx], Y1Min, Y1Max)) / (Y1Max - Y1Min) * gheight + 1;
10041001
if (barchart_mode) {
10051002
if (draw_graph) display.fillRect(x2, y2, (gwidth / readings) - 1, y_pos + gheight - y2 + 2, GxEPD_BLACK);
@@ -1030,7 +1027,7 @@ void DrawGraph(int x_pos, int y_pos, int gwidth, int gheight, float Y1Min, float
10301027
for (int j = 0; j < number_of_dashes; j++) { // Draw dashed graph grid lines
10311028
if (spacing < y_minor_axis) display.drawFastHLine((x_pos + 3 + j * gwidth / number_of_dashes), y_pos + (gheight * spacing / y_minor_axis), gwidth / (2 * number_of_dashes), GxEPD_BLACK);
10321029
}
1033-
if ((Y1Max - (float)(Y1Max - Y1Min) / y_minor_axis * spacing) < 5 || title == TXT_PRESSURE_IN) {
1030+
if ((((Y1Max - (float)(Y1Max - Y1Min) / y_minor_axis * spacing) < 5) && ((Y1Max - (float)(Y1Max - Y1Min) / y_minor_axis * spacing) > 0)) || title == TXT_PRESSURE_IN) {
10341031
drawString(x_pos - 1, y_pos + gheight * spacing / y_minor_axis - 5, String((Y1Max - (float)(Y1Max - Y1Min) / y_minor_axis * spacing + 0.01), 1), RIGHT);
10351032
}
10361033
else

0 commit comments

Comments
 (0)