Skip to content

Commit

Permalink
Merge 'yusufmte/dice'
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 4d37e1c
Merge: 159e73c 81bc16f
Author: Riku Isokoski <[email protected]>
Date:   Tue Jan 31 22:42:40 2023 +0200

    Merge branch 'develop' into dice

commit 159e73c
Merge: 7131e3c 09db67e
Author: Yusuf Ebrahim <[email protected]>
Date:   Sat Jan 7 19:51:07 2023 -0500

    Merge branch 'InfiniTimeOrg:develop' into dice

commit 7131e3c
Merge: feb8d14 afea7ca
Author: Yusuf Ebrahim <[email protected]>
Date:   Mon Dec 19 11:51:31 2022 -0500

    Merge branch 'InfiniTimeOrg:develop' into dice

commit feb8d14
Merge: 2f079de 081cc60
Author: Yusuf Ebrahim <[email protected]>
Date:   Sat Dec 17 13:39:47 2022 -0500

    Merge branch 'InfiniTimeOrg:develop' into dice

commit 2f079de
Merge: ee1f39b 361e381
Author: Yusuf Ebrahim <[email protected]>
Date:   Sun Dec 11 15:44:46 2022 -0500

    Merge branch 'InfiniTimeOrg:develop' into dice

commit ee1f39b
Author: yusuf ebrahim <[email protected]>
Date:   Sat Nov 5 18:31:05 2022 -0400

    in Dice, when rolling 1d2, also show "HEADS" or "TAILS" -- suggestion by @medeyko

commit cb940bf
Author: yusuf ebrahim <[email protected]>
Date:   Wed Nov 2 14:37:15 2022 -0400

    fix formatting

commit 5953c55
Author: yusuf ebrahim <[email protected]>
Date:   Mon Oct 31 10:45:44 2022 -0400

    in Dice, only show individual results if there are multiple dice

commit 257a593
Merge: b47b13a b56c5f2
Author: Yusuf Ebrahim <[email protected]>
Date:   Sun Oct 16 12:28:13 2022 -0400

    Merge branch 'InfiniTimeOrg:develop' into dice

commit b47b13a
Author: yusuf ebrahim <[email protected]>
Date:   Sat Oct 15 23:40:01 2022 -0400

    make Dice vibrate on roll

commit e789622
Merge: 8020132 d3ba184
Author: Yusuf Ebrahim <[email protected]>
Date:   Sat Oct 15 23:40:25 2022 -0400

    Merge branch 'InfiniTimeOrg:develop' into dice

commit 8020132
Merge: 11067bc 37155ec
Author: Yusuf Ebrahim <[email protected]>
Date:   Tue Oct 4 18:41:50 2022 -0400

    Merge branch 'InfiniTimeOrg:develop' into dice

commit 11067bc
Merge: 349e63a 7b115fe
Author: Yusuf Ebrahim <[email protected]>
Date:   Sun Oct 2 12:22:59 2022 -0400

    Merge branch 'InfiniTimeOrg:develop' into dice

commit 349e63a
Author: yusuf ebrahim <[email protected]>
Date:   Fri Sep 30 12:20:35 2022 -0400

    fix Dice.cpp formatting according to clang-format

commit f7bd3d6
Merge: 9d332c7 7972873
Author: Yusuf Ebrahim <[email protected]>
Date:   Fri Sep 30 03:22:33 2022 -0400

    Merge branch 'InfiniTimeOrg:develop' into dice

commit 9d332c7
Author: Yusuf Ebrahim <[email protected]>
Date:   Tue Sep 20 07:54:05 2022 -0400

    fix Dice.h code formatting

    Co-authored-by: NeroBurner <[email protected]>

commit 4647f8a
Author: yusuf ebrahim <[email protected]>
Date:   Tue Sep 20 07:40:13 2022 -0400

    fix formatting in Dice.h

commit 3923522
Author: yusuf ebrahim <[email protected]>
Date:   Mon Sep 19 17:32:57 2022 -0400

    fix code formatting in Dice.h

commit d64f926
Author: yusuf ebrahim <[email protected]>
Date:   Mon Sep 19 17:01:36 2022 -0400

    make typecasting within sseq uniform

commit 20e3023
Author: yusuf ebrahim <[email protected]>
Date:   Mon Sep 19 16:31:40 2022 -0400

    in Dice use <random> for initial color picking as well

