init rgb only once on boot if mod is not present

testing required!!!
This commit is contained in:
MX
2025-03-16 04:21:02 +03:00
parent 94b369657d
commit edd75a9b01
6 changed files with 88 additions and 165 deletions

View File

@@ -12,5 +12,6 @@ App(
"power",
"namechanger_srv",
"rgb_backlight",
"rgb_backlight_startup",
],
)

View File

@@ -1,10 +0,0 @@
App(
appid="region",
name="RegionSrv",
apptype=FlipperAppType.STARTUP,
targets=["f7"],
entry_point="region_on_system_start",
cdefines=["SRV_REGION"],
requires=["storage"],
order=170,
)

View File

@@ -1,140 +0,0 @@
#include <furi_hal_region.h>
#include <furi.h>
#include <storage/storage.h>
#include <flipper.pb.h>
#include <pb_decode.h>
#define TAG "RegionSrv"
#define SUBGHZ_REGION_FILENAME INT_PATH(".region_data")
static bool region_istream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
File* file = istream->state;
size_t ret = storage_file_read(file, buf, count);
return count == ret;
}
static bool region_istream_decode_band(pb_istream_t* stream, const pb_field_t* field, void** arg) {
UNUSED(field);
FuriHalRegion* region = *arg;
PB_Region_Band band = {0};
if(!pb_decode(stream, PB_Region_Band_fields, &band)) {
FURI_LOG_E(TAG, "PB Region band decode error: %s", PB_GET_ERROR(stream));
return false;
}
region->bands_count += 1;
region = realloc( //-V701
region,
sizeof(FuriHalRegion) + sizeof(FuriHalRegionBand) * region->bands_count);
size_t pos = region->bands_count - 1;
region->bands[pos].start = band.start;
region->bands[pos].end = band.end;
region->bands[pos].power_limit = band.power_limit;
region->bands[pos].duty_cycle = band.duty_cycle;
*arg = region;
FURI_LOG_I(
TAG,
"Add allowed band: start %luHz, stop %luHz, power_limit %ddBm, duty_cycle %u%%",
band.start,
band.end,
band.power_limit,
band.duty_cycle);
return true;
}
static int32_t region_load_file(void* context) {
UNUSED(context);
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
PB_Region pb_region = {0};
pb_region.bands.funcs.decode = region_istream_decode_band;
do {
FileInfo fileinfo = {0};
if(storage_common_stat(storage, SUBGHZ_REGION_FILENAME, &fileinfo) != FSE_OK ||
fileinfo.size == 0) {
FURI_LOG_W(TAG, "Region file missing or empty");
break;
} else if(!storage_file_open(file, SUBGHZ_REGION_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING)) {
FURI_LOG_E(TAG, "Failed to open region file");
break;
}
pb_istream_t istream = {
.callback = region_istream_read,
.state = file,
.errmsg = NULL,
.bytes_left = fileinfo.size,
};
pb_region.bands.arg = malloc(sizeof(FuriHalRegion));
if(!pb_decode(&istream, PB_Region_fields, &pb_region)) {
FURI_LOG_E(TAG, "Failed to decode region file");
free(pb_region.bands.arg);
break;
}
FuriHalRegion* region = pb_region.bands.arg;
memcpy(
region->country_code,
pb_region.country_code->bytes,
MIN(pb_region.country_code->size, sizeof(region->country_code) - 1));
furi_hal_region_set(region);
FURI_LOG_I(TAG, "Dynamic region set: %s", region->country_code);
} while(0);
pb_release(PB_Region_fields, &pb_region);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return 0;
}
static void
region_loader_release_callback(FuriThread* thread, FuriThreadState state, void* context) {
UNUSED(context);
if(state == FuriThreadStateStopped) {
furi_thread_free(thread);
}
}
static void region_storage_callback(const void* message, void* context) {
UNUSED(context);
const StorageEvent* event = message;
if(event->type == StorageEventTypeCardMount) {
FuriThread* loader = furi_thread_alloc_ex(NULL, 2048, region_load_file, NULL);
furi_thread_set_state_callback(loader, region_loader_release_callback);
furi_thread_start(loader);
}
}
int32_t region_on_system_start(void* p) {
UNUSED(p);
Storage* storage = furi_record_open(RECORD_STORAGE);
furi_pubsub_subscribe(storage_get_pubsub(storage), region_storage_callback, NULL);
if(storage_sd_status(storage) != FSE_OK) {
FURI_LOG_D(TAG, "SD Card not ready, skipping dynamic region");
return 0;
}
region_load_file(NULL);
return 0;
}

View File

@@ -7,4 +7,14 @@ App(
stack_size=1 * 1024,
order=95,
sdk_headers=["rgb_backlight.h"],
)
)
App(
appid="rgb_backlight_startup",
name="RgbBackLightBootSrv",
apptype=FlipperAppType.STARTUP,
targets=["f7"],
entry_point="rgb_backlight_on_system_start",
cdefines=["SRV_RGB_BACKLIGHT"],
order=270,
)

View File

