Files
Arduino/ESP32_WAVESHARE_7x5/Connect.cpp
Jérôme Delacotte 7b30d6e298 first commit
2025-03-06 11:15:32 +01:00

511 lines
18 KiB
C++

#include "Arduino.h"
#include "Connect.h"
#include "Ecran.h"
#include <WiFi.h> // Built-in
const char* ssid = "Livebox-37cc"; // WiFi SSID to connect to
const char* password = "8A6060920A8A86896F770F2C47"; // WiFi password needed for the SSID
Connect::Connect(Ecran * _ecran, const char* _ssid, const char* _password)
{
ssid = _ssid;
password = _password;
ecran = _ecran;
}
//#########################################################################################
uint8_t Connect::start() {
if (debug) Serial.println("B - start");
if (debug) Serial.print(" Connecting to: ");
if (debug) Serial.println(String(ssid));
IPAddress dns(8, 8, 8, 8); // Google DNS
WiFi.disconnect();
WiFi.mode(WIFI_STA); // switch off AP
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
WiFi.begin(ssid, password);
unsigned long start = millis();
uint8_t connectionStatus;
bool AttemptConnection = true;
while (AttemptConnection) {
connectionStatus = WiFi.status();
if (millis() > start + 15000) { // Wait 15-secs maximum
AttemptConnection = false;
}
if (connectionStatus == WL_CONNECTED || connectionStatus == WL_CONNECT_FAILED) {
AttemptConnection = false;
}
delay(50);
}
if (connectionStatus == WL_CONNECTED) {
wifi_signal = WiFi.RSSI(); // Get Wifi Signal strength now, because the WiFi will be turned off to save power!
if (debug) Serial.println(" WiFi connected at: " + WiFi.localIP().toString());
}
else if (debug) Serial.println(" WiFi connection *** FAILED ***");
if (debug) Serial.println("E - start" + String(connectionStatus));
return connectionStatus;
}
//#########################################################################################
void Connect::stop() {
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
}
//#########################################################################################
void Connect::drawRSSI(int x, int y) {
int WIFIsignal = 0;
int xpos = 1;
for (int _rssi = -100; _rssi <= wifi_signal; _rssi = _rssi + 20) {
if (_rssi <= -20) WIFIsignal = 20; // <-20dbm displays 5-bars
if (_rssi <= -40) WIFIsignal = 16; // -40dbm to -21dbm displays 4-bars
if (_rssi <= -60) WIFIsignal = 12; // -60dbm to -41dbm displays 3-bars
if (_rssi <= -80) WIFIsignal = 8; // -80dbm to -61dbm displays 2-bars
if (_rssi <= -100) WIFIsignal = 4; // -100dbm to -81dbm displays 1-bar
ecran->_display->fillRect(x + xpos * 6, y - WIFIsignal, 5, WIFIsignal, GxEPD_RED);
xpos++;
}
ecran->_display->fillRect(x, y - 1, 5, 1, GxEPD_BLACK);
ecran->drawString(x - 26, y - 10, String(wifi_signal) + "dBm", CENTER);
}
bool Connect::stopClient()
{
if (debug) Serial.println("B - stopClient");
client.stop();
if (debug) Serial.println("E - stopClient");
}
bool Connect::connectClient(String host, String port)
{
if (debug) Serial.println("B - connectClient" + host + " " + port);
// Domo
char _domoc[host.length() + 1];
host.toCharArray(_domoc, host.length() + 1);
// Port
char portc[port.length() + 1];
port.toCharArray(portc, port.length() + 1);
int _iport = atoi(portc);
bool connected = client.connect(_domoc, _iport);
if (debug) Serial.print(_domoc);
if (debug) Serial.print(" ");
if (debug) Serial.print(_iport);
if (debug) Serial.print(" ");
if (debug) Serial.println(" connected ");
// // printf(connected);
if (debug) Serial.println("E - connectClient " + String(connected));
return connected;
}
String Connect::callJson(String host, String port, const String& uri, String bearer) {
if (debug) Serial.println("B - callJson " + String(uri));
client.stop(); // close connection before sending a new request
// connectDomoticz();
connectClient(host, port);
char portc[port.length() + 1];
port.toCharArray(portc, port.length() + 1);
int iport = atoi(portc);
HTTPClient http;
String json = "";
http.begin(client, host, iport, uri);
if (bearer != "") {
http.addHeader("Authorization", String("Bearer " + bearer));
}
int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK) {
json = http.getString();
client.stop();
http.end();
if (debug) Serial.println("E - callJson");
return json;
}
else
{
if (debug) Serial.printf("connection failed, error: %s", http.errorToString(httpCode).c_str());
client.stop();
http.end();
if (debug) Serial.println("E - callJson");
return json;
}
http.end();
return json;
}
// Source from https://github.com/ZinggJM/GxEPD2
// and adapted
void Connect::loadBitmap(String host, String port, const String& uri)
{
uint16_t input_buffer_pixels = SCREEN_WIDTH; // may affect performance
uint16_t max_row_width = SCREEN_WIDTH; // for up to 7.5" display
uint16_t max_palette_pixels = 256; // for depth <= 8
uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
uint16_t rgb_palette_buffer[max_palette_pixels]; // palette buffer for depth <= 8 for buffered graphics, needed for 7-color display
int16_t x = 0;
int16_t y = 0;
bool with_color = true;
bool has_multicolors = (ecran->_display->epd2.panel == GxEPD2::ACeP565) || (ecran->_display->epd2.panel == GxEPD2::GDEY073D46);
bool connection_ok = false;
bool valid = false; // valid format to be handled
bool flip = true; // bitmap is stored bottom-to-top
uint32_t startTime = millis();
// x 0 ... if ((x > ecran->_display->width()) || (y > ecran->_display->height())) return;
if (debug) Serial.println();
if (debug) Serial.print("downloading file \"");
if (debug) Serial.print(uri);
if (debug) Serial.println("\"");
if (debug) Serial.print("connecting to ");
if (debug) Serial.println(host);
client.stop(); // close connection before sending a new request
if (!connectClient(host, "80"))
{
if (debug) Serial.println("connection failed");
return;
}
if (debug) Serial.print("requesting URL: ");
if (debug) Serial.println(String("http://") + host + uri);
client.print(String("GET ") + uri + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: GxEPD2_WiFi_Example\r\n" +
"Connection: close\r\n\r\n");
if (debug) Serial.println("request sent");
while (client.connected())
{
String line = client.readStringUntil('\n');
if (!connection_ok)
{
connection_ok = line.startsWith("HTTP/1.1 200 OK");
if (connection_ok) if (debug) Serial.println(line);
//if (!connection_ok) if (debug) Serial.println(line);
}
if (!connection_ok) if (debug) Serial.println(line);
//if (debug) Serial.println(line);
if (line == "\r")
{
if (debug) Serial.println("headers received");
break;
}
}
if (!connection_ok) return;
// Parse BMP header
if (read16() == 0x4D42) // BMP signature
{
uint32_t fileSize = read32();
uint32_t creatorBytes = read32();
uint32_t imageOffset = read32(); // Start of image data
uint32_t headerSize = read32();
uint32_t width = read32();
uint32_t height = read32();
uint16_t planes = read16();
uint16_t depth = read16(); // bits per pixel
uint32_t format = read32();
if (debug) Serial.println();
if (debug) Serial.println("-------");
if (debug) Serial.println(width);
if (debug) Serial.println(height);
if (debug) Serial.println(planes);
if (debug) Serial.println(depth);
if (debug) Serial.println(format);
if (debug) Serial.println("-------");
uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
{
if (debug) Serial.print("File size: "); if (debug) Serial.println(fileSize);
if (debug) Serial.print("Image Offset: "); if (debug) Serial.println(imageOffset);
if (debug) Serial.print("Header size: "); if (debug) Serial.println(headerSize);
if (debug) Serial.print("Bit Depth: "); if (debug) Serial.println(depth);
if (debug) Serial.print("Image size: "); if (debug) Serial.print(width);
if (debug) Serial.print('x'); if (debug) Serial.println(height);
// BMP rows are padded (if needed) to 4-byte boundary
uint32_t rowSize = (width * depth / 8 + 3) & ~3;
if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
if (height < 0)
{
height = -height;
flip = false;
}
uint16_t w = width;
uint16_t h = height;
if ((x + w - 1) >= ecran->_display->epd2.WIDTH) w = ecran->_display->epd2.WIDTH - x;
if ((y + h - 1) >= ecran->_display->epd2.HEIGHT) h = ecran->_display->epd2.HEIGHT - y;
if (debug) Serial.print('w'); if (debug) Serial.println(w);
if (debug) Serial.print('h'); if (debug) Serial.println(h);
//if (w <= max_row_width) // handle with direct drawing
{
valid = true;
uint8_t bitmask = 0xFF;
uint8_t bitshift = 8 - depth;
uint16_t red, green, blue;
bool whitish = false;
bool colored = false;
if (depth == 1) with_color = false;
if (depth <= 8)
{
if (depth < 8) bitmask >>= depth;
//bytes_read += skip(54 - bytes_read); //palette is always @ 54
bytes_read += skip(imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
for (uint16_t pn = 0; pn < (1 << depth); pn++)
{
blue = client.read();
green = client.read();
red = client.read();
client.read();
bytes_read += 4;
whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
mono_palette_buffer[pn / 8] |= whitish << pn % 8;
if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
color_palette_buffer[pn / 8] |= colored << pn % 8;
//if (debug) Serial.print("0x00"); if (debug) Serial.print(red, HEX); if (debug) Serial.print(green, HEX); if (debug) Serial.print(blue, HEX);
//if (debug) Serial.print(" : "); if (debug) Serial.print(whitish); if (debug) Serial.print(", "); if (debug) Serial.println(colored);
rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
}
}
uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
//if (debug) Serial.print("skip "); if (debug) Serial.println(rowPosition - bytes_read);
bytes_read += skip(rowPosition - bytes_read);
for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
{
if (debug) Serial.print("debut row="); if (debug) Serial.println(row);
if (!connection_ok || !(client.connected() || client.available())) {
if (debug) Serial.println("connexion lost");
break;
}
delay(1); // yield() to avoid WDT
uint32_t in_remain = rowSize;
uint32_t in_idx = 0;
uint32_t in_bytes = 0;
uint8_t in_byte = 0; // for depth <= 8
uint8_t in_bits = 0; // for depth <= 8
uint16_t color = GxEPD_WHITE;
for (uint16_t col = 0; col < w; col++) // for each pixel
{
yield();
if (!connection_ok || !(client.connected() || client.available())) break;
// Time to read more pixel data?
if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
{
uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
uint32_t got = read8n(input_buffer, get);
while ((got < get) && connection_ok)
{
//if (debug) Serial.print("got "); if (debug) Serial.print(got); if (debug) Serial.print(" < "); if (debug) Serial.print(get); if (debug) Serial.print(" @ "); if (debug) Serial.println(bytes_read);
uint32_t gotmore = read8n(input_buffer + got, get - got);
got += gotmore;
connection_ok = gotmore > 0;
}
in_bytes = got;
in_remain -= got;
bytes_read += got;
}
if (!connection_ok)
{
if (debug) Serial.print("Error: got no more after "); if (debug) Serial.print(bytes_read); if (debug) Serial.println(" bytes read!");
break;
}
switch (depth)
{
case 32:
blue = input_buffer[in_idx++];
green = input_buffer[in_idx++];
red = input_buffer[in_idx++];
in_idx++; // skip alpha
whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
break;
case 24:
blue = input_buffer[in_idx++];
green = input_buffer[in_idx++];
red = input_buffer[in_idx++];
whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
break;
case 16:
{
uint8_t lsb = input_buffer[in_idx++];
uint8_t msb = input_buffer[in_idx++];
if (format == 0) // 555
{
blue = (lsb & 0x1F) << 3;
green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
red = (msb & 0x7C) << 1;
color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
}
else // 565
{
blue = (lsb & 0x1F) << 3;
green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
red = (msb & 0xF8);
color = (msb << 8) | lsb;
}
whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
}
break;
case 1:
case 2:
case 4:
case 8:
{
if (0 == in_bits)
{
in_byte = input_buffer[in_idx++];
in_bits = 8;
}
uint16_t pn = (in_byte >> bitshift) & bitmask;
whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
in_byte <<= depth;
in_bits -= depth;
color = rgb_palette_buffer[pn];
}
break;
}
if (with_color && has_multicolors)
{
// keep color
}
else if (whitish)
{
color = GxEPD_WHITE;
}
else if (colored && with_color)
{
color = GxEPD_COLORED;
}
else
{
color = GxEPD_BLACK;
}
uint16_t yrow = y + (flip ? h - row - 1 : row);
ecran->_display->drawPixel(x + col, yrow, color);
if (debug) Serial.print(".");
} // end pixel
if (debug) Serial.print("end row="); if (debug) Serial.println(row);
} // end line
}
if (debug) Serial.print("bytes read "); if (debug) Serial.println(bytes_read);
}
}
if (debug) Serial.print("loaded in "); if (debug) Serial.print(millis() - startTime); if (debug) Serial.println(" ms");
client.stop();
if (!valid)
{
if (debug) Serial.println("bitmap format not handled.");
}
}
uint32_t Connect::skip(int32_t bytes)
{
int32_t remain = bytes;
uint32_t start = millis();
while ((client.connected() || client.available()) && (remain > 0))
{
if (client.available())
{
int16_t v = client.read();
remain--;
}
else delay(1);
if (millis() - start > 2000) break; // don't hang forever
}
return bytes - remain;
}
uint32_t Connect::read(uint8_t* buffer, int32_t bytes)
{
int32_t remain = bytes;
uint32_t start = millis();
while ((client.connected() || client.available()) && (remain > 0))
{
if (client.available())
{
int16_t v = client.read();
*buffer++ = uint8_t(v);
remain--;
}
else delay(1);
if (millis() - start > 2000) break; // don't hang forever
}
return bytes - remain;
}
uint16_t Connect::read16()
{
// BMP data is stored little-endian, same as Arduino.
uint16_t result;
((uint8_t *)&result)[0] = client.read(); // LSB
((uint8_t *)&result)[1] = client.read(); // MSB
return result;
}
uint32_t Connect::read32()
{
// BMP data is stored little-endian, same as Arduino.
uint32_t result;
((uint8_t *)&result)[0] = client.read(); // LSB
((uint8_t *)&result)[1] = client.read();
((uint8_t *)&result)[2] = client.read();
((uint8_t *)&result)[3] = client.read(); // MSB
return result;
}
uint32_t Connect::read8n(uint8_t* buffer, int32_t bytes)
{
int32_t remain = bytes;
uint32_t start = millis();
while ((client.connected() || client.available()) && (remain > 0))
{
if (client.available())
{
int16_t v = client.read();
*buffer++ = uint8_t(v);
remain--;
}
else delay(1);
if (millis() - start > 2000) break; // don't hang forever
}
return bytes - remain;
}