commit c1b435b
Author: yusuf ebrahim <[email protected]>
Date:   Mon Sep 19 16:13:29 2022 -0400

    use <random> library for RNG in Dice and include motion controller in seed

    there are known problems with rand() and srand() (see https://en.cppreference.com/w/cpp/numeric/random/rand) and the <random> library is preferred for this reason. the function used from <random> also avoids a very rare bias that would occur using rand() and modulo, when RAND_MAX is not a multiple of d and the initially generated number falls in the last "short" segment. this commit also updates the seed to derive entropy (via seed_seq) from a mix of the system tick count and the x,y,z components of the pinetime motion controller-- taking inspiration from and with credit to @w4tsn (InfiniTimeOrg#1199)

commit f0cc97c
Author: yusuf ebrahim <[email protected]>
Date:   Sat Sep 17 22:19:39 2022 -0400

    clean up labels in Dice.cpp and remove duplicate code

commit ffb67d4
Merge: 13758ad 30e8353
Author: Yusuf Ebrahim <[email protected]>
Date:   Sat Sep 17 22:01:27 2022 -0400

    Merge branch 'InfiniTimeOrg:develop' into dice

commit 13758ad
Author: yusuf ebrahim <[email protected]>
Date:   Sat Sep 17 21:58:17 2022 -0400

    change starting die to d6

commit adc97a0
Author: yusuf ebrahim <[email protected]>
Date:   Fri Sep 16 16:19:42 2022 -0400

    removed unnecessary reference to SystemTask

commit 52e4af5
Author: yusuf ebrahim <[email protected]>
Date:   Thu Sep 15 14:44:50 2022 -0400

    implement rolling multiple dice in Dice

commit 6d23a14
Author: yusuf ebrahim <[email protected]>
Date:   Thu Sep 15 12:20:31 2022 -0400

    fix Dice seed to use system tick count

commit 616413d
Author: yusuf ebrahim <[email protected]>
Date:   Wed Sep 14 17:20:16 2022 -0400

    fix unchanging seed

commit 8ecfb49
Author: yusuf ebrahim <[email protected]>
Date:   Wed Sep 14 16:48:35 2022 -0400

    modify seed to avoid overflow error

commit b1e522f
Author: yusuf ebrahim <[email protected]>
Date:   Tue Sep 13 17:04:54 2022 -0400

    move NextColor() into private namespace of Dice

commit 21e5ec1
Author: yusuf ebrahim <[email protected]>
Date:   Tue Sep 13 17:03:18 2022 -0400

    remove unnecessary member variable in Dice.h

commit 3bd3d81
Author: yusuf ebrahim <[email protected]>
Date:   Tue Sep 13 17:00:56 2022 -0400

    use std::array to reduce unnecessary variables

commit 6c7d8c2
Author: yusuf ebrahim <[email protected]>
Date:   Tue Sep 13 16:56:21 2022 -0400

    remove unnecessary stored reference to dateTime in Dice.cpp

commit 1edfddd
Author: yusuf ebrahim <[email protected]>
Date:   Tue Sep 13 16:47:10 2022 -0400

    remove unnecessary stored reference to dateTime in Dice.h

commit b073b48
Author: yusuf ebrahim <[email protected]>
Date:   Tue Sep 13 16:29:29 2022 -0400

    apply clang-format to Dice.h

commit 7c011d6
Merge: f00d779 c853681
Author: Yusuf Ebrahim <[email protected]>
Date:   Mon Sep 12 10:11:08 2022 -0400

    Merge branch 'InfiniTimeOrg:develop' into dice

commit f00d779
Author: yusuf ebrahim <[email protected]>
Date:   Sun Sep 11 22:55:11 2022 -0400

    modify dice seed to use epoch time

commit 1996113
Author: yusuf ebrahim <[email protected]>
Date:   Sun Sep 11 22:54:13 2022 -0400

    increase number of app screens to 3

commit 172f20d
Merge: b37781a c9a5c3f
Author: Yusuf Ebrahim <[email protected]>
Date:   Sun Sep 11 09:02:38 2022 -0400

    Merge branch 'InfiniTimeOrg:develop' into dice

commit b37781a
Author: yusuf ebrahim <[email protected]>
Date:   Sun Sep 11 00:25:16 2022 -0400

    add dice app to app launcher

commit 12a565a
Author: yusuf ebrahim <[email protected]>
Date:   Sun Sep 11 00:16:46 2022 -0400

    add dice symbol

commit 6278008
Author: yusuf ebrahim <[email protected]>
Date:   Sun Sep 11 00:09:16 2022 -0400

    add "d" (0x64) to the range in jetbrains_mono_42 in fonts.json

commit b238d06
Author: yusuf ebrahim <[email protected]>
Date:   Sun Sep 11 00:07:37 2022 -0400

    include Dice.h and Dice.cpp into compilation in CMakeLists.txt

commit 4cc6776
Author: yusuf ebrahim <[email protected]>
Date:   Sun Sep 11 00:05:27 2022 -0400

    initial commit for Dice.h and Dice.cpp
  • Loading branch information
FintasticMan committed Feb 3, 2023
1 parent 8c3fa74 commit 927d71a
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,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 @@ -613,6 +614,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/Apps.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace Pinetime {
Motion,
Steps,
ScreenLockConfirmation,
Dice,
Weather,
PassKey,
QuickSettings,
Expand Down
4 changes: 4 additions & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "displayapp/screens/BatteryInfo.h"
#include "displayapp/screens/Steps.h"
#include "displayapp/screens/ScreenLockConfirmation.h"
#include "displayapp/screens/Dice.h"
#include "displayapp/screens/PassKey.h"
#include "displayapp/screens/Error.h"

Expand Down Expand Up @@ -497,6 +498,9 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio
case Apps::ScreenLockConfirmation:
currentScreen = std::make_unique<Screens::ScreenLockConfirmation>(this, touchHandler);
break;
case Apps::Dice:
currentScreen = std::make_unique<Screens::Dice>(this, motionController, motorController);
break;
}
currentApp = app;
}
Expand Down
4 changes: 2 additions & 2 deletions 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, 0xf00c"
"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, 0xf00c, 0xf522"
},
{
"file": "material-design-icons/MaterialIcons-Regular.ttf",
Expand All @@ -22,7 +22,7 @@
"sources": [
{
"file": "JetBrainsMono-Regular.ttf",
"range": "0x25, 0x2b, 0x2d, 0x30-0x3a"
"range": "0x25, 0x2b, 0x2d, 0x30-0x3a, 0x64"
}
],
"bpp": 1,
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/screens/ApplicationList.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace Pinetime {
{Symbols::drum, Apps::Metronome},
{Symbols::map, Apps::Navigation},

{Symbols::dice, Apps::Dice},
{Symbols::flashlight, Apps::FlashLight},
};

Expand Down
158 changes: 158 additions & 0 deletions src/displayapp/screens/Dice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include "displayapp/screens/Dice.h"

#include "displayapp/screens/Screen.h"
#include "displayapp/screens/Symbols.h"

#include <lvgl/lvgl.h>

using namespace Pinetime::Applications::Screens;

namespace {
lv_obj_t* MakeLabel(lv_font_t* font,
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) {
auto* screen = static_cast<Dice*>(obj->user_data);
if (event == LV_EVENT_CLICKED) {
screen->Roll();
}
}
}

Dice::Dice(DisplayApp* app, Controllers::MotionController& motion, Controllers::MotorController& motor) : Screen(app), motor {motor} {
std::seed_seq sseq {static_cast<uint32_t>(xTaskGetTickCount()),
static_cast<uint32_t>(motion.X()),
static_cast<uint32_t>(motion.Y()),
static_cast<uint32_t>(motion.Z())};
gen.seed(sseq);

nCounter.Create();
lv_obj_align(nCounter.GetObject(), lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 0, 24);
nCounter.SetValue(1);

lv_obj_t* dLabel = MakeLabel(&jetbrains_mono_42,
LV_COLOR_WHITE,
LV_LABEL_LONG_EXPAND,
0,
LV_LABEL_ALIGN_LEFT,
"d",
nCounter.GetObject(),
LV_ALIGN_OUT_RIGHT_MID,
0,
0);

dCounter.Create();
lv_obj_align(dCounter.GetObject(), dLabel, LV_ALIGN_OUT_RIGHT_MID, 2, 0);
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,
13,
30);
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);
}

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

