debugging, comments etc.

This commit is contained in:
nitko12 2020-07-30 13:52:43 +02:00
parent 42f43d4b26
commit 1e534c3bd8
59 changed files with 67 additions and 28 deletions

View File

@ -5,12 +5,14 @@
Don't have "Inkplate 6(ESP32)" option? Follow our tutorial and add it: Don't have "Inkplate 6(ESP32)" option? Follow our tutorial and add it:
https://e-radionica.com/en/blog/add-inkplate-6-to-arduino-ide/ https://e-radionica.com/en/blog/add-inkplate-6-to-arduino-ide/
This example will show you how you can use Inkplate 6 to display API data, This example will show you how you can use Inkplate 6 to display API data.
e.g. coingecko api Here we use Coingecko API to get latest cryptocurrency prices and display
them on the Inkplate screen. If you wish to change the currecny, you can
edit it below.
IMPORTANT: IMPORTANT:
Make sure to change your timezone and wifi credentials below Make sure to change your timezone and wifi credentials below
Also have ArduinoJSON installed in your Arduino libraries Also have ArduinoJSON installed in your Arduino libraries, download here: https://arduinojson.org/
Want to learn more about Inkplate? Visit www.inkplate.io Want to learn more about Inkplate? Visit www.inkplate.io
Looking to get support? Write on our forums: http://forum.e-radionica.com/en/ Looking to get support? Write on our forums: http://forum.e-radionica.com/en/
@ -19,7 +21,7 @@
// ---------- CHANGE HERE -------------: // ---------- CHANGE HERE -------------:
// Adjust your time zone // Adjust your time zone, 2 means UTC+2
int timeZone = 2; int timeZone = 2;
// Put in your ssid and password // Put in your ssid and password
@ -31,6 +33,9 @@ char *pass = "";
char *currency = "bitcoin"; char *currency = "bitcoin";
char *currencyAbbr = "BTC"; char *currencyAbbr = "BTC";
// You can find your currency id here:
// https://api.coingecko.com/api/v3/coins
// ---------------------------------- // ----------------------------------
// Include Inkplate library to the sketch // Include Inkplate library to the sketch
@ -47,10 +52,10 @@ char *currencyAbbr = "BTC";
// create object with all networking functions // create object with all networking functions
Network network; Network network;
// create // create display object
Inkplate display(INKPLATE_3BIT); Inkplate display(INKPLATE_3BIT);
// Delay between API calls // Delay between API calls in miliseconds
#define DELAY_MS 5000 #define DELAY_MS 5000
// Variable for counting partial refreshes // Variable for counting partial refreshes
@ -60,7 +65,7 @@ long refreshes = 0;
const int fullRefresh = 20; const int fullRefresh = 20;
// Used for storing raw price values // Used for storing raw price values
float data[64]; double data[64];
// Used to simplify UI design // Used to simplify UI design
struct textElement struct textElement
@ -106,9 +111,9 @@ textElement elements[] = {
{190, 185, &Roboto_Light_40, fromToDate, 0}, {190, 185, &Roboto_Light_40, fromToDate, 0},
{570, 140, &Roboto_Light_40, "Current price:", 0}, {570, 140, &Roboto_Light_40, "Current price:", 0},
{790, 190, &Roboto_Light_40, current, 1}, {790, 190, &Roboto_Light_40, current, 1},
{630, 275, &Roboto_Light_40, "Minimum", 0}, {630, 275, &Roboto_Light_40, "Minimum:", 0},
{790, 320, &Roboto_Light_40, minimum, 1}, {790, 320, &Roboto_Light_40, minimum, 1},
{625, 420, &Roboto_Light_40, "Maximum", 0}, {625, 420, &Roboto_Light_40, "Maximum:", 0},
{790, 466, &Roboto_Light_40, maximum, 1}, {790, 466, &Roboto_Light_40, maximum, 1},
{18, 570, &Roboto_Light_36, dates, 0}, {18, 570, &Roboto_Light_36, dates, 0},
@ -136,8 +141,8 @@ void drawGraph()
int textMargin = 68; int textMargin = 68;
// Set min to a very high value, and max to very low, so that any real world data changes it // Set min to a very high value, and max to very low, so that any real world data changes it
float minData = 1e9F; double minData = 1e9F;
float maxData = -1e9F; double maxData = -1e9F;
// Find min and max in data // Find min and max in data
for (int i = 0; i < 31; ++i) for (int i = 0; i < 31; ++i)
@ -146,7 +151,9 @@ void drawGraph()
maxData = max(maxData, data[i]); maxData = max(maxData, data[i]);
} }
// Copy current, min and max data to har arrays to be displayed double span = max(0.3D, (double)abs(maxData - minData));
// Copy current, min and max data to char arrays to be displayed
dtostrf(data[30], 8, 2, current); dtostrf(data[30], 8, 2, current);
strcat(current, "$"); strcat(current, "$");
dtostrf(minData, 8, 2, minimum); dtostrf(minData, 8, 2, minimum);
@ -159,7 +166,7 @@ void drawGraph()
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
{ {
dtostrf(minData + (float)i / 4 * (maxData - minData), 5, 0, temp); dtostrf(minData + (double)i / 4 * span, 5, (maxData < 10.0D ? 3 : 0), temp);
strncpy(prices + 16 * (3 - i), temp, 16); strncpy(prices + 16 * (3 - i), temp, 16);
} }
@ -167,11 +174,24 @@ void drawGraph()
int day; int day;
sscanf(date + 3, "%d", &day); sscanf(date + 3, "%d", &day);
// Find current month
int month = 0;
for (int i = 0; i < 12; ++i)
{
if (strncmp(months[i], date, 3) == 0)
month = ((i + 2) % 12 ? i + 2 : 12);
}
// Find days to display underneath the graph // Find days to display underneath the graph
for (int i = 0; i < 5; ++i) for (int i = 0; i < 5; ++i)
{ {
itoa(((day - i * 7) % 31 + 31) % 31, temp, 10); itoa(((day - i * 7) % 31 + 31) % 31, temp, 10);
itoa(month - ((day - i * 7) <= 0), temp + 32, 10);
strncpy(dates + 8 * (4 - i), temp, 8); strncpy(dates + 8 * (4 - i), temp, 8);
strcat(dates + 8 * (4 - i), ".");
strcat(dates + 8 * (4 - i), temp + 32);
strcat(dates + 8 * (4 - i), ".");
} }
// Used for drawing lines // Used for drawing lines
@ -183,20 +203,20 @@ void drawGraph()
{ {
// Calculate heights and current x value for a data point // Calculate heights and current x value for a data point
int tx = x1 + i * (x2 - x1 - textMargin) / 31; int tx = x1 + i * (x2 - x1 - textMargin) / 31;
float v = data[i]; double v = data[i];
int h = (int)((float)(v - minData) * (float)abs(y1 - y2) / (float)abs(maxData - minData)); int h = (int)((double)(v - minData) * (double)abs(y1 - y2) / span);
int ty = y1 - h; int ty = y1 - h;
// If i is not 0, hence prev x and y exsist so draw gradients under them // If i is not 0, hence prev x and y exsist so draw gradients under them
if (i) if (i)
{ {
// Rise over run for one pixel // Rise over run for one pixel
float dy = (float)(ty - prev_y) / (float)((x2 - x1 - textMargin) / 31); double dy = (double)(ty - prev_y) / (double)((x2 - x1 - textMargin) / 31);
// Draw a gradient line from every pixel to bottom line in graph line // Draw a gradient line from every pixel to bottom line in graph line
for (int j = 0; j < (x2 - x1 - textMargin) / 31 + 1; ++j) for (int j = 0; j < (x2 - x1 - textMargin) / 31 + 1; ++j)
display.drawGradientLine(prev_x + j, display.drawGradientLine(prev_x + j,
(int)round((float)prev_y + dy * (float)j), (int)round((double)prev_y + dy * (double)j),
prev_x + j, prev_x + j,
y1, 3, 7); y1, 3, 7);
} }
@ -211,8 +231,8 @@ void drawGraph()
{ {
// Calculate heights and current x value for a data point // Calculate heights and current x value for a data point
int tx = x1 + i * (x2 - x1 - textMargin) / 31; int tx = x1 + i * (x2 - x1 - textMargin) / 31;
float v = data[i]; double v = data[i];
int h = (int)((float)(v - minData) * (float)abs(y1 - y2) / (float)abs(maxData - minData)); int h = (int)((double)(v - minData) * (double)abs(y1 - y2) / span);
int ty = y1 - h; int ty = y1 - h;
// If i is not 0, hence prev x and y exsist so draw lines // If i is not 0, hence prev x and y exsist so draw lines
@ -255,7 +275,7 @@ void drawAll()
for (int i = 0; i < 12; ++i) for (int i = 0; i < 12; ++i)
{ {
if (strncmp(months[i], date, 3) == 0) if (strncmp(months[i], date, 3) == 0)
sprintf(fromToDate, "%d.%d to %d.%d", ((i + 1) % 12 ? i + 1 : 12), day, ((i + 2) % 12 ? i + 2 : 12), day); sprintf(fromToDate, "%d.%d. to %d.%d.", day, ((i + 1) % 12 ? i + 1 : 12), day, ((i + 2) % 12 ? i + 2 : 12));
} }
// Draw graph // Draw graph
@ -329,14 +349,17 @@ void setup()
// Initial display settings // Initial display settings
display.begin(); display.begin();
display.clearDisplay();
display.clean(); display.clean();
display.clearDisplay();
display.setTextWrap(false); display.setTextWrap(false);
display.setTextColor(0, 7);
// Welcome screen // Welcome screen
display.setCursor(50, 290); display.setCursor(70, 230);
display.setTextSize(3); display.setTextSize(2);
display.print(F("Welcome to Inkplate 6 btc/eth tracker example!")); display.println(F("Welcome to Inkplate 6 btc/eth tracker example!"));
display.setCursor(70, 250);
display.println(F("Connecting to WiFi..."));
display.display(); display.display();
delay(5000); delay(5000);

View File

@ -5,6 +5,8 @@
#include <HTTPClient.h> #include <HTTPClient.h>
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
#include "Inkplate.h"
// Must be installed for this example to work // Must be installed for this example to work
#include <ArduinoJson.h> #include <ArduinoJson.h>
@ -13,6 +15,8 @@ extern char *ssid;
extern char *pass; extern char *pass;
extern char *currency; extern char *currency;
extern Inkplate display;
// WiFiMulti object declaration // WiFiMulti object declaration
WiFiMulti WiFiMulti; WiFiMulti WiFiMulti;
@ -61,7 +65,7 @@ void Network::getTime(char *timeStr)
timeStr[8] = hr % 10 + '0'; timeStr[8] = hr % 10 + '0';
} }
bool Network::getData(float *data) bool Network::getData(double *data)
{ {
bool f = 0; bool f = 0;
@ -111,11 +115,23 @@ bool Network::getData(float *data)
// Save our data to data pointer from main file // Save our data to data pointer from main file
for (int i = 0; i < 31; ++i) for (int i = 0; i < 31; ++i)
{ {
data[i] = doc["prices"][92 - 31 + i][1].as<float>(); data[i] = doc["prices"][92 - 31 + i][1].as<double>();
//Serial.println(data[i]); //Serial.println(data[i]);
} }
f = 0;
} }
} }
else if (httpCode == 404)
{
// Coin id not found
display.clearDisplay();
display.setCursor(50, 230);
display.setTextSize(2);
display.println(F("Your entered coin does not exsist!"));
display.display();
while (1)
;
}
else else
{ {
f = 1; f = 1;

View File

@ -22,7 +22,7 @@ public:
// Functions we can access in main file // Functions we can access in main file
void begin(); void begin();
void getTime(char *timeStr); void getTime(char *timeStr);
bool getData(float *data); bool getData(double *data);
private: private:
// Functions called from within our class // Functions called from within our class