Skip to content

Commit

Permalink
♻️ (filemanager): Use file_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Feb 3, 2023
1 parent e84cb3e commit 8b01529
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 70 deletions.
7 changes: 4 additions & 3 deletions app/bootloader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ namespace factory_reset {

auto getCounter() -> uint8_t
{
FileManagerKit::File file {internal::factory_reset_counter_path, "r"};

if (!file.is_open()) {
if (auto file_does_not_exist = !FileManagerKit::file_exists(internal::factory_reset_counter_path);
file_does_not_exist) {
return default_limit + 1;
}

auto file = FileManagerKit::File {internal::factory_reset_counter_path, "r"};

auto data = std::array<uint8_t, 1> {};
file.read(data);

Expand Down
18 changes: 10 additions & 8 deletions libs/ConfigKit/include/ConfigKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@ class ConfigKit
template <std::size_t SIZE = 1>
[[nodiscard]] auto read(Config<SIZE> const &config) const
{
if (FileManagerKit::File file {config.path(), "r"}; file.is_open()) {
auto data = std::array<uint8_t, SIZE> {};
file.read(data);

if (auto config_does_not_exist = !FileManagerKit::file_exists(config.path()); config_does_not_exist) {
if constexpr (SIZE == 1) {
return data[0];
return config.default_value()[0];
} else {
return data;
return config.default_value();
}
}

auto file = FileManagerKit::File {config.path(), "r"};

auto data = std::array<uint8_t, SIZE> {};
file.read(data);

if constexpr (SIZE == 1) {
return config.default_value()[0];
return data[0];
} else {
return config.default_value();
return data;
}
}

Expand Down
41 changes: 23 additions & 18 deletions libs/FirmwareKit/source/FirmwareKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ auto FirmwareKit::getPathOfVersion(const Version &version) const -> std::filesys

auto FirmwareKit::isVersionAvailable(const Version &version) -> bool
{
auto path = getPathOfVersion(version);
auto file_exists = false;

if (auto is_open = _file.open(path); is_open) {
constexpr auto kMinimalFileSizeInBytes = std::size_t {300'000};
auto path = getPathOfVersion(version);

file_exists = _file.size() >= kMinimalFileSizeInBytes;
if (auto file_does_not_exist = !FileManagerKit::file_exists(path); file_does_not_exist) {
return false;
}

_file.open(path);

constexpr auto kMinimalFileSizeInBytes = std::size_t {300'000};
auto file_size_is_correct = _file.size() >= kMinimalFileSizeInBytes;

_file.close();

return file_exists;
return file_size_is_correct;
}

auto FirmwareKit::loadFirmware(const Version &version) -> bool
Expand Down Expand Up @@ -67,20 +69,23 @@ auto FirmwareKit::loadFactoryFirmware() -> bool

auto FirmwareKit::load(const std::filesystem::path &path) -> bool
{
if (auto is_open = _file.open(path); is_open) {
auto address = uint32_t {0x0};
auto buffer = std::array<uint8_t, 256> {};
if (auto file_does_not_exist = !FileManagerKit::file_exists(path); file_does_not_exist) {
return false;
}

_file.open(path);

_flash.erase();
auto address = uint32_t {0x0};
auto buffer = std::array<uint8_t, 256> {};

while (auto bytes_read = _file.read(buffer.data(), std::size(buffer))) {
_flash.write(address, buffer, bytes_read);
address += bytes_read;
}
_flash.erase();

_file.close();
return true;
while (auto bytes_read = _file.read(buffer.data(), std::size(buffer))) {
_flash.write(address, buffer, bytes_read);
address += bytes_read;
}

return false;
_file.close();

return true;
}
82 changes: 45 additions & 37 deletions libs/VideoKit/source/VideoKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ void VideoKit::displayImage(const std::filesystem::path &path)
return;
}

if (auto file = FileManagerKit::File {path}; file.is_open()) {
_must_stop = true;
_event_loop.stop();
if (auto file_does_not_exist = !FileManagerKit::file_exists(path); file_does_not_exist) {
return;
}

auto file = FileManagerKit::File {path};

_current_path = path;
_must_stop = true;
_event_loop.stop();

rtos::ThisThread::sleep_for(100ms);
_current_path = path;

_video.displayImage(file);
rtos::ThisThread::sleep_for(100ms);

file.close();
}
_video.displayImage(file);

file.close();
}

void VideoKit::fillWhiteBackgroundAndDisplayImage(const std::filesystem::path &path)
Expand All @@ -59,60 +63,64 @@ void VideoKit::fillWhiteBackgroundAndDisplayImage(const std::filesystem::path &p
return;
}

if (auto file = FileManagerKit::File {path}; file.is_open()) {
_must_stop = true;
_event_loop.stop();
if (auto file_does_not_exist = !FileManagerKit::file_exists(path); file_does_not_exist) {
return;
}

auto file = FileManagerKit::File {path};

_current_path = path;
_must_stop = true;
_event_loop.stop();

rtos::ThisThread::sleep_for(100ms);
_current_path = path;

_video.clearScreen();
_video.displayImage(file);
rtos::ThisThread::sleep_for(100ms);

file.close();
}
_video.clearScreen();
_video.displayImage(file);

file.close();
}

void VideoKit::playVideoOnce(const std::filesystem::path &path, const std::function<void()> &on_video_ended_callback)
{
const std::scoped_lock lock(mutex);

if (auto file = FileManagerKit::File {path}; file.is_open()) {
file.close();
if (auto file_does_not_exist = !FileManagerKit::file_exists(path); file_does_not_exist) {
return;
}

_must_stop = true;
_event_loop.stop();
_must_stop = true;
_event_loop.stop();

_current_path = path;
_must_loop = false;
_current_path = path;
_must_loop = false;

rtos::ThisThread::sleep_for(100ms);
rtos::ThisThread::sleep_for(100ms);

_on_video_ended_callback = on_video_ended_callback;
_event_loop.start();
}
_on_video_ended_callback = on_video_ended_callback;
_event_loop.start();
}

void VideoKit::playVideoOnRepeat(const std::filesystem::path &path,
const std::function<void()> &on_video_ended_callback)
{
const std::scoped_lock lock(mutex);

if (auto file = FileManagerKit::File {path}; file.is_open()) {
file.close();
if (auto file_does_not_exist = !FileManagerKit::file_exists(path); file_does_not_exist) {
return;
}

_must_stop = true;
_event_loop.stop();
_must_stop = true;
_event_loop.stop();

_current_path = path;
_must_loop = true;
_current_path = path;
_must_loop = true;

rtos::ThisThread::sleep_for(100ms);
rtos::ThisThread::sleep_for(100ms);

_on_video_ended_callback = on_video_ended_callback;
_event_loop.start();
}
_on_video_ended_callback = on_video_ended_callback;
_event_loop.start();
}

void VideoKit::stopVideo()
Expand Down
11 changes: 7 additions & 4 deletions spikes/lk_audio/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ auto main() -> int

initializeSD();

if (auto file_does_not_exist = !FileManagerKit::file_exists(sound_file_path); file_does_not_exist) {
return 1;
}

while (true) {
if (auto is_open = file.open(sound_file_path); is_open) {
playSound();
file.close();
}
file.open(sound_file_path);
playSound();
file.close();

rtos::ThisThread::sleep_for(1s);
}
Expand Down

0 comments on commit 8b01529

Please sign in to comment.