Hi glory-man
Hi here is the latest version of my codeā¦
// DHT prep
#include "DHTesp.h"
#include <Ticker.h>
#include <lvgl.h>
#include <TFT_eSPI.h>
#include "CST820.h"
#include <demos/lv_demos.h>
/*ę“ę¹å±å¹åč¾Øē*/
static const uint16_t screenWidth = 240;
static const uint16_t screenHeight = 320;
/*å®ä¹č§¦ęøå±å¼č*/
#define I2C_SDA 33
#define I2C_SCL 32
#define TP_RST 25
#define TP_INT 21
#define LED_BLUE 17 // GPIO17 - PIN28 LED LOW is lighting is on LED HIGH is lighting off
#define LED_RED 4 //GPIO04 - PIN26
#define LED_GREEN 16 //GPIO16 - PIN27
#define TFT_Backlight 27 // GPIO27 - PIN 12 - this is used as Backlight Control Pin TFTe_SPI User_Setup.h !!!
static lv_disp_draw_buf_t draw_buf;
static lv_color_t *buf1;
static lv_color_t *buf2;
void tempTask(void *pvParameters);
bool getTemperature();
void triggerGetTemp();
DHTesp dht;
TFT_eSPI tft = TFT_eSPI(); /* TFTå®ä¾ */
CST820 touch(I2C_SDA, I2C_SCL, TP_RST, TP_INT); /* 触ęøå®ä¾ */
/** Task handle for the light value read task */
TaskHandle_t tempTaskHandle = NULL;
/** Ticker for temperature reading */
Ticker tempTicker;
/** Comfort profile */
ComfortState cf;
/** Flag if task should run */
bool tasksEnabled = false;
/** Pin number for DHT11 data pin */
//int dhtPin = 4;
int dhtPin = 22; // Sunton CYD P3 Yellow wire
/**
* initTemp
* Setup DHT library
* Setup task and timer for repeated measurement
* @return bool
* true if task and timer are started
* false if task or timer couldn't be started
*/
bool initTemp() {
byte resultValue = 0;
// Initialize temperature sensor
dht.setup(dhtPin, DHTesp::DHT11);
Serial.println("DHT initiated");
// Start task to get temperature
xTaskCreatePinnedToCore(
tempTask, /* Function to implement the task */
"tempTask ", /* Name of the task */
4000, /* Stack size in words */
NULL, /* Task input parameter */
5, /* Priority of the task */
&tempTaskHandle, /* Task handle. */
1); /* Core where the task should run */
if (tempTaskHandle == NULL) {
Serial.println("Failed to start task for temperature update");
return false;
} else {
// Start update of environment data every 2 seconds
tempTicker.attach(2, triggerGetTemp);
}
return true;
}
/**
* triggerGetTemp
* Sets flag dhtUpdated to true for handling in loop()
* called by Ticker getTempTimer
*/
void triggerGetTemp() {
if (tempTaskHandle != NULL) {
xTaskResumeFromISR(tempTaskHandle);
}
}
/**
* Task to reads temperature from DHT11 sensor
* @param pvParameters
* pointer to task parameters
*/
void tempTask(void *pvParameters) {
Serial.println("tempTask loop started");
while (1) // tempTask loop
{
if (tasksEnabled) {
// Get temperature values
getTemperature();
digitalWrite(LED_BLUE, HIGH);
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_BLUE, HIGH);
}
// Got sleep again
vTaskSuspend(NULL);
}
}
/**
* getTemperature
* Reads temperature from DHT11 sensor
* @return bool
* true if temperature could be aquired
* false if aquisition failed
*/
bool getTemperature() {
// Reading temperature for humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
TempAndHumidity newValues = dht.getTempAndHumidity();
// Check if any reads failed and exit early (to try again).
if (dht.getStatus() != 0) {
Serial.println("DHT11 error status: " + String(dht.getStatusString()));
return false;
}
float heatIndex = dht.computeHeatIndex(newValues.temperature, newValues.humidity);
float dewPoint = dht.computeDewPoint(newValues.temperature, newValues.humidity);
float cr = dht.getComfortRatio(cf, newValues.temperature, newValues.humidity);
String comfortStatus;
switch(cf) {
case Comfort_OK:
comfortStatus = "Comfort_OK";
break;
case Comfort_TooHot:
comfortStatus = "Comfort_TooHot";
break;
case Comfort_TooCold:
comfortStatus = "Comfort_TooCold";
break;
case Comfort_TooDry:
comfortStatus = "Comfort_TooDry";
break;
case Comfort_TooHumid:
comfortStatus = "Comfort_TooHumid";
break;
case Comfort_HotAndHumid:
comfortStatus = "Comfort_HotAndHumid";
break;
case Comfort_HotAndDry:
comfortStatus = "Comfort_HotAndDry";
break;
case Comfort_ColdAndHumid:
comfortStatus = "Comfort_ColdAndHumid";
break;
case Comfort_ColdAndDry:
comfortStatus = "Comfort_ColdAndDry";
break;
default:
comfortStatus = "Unknown:";
break;
};
Serial.println(" T:" + String(newValues.temperature) + " H:" + String(newValues.humidity) + " I:" + String(heatIndex) + " D:" + String(dewPoint) + " " + comfortStatus);
return true;
lv_label_set_text(label1,String(newValues.temperature));
}
#if LV_USE_LOG != 0
/* äø²č”č°čÆ */
void my_print(const char *buf)
{
Serial.printf(buf);
Serial.flush();
}
#endif
//_
static void btnBLUE_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * btn = lv_event_get_target(e);
if(code == LV_EVENT_CLICKED) {
//static uint8_t cnt = 0;
digitalWrite(LED_BLUE, !digitalRead(LED_BLUE));
/*Get the first child of the button which is the label and change its text*/
lv_obj_t * label = lv_obj_get_child(btn, 0);
lv_label_set_text_fmt(label, "BLUE");
}
}
static void btnRED_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * btn = lv_event_get_target(e);
if(code == LV_EVENT_CLICKED) {
//static uint8_t cnt = 0;
digitalWrite(LED_RED, !digitalRead(LED_RED));
/*Get the first child of the button which is the label and change its text*/
lv_obj_t * label = lv_obj_get_child(btn, 0);
lv_label_set_text_fmt(label, "RED");
}
}
static void btnGREEN_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * btn = lv_event_get_target(e);
if(code == LV_EVENT_CLICKED) {
//static uint8_t cnt = 0;
digitalWrite(LED_GREEN, !digitalRead(LED_GREEN));
/*Get the first child of the button which is the label and change its text*/
lv_obj_t * label = lv_obj_get_child(btn, 0);
lv_label_set_text_fmt(label, "GREEN");
}
}
void lv_example_btn(void)
{
lv_timer_t * timer = lv_timer_create( * label_refresher_task, 1000, NULL);
// BLUE button
lv_obj_t * blue_btn = lv_btn_create(lv_scr_act()); /*Add a button the current screen*/
//lv_obj_set_pos(blue_btn, 60, 10); /*Set its position*/
lv_obj_set_size(blue_btn, 120, 50); /*Set its size*/
lv_obj_add_event_cb(blue_btn, btnBLUE_event_cb, LV_EVENT_ALL, NULL); /*Assign a callback to the button*/
lv_obj_align(blue_btn, LV_ALIGN_CENTER, 0, -100);
lv_obj_t * blue_label = lv_label_create(blue_btn); /*Add a label to the button*/
lv_label_set_text(blue_label, "BLUE"); /*Set the labels text*/
lv_obj_center(blue_label);
// RED button
lv_obj_t * red_btn = lv_btn_create(lv_scr_act()); /*Add a button the current screen*/
//lv_obj_set_pos(red_btn, 60, 110); /*Set its position*/
lv_obj_set_size(red_btn, 120, 50); /*Set its size*/
lv_obj_add_event_cb(red_btn, btnRED_event_cb, LV_EVENT_ALL, NULL); /*Assign a callback to the button*/
lv_obj_align(red_btn, LV_ALIGN_CENTER, 0, 0);
lv_obj_t * red_label = lv_label_create(red_btn); /*Add a label to the button*/
lv_label_set_text(red_label, "RED"); /*Set the labels text*/
lv_obj_center(red_label);
// GREEN button
lv_obj_t * green_btn = lv_btn_create(lv_scr_act()); /*Add a button the current screen*/
//lv_obj_set_pos(green_btn, 60, 230); /*Set its position*/
lv_obj_set_size(green_btn, 120, 50); /*Set its size*/
lv_obj_add_event_cb(green_btn, btnGREEN_event_cb, LV_EVENT_ALL, NULL);
lv_obj_align(green_btn, LV_ALIGN_CENTER, 0, +100); /*Assign a callback to the button*/
lv_obj_t * green_label = lv_label_create(green_btn); /*Add a label to the button*/
lv_label_set_text(green_label, "GREEN"); /*Set the labels text*/
lv_obj_center(green_label);
lv_obj_t * label1 = lv_label_create(lv_scr_act()); /*Add a label to the button*/
lv_label_set_text(label1, "LABEL1"); /*Set the labels text*/
lv_obj_align(label1, LV_ALIGN_CENTER, 0, +50 );
}
//_______________________
/* Monitor frissĆtĆ©s */
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);
//tft.startWrite();
tft.pushImageDMA(area->x1, area->y1, w, h, (uint16_t *)color_p);
//tft.endWrite();
lv_disp_flush_ready(disp);
}
/*Az Ć©rintÅpadot olvasĆ³ funkciĆ³*/
void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
{
bool touched;
uint8_t gesture;
uint16_t touchX, touchY;
touched = touch.getTouch(&touchX, &touchY, &gesture);
if (!touched)
{
data->state = LV_INDEV_STATE_REL;
}
else
{
data->state = LV_INDEV_STATE_PR;
/*Set the coordinates*/
data->point.x = touchX;
data->point.y = touchY;
}
}
void setup()
{
Serial.begin(115200); /*åå§åäø²å£*/
String LVGL_Arduino = "Hello Arduino! ";
LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
Serial.println(LVGL_Arduino);
Serial.println("I am LVGL_Arduino");
Serial.println();
Serial.println("DHT ESP32 example with tasks");
initTemp();
// Signal end of setup() to tasks
tasksEnabled = true;
lv_init();
#if LV_USE_LOG != 0
lv_log_register_print_cb(my_print); /* ēØäŗč°čÆēę³Øåęå°åč½ */
#endif
pinMode(LED_BLUE, OUTPUT);//TTH
pinMode(LED_RED, OUTPUT);//TTH
pinMode(LED_GREEN, OUTPUT);//TTH
digitalWrite(LED_BLUE, HIGH);
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
delay(500);
digitalWrite(LED_BLUE, LOW);
delay(500);
digitalWrite(LED_BLUE, HIGH);
delay(500);
digitalWrite(LED_RED, LOW);
delay(500);
digitalWrite(LED_RED, HIGH);
delay(500);
digitalWrite(LED_GREEN, LOW);
delay(500);
digitalWrite(LED_GREEN, HIGH);
pinMode(TFT_Backlight, OUTPUT);
digitalWrite(TFT_Backlight, LOW);
tft.begin(); /*åå§å*/
tft.setRotation(0); /* ęč½¬ */
tft.initDMA(); /* åå§åDMA */
touch.begin(); /*åå§å触ęøęæ*/
//digitalWrite(LED3, HIGH);
digitalWrite(TFT_Backlight, HIGH);
tft.fillScreen(TFT_RED);
delay(500);
tft.fillScreen(TFT_GREEN);
delay(500);
tft.fillScreen(TFT_BLUE);
delay(500);
tft.fillScreen(TFT_BLACK);
delay(500);
buf1 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * screenWidth * 200 , MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);//screenWidth * screenHeight/2
buf2 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * screenWidth * 200 , MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
lv_disp_draw_buf_init(&draw_buf, buf1, buf2, screenWidth * 200);
/*KijelzÅ inicializĆ”lĆ”sa*/
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
/*KijelzÅfelbontĆ”s beĆ”llĆtĆ”sa*/
disp_drv.hor_res = screenWidth;
disp_drv.ver_res = screenHeight;
disp_drv.flush_cb = my_disp_flush;
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register(&disp_drv);
/*InicializĆ”lja a (virtuĆ”lis) beviteli eszkƶz illesztÅprogramjĆ”t*/
static lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = my_touchpad_read;
lv_indev_drv_register(&indev_drv);
lv_example_btn();
Serial.println("Setup done");
tft.startWrite();
}
void label_refresher_task(void * p)
{
//static uint32_t prev_value = 0;
//if(prev_value != adc_value) {
if(lv_obj_get_screen(label1) == lv_scr_act()) {
//char buf[32];
//snprintf(buf, 32, "Voltage: %d", adc_value);
lv_label_set_text(label1, "MMMM");
}
//prev_value = adc_value;
//}
}
void loop()
{
lv_timer_handler(); /* 让GUIå®ęå®ēå·„ä½ */
delay(5);
if (!tasksEnabled) {
// Wait 2 seconds to let system settle down
delay(2000);
// Enable task that will read values from the DHT sensor
tasksEnabled = true;
if (tempTaskHandle != NULL) {
vTaskResume(tempTaskHandle);
}
}
yield();
}
type or paste code here
This is the latest version. (gives error)
I know it is messy becuse I merged together several examplesā¦
(by the way any advise is appreciated to rearrange the code.)
Just little bit about programming tutorialā¦ I am older than 50 yearsā¦ I started with basic. Mean programming languageā¦ I keep alive with programming little java, matlab, octave, openCV, pythonā¦ nothing special, just only basicsā¦
I am doing some C course right now: (200hour) You are right I do not know some basics, because I had no chance to learn when I was into the perfect ageā¦
BUTā¦ what I have learn that 99% of the tutorials to the teachers, not to a studentā¦
ā¦ this is why I appreciate any HUMAN advice except advice from TEACHERSā¦
I have taught many people life meaning profession during my life, but my lessons is about the student, not about myselfā¦
![:slight_smile: :slight_smile:](https://forum.lvgl.io/images/emoji/twitter/slight_smile.png?v=12)
Ferenc