Skip to content

Commit

Permalink
Merge pull request #37 from suchmememanyskill/dev
Browse files Browse the repository at this point in the history
v1.3.0
  • Loading branch information
suchmememanyskill authored Feb 10, 2024
2 parents 5bbdc9e + c443bb7 commit 2b92b8d
Show file tree
Hide file tree
Showing 25 changed files with 495 additions and 104 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/compile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ permissions:

on:
push:
branches:
- '*'
tags-ignore:
- '*'
pull_request:
workflow_dispatch:
release:
Expand Down
2 changes: 1 addition & 1 deletion CYD-Klipper/boards/esp32-3248S035C.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"-DLOAD_FONT2=1",
"-DLOAD_GFXFF=1",
"-DSMOOTH_FONT=1",
"-DSPI_FREQUENCY=55000000",
"-DSPI_FREQUENCY=80000000",
"-DSPI_READ_FREQUENCY=20000000",
"-DSPI_TOUCH_FREQUENCY=2500000",
"-DTOUCH_CS=-1",
Expand Down
5 changes: 3 additions & 2 deletions CYD-Klipper/src/conf/global_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

#include "lvgl.h"

#define CONFIG_VERSION 3
#define CONFIG_VERSION 4

typedef struct _GLOBAL_CONFIG {
unsigned char version;
union {
unsigned char raw;
unsigned int raw;
struct {
// Internal
bool screenCalibrated : 1;
Expand All @@ -20,6 +20,7 @@ typedef struct _GLOBAL_CONFIG {
bool invertColors : 1;
bool rotateScreen : 1;
bool onDuringPrint : 1;
bool autoOtaUpdate : 1;
};
};
float screenCalXOffset;
Expand Down
2 changes: 1 addition & 1 deletion CYD-Klipper/src/conf/lv_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@
#define LV_FONT_CUSTOM_DECLARE

/*Always set a default font*/
#define LV_FONT_DEFAULT &lv_font_montserrat_14
#define LV_FONT_DEFAULT &CYD_SCREEN_FONT

/*Enable handling large font and/or fonts with a lot of characters.
*The limit depends on the font size, font face and bpp.
Expand Down
8 changes: 4 additions & 4 deletions CYD-Klipper/src/core/device/ESP32-2432S028R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ void touchscreen_calibrate(bool force)
tft.drawFastHLine(300, 230, 20, ILI9341_BLACK);
tft.drawFastVLine(310, 220, 20, ILI9341_BLACK);

int16_t xDist = 320 - 40;
int16_t yDist = 240 - 40;
int16_t xDist = 320 - 20;
int16_t yDist = 240 - 20;

global_config.screenCalXMult = (float)xDist / (float)(x2 - x1);
global_config.screenCalXOffset = 20.0 - ((float)x1 * global_config.screenCalXMult);
global_config.screenCalXOffset = 10.0 - ((float)x1 * global_config.screenCalXMult);

global_config.screenCalYMult = (float)yDist / (float)(y2 - y1);
global_config.screenCalYOffset = 20.0 - ((float)y1 * global_config.screenCalYMult);
global_config.screenCalYOffset = 10.0 - ((float)y1 * global_config.screenCalYMult);

global_config.screenCalibrated = true;
WriteGlobalConfig();
Expand Down
12 changes: 3 additions & 9 deletions CYD-Klipper/src/core/lv_setup.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "lv_setup.h"
#include "screen_driver.h"
#include "../conf/global_config.h"
#include "lvgl.h"
#include <Esp.h>

#ifndef CPU_FREQ_HIGH
Expand All @@ -10,9 +11,10 @@
#define CPU_FREQ_LOW 80
#endif

typedef void (*lv_indev_drv_read_cb_t)(struct _lv_indev_drv_t * indev_drv, lv_indev_data_t * data);

bool isScreenInSleep = false;
lv_timer_t *screenSleepTimer;
static lv_style_t default_label_style;

void set_screen_brightness()
{
Expand Down Expand Up @@ -120,9 +122,6 @@ void lv_touch_intercept(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)

void lv_setup()
{
lv_style_init(&default_label_style);
lv_style_set_text_font(&default_label_style, &CYD_SCREEN_FONT);

screen_timer_setup();
screen_timer_start();
set_color_scheme();
Expand All @@ -138,9 +137,4 @@ void lv_setup()
bool is_screen_asleep()
{
return isScreenInSleep;
}

lv_style_t * get_default_label_style()
{
return &default_label_style;
}
8 changes: 1 addition & 7 deletions CYD-Klipper/src/core/lv_setup.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#pragma once

#include "lvgl.h"

typedef void (*lv_indev_drv_read_cb_t)(struct _lv_indev_drv_t * indev_drv, lv_indev_data_t * data);

void set_screen_brightness();
void set_screen_timer_period();
void screen_timer_wake();
void screen_timer_start();
void screen_timer_stop();
void set_color_scheme();
void lv_setup();
bool is_screen_asleep();

lv_style_t * get_default_label_style();
bool is_screen_asleep();
230 changes: 230 additions & 0 deletions CYD-Klipper/src/lib/ESP32OTAPull.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
ESP32-OTA-Pull - a library for doing "pull" based OTA ("Over The Air") firmware
updates, where the image updates are posted on the web.
MIT License
Copyright (c) 2022-3 Mikal Hart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#pragma once
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Update.h>
#include <WiFi.h>

class ESP32OTAPull
{
public:
enum ActionType { DONT_DO_UPDATE, UPDATE_BUT_NO_BOOT, UPDATE_AND_BOOT };

// Return codes from CheckForOTAUpdate
enum ErrorCode { UPDATE_AVAILABLE = -3, NO_UPDATE_PROFILE_FOUND = -2, NO_UPDATE_AVAILABLE = -1, UPDATE_OK = 0, HTTP_FAILED = 1, WRITE_ERROR = 2, JSON_PROBLEM = 3, OTA_UPDATE_FAIL = 4 };

private:
void (*Callback)(int offset, int totallength) = NULL;
ActionType Action = UPDATE_AND_BOOT;
String Board = ARDUINO_BOARD;
String Device = "";
String Config = "";
String CVersion = "";
bool DowngradesAllowed = false;

int DownloadJson(const char* URL, String& payload)
{
HTTPClient http;
http.begin(URL);

// Send HTTP GET request
int httpResponseCode = http.GET();

if (httpResponseCode == 200)
{
payload = http.getString();
}

// Free resources
http.end();
return httpResponseCode;
}

int DoOTAUpdate(const char* URL, ActionType Action)
{
HTTPClient http;
http.begin(URL);

// Send HTTP GET request
int httpResponseCode = http.GET();

if (httpResponseCode == 200)
{
int totalLength = http.getSize();

// this is required to start firmware update process
if (!Update.begin(UPDATE_SIZE_UNKNOWN))
return OTA_UPDATE_FAIL;

// create buffer for read
uint8_t buff[1280] = { 0 };

// get tcp stream
WiFiClient* stream = http.getStreamPtr();

// read all data from server
int offset = 0;
while (http.connected() && offset < totalLength)
{
size_t sizeAvail = stream->available();
if (sizeAvail > 0)
{
size_t bytes_to_read = min(sizeAvail, sizeof(buff));
size_t bytes_read = stream->readBytes(buff, bytes_to_read);
size_t bytes_written = Update.write(buff, bytes_read);
if (bytes_read != bytes_written)
{
// Serial.printf("Unexpected error in OTA: %d %d %d\n", bytes_to_read, bytes_read, bytes_written);
break;
}
offset += bytes_written;
if (Callback != NULL)
Callback(offset, totalLength);
}
}

if (offset == totalLength)
{
Update.end(true);
delay(1000);

// Restart ESP32 to see changes
if (Action == UPDATE_BUT_NO_BOOT)
return UPDATE_OK;
ESP.restart();
}
return WRITE_ERROR;
}

http.end();
return httpResponseCode;
}

public:
/// @brief Return the version string of the binary, as reported by the JSON
/// @return The firmware version
String GetVersion()
{
return CVersion;
}

/// @brief Override the default "Device" id (MAC Address)
/// @param device A string identifying the particular device (instance) (typically e.g., a MAC address)
/// @return The current ESP32OTAPull object for chaining
ESP32OTAPull &OverrideDevice(const char *device)
{
Device = device;
return *this;
}

/// @brief Override the default "Board" value of ARDUINO_BOARD
/// @param board A string identifying the board (class) being targeted
/// @return The current ESP32OTAPull object for chaining
ESP32OTAPull &OverrideBoard(const char *board)
{
Board = board;
return *this;
}

/// @brief Specify a configuration string that must match any "Config" in JSON
/// @param config An arbitrary string showing the current configuration
/// @return The current ESP32OTAPull object for chaining
ESP32OTAPull &SetConfig(const char *config)
{
Config = config;
return *this;
}

/// @brief Specify whether downgrades (posted version is lower) are allowed
/// @param allow_downgrades true if downgrades are allowed
/// @return The current ESP32OTAPull object for chaining
ESP32OTAPull &AllowDowngrades(bool allow_downgrades)
{
DowngradesAllowed = allow_downgrades;
return *this;
}

/// @brief Specify a callback function to monitor update progress
/// @param callback Pointer to a function that is called repeatedly during update
/// @return The current ESP32OTAPull object for chaining
ESP32OTAPull &SetCallback(void (*callback)(int offset, int totallength))
{
Callback = callback;
return *this;
}

/// @brief The main entry point for OTA Update
/// @param JSON_URL The URL for the JSON filter file
/// @param CurrentVersion The version # of the current (i.e. to be replaced) sketch
/// @param ActionType The action to be performed. May be any of DONT_DO_UPDATE, UPDATE_BUT_NO_BOOT, UPDATE_AND_BOOT (default)
/// @return ErrorCode or HTTP failure code (see enum above)
int CheckForOTAUpdate(const char* JSON_URL, const char *CurrentVersion, ActionType Action = UPDATE_AND_BOOT)
{
CurrentVersion = CurrentVersion == NULL ? "" : CurrentVersion;

// Downloading OTA Json...
String Payload;
int httpResponseCode = DownloadJson(JSON_URL, Payload);
if (httpResponseCode != 200)
return httpResponseCode > 0 ? httpResponseCode : HTTP_FAILED;

// Deserialize the JSON file downloaded from user's site
JsonDocument doc;
DeserializationError deserialization = deserializeJson(doc, Payload.c_str());
if (deserialization != DeserializationError::Ok)
return JSON_PROBLEM;

String DeviceName = Device.isEmpty() ? WiFi.macAddress() : Device;
String BoardName = Board.isEmpty() ? ARDUINO_BOARD : Board;
String ConfigName = Config.isEmpty() ? "" : Config;
bool foundProfile = false;

// Step through the configurations looking for a match
for (auto config : doc["Configurations"].as<JsonArray>())
{
String CBoard = config["Board"].isNull() ? "" : (const char *)config["Board"];
String CDevice = config["Device"].isNull() ? "" : (const char *)config["Device"];
CVersion = config["Version"].isNull() ? "" : (const char *)config["Version"];
String CConfig = config["Config"].isNull() ? "" : (const char *)config["Config"];
//Serial.printf("Checking %s %s %s %s\n", CBoard.c_str(), CDevice.c_str(), CVersion.c_str(), CConfig.c_str());
//Serial.printf("Against %s %s %s %s\n", BoardName.c_str(), DeviceName.c_str(), CurrentVersion, ConfigName.c_str());
if ((CBoard.isEmpty() || CBoard == BoardName) &&
(CDevice.isEmpty() || CDevice == DeviceName) &&
(CConfig.isEmpty() || CConfig == ConfigName))
{
if (CVersion.isEmpty() || CVersion > String(CurrentVersion) ||
(DowngradesAllowed && CVersion != String(CurrentVersion)))
return Action == DONT_DO_UPDATE ? UPDATE_AVAILABLE : DoOTAUpdate(config["URL"], Action);
foundProfile = true;
}
}
return foundProfile ? NO_UPDATE_AVAILABLE : NO_UPDATE_PROFILE_FOUND;
}
};

7 changes: 7 additions & 0 deletions CYD-Klipper/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ui/nav_buttons.h"
#include <Esp.h>
#include "core/lv_setup.h"
#include "ui/ota_setup.h"

void setup() {
Serial.begin(115200);
Expand All @@ -18,6 +19,7 @@ void setup() {
Serial.println("Screen init done");

wifi_init();
ota_init();
ip_init();
data_setup();

Expand All @@ -31,4 +33,9 @@ void loop(){
data_loop();
lv_timer_handler();
lv_task_handler();

if (is_ready_for_ota_update())
{
ota_do_update();
}
}
Loading

0 comments on commit 2b92b8d

Please sign in to comment.