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

WIP: screens: add dice app #1199

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 26 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"cmake.generator": "Unix Makefiles",
"clang-tidy.buildPath": "build/compile_commands.json",
"files.associations": {
"*.j2": "jinja",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
Expand Down Expand Up @@ -54,6 +55,30 @@
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"variant": "cpp",
"__bit_reference": "cpp",
"__node_handle": "cpp",
"__memory": "cpp",
"filesystem": "cpp",
"locale": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstring": "cpp",
"mutex": "cpp",
"numbers": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tuple": "cpp",
"ios": "cpp",
"queue": "cpp"
}
}
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ list(APPEND SOURCE_FILES
displayapp/screens/Error.cpp
displayapp/screens/Alarm.cpp
displayapp/screens/Styles.cpp
displayapp/screens/Dice.cpp
displayapp/Colors.cpp
displayapp/widgets/Counter.cpp

Expand Down Expand Up @@ -607,6 +608,7 @@ set(INCLUDE_FILES
displayapp/screens/Motion.h
displayapp/screens/Timer.h
displayapp/screens/Alarm.h
displayapp/screens/Dice.h
displayapp/Colors.h
displayapp/widgets/Counter.h
drivers/St7789.h
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/Apps.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Pinetime {
Steps,
Weather,
PassKey,
Dice,
QuickSettings,
Settings,
SettingWatchFace,
Expand Down
4 changes: 4 additions & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "displayapp/screens/Steps.h"
#include "displayapp/screens/PassKey.h"
#include "displayapp/screens/Error.h"
#include "displayapp/screens/Dice.h"

#include "drivers/Cst816s.h"
#include "drivers/St7789.h"
Expand Down Expand Up @@ -473,6 +474,9 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
case Apps::Steps:
currentScreen = std::make_unique<Screens::Steps>(this, motionController, settingsController);
break;
case Apps::Dice:
currentScreen = std::make_unique<Screens::Dice>(this, settingsController, motionController);
break;
}
currentApp = app;
}
Expand Down
2 changes: 1 addition & 1 deletion src/displayapp/fonts/fonts.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
{
"file": "FontAwesome5-Solid+Brands+Regular.woff",
"range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf201, 0xf06e, 0xf015"
"range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf201, 0xf06e, 0xf015, 0xf6cf"
}
],
"bpp": 1,
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/screens/ApplicationList.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Pinetime {
static constexpr int appsPerScreen = 6;

// Increment this when more space is needed
static constexpr int nScreens = 2;
static constexpr int nScreens = 3;

static constexpr std::array<Tile::Applications, appsPerScreen * nScreens> applications {{
{Symbols::stopWatch, Apps::StopWatch},
Expand All @@ -50,6 +50,7 @@ namespace Pinetime {
{Symbols::chartLine, Apps::Motion},
{Symbols::drum, Apps::Metronome},
{Symbols::map, Apps::Navigation},
{Symbols::diceD20, Apps::Dice},
}};
ScreenList<nScreens> screens;
};
Expand Down
235 changes: 235 additions & 0 deletions src/displayapp/screens/Dice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
#include "random"
#include "functional"
#include "displayapp/screens/Dice.h"
#include "displayapp/DisplayApp.h"

using namespace Pinetime::Applications::Screens;

static void btnEventHandler(lv_obj_t* obj, lv_event_t event) {
auto* screen = static_cast<Dice*>(obj->user_data);
screen->OnButtonEvent(obj, event);
}

Dice::Dice(DisplayApp* app, Controllers::Settings& settingsController, Controllers::MotionController& motionController)
: Screen(app), settingsController {settingsController}, motionController {motionController} {

count = 1;
grade = 20;
substractor = 0;

txtCount = lv_label_create(lv_scr_act(), nullptr);
txtGradeSeparator = lv_label_create(lv_scr_act(), nullptr);
txtGrade = lv_label_create(lv_scr_act(), nullptr);
txtSubstractorSeparator = lv_label_create(lv_scr_act(), nullptr);
txtSubstractor = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(txtCount, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
lv_obj_set_style_local_text_font(txtGrade, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
lv_obj_set_style_local_text_font(txtSubstractor, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
lv_obj_set_style_local_text_color(txtCount, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
lv_obj_set_style_local_text_color(txtGrade, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
lv_obj_set_style_local_text_color(txtSubstractor, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
lv_label_set_text_static(txtGradeSeparator, "d");
lv_label_set_text_static(txtSubstractorSeparator, "-");

UpdateRollConfig();

lv_obj_align(txtCount, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 22, -25);
lv_obj_align(txtGradeSeparator, lv_scr_act(), LV_ALIGN_CENTER, -36, -25);
lv_obj_align(txtGrade, lv_scr_act(), LV_ALIGN_CENTER, 0, -25);
lv_obj_align(txtSubstractorSeparator, lv_scr_act(), LV_ALIGN_CENTER, 36, -25);
lv_obj_align(txtSubstractor, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -22, -25);

lblampm = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(lblampm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_bold_20);
lv_obj_set_style_local_text_color(lblampm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
lv_label_set_text_static(lblampm, " ");
lv_label_set_align(lblampm, LV_LABEL_ALIGN_CENTER);
lv_obj_align(lblampm, lv_scr_act(), LV_ALIGN_CENTER, 0, 30);

btnCountUp = lv_btn_create(lv_scr_act(), nullptr);
btnCountUp->user_data = this;
lv_obj_set_event_cb(btnCountUp, btnEventHandler);
lv_obj_set_size(btnCountUp, 60, 40);
lv_obj_align(btnCountUp, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, -85);
txtCountUp = lv_label_create(btnCountUp, nullptr);
lv_label_set_text_static(txtCountUp, "+");

btnCountDown = lv_btn_create(lv_scr_act(), nullptr);
btnCountDown->user_data = this;
lv_obj_set_event_cb(btnCountDown, btnEventHandler);
lv_obj_set_size(btnCountDown, 60, 40);
lv_obj_align(btnCountDown, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, 35);
txtCountDown = lv_label_create(btnCountDown, nullptr);
lv_label_set_text_static(txtCountDown, "-");

btnGradeUp = lv_btn_create(lv_scr_act(), nullptr);
btnGradeUp->user_data = this;
lv_obj_set_event_cb(btnGradeUp, btnEventHandler);
lv_obj_set_size(btnGradeUp, 60, 40);
lv_obj_align(btnGradeUp, lv_scr_act(), LV_ALIGN_CENTER, 0, -85);
txtGradeUp = lv_label_create(btnGradeUp, nullptr);
lv_label_set_text_static(txtGradeUp, "+");

btnGradeDown = lv_btn_create(lv_scr_act(), nullptr);
btnGradeDown->user_data = this;
lv_obj_set_event_cb(btnGradeDown, btnEventHandler);
lv_obj_set_size(btnGradeDown, 60, 40);
lv_obj_align(btnGradeDown, lv_scr_act(), LV_ALIGN_CENTER, 0, 35);
txtGradeDown = lv_label_create(btnGradeDown, nullptr);
lv_label_set_text_static(txtGradeDown, "-");

btnSubstractorUp = lv_btn_create(lv_scr_act(), nullptr);
btnSubstractorUp->user_data = this;
lv_obj_set_event_cb(btnSubstractorUp, btnEventHandler);
lv_obj_set_size(btnSubstractorUp, 60, 40);
lv_obj_align(btnSubstractorUp, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, -85);
txtSubstractorUp = lv_label_create(btnSubstractorUp, nullptr);
lv_label_set_text_static(txtSubstractorUp, "+");

btnSubstractorDown = lv_btn_create(lv_scr_act(), nullptr);
btnSubstractorDown->user_data = this;
lv_obj_set_event_cb(btnSubstractorDown, btnEventHandler);
lv_obj_set_size(btnSubstractorDown, 60, 40);
lv_obj_align(btnSubstractorDown, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, 35);
txtSubstractorDown = lv_label_create(btnSubstractorDown, nullptr);
lv_label_set_text_static(txtSubstractorDown, "-");

btnRoll = lv_btn_create(lv_scr_act(), nullptr);
btnRoll->user_data = this;
lv_obj_set_event_cb(btnRoll, btnEventHandler);
lv_obj_set_size(btnRoll, 115, 50);
lv_obj_align(btnRoll, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0);
txtRoll = lv_label_create(btnRoll, nullptr);
lv_label_set_text_static(txtRoll, "Roll");

refreshTask = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this);
}

Dice::~Dice() {
lv_task_del(refreshTask);
lv_obj_clean(lv_scr_act());
}

void Dice::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
if (event == LV_EVENT_CLICKED) {
if (obj == btnRoll) {
ShowRoll();
return;
}
if (obj == btnMessage) {
HideRoll();
return;
}
if (obj == btnCountUp) {
if (count >= 99) {
count = 1;
} else {
count++;
}
UpdateRollConfig();
return;
}
if (obj == btnCountDown) {
if (count == 1) {
count = 99;
} else {
count--;
}
UpdateRollConfig();
return;
}
if (obj == btnGradeUp) {
if (grade >= 20) {
grade = 2;
} else {
grade++;
}
UpdateRollConfig();
return;
}
if (obj == btnGradeDown) {
if (grade == 2) {
grade = 20;
} else {
grade--;
}
UpdateRollConfig();
return;
}
if (obj == btnSubstractorUp) {
if (substractor >= 99) {
substractor = 0;
} else {
substractor++;
}
UpdateRollConfig();
return;
}
if (obj == btnSubstractorDown) {
if (substractor == 0) {
substractor = 99;
} else {
substractor--;
}
UpdateRollConfig();
return;
}
}
}

bool Dice::OnButtonPushed() {
if (txtMessage != nullptr && btnMessage != nullptr) {
HideRoll();
return true;
}
return false;
}

void Dice::UpdateRollConfig() {
lv_label_set_text_fmt(txtCount, "%02u", count);
lv_label_set_text_fmt(txtGrade, "%02u", grade);
lv_label_set_text_fmt(txtSubstractor, "%02u", substractor);
}

void Dice::ShowRoll() {
if (txtMessage != nullptr && btnMessage != nullptr) {
HideRoll();
}
btnMessage = lv_btn_create(lv_scr_act(), nullptr);
btnMessage->user_data = this;
lv_obj_set_event_cb(btnMessage, btnEventHandler);
lv_obj_set_height(btnMessage, 200);
lv_obj_set_width(btnMessage, 150);
lv_obj_align(btnMessage, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
txtMessage = lv_label_create(btnMessage, nullptr);
lv_obj_set_style_local_bg_color(btnMessage, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_NAVY);
lv_label_set_text_fmt(txtMessage, "Rolled:\n%2u", Roll());
}

void Dice::HideRoll() {
lv_obj_del(btnMessage);
txtMessage = nullptr;
btnMessage = nullptr;
}

uint8_t Dice::Roll() {
std::seed_seq seed {static_cast<uint16_t>(std::random_device()() % 65535),
static_cast<uint16_t>((motionController.X() + motionController.Y() + motionController.Z()) % 32767)};
std::default_random_engine generator {seed};
std::uniform_int_distribution<uint8_t> distribution(1, grade);
auto dice = std::bind(distribution, generator);
uint8_t sum = 0;
for (uint8_t i = 1; i <= count; i++) {
sum += dice();
}
if (substractor > sum) {
return 0;
}
return sum - substractor;
}

void Dice::Refresh() {
if (motionController.Should_ShakeWake(std::min(settingsController.GetShakeThreshold() * 4, 65535))) {
ShowRoll();
}
}
36 changes: 36 additions & 0 deletions src/displayapp/screens/Dice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "displayapp/screens/Screen.h"
#include "components/settings/Settings.h"
#include <components/motion/MotionController.h>
#include <lvgl/lvgl.h>

namespace Pinetime {
namespace Applications {
namespace Screens {
class Dice : public Screen {
public:
Dice(DisplayApp* app, Pinetime::Controllers::Settings& settingsController, Controllers::MotionController& motionController);
~Dice() override;
void OnButtonEvent(lv_obj_t* obj, lv_event_t event);
bool OnButtonPushed() override;
void Refresh() override;

private:
Controllers::Settings& settingsController;
Controllers::MotionController& motionController;
uint8_t count, grade, substractor;
lv_obj_t *txtCount, *txtGrade, *txtGradeSeparator, *txtSubstractor, *txtSubstractorSeparator, *lblampm, *btnCountUp, *btnCountDown,
*btnGradeUp, *txtGradeUp, *btnGradeDown, *txtGradeDown, *txtCountUp, *txtCountDown, *btnSubstractorUp, *txtSubstractorUp,
*btnSubstractorDown, *txtSubstractorDown, *btnRoll, *txtRoll;
lv_obj_t* txtMessage = nullptr;
lv_obj_t* btnMessage = nullptr;
lv_task_t* refreshTask;
void ShowRoll();
void HideRoll();
void UpdateRollConfig();
uint8_t Roll();
};
}
}
}
1 change: 1 addition & 0 deletions src/displayapp/screens/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Pinetime {
static constexpr const char* chartLine = "\xEF\x88\x81";
static constexpr const char* eye = "\xEF\x81\xAE";
static constexpr const char* home = "\xEF\x80\x95";
static constexpr const char* diceD20 = "\xEF\x9B\x8F";

// lv_font_sys_48.c
static constexpr const char* settings = "\xEE\xA4\x82"; // e902
Expand Down