void Dice::Roll() {
uint8_t resultIndividual;
uint16_t resultTotal = 0;
std::uniform_int_distribution<> distrib(1, dCounter.GetValue());

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) {
motor.RunForDuration(30);
NextColor();
}
}

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]);
}
40 changes: 40 additions & 0 deletions src/displayapp/screens/Dice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "components/motion/MotionController.h"
#include "components/motor/MotorController.h"
#include "displayapp/screens/Screen.h"
#include "displayapp/widgets/Counter.h"
#include "systemtask/SystemTask.h"

#include <lvgl/lvgl.h>

#include <array>
#include <random>

namespace Pinetime::Applications::Screens {
class Dice : public Screen {
public:
Dice(DisplayApp* app, Controllers::MotionController& motion, Controllers::MotorController& motor);
~Dice() override;
void Roll();

private:
lv_obj_t* btnRoll;
lv_obj_t* btnRollLabel;
lv_obj_t* resultTotalLabel;
lv_obj_t* resultIndividualLabel;

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;

Controllers::MotorController& motor;
};
}
1 change: 1 addition & 0 deletions src/displayapp/screens/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ 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";
static constexpr const char* eye = "\xEF\x81\xAE";
static constexpr const char* home = "\xEF\x80\x95";
Expand Down

0 comments on commit 927d71a

Please sign in to comment.