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

New dice-rolling app: InfiniDice! #1326

Merged
merged 83 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
4cc6776
initial commit for Dice.h and Dice.cpp
yusufmte Sep 11, 2022
b238d06
include Dice.h and Dice.cpp into compilation in CMakeLists.txt
yusufmte Sep 11, 2022
6278008
add "d" (0x64) to the range in jetbrains_mono_42 in fonts.json
yusufmte Sep 11, 2022
12a565a
add dice symbol
yusufmte Sep 11, 2022
b37781a
add dice app to app launcher
yusufmte Sep 11, 2022
172f20d
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Sep 11, 2022
1996113
increase number of app screens to 3
yusufmte Sep 12, 2022
f00d779
modify dice seed to use epoch time
yusufmte Sep 12, 2022
7c011d6
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Sep 12, 2022
b073b48
apply clang-format to Dice.h
yusufmte Sep 13, 2022
1edfddd
remove unnecessary stored reference to dateTime in Dice.h
yusufmte Sep 13, 2022
6c7d8c2
remove unnecessary stored reference to dateTime in Dice.cpp
yusufmte Sep 13, 2022
3bd3d81
use std::array to reduce unnecessary variables
yusufmte Sep 13, 2022
21e5ec1
remove unnecessary member variable in Dice.h
yusufmte Sep 13, 2022
b1e522f
move NextColor() into private namespace of Dice
yusufmte Sep 13, 2022
8ecfb49
modify seed to avoid overflow error
yusufmte Sep 14, 2022
616413d
fix unchanging seed
yusufmte Sep 14, 2022
6d23a14
fix Dice seed to use system tick count
yusufmte Sep 15, 2022
52e4af5
implement rolling multiple dice in Dice
yusufmte Sep 15, 2022
adc97a0
removed unnecessary reference to SystemTask
yusufmte Sep 16, 2022
13758ad
change starting die to d6
yusufmte Sep 18, 2022
ffb67d4
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Sep 18, 2022
f0cc97c
clean up labels in Dice.cpp and remove duplicate code
yusufmte Sep 18, 2022
c1b435b
use <random> library for RNG in Dice and include motion controller in…
yusufmte Sep 19, 2022
20e3023
in Dice use <random> for initial color picking as well
yusufmte Sep 19, 2022
d64f926
make typecasting within sseq uniform
yusufmte Sep 19, 2022
3923522
fix code formatting in Dice.h
yusufmte Sep 19, 2022
4647f8a
fix formatting in Dice.h
yusufmte Sep 20, 2022
9d332c7
fix Dice.h code formatting
yusufmte Sep 20, 2022
f7bd3d6
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Sep 30, 2022
349e63a
fix Dice.cpp formatting according to clang-format
yusufmte Sep 30, 2022
11067bc
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Oct 2, 2022
8020132
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Oct 4, 2022
e789622
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Oct 16, 2022
b47b13a
make Dice vibrate on roll
yusufmte Oct 16, 2022
257a593
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Oct 16, 2022
5953c55
in Dice, only show individual results if there are multiple dice
yusufmte Oct 31, 2022
cb940bf
fix formatting
yusufmte Nov 2, 2022
ee1f39b
in Dice, when rolling 1d2, also show "HEADS" or "TAILS" -- suggestion…
yusufmte Nov 5, 2022
2f079de
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Dec 11, 2022
feb8d14
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Dec 17, 2022
7131e3c
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Dec 19, 2022
159e73c
Merge branch 'InfiniTimeOrg:develop' into dice
yusufmte Jan 8, 2023
4d37e1c
Merge branch 'develop' into dice
Riksu9000 Jan 31, 2023
f1f4932
Merge branch 'main' into dice
yusufmte Mar 16, 2023
f026f02
Merge branch 'InfiniTimeOrg:main' into dice
yusufmte Mar 22, 2023
8a217aa
fix build error in Dice caused by rebase
yusufmte Mar 22, 2023
6f494a0
Merge branch 'InfiniTimeOrg:main' into dice
yusufmte Apr 1, 2023
ae2cc9d
Merge branch 'main' into dice
yusufmte Apr 5, 2023
280b3a4
update number of screens
yusufmte Apr 7, 2023
f362325
Merge branch 'main' into dice
yusufmte Apr 18, 2023
f32a9db
Merge branch 'main' into dice
yusufmte Jun 10, 2023
7eedbd6
Merge branch 'main' into dice
yusufmte Jul 6, 2023
1415cf9
removed unused chart-line icon
yusufmte Jul 6, 2023
791ef91
Merge branch 'main' into dice
yusufmte Jul 23, 2023
297a150
Motion controlls
Poohl Jul 30, 2023
5fd99a4
ui adjustments and result realignment at @Boteium suggestion
yusufmte Jul 30, 2023
c9783e7
correct formatting with clang-format
yusufmte Jul 31, 2023
9a17912
Merge branch 'main' into dice
yusufmte Aug 27, 2023
7a327f2
make label static
Poohl Sep 6, 2023
6ff5155
Merge branch 'yusufmte/dice' into dice
Poohl Sep 6, 2023
dd11b47
ignore node shit
Poohl Sep 7, 2023
9a640d7
keep display awake
Poohl Sep 10, 2023
f8d1bb7
doc
Poohl Sep 10, 2023
796b9dc
Merge branch 'main' into dice
yusufmte Sep 15, 2023
e77f053
Merge branch 'InfiniTimeOrg:main' into dice
yusufmte Oct 1, 2023
e29bd40
Merge pull request #1 from Poohl/dice
yusufmte Oct 1, 2023
8c6e98b
update as user/optional app
yusufmte Dec 13, 2023
b5c2057
Merge branch 'main' into dice
yusufmte Dec 13, 2023
ee8aaad
updated for new optional user app interface, fixed bugs and formatting
yusufmte Dec 13, 2023
3e0e30f
fix formatting
yusufmte Dec 15, 2023
f504a8b
Merge branch 'InfiniTimeOrg:main' into dice
yusufmte Dec 16, 2023
5a6cd44
Merge remote-tracking branch 'upstream/main' into dice
yusufmte Jan 10, 2024
e231b58
add to user apps
yusufmte Jan 10, 2024
1166939
remove unnecessary static declaration
yusufmte Jan 14, 2024
945a099
remove unnecessary static declaration
yusufmte Jan 14, 2024
3c4027a
removed vestigial chartLine symbol
yusufmte Jan 14, 2024
d4c6975
replace preprocessor def with static constexpr value, and formatting fix
yusufmte Jan 14, 2024
446e49e
Merge branch 'main' into dice
yusufmte Jan 14, 2024
e6cfd9e
Merge branch 'InfiniTimeOrg:main' into dice
yusufmte Jan 15, 2024
8880acc
remove unrelated .gitignore changes
yusufmte Jan 15, 2024
01b241b
use camelCase for rollHysteresis constant
yusufmte Jan 15, 2024
0c7a77a
Merge branch 'InfiniTimeOrg:main' into dice
yusufmte Jan 18, 2024
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ src/arm-none-eabi

