Skip to content

Commit

Permalink
Merge branch 'main' into lvgl_9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausMu committed Dec 24, 2024
2 parents 184caa5 + fb0d414 commit 1b2beba
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 123 deletions.
103 changes: 64 additions & 39 deletions Platformio/hardware/ESP32/boardtest/main_boardtest.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// OMOTE firmware for ESP32
// 2023 Maximilian Kern

#include <TFT_eSPI.h> // Hardware-specific library
#include <LovyanGFX.hpp>
#include <Keypad.h> // modified for inverted logic
#include <Preferences.h>
#include "SparkFunLIS3DH.h"
Expand All @@ -12,7 +12,6 @@
#include <IRutils.h>
#include <lvgl.h>
#include "WiFi.h"
#include <Adafruit_FT6206.h>
#include "driver/ledc.h"
#include <SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h>
#include "secrets.h"
Expand All @@ -21,7 +20,7 @@

// Pin assignment -----------------------------------------------------------------------------------------------------------------------

#define LCD_DC 9 // defined in TFT_eSPI User_Setup.h
#define LCD_DC 9
#define LCD_CS 5
#define LCD_MOSI 23
#define LCD_SCK 18
Expand Down Expand Up @@ -69,12 +68,57 @@ bool wakeupByIMUEnabled = true;
LIS3DH IMU(I2C_MODE, 0x19); // Default constructor is I2C, addr 0x19.

// LCD declarations
TFT_eSPI tft = TFT_eSPI();
#define screenWidth 240
#define screenHeight 320
Adafruit_FT6206 touch = Adafruit_FT6206();
TS_Point touchPoint;
TS_Point oldPoint;
class LGFX : public lgfx::LGFX_Device
{
lgfx::Panel_ILI9341 _panel_instance;
lgfx::Bus_SPI _bus_instance;
lgfx::Touch_FT5x06 _touch_instance;

public:

LGFX(void)
{
{
auto cfg = _bus_instance.config();
cfg.freq_write = 40000000;
cfg.freq_read = 16000000;
cfg.dma_channel = SPI_DMA_CH_AUTO;
cfg.pin_sclk = LCD_SCK;
cfg.pin_mosi = LCD_MOSI;
cfg.pin_dc = LCD_DC;

_bus_instance.config(cfg);
_panel_instance.setBus(&_bus_instance);
}
{
auto cfg = _panel_instance.config();
cfg.pin_cs = LCD_CS;
cfg.pin_rst = -1;
cfg.pin_busy = -1;
cfg.memory_width = screenWidth;
cfg.memory_height = screenHeight; // 162 or 160 or 132
cfg.panel_width = screenWidth;
cfg.panel_height = screenHeight;
cfg.offset_rotation = 2;

_panel_instance.config(cfg);
}
{
auto cfg = _touch_instance.config();
cfg.i2c_addr = 0x38;
cfg.i2c_port = 0;
cfg.pin_sda = SDA;
cfg.pin_scl = SCL;
cfg.freq = 400000;
_touch_instance.config(cfg);
_panel_instance.setTouch(&_touch_instance);
}
setPanel(&_panel_instance);
}
};
static LGFX tft;
int backlight_brightness = 255;

lv_obj_t* objBattPercentage;
Expand Down Expand Up @@ -158,34 +202,15 @@ static void my_disp_flush( lv_display_t *disp, const lv_area_t *area, uint8_t *p
}

