mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-07 16:17:38 +03:00
Moves Calibre settings into 1 single Calibre setting which will match how the KOReader PR works too
This commit is contained in:
parent
e457c8683c
commit
2976113c0a
188
src/activities/settings/CalibreSettingsActivity.cpp
Normal file
188
src/activities/settings/CalibreSettingsActivity.cpp
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
#include "CalibreSettingsActivity.h"
|
||||||
|
|
||||||
|
#include <GfxRenderer.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "CrossPointSettings.h"
|
||||||
|
#include "MappedInputManager.h"
|
||||||
|
#include "activities/network/CalibreWirelessActivity.h"
|
||||||
|
#include "activities/network/WifiSelectionActivity.h"
|
||||||
|
#include "activities/util/KeyboardEntryActivity.h"
|
||||||
|
#include "fontIds.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
constexpr int MENU_ITEMS = 2;
|
||||||
|
const char* menuNames[MENU_ITEMS] = {"Calibre Web URL", "Connect as Wireless Device"};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void CalibreSettingsActivity::taskTrampoline(void* param) {
|
||||||
|
auto* self = static_cast<CalibreSettingsActivity*>(param);
|
||||||
|
self->displayTaskLoop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalibreSettingsActivity::onEnter() {
|
||||||
|
ActivityWithSubactivity::onEnter();
|
||||||
|
|
||||||
|
renderingMutex = xSemaphoreCreateMutex();
|
||||||
|
selectedIndex = 0;
|
||||||
|
updateRequired = true;
|
||||||
|
|
||||||
|
xTaskCreate(&CalibreSettingsActivity::taskTrampoline, "CalibreSettingsTask",
|
||||||
|
4096, // Stack size
|
||||||
|
this, // Parameters
|
||||||
|
1, // Priority
|
||||||
|
&displayTaskHandle // Task handle
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalibreSettingsActivity::onExit() {
|
||||||
|
ActivityWithSubactivity::onExit();
|
||||||
|
|
||||||
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||||
|
if (displayTaskHandle) {
|
||||||
|
vTaskDelete(displayTaskHandle);
|
||||||
|
displayTaskHandle = nullptr;
|
||||||
|
}
|
||||||
|
vSemaphoreDelete(renderingMutex);
|
||||||
|
renderingMutex = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalibreSettingsActivity::loop() {
|
||||||
|
if (subActivity) {
|
||||||
|
subActivity->loop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
|
onBack();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
||||||
|
handleSelection();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mappedInput.wasPressed(MappedInputManager::Button::Up) ||
|
||||||
|
mappedInput.wasPressed(MappedInputManager::Button::Left)) {
|
||||||
|
selectedIndex = (selectedIndex + MENU_ITEMS - 1) % MENU_ITEMS;
|
||||||
|
updateRequired = true;
|
||||||
|
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
|
||||||
|
mappedInput.wasPressed(MappedInputManager::Button::Right)) {
|
||||||
|
selectedIndex = (selectedIndex + 1) % MENU_ITEMS;
|
||||||
|
updateRequired = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalibreSettingsActivity::handleSelection() {
|
||||||
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||||
|
|
||||||
|
if (selectedIndex == 0) {
|
||||||
|
// Calibre Web URL
|
||||||
|
exitActivity();
|
||||||
|
enterNewActivity(new KeyboardEntryActivity(
|
||||||
|
renderer, mappedInput, "Calibre Web URL", SETTINGS.opdsServerUrl, 10,
|
||||||
|
127, // maxLength
|
||||||
|
false, // not password
|
||||||
|
[this](const std::string& url) {
|
||||||
|
strncpy(SETTINGS.opdsServerUrl, url.c_str(), sizeof(SETTINGS.opdsServerUrl) - 1);
|
||||||
|
SETTINGS.opdsServerUrl[sizeof(SETTINGS.opdsServerUrl) - 1] = '\0';
|
||||||
|
SETTINGS.saveToFile();
|
||||||
|
exitActivity();
|
||||||
|
updateRequired = true;
|
||||||
|
},
|
||||||
|
[this]() {
|
||||||
|
exitActivity();
|
||||||
|
updateRequired = true;
|
||||||
|
}));
|
||||||
|
} else if (selectedIndex == 1) {
|
||||||
|
// Wireless Device - toggle and launch activity if enabling
|
||||||
|
const bool wasEnabled = SETTINGS.calibreWirelessEnabled;
|
||||||
|
SETTINGS.calibreWirelessEnabled = !wasEnabled;
|
||||||
|
SETTINGS.saveToFile();
|
||||||
|
|
||||||
|
if (!wasEnabled) {
|
||||||
|
// Just enabled - launch the wireless activity
|
||||||
|
exitActivity();
|
||||||
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
|
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput, [this](bool connected) {
|
||||||
|
exitActivity();
|
||||||
|
if (connected) {
|
||||||
|
enterNewActivity(new CalibreWirelessActivity(renderer, mappedInput, [this] {
|
||||||
|
exitActivity();
|
||||||
|
updateRequired = true;
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
// WiFi connection failed/cancelled, turn off the setting
|
||||||
|
SETTINGS.calibreWirelessEnabled = 0;
|
||||||
|
SETTINGS.saveToFile();
|
||||||
|
updateRequired = true;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
enterNewActivity(new CalibreWirelessActivity(renderer, mappedInput, [this] {
|
||||||
|
exitActivity();
|
||||||
|
updateRequired = true;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Just disabled - just update the display
|
||||||
|
updateRequired = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
xSemaphoreGive(renderingMutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalibreSettingsActivity::displayTaskLoop() {
|
||||||
|
while (true) {
|
||||||
|
if (updateRequired && !subActivity) {
|
||||||
|
updateRequired = false;
|
||||||
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||||
|
render();
|
||||||
|
xSemaphoreGive(renderingMutex);
|
||||||
|
}
|
||||||
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CalibreSettingsActivity::render() {
|
||||||
|
renderer.clearScreen();
|
||||||
|
|
||||||
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
|
|
||||||
|
// Draw header
|
||||||
|
renderer.drawCenteredText(UI_12_FONT_ID, 15, "Calibre", true, EpdFontFamily::BOLD);
|
||||||
|
|
||||||
|
// Draw selection highlight
|
||||||
|
renderer.fillRect(0, 60 + selectedIndex * 30 - 2, pageWidth - 1, 30);
|
||||||
|
|
||||||
|
// Draw menu items
|
||||||
|
for (int i = 0; i < MENU_ITEMS; i++) {
|
||||||
|
const int settingY = 60 + i * 30;
|
||||||
|
const bool isSelected = (i == selectedIndex);
|
||||||
|
|
||||||
|
renderer.drawText(UI_10_FONT_ID, 20, settingY, menuNames[i], !isSelected);
|
||||||
|
|
||||||
|
// Draw status
|
||||||
|
const char* status = "";
|
||||||
|
if (i == 0) {
|
||||||
|
// Calibre Web URL
|
||||||
|
status = (strlen(SETTINGS.opdsServerUrl) > 0) ? "[Set]" : "[Not Set]";
|
||||||
|
} else if (i == 1) {
|
||||||
|
// Wireless Device
|
||||||
|
status = SETTINGS.calibreWirelessEnabled ? "ON" : "OFF";
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto width = renderer.getTextWidth(UI_10_FONT_ID, status);
|
||||||
|
renderer.drawText(UI_10_FONT_ID, pageWidth - 20 - width, settingY, status, !isSelected);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw button hints
|
||||||
|
const auto labels = mappedInput.mapLabels("« Back", "Select", "", "");
|
||||||
|
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
||||||
|
|
||||||
|
renderer.displayBuffer();
|
||||||
|
}
|
||||||
36
src/activities/settings/CalibreSettingsActivity.h
Normal file
36
src/activities/settings/CalibreSettingsActivity.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/semphr.h>
|
||||||
|
#include <freertos/task.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
#include "activities/ActivityWithSubactivity.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Submenu for Calibre settings.
|
||||||
|
* Shows Calibre Web URL and Calibre Wireless Device options.
|
||||||
|
*/
|
||||||
|
class CalibreSettingsActivity final : public ActivityWithSubactivity {
|
||||||
|
public:
|
||||||
|
explicit CalibreSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||||
|
const std::function<void()>& onBack)
|
||||||
|
: ActivityWithSubactivity("CalibreSettings", renderer, mappedInput), onBack(onBack) {}
|
||||||
|
|
||||||
|
void onEnter() override;
|
||||||
|
void onExit() override;
|
||||||
|
void loop() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
TaskHandle_t displayTaskHandle = nullptr;
|
||||||
|
SemaphoreHandle_t renderingMutex = nullptr;
|
||||||
|
bool updateRequired = false;
|
||||||
|
|
||||||
|
int selectedIndex = 0;
|
||||||
|
const std::function<void()> onBack;
|
||||||
|
|
||||||
|
static void taskTrampoline(void* param);
|
||||||
|
[[noreturn]] void displayTaskLoop();
|
||||||
|
void render();
|
||||||
|
void handleSelection();
|
||||||
|
};
|
||||||
@ -1,21 +1,18 @@
|
|||||||
#include "SettingsActivity.h"
|
#include "SettingsActivity.h"
|
||||||
|
|
||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
#include <WiFi.h>
|
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "CalibreSettingsActivity.h"
|
||||||
#include "CrossPointSettings.h"
|
#include "CrossPointSettings.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "OtaUpdateActivity.h"
|
#include "OtaUpdateActivity.h"
|
||||||
#include "activities/network/CalibreWirelessActivity.h"
|
|
||||||
#include "activities/network/WifiSelectionActivity.h"
|
|
||||||
#include "activities/util/KeyboardEntryActivity.h"
|
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
|
|
||||||
// Define the static settings list
|
// Define the static settings list
|
||||||
namespace {
|
namespace {
|
||||||
constexpr int settingsCount = 15;
|
constexpr int settingsCount = 14;
|
||||||
const SettingInfo settingsList[settingsCount] = {
|
const SettingInfo settingsList[settingsCount] = {
|
||||||
// Should match with SLEEP_SCREEN_MODE
|
// Should match with SLEEP_SCREEN_MODE
|
||||||
{"Sleep Screen", SettingType::ENUM, &CrossPointSettings::sleepScreen, {"Dark", "Light", "Custom", "Cover"}},
|
{"Sleep Screen", SettingType::ENUM, &CrossPointSettings::sleepScreen, {"Dark", "Light", "Custom", "Cover"}},
|
||||||
@ -45,8 +42,7 @@ const SettingInfo settingsList[settingsCount] = {
|
|||||||
&CrossPointSettings::paragraphAlignment,
|
&CrossPointSettings::paragraphAlignment,
|
||||||
{"Justify", "Left", "Center", "Right"}},
|
{"Justify", "Left", "Center", "Right"}},
|
||||||
{"Reader Side Margin", SettingType::ENUM, &CrossPointSettings::sideMargin, {"None", "Small", "Medium", "Large"}},
|
{"Reader Side Margin", SettingType::ENUM, &CrossPointSettings::sideMargin, {"None", "Small", "Medium", "Large"}},
|
||||||
{"Calibre Web URL", SettingType::ACTION, nullptr, {}},
|
{"Calibre Settings", SettingType::ACTION, nullptr, {}},
|
||||||
{"Calibre Wireless Device", SettingType::TOGGLE, &CrossPointSettings::calibreWirelessEnabled, {}},
|
|
||||||
{"Check for updates", SettingType::ACTION, nullptr, {}},
|
{"Check for updates", SettingType::ACTION, nullptr, {}},
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
@ -135,59 +131,19 @@ void SettingsActivity::toggleCurrentSetting() {
|
|||||||
// Toggle the boolean value using the member pointer
|
// Toggle the boolean value using the member pointer
|
||||||
const bool currentValue = SETTINGS.*(setting.valuePtr);
|
const bool currentValue = SETTINGS.*(setting.valuePtr);
|
||||||
SETTINGS.*(setting.valuePtr) = !currentValue;
|
SETTINGS.*(setting.valuePtr) = !currentValue;
|
||||||
|
|
||||||
// Special handling for Calibre Wireless Device - launch activity when toggled ON
|
|
||||||
if (std::string(setting.name) == "Calibre Wireless Device" && !currentValue) {
|
|
||||||
SETTINGS.saveToFile();
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
exitActivity();
|
|
||||||
// Check WiFi connectivity first
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput, [this](bool connected) {
|
|
||||||
exitActivity();
|
|
||||||
if (connected) {
|
|
||||||
enterNewActivity(new CalibreWirelessActivity(renderer, mappedInput, [this] {
|
|
||||||
exitActivity();
|
|
||||||
updateRequired = true;
|
|
||||||
}));
|
|
||||||
} else {
|
|
||||||
// WiFi connection failed/cancelled, turn off the setting
|
|
||||||
SETTINGS.calibreWirelessEnabled = 0;
|
|
||||||
SETTINGS.saveToFile();
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
} else {
|
|
||||||
enterNewActivity(new CalibreWirelessActivity(renderer, mappedInput, [this] {
|
|
||||||
exitActivity();
|
|
||||||
updateRequired = true;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
xSemaphoreGive(renderingMutex);
|
|
||||||
return; // Don't save again at the end
|
|
||||||
}
|
|
||||||
} else if (setting.type == SettingType::ENUM && setting.valuePtr != nullptr) {
|
} else if (setting.type == SettingType::ENUM && setting.valuePtr != nullptr) {
|
||||||
const uint8_t currentValue = SETTINGS.*(setting.valuePtr);
|
const uint8_t currentValue = SETTINGS.*(setting.valuePtr);
|
||||||
SETTINGS.*(setting.valuePtr) = (currentValue + 1) % static_cast<uint8_t>(setting.enumValues.size());
|
SETTINGS.*(setting.valuePtr) = (currentValue + 1) % static_cast<uint8_t>(setting.enumValues.size());
|
||||||
} else if (setting.type == SettingType::ACTION) {
|
} else if (setting.type == SettingType::ACTION) {
|
||||||
if (std::string(setting.name) == "Calibre Web URL") {
|
if (strcmp(setting.name, "Calibre Settings") == 0) {
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||||
exitActivity();
|
exitActivity();
|
||||||
enterNewActivity(new KeyboardEntryActivity(
|
enterNewActivity(new CalibreSettingsActivity(renderer, mappedInput, [this] {
|
||||||
renderer, mappedInput, "Calibre Web URL", SETTINGS.opdsServerUrl, 10, 127, false,
|
|
||||||
[this](const std::string& url) {
|
|
||||||
strncpy(SETTINGS.opdsServerUrl, url.c_str(), sizeof(SETTINGS.opdsServerUrl) - 1);
|
|
||||||
SETTINGS.opdsServerUrl[sizeof(SETTINGS.opdsServerUrl) - 1] = '\0';
|
|
||||||
SETTINGS.saveToFile();
|
|
||||||
exitActivity();
|
|
||||||
updateRequired = true;
|
|
||||||
},
|
|
||||||
[this] {
|
|
||||||
exitActivity();
|
exitActivity();
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
}));
|
}));
|
||||||
xSemaphoreGive(renderingMutex);
|
xSemaphoreGive(renderingMutex);
|
||||||
} else if (std::string(setting.name) == "Check for updates") {
|
} else if (strcmp(setting.name, "Check for updates") == 0) {
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||||
exitActivity();
|
exitActivity();
|
||||||
enterNewActivity(new OtaUpdateActivity(renderer, mappedInput, [this] {
|
enterNewActivity(new OtaUpdateActivity(renderer, mappedInput, [this] {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user