Paperd.Ink Library 0.0.5
Library for interacting with Paperd.Ink devices.
Loading...
Searching...
No Matches
crypto.cpp
Go to the documentation of this file.
1#include <WiFiClientSecure.h>
2#include <HTTPClient.h>
3
4#include "crypto.h"
5#include "crypto_parser.h"
6
7static String url = "https://api.coingecko.com/api/v3/simple/price?vs_currencies=USD&include_24hr_change=true&precision=5&ids=";
9
10// Valid till 3 July 2023
11static const char *cert = "-----BEGIN CERTIFICATE-----\n" \
12 "MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ\n" \
13 "RTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJlclRydXN0MSIwIAYD\n" \
14 "VQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTAwMDUxMjE4NDYwMFoX\n" \
15 "DTI1MDUxMjIzNTkwMFowWjELMAkGA1UEBhMCSUUxEjAQBgNVBAoTCUJhbHRpbW9y\n" \
16 "ZTETMBEGA1UECxMKQ3liZXJUcnVzdDEiMCAGA1UEAxMZQmFsdGltb3JlIEN5YmVy\n" \
17 "VHJ1c3QgUm9vdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKMEuyKr\n" \
18 "mD1X6CZymrV51Cni4eiVgLGw41uOKymaZN+hXe2wCQVt2yguzmKiYv60iNoS6zjr\n" \
19 "IZ3AQSsBUnuId9Mcj8e6uYi1agnnc+gRQKfRzMpijS3ljwumUNKoUMMo6vWrJYeK\n" \
20 "mpYcqWe4PwzV9/lSEy/CG9VwcPCPwBLKBsua4dnKM3p31vjsufFoREJIE9LAwqSu\n" \
21 "XmD+tqYF/LTdB1kC1FkYmGP1pWPgkAx9XbIGevOF6uvUA65ehD5f/xXtabz5OTZy\n" \
22 "dc93Uk3zyZAsuT3lySNTPx8kmCFcB5kpvcY67Oduhjprl3RjM71oGDHweI12v/ye\n" \
23 "jl0qhqdNkNwnGjkCAwEAAaNFMEMwHQYDVR0OBBYEFOWdWTCCR1jMrPoIVDaGezq1\n" \
24 "BE3wMBIGA1UdEwEB/wQIMAYBAf8CAQMwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3\n" \
25 "DQEBBQUAA4IBAQCFDF2O5G9RaEIFoN27TyclhAO992T9Ldcw46QQF+vaKSm2eT92\n" \
26 "9hkTI7gQCvlYpNRhcL0EYWoSihfVCr3FvDB81ukMJY2GQE/szKN+OMY3EU/t3Wgx\n" \
27 "jkzSswF07r51XgdIGn9w/xZchMB5hbgF/X++ZRGjD8ACtPhSNzkE1akxehi/oCr0\n" \
28 "Epn3o0WC4zxe9Z2etciefC7IpJ5OCBRLbf1wbWsaY71k5h+3zvDyny67G7fyUIhz\n" \
29 "ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS\n" \
30 "R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp\n" \
31 "-----END CERTIFICATE-----\n";
32
33int8_t PaperdinkUICryptoClass::fetch_data(const char *ticker)
34{
35 int httpCode, ret = 0;
36 String payload;
37 WiFiClientSecure *client = new WiFiClientSecure;
38 client->setCACert(cert);
39
40 {
41 HTTPClient https;
42
43 String get_url = url + ticker;
44
45 https.begin(*client, get_url.c_str());
46 https.addHeader("Content-Type", "application/json", 0, 0);
47
48 httpCode = https.GET();
49
50 if (httpCode == HTTP_CODE_OK) {
51 // HTTP header has been send and Server response header has been handled
52 DEBUG.printf("[HTTP] GET SUCCESS\r\n");
53 //String payload = https.getString();
54 //Serial.println(payload);
56 parser.setListener(&crypto_listener);
57 https.writeToStream(&parser);
58 } else {
59 DEBUG.printf("[HTTP] Failed, error: %s\r\n", https.errorToString(httpCode).c_str());
60 ret = -1;
61 }
62
63 https.end();
64 }
65
66 delete client;
67 DEBUG.printf("[HTTP] COMPLETED \r\n");
68 return ret;
69}
70
71void PaperdinkUICryptoClass::display_med_box(GxEPD2_GFX& display, int16_t x, int16_t y, const char *ticker)
72{
73 int16_t xt, yt;
74 uint16_t wt, ht, prev_height = 0, prev_width = 0;
75 const int16_t block_width = 100;
76 const int16_t block_height = 50;
77
78 //const char ticker[] = "Bitcoin";
79 const char dummy_price[] = "USD 7.77777777"; // decides width of the box
80 //const char change[] = "1.32%";
81
82 display.setFont(font_bold);
83 display.setTextColor(primary_color);
84 display.setTextSize(1);
85
86 /* Display ticker string */
87 display.getTextBounds(F("Bitcoin"), 0, 0, &xt, &yt, &wt, &ht);
88 display.setCursor(x + 14, y + ht + 3);
89 display.print(ticker);
90
91 display.setFont(font);
92 /* Display background */
93 display.getTextBounds(dummy_price, 0, 0, &xt, &yt, &wt, &ht);
94 display.fillRoundRect(x + 5, y + 30, wt + 20, ht + 10, ht + 10, secondary_color);
95 prev_width += x + 5 + wt + 20;
96 prev_height += y + 30;
97
98 /* Display current price */
99 display.setTextColor(tertiary_color);
100 display.getTextBounds(price, 0, 0, &xt, &yt, &wt, &ht);
101 display.setCursor(5 + ((x + prev_width - wt) / 2), prev_height + ht + 4);
102 display.print(price);
103 prev_width += 4;
104 prev_height += -2;
105
106 /* Display arrow */
107 const uint8_t *arrow = up_arrow_sml;
108 uint16_t color = primary_color;
109 if (change[0] == '-') {
110 arrow = down_arrow_sml;
111 if (display.epd2.hasColor)
112 color = secondary_color;
113 }
114 display.drawBitmap(prev_width, prev_height, arrow, 24, 24, color);
115 prev_width += 24; // Size of bitmap
116
117 // Display percentage change
118 display.setTextColor(primary_color);
119 display.getTextBounds(change, 0, 0, &xt, &yt, &wt, &ht);
120 display.setCursor(prev_width - wt, y + ht + 3);
121 display.print(change);
122}
123
const bool hasColor
Definition GxEPD2_EPD.h:30
GxEPD2_EPD & epd2
Definition GxEPD2_GFX.h:94
const GFXfont * font
Definition ui_base.h:9
const GFXfont * font_bold
Definition ui_base.h:10
int8_t fetch_data(const char *ticker)
Definition crypto.cpp:33
char change[CRYPTO_MAX_CHANGE_LENGTH_STR]
Definition crypto.h:23
void display_med_box(GxEPD2_GFX &display, int16_t x, int16_t y, const char *ticker)
Definition crypto.cpp:71
char price[CRYPTO_MAX_PRICE_LENGTH_STR]
Definition crypto.h:22
PaperdinkUICryptoClass Paperdink_Crypto
Definition crypto.cpp:124
CryptoJsonListener crypto_listener
Definition crypto.cpp:8
#define DEBUG
Definition crypto.h:6
const unsigned char down_arrow_sml[72]
const unsigned char up_arrow_sml[72]
Definition up_arrow_sml.h:1