# clangd
.cache/

# node
/node_modules
/package-lock.json
/package.json
yusufmte marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ list(APPEND SOURCE_FILES
displayapp/screens/BatteryInfo.cpp
displayapp/screens/Steps.cpp
displayapp/screens/Timer.cpp
displayapp/screens/Dice.cpp
displayapp/screens/PassKey.cpp
displayapp/screens/Error.cpp
displayapp/screens/Alarm.cpp
Expand Down Expand Up @@ -608,6 +609,7 @@ set(INCLUDE_FILES
displayapp/screens/Metronome.h
displayapp/screens/Motion.h
displayapp/screens/Timer.h
displayapp/screens/Dice.h
displayapp/screens/Alarm.h
displayapp/Colors.h
displayapp/widgets/Counter.h
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "displayapp/screens/FlashLight.h"
#include "displayapp/screens/BatteryInfo.h"
#include "displayapp/screens/Steps.h"
#include "displayapp/screens/Dice.h"
#include "displayapp/screens/PassKey.h"
#include "displayapp/screens/Error.h"

Expand Down
1 change: 1 addition & 0 deletions src/displayapp/UserApps.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Controllers.h"

#include "displayapp/screens/Alarm.h"
#include "displayapp/screens/Dice.h"
#include "displayapp/screens/Timer.h"
#include "displayapp/screens/Twos.h"
#include "displayapp/screens/Tile.h"
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/apps/Apps.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Pinetime {
Metronome,
Motion,
Steps,
Dice,
PassKey,
QuickSettings,
Settings,
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ else ()
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Paint")
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Paddle")
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Twos")
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Dice")
yusufmte marked this conversation as resolved.
Show resolved Hide resolved
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Metronome")
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Navigation")
#set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Weather")
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, 0xf06e, 0xf015, 0xf00c, 0xf743"
"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, 0xf06e, 0xf015, 0xf00c, 0xf522, 0xf743"
}
],
"bpp": 1,
Expand Down
199 changes: 199 additions & 0 deletions src/displayapp/screens/Dice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#include "displayapp/screens/Dice.h"
#include "displayapp/screens/Screen.h"
#include "displayapp/screens/Symbols.h"
#include "components/settings/Settings.h"
#include "components/motor/MotorController.h"
#include "components/motion/MotionController.h"

using namespace Pinetime::Applications::Screens;

namespace {
static lv_obj_t* MakeLabel(lv_font_t* font,
yusufmte marked this conversation as resolved.
Show resolved Hide resolved
lv_color_t color,
lv_label_long_mode_t longMode,
uint8_t width,
lv_label_align_t labelAlignment,
const char* text,
lv_obj_t* reference,
lv_align_t alignment,
int8_t x,
int8_t y) {
lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, font);
lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, color);
lv_label_set_long_mode(label, longMode);
if (width != 0) {
lv_obj_set_width(label, width);
}
lv_label_set_align(label, labelAlignment);
lv_label_set_text(label, text);
lv_obj_align(label, reference, alignment, x, y);
return label;
}

