@@ -63,13 +63,13 @@ Network network;
6363// create display object
6464Inkplate display (INKPLATE_3BIT);
6565
66- // Delay between API calls in miliseconds
67- #define DELAY_MS 5000
66+ // Delay between refreshed calls in miliseconds
67+ #define DELAY_MS 3 * 60 * 1000
6868
6969// Variable for counting partial refreshes
70- long refreshes = 0 ;
70+ RTC_DATA_ATTR unsigned refreshes = 0 ;
7171
72- // Constant to determine when to full update
72+ // Constant to determine when to full update and fetch
7373const int fullRefresh = 20 ;
7474
7575// Used for storing raw price values
@@ -98,18 +98,7 @@ char maximum[16];
9898
9999// All months in a year, for finding current date
100100char *months[] = {
101- " Jan"
102- " Feb" ,
103- " Mar" ,
104- " Apr" ,
105- " May" ,
106- " Jun" ,
107- " Jul" ,
108- " Aug" ,
109- " Sep" ,
110- " Oct" ,
111- " Nov" ,
112- " Dec" ,
101+ " Jan" , " Feb" , " Mar" , " Apr" , " May" , " Jun" , " Jul" , " Aug" , " Sep" , " Oct" , " Nov" , " Dec" ,
113102};
114103
115104// Out UI elements data
@@ -139,44 +128,76 @@ void setup()
139128
140129 // Initial display settings
141130 display.begin ();
142- display.clearDisplay ();
143- display.display ();
144131 display.setTextWrap (false );
145132 display.setTextColor (0 , 7 );
146133
147- // Welcome screen
148- display.setCursor (70 , 230 );
149- display.setTextSize (2 );
150- display.println (F (" Welcome to Inkplate 10 cryptocurrency tracker example!" ));
151- display.setCursor (70 , 250 );
152- display.println (F (" Connecting to WiFi..." ));
153- display.display ();
154-
155- delay (5000 );
156-
157- // Our begin function
158- network.begin ();
159- }
134+ if (refreshes == 0 )
135+ {
136+ // Welcome screen
137+ display.setCursor (70 , 230 );
138+ display.setTextSize (2 );
139+ display.println (F (" Welcome to Inkplate 10 cryptocurrency tracker example!" ));
140+ display.setCursor (70 , 250 );
141+ display.println (F (" Connecting to WiFi..." ));
142+ display.display ();
143+ display.clearDisplay ();
144+ delay (1000 );
145+ }
160146
161- void loop ()
162- {
163- // Do a new network request every fullRefresh times, defined above
164147 if (refreshes % fullRefresh == 0 )
148+ {
149+ // Our begin function
150+ network.begin ();
151+
165152 while (!network.getData (data))
166153 {
167154 Serial.println (" Retrying retriving data!" );
168155 delay (1000 );
169156 }
170157
171- // Our main drawing function
172- drawAll ();
158+ // Our main drawing function
159+ drawAll ();
160+ // Time drawing function
161+ drawTime ();
162+ // Full refresh
163+ display.display ();
164+ }
165+ else
166+ {
167+ display.setDisplayMode (INKPLATE_1BIT);
168+ Serial.println (" heree" );
169+ // Reset screen where date is drawn
170+ int16_t x1, y1;
171+ uint16_t w1, h1;
172+ display.setFont (elements[1 ].font );
173+ display.setTextSize (1 );
174+ network.getTime (date);
175+ display.getTextBounds (date, (int )(elements[1 ].x * 0.96 ), (int )(elements[1 ].y ), &x1, &y1, &w1, &h1);
176+
177+ Serial.printf (" %d %d %d %d\n " , x1, y1, w1, h1);
178+ display.fillRect (x1, y1, w1, h1, BLACK);
179+ display.partialUpdate ();
180+ display.fillRect (x1, y1, w1, h1, WHITE);
181+ display.partialUpdate ();
173182
174- // Go to sleep before checking again
175- esp_sleep_enable_timer_wakeup (1000L * DELAY_MS);
176- (void )esp_light_sleep_start ();
183+ // Time drawing function
184+ drawTime ();
185+
186+ // Just update time
187+ display.partialUpdate ();
188+ }
177189
178190 // Increment refresh count
179191 ++refreshes;
192+
193+ // Go to sleep before checking again
194+ esp_sleep_enable_timer_wakeup (1000ll * DELAY_MS);
195+ (void )esp_deep_sleep_start ();
196+ }
197+
198+ void loop ()
199+ {
200+ // Never here
180201}
181202
182203// Function to draw our graph
@@ -230,7 +251,7 @@ void drawGraph()
230251 for (int i = 0 ; i < 12 ; ++i)
231252 {
232253 if (strncmp (months[i], date, 3 ) == 0 )
233- month = (( i + 2 ) % 12 ? i + 2 : 12 ) ;
254+ month = i + 1 ;
234255 }
235256
236257 // Find days to display underneath the graph
@@ -304,72 +325,54 @@ void drawGraph()
304325 display.drawThickLine (x1, y1, x2, y1, 0 , 3 );
305326}
306327
307- // Our main drawing function
308- void drawAll ()
309- { // Do a full refresh every fullRefresh times, defined above
310- if (refreshes % fullRefresh == 0 )
311- {
312- // Initial screen clear
313- display.clearDisplay ();
328+ // Function to draw time
329+ void drawTime ()
330+ {
331+ // Just draw time
332+ int i = 1 ;
314333
315- // Save current date string, more about it in Network.cpp
316- network.getTime (date);
334+ // Save current date string, more about it in Network.cpp
335+ network.getTime (date);
317336
318- // Find current day from string
319- int day;
320- sscanf (date + 3 , " %d" , &day);
337+ // Text settings
338+ if (display.getDisplayMode () == INKPLATE_3BIT)
339+ display.setTextColor (0 , 7 );
340+ else
341+ display.setTextColor (BLACK, WHITE);
342+ display.setFont (elements[i].font );
343+ display.setTextSize (1 );
321344
322- // Find what month is it numericly and display it
323- for (int i = 0 ; i < 12 ; ++i)
324- {
325- if (strncmp (months[i], date, 3 ) == 0 )
326- sprintf (fromToDate, " %d.%d. to %d.%d." , day, ((i + 1 ) % 12 ? i + 1 : 12 ), day,
327- ((i + 2 ) % 12 ? i + 2 : 12 ));
328- }
345+ // 0 is aligned by left bottom corner, 1 by right
346+ if (elements[i].align == 0 )
347+ display.setCursor ((int )(elements[i].x * 0.96 ), (int )(elements[i].y ));
329348
330- // Draw graph
331- drawGraph ();
349+ // Print out text to above set cursor location
350+ display.print (date);
351+ }
332352
333- // Draw our UI elements
334- for (int i = 0 ; i < sizeof (elements) / sizeof (elements[0 ]); ++i)
335- {
336- // Text settings
337- display.setTextColor (0 , 7 );
338- display.setFont (elements[i].font );
339- display.setTextSize (1 );
340-
341- // 0 is aligned by left bottom corner, 1 by right
342- if (elements[i].align == 0 )
343- display.setCursor ((int )(elements[i].x * 0.96 ), (int )(elements[i].y ));
344- else if (elements[i].align == 1 )
345- {
346- int16_t x, y;
347- uint16_t w, h;
348-
349- // Get hot much the textx offsets pointer and draw it that much more left
350- display.getTextBounds (elements[i].text , 0 , 0 , &x, &y, &w, &h);
351-
352- display.setCursor ((int )(elements[i].x * 0.96 ) - w, (int )(elements[i].y ));
353- }
354-
355- // Print out text to above set cursor location
356- display.print (elements[i].text );
357- }
353+ // Our main drawing function
354+ void drawAll ()
355+ {
356+ // Save current date string, more about it in Network.cpp
357+ network.getTime (date);
358358
359- // Display all
360- display.display ();
361- }
362- else
363- {
364- // Just draw time
365- int i = 1 ;
359+ // Find current day from string
360+ int day;
361+ sscanf (date + 3 , " %d" , &day);
366362
367- // Initial screen clear
368- display.clearDisplay ();
363+ // Find what month is it numericly and display it
364+ for (int i = 0 ; i < 12 ; ++i)
365+ {
366+ if (strncmp (months[i], date, 3 ) == 0 )
367+ sprintf (fromToDate, " %d.%d. to %d.%d." , day, ((i + 1 ) % 12 ? i + 1 : 12 ), day, ((i + 2 ) % 12 ? i + 2 : 12 ));
368+ }
369369
370- // Save current date string, more about it in Network.cpp
371- network. getTime (date );
370+ // Draw graph
371+ drawGraph ( );
372372
373+ // Draw our UI elements
374+ for (int i = 0 ; i < sizeof (elements) / sizeof (elements[0 ]); ++i)
375+ {
373376 // Text settings
374377 display.setTextColor (0 , 7 );
375378 display.setFont (elements[i].font );
@@ -378,11 +381,18 @@ void drawAll()
378381 // 0 is aligned by left bottom corner, 1 by right
379382 if (elements[i].align == 0 )
380383 display.setCursor ((int )(elements[i].x * 0.96 ), (int )(elements[i].y ));
384+ else if (elements[i].align == 1 )
385+ {
386+ int16_t x, y;
387+ uint16_t w, h;
381388
382- // Print out text to above set cursor location
383- display.print (date );
389+ // Get how much the textx offsets pointer and draw it that much more left
390+ display.getTextBounds (elements[i]. text , 0 , 0 , &x, &y, &w, &h );
384391
385- // Just update time
386- display.partialUpdate ();
392+ display.setCursor ((int )(elements[i].x * 0.96 ) - w, (int )(elements[i].y ));
393+ }
394+
395+ // Print out text to above set cursor location
396+ display.print (elements[i].text );
387397 }
388398}
0 commit comments