@@ -82,14 +82,15 @@ void rgb_backlight_set_custom_color(uint8_t red, uint8_t green, uint8_t blue) {
// use RECORD for acces to rgb service instance, use current_* colors and update backlight
void rgb_backlight_update(float brightness) {
RGBBacklightApp* app = furi_record_open(RECORD_RGB_BACKLIGHT);
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
uint8_t r = app->current_red * (brightness / 1.0f);
uint8_t g = app->current_green * (brightness / 1.0f);
uint8_t b = app->current_blue * (brightness / 1.0f);
SK6805_set_led_color(i, r, g, b);
if(app->settings->rgb_mod_installed) {
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
uint8_t r = app->current_red * (brightness / 1.0f);
uint8_t g = app->current_green * (brightness / 1.0f);
uint8_t b = app->current_blue * (brightness / 1.0f);
SK6805_set_led_color(i, r, g, b);
}
SK6805_update();
}
SK6805_update();
furi_record_close(RECORD_RGB_BACKLIGHT);
}
@@ -171,8 +172,8 @@ static void rainbow_timer_callback(void* context) {
default:
break;
}
rgb_backlight_update(app->settings->brightness);
}
rgb_backlight_update(app->settings->brightness);
// if rainbow_mode is ..... do another effect
// if(app->settings.rainbow_mode == ...) {
@@ -185,7 +186,6 @@ int32_t rgb_backlight_srv(void* p) {
// Define object app (full app with settings and running variables),
// allocate memory and create RECORD for access to app structure from outside
RGBBacklightApp* app = malloc(sizeof(RGBBacklightApp));
furi_record_create(RECORD_RGB_BACKLIGHT, app);
//define rainbow_timer and they callback
app->rainbow_timer = furi_timer_alloc(rainbow_timer_callback, FuriTimerTypePeriodic, app);
@@ -197,6 +197,8 @@ int32_t rgb_backlight_srv(void* p) {
// Init app variables
app->rainbow_stage = 1;
furi_record_create(RECORD_RGB_BACKLIGHT, app);
// if rgb mod installed - start rainbow or set static color from settings (default index = 0)
if(app->settings->rgb_mod_installed) {
if(app->settings->rainbow_mode > 0) {
@@ -206,15 +208,19 @@ int32_t rgb_backlight_srv(void* p) {
rgb_backlight_update(app->settings->brightness);
}
// if rgb mod not installed - set default static orange color (index=0)
} else {
rgb_backlight_set_static_color(0);
rgb_backlight_update(app->settings->brightness);
}
} //else {
// rgb_backlight_set_static_color(0);
// rgb_backlight_update(app->settings->brightness);
//}
while(1) {
// place for message queue and other future options
furi_delay_ms(5000);
FURI_LOG_I(TAG, "Service is running");
if(app->settings->rgb_mod_installed) {
FURI_LOG_D(TAG, "Mod is enabled - serivce is running");
} else {
FURI_LOG_D(TAG, "Mod is DISABLED - serivce is running");
}
}
return 0;
}

View File

@@ -0,0 +1,56 @@
#include <furi.h>
#include <storage/storage.h>
#include "applications/services/rgb_backlight/rgb_backlight.h"
static int32_t boot_rgb_backlight_update(void* context) {
UNUSED(context);
RGBBacklightApp* app = furi_record_open(RECORD_RGB_BACKLIGHT);
if(!app->settings->rgb_mod_installed) {
rgb_backlight_set_static_color(0);
for(uint8_t i = 0; i < SK6805_get_led_count(); i++) {
uint8_t r = app->current_red * (1.0f / 1.0f);
uint8_t g = app->current_green * (1.0f / 1.0f);
uint8_t b = app->current_blue * (1.0f / 1.0f);
SK6805_set_led_color(i, r, g, b);
}
SK6805_update();
}
furi_record_close(RECORD_RGB_BACKLIGHT);
return 0;
}
static void
rgb_boot_loader_release_callback(FuriThread* thread, FuriThreadState state, void* context) {
UNUSED(context);
if(state == FuriThreadStateStopped) {
furi_thread_free(thread);
}
}
static void rgb_boot_storage_callback(const void* message, void* context) {
UNUSED(context);
const StorageEvent* event = message;
if(event->type == StorageEventTypeCardMount) {
FuriThread* loader = furi_thread_alloc_ex(NULL, 2048, boot_rgb_backlight_update, NULL);
furi_thread_set_state_callback(loader, rgb_boot_loader_release_callback);
furi_thread_start(loader);
}
}
int32_t rgb_backlight_on_system_start(void* p) {
UNUSED(p);
Storage* storage = furi_record_open(RECORD_STORAGE);
furi_pubsub_subscribe(storage_get_pubsub(storage), rgb_boot_storage_callback, NULL);
if(storage_sd_status(storage) != FSE_OK) {
FURI_LOG_D("RGB_Boot_Init", "SD Card not ready, skipping rgb backlight init");
return 0;
}
boot_rgb_backlight_update(NULL);
return 0;
}