Skip to content

Commit

Permalink
Watch face selection using CMake
Browse files Browse the repository at this point in the history
The list of watch face to build into the firmware is now set by CMake (-DENABLE_WATCHFACES).

Fix SettingWatchFace : convert to index to/from WatchFace when needed.
  • Loading branch information
JF002 committed Dec 24, 2023
1 parent 02a7a3a commit 148e275
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,10 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio
currentScreen = std::make_unique<Screens::Settings>(this, settingsController);
break;
case Apps::SettingWatchFace: {
std::array<Screens::CheckboxList::Item, UserWatchFaceTypes::Count> items;
std::array<Screens::SettingWatchFace::Item, UserWatchFaceTypes::Count> items;
int i = 0;
for (const auto& userWatchFace : userWatchFaces) {
items[i++] = Screens::CheckboxList::Item {userWatchFace.name, userWatchFace.isAvailable(controllers.filesystem)};
items[i++] = Screens::SettingWatchFace::Item {userWatchFace.name, userWatchFace.watchFace, userWatchFace.isAvailable(controllers.filesystem)};
}
currentScreen = std::make_unique<Screens::SettingWatchFace>(this, std::move(items), settingsController, filesystem);
} break;
Expand Down
7 changes: 1 addition & 6 deletions src/displayapp/apps/Apps.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@ namespace Pinetime {
static constexpr size_t Count = sizeof...(Ws);
};

using UserWatchFaceTypes = WatchFaceTypeList<WatchFace::Digital,
WatchFace::Analog,
WatchFace::PineTimeStyle,
WatchFace::Terminal,
WatchFace::Infineat,
WatchFace::CasioStyleG7710>;
using UserWatchFaceTypes = WatchFaceTypeList<@WATCHFACE_TYPES@>;

static_assert(UserWatchFaceTypes::Count >= 1);
}
Expand Down
6 changes: 6 additions & 0 deletions src/displayapp/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ else ()
set(USERAPP_TYPES "Apps::Navigation, Apps::StopWatch, Apps::Alarm, Apps::Timer, Apps::Steps, Apps::HeartRate, Apps::Music, Apps::Paint, Apps::Paddle, Apps::Twos, Apps::Metronome" CACHE STRING "List of user apps to build into the firmware")
endif ()

if(DEFINED ENABLE_WATCHFACES)
set(WATCHFACE_TYPES ${ENABLE_WATCHFACES} CACHE STRING "List of watch faces to build into the firmware")
else()
set(WATCHFACE_TYPES "WatchFace::Digital, WatchFace::Analog, WatchFace::PineTimeStyle, WatchFace::Terminal, WatchFace::Infineat, WatchFace::CasioStyleG7710" CACHE STRING "List of watch faces to build into the firmware")
endif()

add_library(infinitime_apps INTERFACE)
target_sources(infinitime_apps INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/Apps.h")
target_include_directories(infinitime_apps INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/")
Expand Down
40 changes: 35 additions & 5 deletions src/displayapp/screens/settings/SettingWatchFace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@ using namespace Pinetime::Applications::Screens;
constexpr const char* SettingWatchFace::title;
constexpr const char* SettingWatchFace::symbol;

namespace {
uint32_t IndexOf(const std::array<Pinetime::Applications::Screens::SettingWatchFace::Item,
Pinetime::Applications::UserWatchFaceTypes::Count>& watchfaces,
Pinetime::Applications::WatchFace watchface) {
size_t index = 0;
auto found = std::find_if(watchfaces.begin(), watchfaces.end(), [&index, &watchface](const Pinetime::Applications::Screens::SettingWatchFace::Item& item) {
const bool result = item.watchface == watchface;
if(!result) {
index++;
}
return result;
});
if(found == watchfaces.end()) {
index = 0;
}

return index;
}

Pinetime::Applications::WatchFace IndexToWatchFace(const std::array<Pinetime::Applications::Screens::SettingWatchFace::Item,
Pinetime::Applications::UserWatchFaceTypes::Count>& watchfaces,
size_t index) {
if(index >= watchfaces.size()) {
return watchfaces[0].watchface;
}
return watchfaces[index].watchface;
}
}

auto SettingWatchFace::CreateScreenList() const {
std::array<std::function<std::unique_ptr<Screen>()>, nScreens> screens;
for (size_t i = 0; i < screens.size(); i++) {
Expand All @@ -20,7 +49,7 @@ auto SettingWatchFace::CreateScreenList() const {
}

SettingWatchFace::SettingWatchFace(Pinetime::Applications::DisplayApp* app,
std::array<Screens::CheckboxList::Item, UserWatchFaceTypes::Count>&& watchfaceItems,
std::array<Screens::SettingWatchFace::Item, UserWatchFaceTypes::Count>&& watchfaceItems,
Pinetime::Controllers::Settings& settingsController,
Pinetime::Controllers::FS& filesystem)
: app {app},
Expand All @@ -44,7 +73,8 @@ std::unique_ptr<Screen> SettingWatchFace::CreateScreen(unsigned int screenNum) c
if (i + (screenNum * settingsPerScreen) >= watchfaceItems.size()) {
watchfacesOnThisScreen[i] = {"", false};
} else {
watchfacesOnThisScreen[i] = watchfaceItems[i + (screenNum * settingsPerScreen)];
auto& item = watchfaceItems[i + (screenNum * settingsPerScreen)];
watchfacesOnThisScreen[i] = Screens::CheckboxList::Item{item.name, item.enabled};
}
}

Expand All @@ -53,9 +83,9 @@ std::unique_ptr<Screen> SettingWatchFace::CreateScreen(unsigned int screenNum) c
nScreens,
title,
symbol,
static_cast<uint32_t>(settingsController.GetWatchFace()),
[&settings = settingsController](uint32_t index) {
settings.SetWatchFace(static_cast<WatchFace>(index));
static_cast<uint32_t>(IndexOf(watchfaceItems, settingsController.GetWatchFace())),
[this, &settings = settingsController](uint32_t index) {
settings.SetWatchFace(IndexToWatchFace(watchfaceItems, index));
settings.SaveSettings();
},
watchfacesOnThisScreen);
Expand Down
10 changes: 8 additions & 2 deletions src/displayapp/screens/settings/SettingWatchFace.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ namespace Pinetime {

class SettingWatchFace : public Screen {
public:
struct Item {
const char* name;
WatchFace watchface;
bool enabled;
};

SettingWatchFace(DisplayApp* app,
std::array<Screens::CheckboxList::Item, UserWatchFaceTypes::Count>&& watchfaceItems,
std::array<Item, UserWatchFaceTypes::Count>&& watchfaceItems,
Pinetime::Controllers::Settings& settingsController,
Pinetime::Controllers::FS& filesystem);
~SettingWatchFace() override;
Expand All @@ -33,7 +39,7 @@ namespace Pinetime {
std::unique_ptr<Screen> CreateScreen(unsigned int screenNum) const;

static constexpr int settingsPerScreen = 4;
std::array<Screens::CheckboxList::Item, UserWatchFaceTypes::Count> watchfaceItems;
std::array<Item, UserWatchFaceTypes::Count> watchfaceItems;
static constexpr int nScreens = UserWatchFaceTypes::Count > 0 ? (UserWatchFaceTypes ::Count - 1) / settingsPerScreen + 1 : 1;

Controllers::Settings& settingsController;
Expand Down

0 comments on commit 148e275

Please sign in to comment.