Skip to content

Commit

Permalink
keira: liltracker: implement track loading/saving
Browse files Browse the repository at this point in the history
  • Loading branch information
and3rson committed Apr 12, 2024
1 parent b443aee commit 3594e54
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
70 changes: 56 additions & 14 deletions firmware/keira/src/apps/liltracker/liltracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "liltracker.h"
#include "note.h"
#include "utils/defer.h"

#define LILTRACKER_DIR "/liltracker"

Expand Down Expand Up @@ -637,16 +638,25 @@ void LilTrackerApp::run() {
if (controlCursorX == 0) {
// Load
String filename = filePicker(false);
Serial.println("Loading file: " + filename);
if (filename.length()) {
loadTrack(&track, filename);
pageIndex = 0;
scoreCursorX = 0;
scoreCursorY = 0;
}
} else if (controlCursorX == 1) {
// Save
String filename = filePicker(true);
Serial.println("Saving file: " + filename);
if (filename.length()) {
saveTrack(&track, filename);
}
} else if (controlCursorX == 2) {
// Reset
if (confirm("Увага", "Очистити всі дані композиції?")) {
pageIndex = 0;
if (confirm("Увага", "Очистити всі дані\nкомпозиції?")) {
track.reset();
pageIndex = 0;
scoreCursorX = 0;
scoreCursorY = 0;
}
}
} else {
Expand Down Expand Up @@ -861,23 +871,23 @@ void LilTrackerApp::startPreview(
}

void LilTrackerApp::alert(String title, String message) {
lilka::Alert alert(title, message);
alert.draw(canvas);
lilka::Alert alertDialog(title, message);
alertDialog.draw(canvas);
queueDraw();
while (!alert.isFinished()) {
alert.update();
while (!alertDialog.isFinished()) {
alertDialog.update();
}
}

bool LilTrackerApp::confirm(String title, String message) {
lilka::Alert alert(title, message + "\n\n[START] - Так\n[A] - Ні");
alert.addActivationButton(lilka::Button::START);
alert.draw(canvas);
lilka::Alert confirmDialog(title, message + "\n\n[START] - Так\n[A] - Ні");
confirmDialog.addActivationButton(lilka::Button::START);
confirmDialog.draw(canvas);
queueDraw();
while (!alert.isFinished()) {
alert.update();
while (!confirmDialog.isFinished()) {
confirmDialog.update();
}
return alert.getButton() == lilka::Button::START;
return confirmDialog.getButton() == lilka::Button::START;
}

String LilTrackerApp::filePicker(bool isSave) {
Expand Down Expand Up @@ -943,6 +953,12 @@ String LilTrackerApp::filePicker(bool isSave) {
if (filename.length() == 0) {
alert("Помилка", "Назва файлу не може бути порожньою");
} else {
String lowerFilename = filename;
lowerFilename.toLowerCase();
if (lowerFilename.endsWith(".lt")) {
filename.remove(filename.length() - 3);
}
filename += ".lt";
return String(LILTRACKER_DIR) + "/" + filename;
}
}
Expand All @@ -960,3 +976,29 @@ String LilTrackerApp::filePicker(bool isSave) {
return String(LILTRACKER_DIR) + "/" + item.title;
}
}

void LilTrackerApp::loadTrack(Track* track, String filename) {
File file = SD.open(filename, FILE_READ);
if (!file) {
alert("Помилка", "Не вдалося відкрити файл " + filename);
return;
}
Defer closeFile([&file]() { file.close(); });
uint8_t* buff = new uint8_t[file.size()];
std::unique_ptr<uint8_t[]> buffPtr(buff);
file.read(buff, file.size());
track->readFromBuffer(buff);
}

void LilTrackerApp::saveTrack(Track* track, String filename) {
File file = SD.open(filename, FILE_WRITE);
if (!file) {
alert("Помилка", "Не вдалося відкрити файл " + filename);
return;
}
Defer closeFile([&file]() { file.close(); });
uint8_t* buff = new uint8_t[track->calculateWriteBufferSize()];
std::unique_ptr<uint8_t[]> buffPtr(buff);
track->writeToBuffer(buff);
file.write(buff, track->calculateWriteBufferSize());
}
2 changes: 2 additions & 0 deletions firmware/keira/src/apps/liltracker/liltracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ class LilTrackerApp : public App {
void alert(String title, String message);
bool confirm(String title, String message);
String filePicker(bool isSave);
void loadTrack(Track* track, String path);
void saveTrack(Track* track, String path);
};
2 changes: 1 addition & 1 deletion sdk/lib/lilka/src/lilka/inputdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void InputDialog::draw(Arduino_GFX* canvas) {
canvas->setFont(FONT_10x20);

canvas->setTextBound(4, 4, canvas->width() - 8, canvas->height() - 8);
canvas->setCursor(4, 20);
canvas->setCursor(16, 20);
canvas->println(title);

canvas->setTextBound(16, 16, canvas->width() - 32, canvas->height() - 32);
Expand Down

0 comments on commit 3594e54

Please sign in to comment.