Skip to content
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

Backlight flicker fix #14

Open
wants to merge 7 commits into
base: master
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
4 changes: 2 additions & 2 deletions main/src/Devices/Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class Battery : private SleepyThreaded {
private:
static constexpr uint32_t MeasureIntverval = 100;

static constexpr float VoltEmpty = 2900;
static constexpr float VoltEmpty = 3000;
static constexpr float VoltFull = 3200;
static constexpr float Factor = 4.0f;
static constexpr float Offset = 100;
static constexpr float EmaA = 0.05f;
static constexpr float EmaA = 0.01f;

static constexpr int CalReads = 10;
static constexpr float CalExpected = 2600;
Expand Down
42 changes: 42 additions & 0 deletions main/src/FS/ArchiveCache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "ArchiveCache.h"
#include "SPIFFS.h"
#include <esp_log.h>
#include <cstring>

ArchiveCache::ArchiveCache(const std::vector<std::string>& paths) : paths(paths){
archives.reserve(paths.size());
}

void ArchiveCache::load(){
if(loaded) return;
loaded = true;

for(const auto& path : paths){
const auto filename = path + ".sz";

auto file = SPIFFS::open(filename.c_str());
if(!file){
ESP_LOGW("ArchiveCache", "Can't open archive %s", path.c_str());
continue;
}

archives.emplace(path, FileArchive(file));
}
}

void ArchiveCache::unload(){
if(!loaded) return;
loaded = false;

archives.clear();
}

File ArchiveCache::open(const char* path){
const char* slash = strchr(path+1, '/');
if(slash == nullptr) return { };

const auto archive = archives.find(std::string(path, slash));
if(archive == archives.end()) return { };

return archive->second.get(slash+1, path);
}
25 changes: 25 additions & 0 deletions main/src/FS/ArchiveCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef BIT_FIRMWARE_ARCHIVECACHE_H
#define BIT_FIRMWARE_ARCHIVECACHE_H

#include "FileCache.h"
#include "FileArchive.h"

class ArchiveCache : public FileCache {
public:
explicit ArchiveCache(const std::vector<std::string>& paths);

void load() override;
void unload() override;

File open(const char* path) override;

private:
const std::vector<std::string> paths;

bool loaded = false;
std::unordered_map<std::string, FileArchive> archives;

};


#endif //BIT_FIRMWARE_ARCHIVECACHE_H
86 changes: 86 additions & 0 deletions main/src/FS/FileArchive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <esp_log.h>
#include "FileArchive.h"
#include "FS/RamFile.h"

static const char* TAG = "FileArchive";

FileArchive::FileArchive(File file, const std::unordered_set<std::string>& excluded){
file.seek(0);
uint32_t count = 0;
file.read((uint8_t*) &count, 4);

std::vector<Entry> tmp;
tmp.reserve(count);

size_t totalSize = 0;

while(file.available()){
std::string name;
name.reserve(32);
while(file.available()){
if(name.length() >= 32){
ESP_LOGE("FileArchive", "Too big filename; %s...", name.c_str());
abort();
}

char c = 0;
file.read((uint8_t*) &c, 1);
if(c == 0) break;

name.append(1, c);
}

if(name.empty()) break;

uint32_t size = 0;
file.read((uint8_t*) &size, 4);

if(!excluded.contains(name)){
totalSize += size;
}

tmp.emplace_back(Entry { name, size, 0 });
}

data = (uint8_t*) malloc(totalSize);
externalData = false;

if(data == nullptr){
ESP_LOGW(TAG, "Failed allocating data buffer of %zu B", totalSize);
return;
}

size_t offset = 0;

for(auto& entry : tmp){
if(excluded.contains(entry.name)){
file.seek(entry.size, SeekMode::SeekCur);
continue;
}

file.read(data + offset, entry.size);
entry.offset = offset;

offset += entry.size;

entries.insert(std::make_pair(entry.name, std::move(entry)));
}
}

FileArchive::~FileArchive(){
if(!externalData){
free(data);
}
}

File FileArchive::get(const char* file, const char* name){
auto it = entries.find(file);
if(it == entries.end()) return File();

if(!name){
name = file;
}

auto f = std::make_shared<RamFile>(data + it->second.offset, it->second.size, name);
return File(f);
}
32 changes: 32 additions & 0 deletions main/src/FS/FileArchive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef BIT_FIRMWARE_FILEARCHIVE_H
#define BIT_FIRMWARE_FILEARCHIVE_H

#include <vector>
#include <unordered_map>
#include <string>
#include "FS/File.h"
#include <unordered_set>

class FileArchive {
public:
FileArchive(File file, const std::unordered_set<std::string>& excluded = {});
virtual ~FileArchive();

File get(const char* file, const char* name = nullptr);

private:
struct Entry {
const std::string name;
size_t size;
size_t offset;
};

std::unordered_map<std::string, const Entry> entries;

bool externalData = false;
uint8_t* data = nullptr;

};


#endif //BIT_FIRMWARE_FILEARCHIVE_H
67 changes: 15 additions & 52 deletions main/src/LV_Interface/FSLVGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,6 @@
#include <unordered_map>
#include <cstring>

std::initializer_list<std::string> GeneralCache = {
"/Stats/Bar.bin",
"/Stats/BarLong.bin",
"/Stats/Batt.bin",
"/Stats/Happ.bin",
"/Stats/Oil.bin",
"/Stats/Xp.bin",

"/OS/1_0.bin",
"/OS/10.bin",
"/OS/9000.bin",
"/OS/9001.bin",
"/OS/One.bin",
"/OS/X.bin",

"/Menu/Frame.bin",
"/Menu/Game1.bin",
"/Menu/Game1L.bin",
"/Menu/Game2.bin",
"/Menu/Game2L.bin",
"/Menu/Game3.bin",
"/Menu/Game3L.bin",
"/Menu/Game4.bin",
"/Menu/Game4L.bin",
"/Menu/Game5.bin",
"/Menu/Game5L.bin",
"/Menu/Game6.bin",
"/Menu/Game6L.bin",
"/Menu/Settings.bin"
};

const char* TAG = "FSLVGL";

FSLVGL* FSLVGL::instance = nullptr;
Expand Down Expand Up @@ -70,29 +39,12 @@ FSLVGL::~FSLVGL(){
}

void FSLVGL::loadCache(){
if(cacheLoaded) return;
cacheLoaded = true;
if(archive) return;

cache.setPaths(getCacheFiles());
cache.load();
archive = new FileArchive(SPIFFS::open("/main.sz"));
}

void FSLVGL::unloadCache(){
if(!cacheLoaded) return;
cacheLoaded = false;

cache.unload();
}

std::vector<std::string> FSLVGL::getCacheFiles() const{
std::vector<std::string> paths;
paths.reserve(GeneralCache.size());

for(const auto& file : GeneralCache){
paths.emplace_back(file);
}

return paths;
}

void* FSLVGL::lvOpen(const char* path, lv_fs_mode_t mode){
Expand All @@ -105,8 +57,19 @@ void* FSLVGL::lvOpen(const char* path, lv_fs_mode_t mode){
return filePtr;
};

file = cache.open(path);
if(file) return mkPtr(file);
std::string stringPath = path;
const char* ArchivePrefixes[] = {"/Anim", "/Bg", "/GameSplash", "/LevelUp", "/Menu", "/OS", "/Pingo", "/Stats"};
bool archivePrefixFound = false;
for(const auto& prefix : ArchivePrefixes){
if(stringPath.starts_with(prefix)){
archivePrefixFound = true;
break;
}
}
if(archive && archivePrefixFound){
file = archive->get(path);
if(file) return mkPtr(file);
}

static const std::unordered_map<lv_fs_mode_t, const char*> Map = {
{ LV_FS_MODE_WR, "w" },
Expand Down
8 changes: 3 additions & 5 deletions main/src/LV_Interface/FSLVGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <unordered_map>
#include <memory>
#include "FS/RawCache.h"
#include "FS/FileArchive.h"

class FSLVGL {
public:
Expand All @@ -20,15 +21,12 @@ class FSLVGL {
lv_fs_drv_t drv;
const std::string Root = "/spiffs";

RawCache cache;

std::vector<std::string> getCacheFiles() const;
bool cacheLoaded = false;

static FSLVGL* instance;

static constexpr File* getFile(void* fp){ return (File*) fp; }

FileArchive* archive = nullptr;

void* lvOpen(const char* path, lv_fs_mode_t mode);
lv_fs_res_t lvClose(void* file);
lv_fs_res_t lvRead(void* fp, void* buf, uint32_t btr, uint32_t* br);
Expand Down
2 changes: 1 addition & 1 deletion main/src/LV_Interface/LVGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LVGL::LVGL(Display& display) : display(display){
lv_display_set_flush_cb(lvDisplay, flush);

Sprite& canvas = display.getCanvas();
lv_display_set_buffers(lvDisplay, canvas.getBuffer(), nullptr, canvas.width()*canvas.height()*2, LV_DISPLAY_RENDER_MODE_PARTIAL);
lv_display_set_buffers(lvDisplay, canvas.getBuffer(), nullptr, canvas.width()*canvas.height()*2, LV_DISPLAY_RENDER_MODE_FULL);

auto theme = theme_init(lvDisplay);
lv_display_set_theme(lvDisplay, theme);
Expand Down
2 changes: 1 addition & 1 deletion main/src/LV_Interface/LVModal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ LVModal::LVModal(LVScreen* parent) : LVObject((lv_obj_t*) *parent), parentScreen
lv_obj_set_style_pad_all(container, 8, 0);
lv_obj_set_style_pad_left(container, 5, 0);
lv_obj_set_style_pad_bottom(container, 5, 0);
lv_obj_set_style_bg_image_src(container, "S:/Modal.bin", 0);
lv_obj_set_style_bg_image_src(container, "S:/Bg/Modal.bin", 0);
lv_obj_set_style_bg_image_opa(container, LV_OPA_COVER, 0);

lv_obj_set_size(*this, 86, 76);
Expand Down
2 changes: 1 addition & 1 deletion main/src/Screens/HatchScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ HatchScreen::HatchScreen() : queue(4){
lv_obj_add_flag(*gif, LV_OBJ_FLAG_HIDDEN);

modal = lv_image_create(*this);
lv_image_set_src(modal, "S:/Modal.bin");
lv_image_set_src(modal, "S:/Bg/Modal.bin");
lv_obj_center(modal);

label = lv_label_create(modal);
Expand Down
2 changes: 1 addition & 1 deletion main/src/Screens/ScoreScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ScoreScreen::ScoreScreen(Stats statsIncrease) : statsManager((StatsManager*) Ser
lv_obj_set_style_bg_image_src(*this, bgPath, 0);

frame = lv_image_create(*this);
lv_image_set_src(frame, "S:/Modal.bin");
lv_image_set_src(frame, "S:/Bg/Modal.bin");
lv_obj_center(frame);
lv_obj_set_size(frame, 115, 82);
lv_obj_set_style_pad_all(frame, 8, 0);
Expand Down
2 changes: 1 addition & 1 deletion main/src/Services/BacklightBrightness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ constexpr uint8_t BacklightBrightness::mapDuty(uint8_t level){
fVal = std::pow(fVal, 2);
fVal = std::round(fVal * 100.0f);

fVal = map(fVal, 0, 100, MinDuty, 100);
fVal = map(fVal, 0, 100, MinDuty, MaxDuty);

return (uint8_t)fVal;
}
Expand Down
1 change: 1 addition & 0 deletions main/src/Services/BacklightBrightness.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class BacklightBrightness {
static constexpr uint8_t mapDuty(uint8_t level);
static constexpr uint8_t FadeDelay = 2;
static constexpr uint8_t MinDuty = 10;
static constexpr uint8_t MaxDuty = 80;

bool state = false;
};
Expand Down
Loading