// Read the touchpad
static void my_touchpad_read(lv_indev_t *indev_driver, lv_indev_data_t *data) {
// int16_t touchX, touchY;
touchPoint = touch.getPoint();
int16_t touchX = touchPoint.x;
int16_t touchY = touchPoint.y;
bool touched = false;
if ((touchX > 0) || (touchY > 0)) {
touched = true;
standbyTimer = SLEEP_TIMEOUT;
}

if( !touched ){
data->state = LV_INDEV_STATE_RELEASED;
}
else{
data->state = LV_INDEV_STATE_PRESSED;

// Set the coordinates
data->point.x = screenWidth - touchX;
data->point.y = screenHeight - touchY;

//Serial.print( "touchpoint: x" );
//Serial.print( touchX );
//Serial.print( " y" );
//Serial.println( touchY );
//tft.drawFastHLine(0, screenHeight - touchY, screenWidth, TFT_RED);
//tft.drawFastVLine(screenWidth - touchX, 0, screenHeight, TFT_RED);
}
void my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {
uint16_t x, y;
if (tft.getTouch(&x, &y)) {
data->state = LV_INDEV_STATE_PRESSED;
data->point.x = x;
data->point.y = y;
} else {
data->state = LV_INDEV_STATE_RELEASED;
}
}

void activityDetection(){
Expand Down Expand Up @@ -453,7 +478,6 @@ void setup() {

// Setup touchscreen
Wire.begin(SDA, SCL, 400000); // Configure i2c pins and set frequency to 400kHz
bool TouchInitSuccessful = touch.begin(128); // Initialize touchscreen and set sensitivity threshold

// Setup LVGL
lv_init();
Expand Down Expand Up @@ -562,13 +586,14 @@ void setup() {


// Automated Checks



uint64_t _chipmacid = 0LL;
esp_efuse_mac_get_default((uint8_t*) (&_chipmacid));
Serial.print("ESP32 MAC: ");
Serial.println(_chipmacid);
// Check if the touchscreen is responding
boolean TouchInitSuccessful = false;
Wire.beginTransmission(0x38);
if(Wire.endTransmission() == 0) TouchInitSuccessful = true;


if(IMUInitSuccessful == 0) lv_table_set_cell_value_fmt(ChecksTable, 6, 1, LV_SYMBOL_OK);
Expand Down
37 changes: 9 additions & 28 deletions Platformio/hardware/ESP32/lvgl_hal_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,15 @@ static void my_disp_flush( lv_display_t *disp, const lv_area_t *area, uint8_t *p
}

// Read the touchpad
static void my_touchpad_read(lv_indev_t *indev_driver, lv_indev_data_t *data) {
int16_t touchX;
int16_t touchY;
get_touchpoint(&touchX, &touchY);

bool touched = false;
if ((touchX > 0) || (touchY > 0)) {
touched = true;
setLastActivityTimestamp_HAL();
}

if( !touched ){
data->state = LV_INDEV_STATE_RELEASED;
}
else{
data->state = LV_INDEV_STATE_PRESSED;

// Set the coordinates
data->point.x = SCR_WIDTH - touchX;
data->point.y = SCR_HEIGHT - touchY;

//Serial.print( "touchpoint: x" );
//Serial.print( touchX );
//Serial.print( " y" );
//Serial.println( touchY );
//tft.drawFastHLine(0, SCR_HEIGHT - touchY, SCR_WIDTH, TFT_RED);
//tft.drawFastVLine(SCR_WIDTH - touchX, 0, SCR_HEIGHT, TFT_RED);
}
void my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {
uint16_t x, y;
if (tft.getTouch(&x, &y)) {
data->state = LV_INDEV_STATE_PRESSED;
data->point.x = x;
data->point.y = y;
} else {
data->state = LV_INDEV_STATE_RELEASED;
}
}

/*LVGL draw into this buffer, 1/10 screen size usually works well. The size is in bytes*/
Expand Down
8 changes: 4 additions & 4 deletions Platformio/hardware/ESP32/sleep_hal_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ void enterSleep(){
#endif

// Prepare IO states
digitalWrite(TFT_DC, LOW); // LCD control signals off
digitalWrite(TFT_CS, LOW);
digitalWrite(TFT_MOSI, LOW);
digitalWrite(TFT_SCLK, LOW);
digitalWrite(LCD_DC_GPIO, LOW); // LCD control signals off
digitalWrite(LCD_CS_GPIO, LOW);
digitalWrite(LCD_MOSI_GPIO, LOW);
digitalWrite(LCD_SCK_GPIO, LOW);
digitalWrite(LCD_EN_GPIO, HIGH); // LCD logic off
digitalWrite(LCD_BL_GPIO, HIGH); // LCD backlight off
// pinMode(CRG_STAT, INPUT); // Disable Pull-Up
Expand Down
57 changes: 41 additions & 16 deletions Platformio/hardware/ESP32/tft_hal_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,48 @@ uint8_t SCL_GPIO = 22;

uint8_t LCD_BL_GPIO = 4;
uint8_t LCD_EN_GPIO = 10;
uint8_t LCD_CS_GPIO = 5;
uint8_t LCD_MOSI_GPIO = 23;
uint8_t LCD_SCK_GPIO = 18;
uint8_t LCD_DC_GPIO = 9;

TFT_eSPI tft = TFT_eSPI();
Adafruit_FT6206 touch = Adafruit_FT6206();
TS_Point touchPoint;
LGFX::LGFX(void) {
{
auto cfg = _bus_instance.config();
cfg.freq_write = SPI_FREQUENCY;
cfg.freq_read = 16000000;
cfg.dma_channel = SPI_DMA_CH_AUTO;
cfg.pin_sclk = LCD_SCK_GPIO;
cfg.pin_mosi = LCD_MOSI_GPIO;
cfg.pin_dc = LCD_DC_GPIO;
_bus_instance.config(cfg);
_panel_instance.setBus(&_bus_instance);
}
{
auto cfg = _panel_instance.config();
cfg.pin_cs = LCD_CS_GPIO;
cfg.pin_rst = -1;
cfg.pin_busy = -1;
cfg.memory_width = SCR_WIDTH;
cfg.memory_height = SCR_HEIGHT;
cfg.panel_width = SCR_WIDTH;
cfg.panel_height = SCR_HEIGHT;
cfg.offset_rotation = 2;
_panel_instance.config(cfg);
}
{
auto cfg = _touch_instance.config();
cfg.i2c_addr = 0x38;
cfg.i2c_port = 0;
cfg.pin_sda = SDA_GPIO;
cfg.pin_scl = SCL_GPIO;
cfg.freq = 400000;
_touch_instance.config(cfg);
_panel_instance.setTouch(&_touch_instance);
}
setPanel(&_panel_instance);
}
LGFX tft;
byte backlightBrightness = 255;

void init_tft(void) {
Expand Down Expand Up @@ -65,21 +103,8 @@ void init_tft(void) {
delay(5); // Wait for the LCD driver to power on
tft.init();
tft.initDMA();
tft.setRotation(0);
tft.fillScreen(TFT_BLACK);
tft.setSwapBytes(true);

// SDA and SCL need to be set explicitly, because for IMU you cannot set it explicitly in the constructor.
// Configure i2c pins and set frequency to 400kHz
Wire.begin(SDA_GPIO, SCL_GPIO, 400000);
// Setup touchscreen
touch.begin(128); // Initialize touchscreen and set sensitivity threshold
}

void get_touchpoint(int16_t *touchX, int16_t *touchY) {
touchPoint = touch.getPoint();
*touchX = touchPoint.x;
*touchY = touchPoint.y;
}

void update_backligthBrighness_HAL(void) {
Expand Down
19 changes: 15 additions & 4 deletions Platformio/hardware/ESP32/tft_hal_esp32.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
#pragma once

#include <TFT_eSPI.h>
#include <Adafruit_FT6206.h>
#include <LovyanGFX.hpp>

extern uint8_t LCD_BL_GPIO;
extern uint8_t LCD_EN_GPIO;
extern uint8_t LCD_CS_GPIO;
extern uint8_t LCD_MOSI_GPIO;
extern uint8_t LCD_SCK_GPIO;
extern uint8_t LCD_DC_GPIO;

// used in lvgl_hal.cpp "void my_disp_flush(..."
extern TFT_eSPI tft;
class LGFX : public lgfx::LGFX_Device{
private:
lgfx::Panel_ILI9341 _panel_instance;
lgfx::Bus_SPI _bus_instance;
lgfx::Touch_FT5x06 _touch_instance;

public:
LGFX(void);
};
extern LGFX tft;

// only called from lvgl_hal.cpp, not from the HAL
void init_tft(void);
void get_touchpoint(int16_t *touchX, int16_t *touchY);

// called from the HAL
void update_backligthBrighness_HAL(void);
Expand Down
17 changes: 14 additions & 3 deletions Platformio/hardware/windows_linux/heapUsage_hal_windows_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,32 @@
#if defined(WIN32)
// https://www.daniweb.com/programming/software-development/threads/135188/calculate-the-amount-of-heap-memory
// returns used heap size in bytes or negative if heap is corrupted.
// It seems the while loop sometimes never ends. Reason unknown.
// If this happens, stop after 200 iterations and return the fake numer 800000, as we are doing in Linux.
// Update: and even with this fallback sometimes the function does not return. Weird. So simply return 800000.
long HeapUsed()
{
return 800000;

_HEAPINFO info = { 0, 0, 0 };
long used = 0;
int rc;

while ((rc=_heapwalk(&info)) == _HEAPOK)

int loopCounter = 0;
while (((rc=_heapwalk(&info)) == _HEAPOK) && (loopCounter < 200))
{
if (info._useflag == _USEDENTRY)
used += info._size;
loopCounter++;
}
if (rc != _HEAPEND && rc != _HEAPEMPTY)
used = (used?-used:-1);

return used;
if (loopCounter <= 200) {
return used;
} else {
return 800000;
}
}

#elif defined(__linux__) || defined(__APPLE__)
Expand Down
Loading

0 comments on commit 1b2beba

Please sign in to comment.