static void btnRollEventHandler(lv_obj_t* obj, lv_event_t event) {
yusufmte marked this conversation as resolved.
Show resolved Hide resolved
auto* screen = static_cast<Dice*>(obj->user_data);
if (event == LV_EVENT_CLICKED) {
screen->Roll();
}
}
}

Dice::Dice(Controllers::MotionController& motionController,
Controllers::MotorController& motorController,
Controllers::Settings& settingsController)
: motorController {motorController}, motionController {motionController}, settingsController {settingsController} {
std::seed_seq sseq {static_cast<uint32_t>(xTaskGetTickCount()),
static_cast<uint32_t>(motionController.X()),
static_cast<uint32_t>(motionController.Y()),
static_cast<uint32_t>(motionController.Z())};
gen.seed(sseq);

lv_obj_t* nCounterLabel = MakeLabel(&jetbrains_mono_bold_20,
LV_COLOR_WHITE,
LV_LABEL_LONG_EXPAND,
0,
LV_LABEL_ALIGN_CENTER,
"count",
lv_scr_act(),
LV_ALIGN_IN_TOP_LEFT,
0,
0);

lv_obj_t* dCounterLabel = MakeLabel(&jetbrains_mono_bold_20,
LV_COLOR_WHITE,
LV_LABEL_LONG_EXPAND,
0,
LV_LABEL_ALIGN_CENTER,
"sides",
nCounterLabel,
LV_ALIGN_OUT_RIGHT_MID,
20,
0);

nCounter.Create();
lv_obj_align(nCounter.GetObject(), nCounterLabel, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
nCounter.SetValue(1);

dCounter.Create();
lv_obj_align(dCounter.GetObject(), dCounterLabel, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
dCounter.SetValue(6);

std::uniform_int_distribution<> distrib(0, resultColors.size() - 1);
currentColorIndex = distrib(gen);

resultTotalLabel = MakeLabel(&jetbrains_mono_42,
resultColors[currentColorIndex],
LV_LABEL_LONG_BREAK,
120,
LV_LABEL_ALIGN_CENTER,
"",
lv_scr_act(),
LV_ALIGN_IN_TOP_RIGHT,
11,
38);
resultIndividualLabel = MakeLabel(&jetbrains_mono_bold_20,
resultColors[currentColorIndex],
LV_LABEL_LONG_BREAK,
90,
LV_LABEL_ALIGN_CENTER,
"",
resultTotalLabel,
LV_ALIGN_OUT_BOTTOM_MID,
0,
10);

Roll();
openingRoll = false;

btnRoll = lv_btn_create(lv_scr_act(), nullptr);
btnRoll->user_data = this;
lv_obj_set_event_cb(btnRoll, btnRollEventHandler);
lv_obj_set_size(btnRoll, 240, 50);
lv_obj_align(btnRoll, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0);

btnRollLabel = MakeLabel(&jetbrains_mono_bold_20,
LV_COLOR_WHITE,
LV_LABEL_LONG_EXPAND,
0,
LV_LABEL_ALIGN_CENTER,
Symbols::dice,
btnRoll,
LV_ALIGN_CENTER,
0,
0);

// Spagetti code in motion controller: it only updates the shake speed when shake to wake is on...
enableShakeForDice = !settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake);
if (enableShakeForDice)
yusufmte marked this conversation as resolved.
Show resolved Hide resolved
settingsController.setWakeUpMode(Pinetime::Controllers::Settings::WakeUpMode::Shake, true);

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

Dice::~Dice() {
// reset the shake to wake mode.
if (enableShakeForDice) {
settingsController.setWakeUpMode(Pinetime::Controllers::Settings::WakeUpMode::Shake, false);
enableShakeForDice = false;
}
lv_task_del(refreshTask);
lv_obj_clean(lv_scr_act());
}

void Dice::Refresh() {
// we only reset the hysteresis when at rest
if (motionController.CurrentShakeSpeed() >= settingsController.GetShakeThreshold()) {
if (rollHysteresis <= 0) {
// this timestamp is used for the secreen timeout
lv_disp_get_next(NULL)->last_activity_time = lv_tick_get();

Roll();
}
} else if (rollHysteresis > 0)
--rollHysteresis;
}

void Dice::Roll() {
uint8_t resultIndividual;
uint16_t resultTotal = 0;
std::uniform_int_distribution<> distrib(1, dCounter.GetValue());
NeroBurner marked this conversation as resolved.
Show resolved Hide resolved

lv_label_set_text(resultIndividualLabel, "");

if (nCounter.GetValue() == 1) {
resultTotal = distrib(gen);
if (dCounter.GetValue() == 2) {
switch (resultTotal) {
case 1:
lv_label_set_text(resultIndividualLabel, "HEADS");
break;
case 2:
lv_label_set_text(resultIndividualLabel, "TAILS");
break;
}
}
} else {
for (uint8_t i = 0; i < nCounter.GetValue(); i++) {
resultIndividual = distrib(gen);
resultTotal += resultIndividual;
lv_label_ins_text(resultIndividualLabel, LV_LABEL_POS_LAST, std::to_string(resultIndividual).c_str());
if (i < (nCounter.GetValue() - 1)) {
lv_label_ins_text(resultIndividualLabel, LV_LABEL_POS_LAST, "+");
}
}
}

lv_label_set_text_fmt(resultTotalLabel, "%d", resultTotal);
if (openingRoll == false) {
motorController.RunForDuration(30);
NextColor();
rollHysteresis = ROLL_HYSTERESIS;
}
}

void Dice::NextColor() {
currentColorIndex = (currentColorIndex + 1) % resultColors.size();
lv_obj_set_style_local_text_color(resultTotalLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, resultColors[currentColorIndex]);
lv_obj_set_style_local_text_color(resultIndividualLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, resultColors[currentColorIndex]);
}
62 changes: 62 additions & 0 deletions src/displayapp/screens/Dice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#include "displayapp/apps/Apps.h"
#include "displayapp/screens/Screen.h"
#include "displayapp/widgets/Counter.h"
#include "displayapp/Controllers.h"
#include "Symbols.h"

#include <array>
#include <random>

#define ROLL_HYSTERESIS 10
yusufmte marked this conversation as resolved.
Show resolved Hide resolved

namespace Pinetime {
namespace Applications {
namespace Screens {
class Dice : public Screen {
public:
Dice(Controllers::MotionController& motionController,
Controllers::MotorController& motorController,
Controllers::Settings& settingsController);
~Dice() override;
void Roll();
void Refresh() override;

private:
lv_obj_t* btnRoll;
lv_obj_t* btnRollLabel;
lv_obj_t* resultTotalLabel;
lv_obj_t* resultIndividualLabel;
lv_task_t* refreshTask;
bool enableShakeForDice = false;

std::mt19937 gen;

std::array<lv_color_t, 3> resultColors = {LV_COLOR_YELLOW, LV_COLOR_MAGENTA, LV_COLOR_AQUA};
uint8_t currentColorIndex;
void NextColor();

Widgets::Counter nCounter = Widgets::Counter(1, 9, jetbrains_mono_42);
Widgets::Counter dCounter = Widgets::Counter(2, 99, jetbrains_mono_42);

bool openingRoll = true;
unsigned int rollHysteresis = 0;

Controllers::MotorController& motorController;
Controllers::MotionController& motionController;
Controllers::Settings& settingsController;
};
}

template <>
struct AppTraits<Apps::Dice> {
static constexpr Apps app = Apps::Dice;
static constexpr const char* icon = Screens::Symbols::dice;

static Screens::Screen* Create(AppControllers& controllers) {
return new Screens::Dice(controllers.motionController, controllers.motorController, controllers.settingsController);
};
};
}
}
2 changes: 2 additions & 0 deletions src/displayapp/screens/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace Pinetime {
static constexpr const char* hourGlass = "\xEF\x89\x92";
static constexpr const char* lapsFlag = "\xEF\x80\xA4";
static constexpr const char* drum = "\xEF\x95\xA9";
static constexpr const char* dice = "\xEF\x94\xA2";
static constexpr const char* chartLine = "\xEF\x88\x81";
yusufmte marked this conversation as resolved.
Show resolved Hide resolved
static constexpr const char* eye = "\xEF\x81\xAE";
static constexpr const char* home = "\xEF\x80\x95";
static constexpr const char* sleep = "\xEE\xBD\x84";
Expand Down