this is my code
#include <lvgl.h>
#include <Ticker.h>
#include <tft.h>
#include <SPI.h>
#include <SD.h>
#include <FS.h>
#include <AnalogMultiButton.h>
#define LVGL_TICK_PERIOD 20
#define TFT_CS 22
#define TFT_DC 21
#define SPI_MOSI 23
#define SPI_MISO 19
#define SPI_SCK 18
#define TP_IRQ 5
#define TP_CS 15
#define SD_CS 4
Ticker tick; /* timer for interrupt handler */
/*
touch pad
*/
TFT tft(1); /* TFT instance */
TP tp(TP_CS, TP_IRQ);
uint16_t tp_x, tp_y;
bool released = true;
#if USE_LV_LOG != 0
/* Serial debugging */
void lv_log_register_print(lv_log_level_t level, const char * file, uint32_t line, const char * dsc)
{
Serial.printf("%s@%d->%s\r\n", file, line, dsc);
delay(100);
}
#endif
/*
SD CARD
*/
lv_fs_res_t sd_open_cb(struct _lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode) {
lv_fs_res_t res = LV_FS_RES_NOT_IMP;
File f ;
char buf[256];
sprintf(buf,"/%s",path);
Serial.print("path : ");
Serial.println(buf);
if (mode == LV_FS_MODE_WR)
{
f = SD.open(buf,FILE_WRITE);
res = LV_FS_RES_OK;
}
else if (mode == LV_FS_MODE_RD)
{
f = SD.open(buf);
res = LV_FS_RES_OK;
}
else if (mode == (LV_FS_MODE_WR | LV_FS_MODE_RD))
{
f = SD.open(buf,FILE_WRITE);
res = LV_FS_RES_OK;
}
File * fp = (File *) file_p;
*fp = f; // where the probleme come
return res;
}
lv_fs_res_t sd_read_cb(struct _lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br) {
lv_fs_res_t res = LV_FS_RES_NOT_IMP;
res = LV_FS_RES_OK;
return res;
}
lv_fs_res_t sd_close_cb(struct _lv_fs_drv_t * drv, void * file_p) {
lv_fs_res_t res = LV_FS_RES_NOT_IMP;
return res;
}
/*
Display flush
*/
static lv_disp_buf_t disp_buf;
static lv_color_t buf[LV_HOR_RES_MAX * 10];
/* Display flushing */
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
static uint16_t temp[32];
uint16_t c, old_c;
int i = 0;
tft.startWrite(); /* Start new TFT transaction */
tft.setAddrWindow(area->x1, area->y1, (area->x2 - area->x1 + 1), (area->y2 - area->y1 + 1)); /* set the working window */
tft.writeCommand(0x22);
for (int y = area->y1; y <= area->y2; y++) {
for (int x = area->x1; x <= area->x2; x++) {
temp[i] = color_p->full;
i++;
if (i < 32) {
tft.writePixels(temp, i);
i = 0;
}
color_p++;
}
}
tft.endWrite(); /* terminate TFT transaction */
lv_disp_flush_ready(disp); /* tell lvgl that flushing is done */
}
lv_obj_t * scr1;
/* Interrupt driven periodic handler */
static void lv_tick_handler(void)
{
lv_tick_inc(LVGL_TICK_PERIOD);
}
/*driver for input tp*/
void tp_pressed(uint16_t x, uint16_t y) {
tp_x = x; tp_y = y;
released = false;
}
void tp_released() {
released = true;
}
/*input connection to LittleVgl*/
bool my_input_read_tp(lv_indev_drv_t * drv, lv_indev_data_t*data)
{
data->point.x = tp_x;
data->point.y = tp_y;
data->state = released ? LV_INDEV_STATE_REL : LV_INDEV_STATE_PR;
return false; /*No buffering now so no more data read*/
}
/*********************************************************************************callback*************************************************************/
void btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
if (event == LV_EVENT_CLICKED) {
printf("Clicked\n");
}
}
/************************************ Screen ****************************************************/
void scr_Menu_Start(void) {
lv_obj_clean(scr1);
lv_obj_t * btn_photo = lv_btn_create(scr1, NULL); /*Add a button the current screen*/
lv_obj_set_pos(btn_photo, 10, 10); /*Set its position*/
lv_obj_set_size(btn_photo, 300, 60); /*Set its size*/
lv_obj_set_event_cb(btn_photo, btn_event_cb); /*Assign a callback to the button*/
lv_obj_t * labelb_photo = lv_label_create(btn_photo, NULL); /*Add a label to the button*/
lv_label_set_text(labelb_photo, "Photo"); /*Set the labels text*/
lv_obj_t * btn_Maint = lv_btn_create(scr1, NULL); /*Add a button the current screen*/
lv_obj_set_pos(btn_Maint, 10, 90); /*Set its position*/
lv_obj_set_size(btn_Maint, 300, 60); /*Set its size*/
lv_obj_set_event_cb(btn_Maint, btn_event_cb); /*Assign a callback to the button*/
lv_obj_t * labelb_Maint = lv_label_create(btn_Maint, NULL); /*Add a label to the button*/
lv_label_set_text(labelb_Maint, "Maintenance");
lv_obj_t * btn_Aide = lv_btn_create(scr1, NULL); /*Add a button the current screen*/
lv_obj_set_pos(btn_Aide, 10, 170); /*Set its position*/
lv_obj_set_size(btn_Aide, 300, 60); /*Set its size*/
lv_obj_set_event_cb(btn_Aide, btn_event_cb); /*Assign a callback to the button*/
lv_obj_t * labelb_Aide = lv_label_create(btn_Aide, NULL); /*Add a label to the button*/
lv_label_set_text(labelb_Aide, "Aide");
}
void setup() {
Serial.begin(115200); /* prepare for possible serial debug */
lv_init();
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
tft.begin(); /* TFT init */
tft.begin(TFT_CS, TFT_DC, SPI_MOSI, SPI_MISO, SPI_SCK);
tft.setRotation(1); /* Landscape orientation */
tp.setRotation(1);
/*SD card init*/
pinMode(SD_CS, OUTPUT);
Serial.print(F("Init SD card... "));
if (!SD.begin(SD_CS)) {
Serial.println(F("FAIL"));
for (;;); // RESET stuff
}
/* create the drive */
lv_fs_drv_t sd_drv;
lv_fs_drv_init(&sd_drv);
sd_drv.file_size = sizeof(File);
sd_drv.letter = 'S';
sd_drv.open_cb = sd_open_cb;
sd_drv.close_cb = sd_close_cb;
sd_drv.read_cb = sd_read_cb;
lv_fs_drv_register(&sd_drv);
/*test the first function sd_open_cb*/
lv_fs_file_t f;
lv_fs_res_t res;
res = lv_fs_open(&f, "S:foo.txt", LV_FS_MODE_RD); // system crash here
if(res != LV_FS_RES_OK) Serial.print("don't work nothing open");
File * ftest = (File *) f.file_d;
Serial.print("name outside sd_open_cb : ");
Serial.println(ftest->name());
/**********************************************************/
lv_theme_t * th = lv_theme_night_init(210, NULL); //Set a HUE value and a Font for the Night Theme
lv_theme_set_current(th); //Apply the theme
lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10);
/*Initialize the display*/
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = 320;
disp_drv.ver_res = 240;
disp_drv.flush_cb = my_disp_flush;
disp_drv.buffer = &disp_buf;
lv_disp_drv_register(&disp_drv);
/*Initialize the touch pad*/
lv_indev_drv_t indev_drv_tp;
lv_indev_drv_init(&indev_drv_tp);
indev_drv_tp.type = LV_INDEV_TYPE_POINTER;
indev_drv_tp.read_cb = my_input_read_tp;
lv_indev_drv_register(&indev_drv_tp);
/*Initialize the graphics library's tick*/
tick.attach_ms(LVGL_TICK_PERIOD, lv_tick_handler);
scr1 = lv_obj_create(NULL, NULL);
lv_scr_load(scr1);
scr_Menu_Start();
}
void loop() {
tp.loop();
lv_task_handler(); /* let the GUI do its work */
delay(5);
}