Skip to content
Open
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
10 changes: 5 additions & 5 deletions src/displayapp/screens/Dice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ 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);
uint32_t seed = static_cast<uint32_t>(xTaskGetTickCount());
seed = seed * 31 + static_cast<uint32_t>(motionController.X());
seed = seed * 31 + static_cast<uint32_t>(motionController.Y());
seed = seed * 31 + static_cast<uint32_t>(motionController.Z());
gen.Seed(seed);

lv_obj_t* nCounterLabel = MakeLabel(&jetbrains_mono_bold_20,
LV_COLOR_WHITE,
Expand Down
33 changes: 32 additions & 1 deletion src/displayapp/screens/Dice.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,38 @@ namespace Pinetime {
lv_task_t* refreshTask;
bool enableShakeForDice = false;

std::mt19937 gen;
// std::mt19937 keeps 624 words of state and needs roughly 2.5 KB of
// stack to seed from a seed_seq, on a display task that has 3.2 KB in
// total. A dice roll does not need that much generator.
class Rng {
public:
using result_type = uint32_t;

static constexpr result_type min() {
return 1;
}

static constexpr result_type max() {
return UINT32_MAX;
}

void Seed(result_type value) {
// Zero is the one state xorshift cannot leave again.
state = (value != 0) ? value : 0x9e3779b9;
}

result_type operator()() {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
return state;
}

private:
result_type state = 0x9e3779b9;
};

Rng gen;

std::array<lv_color_t, 3> resultColors = {LV_COLOR_YELLOW, LV_COLOR_MAGENTA, LV_COLOR_AQUA};
uint8_t currentColorIndex;
Expand Down
Loading