Skip to content

Refactoring #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions webscreen/dynamic_js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@

#include "pins_config.h"
#include "rm67162.h"
#include "lvgl_elk.h" // Contains init_lvgl_display(), init_lv_fs(), etc.

#include "lvgl_elk.h" // Contains init_lvgl_display(), init_lv_fs(), etc.
#include "globals.h"

// Initialize with the default script filename
String g_script_filename = "/app.js";

void dynamic_js_setup() {
void dynamic_js_setup()
{
LOG("DYNAMIC_JS: Setting up Elk + script scenario...");

// If needed, mount the SD again (or confirm it’s already mounted):
SD_MMC.setPins(PIN_SD_CLK, PIN_SD_CMD, PIN_SD_D0);
if(!SD_MMC.begin("/sdcard", true, false, 1000000)) {
if (!SD_MMC.begin("/sdcard", true, false, 1000000))
{
LOG("Card Mount Failed => can't run dynamic JS code properly");
return;
}
Expand All @@ -38,19 +41,19 @@ void dynamic_js_setup() {

// 6) Spawn Elk task
xTaskCreatePinnedToCore(
elk_task,
"ElkTask",
16384,
NULL,
1,
NULL,
0
);
elk_task,
"ElkTask",
16384,
NULL,
1,
NULL,
0);

LOG("DYNAMIC_JS: setup done!");
}

void dynamic_js_loop() {
void dynamic_js_loop()
{
// The Elk code runs in the FreeRTOS task (elk_task).
vTaskDelay(pdMS_TO_TICKS(50));
}
2 changes: 1 addition & 1 deletion webscreen/dynamic_js.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

// Functions to set up and loop your “dynamic” (Elk + script) code
void dynamic_js_setup();
void dynamic_js_loop();
void dynamic_js_loop();
194 changes: 103 additions & 91 deletions webscreen/fallback.cpp
Original file line number Diff line number Diff line change
@@ -1,90 +1,107 @@
#include <Arduino.h>
#include "fallback.h"
#include "pins_config.h"
#include "rm67162.h" // LCD driver
#include <lvgl.h> // Ensure you have LVGL
#include "notification.h" // For the GIF data
#include "globals.h"
#include "tick.h"

// We'll store references to the fallback label + gif
static lv_obj_t* fb_label = nullptr;
static lv_obj_t* fb_gif = nullptr;

// A local display buffer/driver just for fallback, if you want separate from dynamic
static lv_disp_draw_buf_t fbDrawBuf;
static lv_color_t* fbBuf = nullptr;

// The flush callback
static void fallback_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);
lcd_PushColors(area->x1, area->y1, w, h, (uint16_t *)&color_p->full);
lv_disp_flush_ready(disp);
}
// fallback.cpp

#include "fallback.h" // Own header first

#include <Arduino.h> // For Serial, String? (check usage)
#include <lvgl.h> // Uses LVGL objects, styles, animations directly

#include "pins_config.h" // Needs pin definitions
#include "rm67162.h" // Needs lcd_PushColors etc.
#include "notification.h"// Needs the image data `notification`
#include "globals.h" // Needs LOG, EXAMPLE_LCD_*, LVGL_LCD_BUF_SIZE, ps_malloc?
#include "tick.h" // Needs start_lvgl_tick()

namespace
{
// how long the label scrolls (in ms)
static constexpr uint32_t SCROLL_DURATION = 10000;

// LVGL objects & buffers
static lv_obj_t *fb_label = nullptr;
static lv_obj_t *fb_gif = nullptr;
static lv_disp_draw_buf_t fb_draw_buf;
static lv_color_t *fb_buf = nullptr;

// our fallback display flush callback
static void 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;
// We know color_p->full is a uint16_t, so take its address as uint16_t*
lcd_PushColors(area->x1,
area->y1,
w,
h,
reinterpret_cast<uint16_t *>(&color_p->full));
lv_disp_flush_ready(disp);
}

// Animation callback
static void scroll_anim_cb(void *var, int32_t v) {
lv_obj_set_y((lv_obj_t *)var, v);
}
// animation callback: move the label vertically
static void scroll_anim_cb(void *var, int32_t v)
{
lv_obj_set_y(static_cast<lv_obj_t *>(var), v);
}

// Helper to create the scrolling animation
static void create_scroll_animation(lv_obj_t *obj, int32_t start, int32_t end, uint32_t duration) {
lv_anim_t a;
lv_anim_init(&a);
lv_anim_set_var(&a, obj);
lv_anim_set_values(&a, start, end);
lv_anim_set_time(&a, duration);
lv_anim_set_exec_cb(&a, scroll_anim_cb);
lv_anim_set_path_cb(&a, lv_anim_path_ease_in_out); // Smooth
lv_anim_set_repeat_count(&a, 2); // Repeat twice

// On animation finish, hide label, show GIF
lv_anim_set_ready_cb(&a, [](lv_anim_t *anim) {
lv_obj_t *obj = (lv_obj_t *)anim->var;
lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(fb_gif, LV_OBJ_FLAG_HIDDEN);
});

lv_anim_start(&a);
// kick off (or re-kick) the scrolling
static void start_scroll_anim(lv_obj_t *obj)
{
lv_anim_t a;
lv_anim_init(&a);
lv_anim_set_var(&a, obj);
// from just off the bottom to just above the top
lv_anim_set_values(&a,
static_cast<int32_t>(EXAMPLE_LCD_V_RES),
-lv_obj_get_height(obj));
lv_anim_set_time(&a, SCROLL_DURATION);
lv_anim_set_exec_cb(&a, scroll_anim_cb);
lv_anim_set_path_cb(&a, lv_anim_path_ease_in_out);
lv_anim_set_repeat_count(&a, 2);
// when done, hide the label & show the GIF
lv_anim_set_ready_cb(&a, [](lv_anim_t *anim)
{
auto* lbl = static_cast<lv_obj_t*>(anim->var);
lv_obj_add_flag(lbl, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(fb_gif, LV_OBJ_FLAG_HIDDEN); });
lv_anim_start(&a);
}
}

void fallback_setup() {
LOG("FALLBACK: Setting up scrolling label + GIF...");
void fallback_setup()
{
LOG("FALLBACK: setting up");

// 1) Minimal LVGL init (only if not already done).
// If your main code calls lv_init() elsewhere,
// you might skip or check if it’s safe to call again.
// 1) LVGL init + tick
lv_init();
start_lvgl_tick();

// 2) Power on the screen, set backlight
// 2) power & display HW
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, HIGH);

// 3) Init your display driver
rm67162_init();
lcd_setRotation(1);

// 4) Allocate a buffer for fallback usage
fbBuf = (lv_color_t*) ps_malloc(sizeof(lv_color_t) * LVGL_LCD_BUF_SIZE);
if(!fbBuf) {
LOG("FALLBACK: Failed to allocate buffer");
// 3) draw‐buffer
fb_buf = static_cast<lv_color_t *>(
ps_malloc(sizeof(lv_color_t) * LVGL_LCD_BUF_SIZE));
if (!fb_buf)
{
LOG("FALLBACK: buffer alloc failed");
return;
}
lv_disp_draw_buf_init(&fb_draw_buf, fb_buf, nullptr, LVGL_LCD_BUF_SIZE);

lv_disp_draw_buf_init(&fbDrawBuf, fbBuf, nullptr, LVGL_LCD_BUF_SIZE);

// 5) Register fallback display driver
// 4) register a dedicated display driver
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = 536;
disp_drv.ver_res = 240;
disp_drv.flush_cb = fallback_disp_flush;
disp_drv.draw_buf = &fbDrawBuf;
disp_drv.hor_res = EXAMPLE_LCD_H_RES;
disp_drv.ver_res = EXAMPLE_LCD_V_RES;
disp_drv.draw_buf = &fb_draw_buf;
disp_drv.flush_cb = disp_flush;
lv_disp_drv_register(&disp_drv);

// 6) Create a style for the label
// 5) style for the label
static lv_style_t style;
lv_style_init(&style);
lv_style_set_text_font(&style, &lv_font_montserrat_40);
Expand All @@ -93,47 +110,42 @@ void fallback_setup() {
lv_style_set_pad_all(&style, 5);
lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);

// 7) Create the label
// 6) create & configure the label
fb_label = lv_label_create(lv_scr_act());
lv_obj_add_style(fb_label, &style, 0);
lv_label_set_text(fb_label,
"/\\_/\\\n"
"= ( • . • ) =\n"
" / \\ \n"
"Welcome to Webscreen! This is the Notification App, you can also run apps from the SD card.\n"
" \n"
" \n"
);
lv_label_set_text(fb_label,
"/\\_/\\\n"
"= ( • . • ) =\n"
" / \\ \n"
"Welcome to Webscreen! This is the Notification App,\n"
"you can also run apps from the SD card.\n");
lv_label_set_long_mode(fb_label, LV_LABEL_LONG_WRAP);
lv_obj_set_width(fb_label, 525);
lv_obj_set_width(fb_label, EXAMPLE_LCD_H_RES - 20);
lv_obj_align(fb_label, LV_ALIGN_CENTER, 0, 0);

// 8) Create the scroll animation
create_scroll_animation(fb_label, 240, -lv_obj_get_height(fb_label), 10000);
// 7) start scrolling
start_scroll_anim(fb_label);

// 9) Create the GIF
// 8) create the GIF (hidden until scroll finishes)
fb_gif = lv_gif_create(lv_scr_act());
lv_gif_set_src(fb_gif, &notification); // from notification.h
lv_gif_set_src(fb_gif, &notification);
lv_obj_align(fb_gif, LV_ALIGN_CENTER, 0, 0);

// Show label first, hide GIF
lv_obj_clear_flag(fb_label, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(fb_gif, LV_OBJ_FLAG_HIDDEN);
}

void fallback_loop() {
// Let LVGL run
void fallback_loop()
{
// let LVGL run
lv_timer_handler();

// If serial data arrives, treat that as an input to update label
if (Serial.available()) {
// if there’s serial input, replace the label & re-scroll
if (Serial.available())
{
String line = Serial.readStringUntil('\n');
lv_label_set_text(fb_label, line.c_str());
lv_obj_align(fb_label, LV_ALIGN_CENTER, 0, 0);
lv_obj_clear_flag(fb_label, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(fb_gif, LV_OBJ_FLAG_HIDDEN);

// re-run animation
create_scroll_animation(fb_label, 240, -lv_obj_get_height(fb_label), 10000);
start_scroll_anim(fb_label);
}
}
4 changes: 2 additions & 2 deletions webscreen/fallback.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once

// Public API
// Public API for fallback mode
void fallback_setup();
void fallback_loop();
void fallback_loop();
7 changes: 4 additions & 3 deletions webscreen/globals.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <Arduino.h>
#include "log.h"
#include <Arduino.h> // Needed for String type
#include "log.h" // Provides LOG macros used elsewhere

// Declare the global script filename variable
extern String g_script_filename;
extern String g_script_filename;
// Add other necessary global declarations here
Loading