Skip to content

Commit

Permalink
better random seeding and win displays
Browse files Browse the repository at this point in the history
  • Loading branch information
jhogsett committed Dec 21, 2024
1 parent a97a905 commit 02346b9
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 34 deletions.
3 changes: 3 additions & 0 deletions include/play_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <Arduino.h>
#include "buffers.h"

#define WIN_SHOW_TIMES 2
#define JACKPOT_SHOW_TIMES 3

extern void display_purse();
extern void display_win(long win);
extern void display_jackpot(long win);
Expand Down
2 changes: 1 addition & 1 deletion include/prompts.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern BillboardsHandler billboards_handler;
void run_billboard(char *data);
void billboard_prompt(voidFuncPtr on_time_out, voidFuncPtr on_press, voidFuncPtr on_long_press);
int button_led_prompt(const char * prompt, const bool *states = NULL);
void title_prompt(const char * title, byte times = 1, int show_panel_leds = false);
void title_prompt(const char * title, byte times = 1, bool show_panel_leds = false);
int panel_led_prompt();
void branch_prompt(const char * prompt, voidFuncPtr on_option_1, voidFuncPtr on_option_2, voidFuncPtr on_option_3, voidFuncPtr on_long_press = NULL, const bool *states = NULL);
int toggle_prompt(const char * prompt, const char **labels, byte current_choice, byte toggle_position, byte num_choices);
Expand Down
10 changes: 10 additions & 0 deletions include/seeding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __SEEDING_H__
#define __SEEDING_H__

#include <random_seed.h>

#define RANDOM_SEED_PIN A1

extern RandomSeed<RANDOM_SEED_PIN> randomizer;

#endif
2 changes: 1 addition & 1 deletion include/timeouts.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#define __TIMEOUTS_H__

#define PROMPT_TIMEOUT 30000L
#define SLEEP_TIMEOUT 300000L
#define IDLE_TIMEOUT 300000L

#endif
8 changes: 4 additions & 4 deletions lib/Randomizer/random_seed.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class RandomSeed

template<byte pin>
void RandomSeed<pin>::randomize(void){
int seed = analogRead(pin);
int seed = 0;
while(seed == 0)
for(byte i = 0; i < RANDOM_SEED_SAMPLES; i++)
seed = (seed << 1) ^ analogRead(0);
randomSeed(seed);
seed = (seed << 1) ^ analogRead(pin);
randomSeed(seed);
}


#endif
18 changes: 6 additions & 12 deletions src/NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// make the jackpot win more prominent


// TODO replace while(true) with a sleep timeout
// TODO rename sleep timeout idle timeout
// time game choose a color and the average is stored with it, allowing for three profiles



Expand All @@ -14,20 +10,18 @@

// last frame of scroll animation seems to skip ahead 1

// time game choose a color and the average is stored with it, allowing for three profiles

// tests - button closure time while pressed, glitchyness etc., timing of millis

// options for idle time

// setting for clock speed
// it's losing about 2 seconds a day, might be able to tweak the daily clock basis

// the clock should look different in idle mode so you know it's ok not to use a long press (or in idle mode long press to exit)

// TODO rename sleep timeout idle timeout

// TODO timer mode should time out per sleep timeout after no activity

// use randomizer on each new spin (find out how noisy it is)

// minimum reaction time (or a bug?) is 50ms

// add lm34
// option to show temp instead of seconds for clock
// option for c or f
7 changes: 4 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <Arduino.h>
#include <Wire.h>
#include <random_seed.h>
// #include <random_seed.h>
#include "buttons.h"
#include "displays.h"
#include "hardware.h"
Expand All @@ -10,12 +10,13 @@
#include "options_mode.h"
#include "play_data.h"
#include "prompts.h"
#include "seeding.h"
#include "slot_game.h"
#include "time_game.h"
#include "utils.h"

#define RANDOM_SEED_PIN A1
static RandomSeed<RANDOM_SEED_PIN> randomizer;
// #define RANDOM_SEED_PIN A1
// static RandomSeed<RANDOM_SEED_PIN> randomizer;

void setup_display(){
Wire.begin();
Expand Down
6 changes: 3 additions & 3 deletions src/play_views.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ void display_purse(){

void display_win(long win){
sprintf(display_buffer, "WIN * $%ld", win);
title_prompt(display_buffer);
title_prompt(display_buffer, WIN_SHOW_TIMES, true);
delay(ROUND_DELAY);
}

void display_jackpot(long win){
sprintf(display_buffer, "Jackpot Words * $%ld", win);
title_prompt(display_buffer);
sprintf(display_buffer, "JACKPOT *** $%ld", win);
title_prompt(display_buffer, JACKPOT_SHOW_TIMES, true);
delay(ROUND_DELAY);
}

Expand Down
10 changes: 5 additions & 5 deletions src/prompts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void billboard_prompt(voidFuncPtr on_time_out, voidFuncPtr on_press, voidFuncPtr
reset_buttons_state();

unsigned long time = millis();
unsigned long sleep_timeout = time + SLEEP_TIMEOUT;
unsigned long idle_timeout = time + IDLE_TIMEOUT;

all_leds.deactivate_leds(true);
billboards_handler.reset();
Expand All @@ -37,7 +37,7 @@ void billboard_prompt(voidFuncPtr on_time_out, voidFuncPtr on_press, voidFuncPtr
micros_to_ms(display_buffer, best_time);
sprintf(copy_buffer, load_f_string(F("Cash $%ld Best Time %s ms")), purse, display_buffer);

while ((time = millis()) < sleep_timeout) {
while ((time = millis()) < idle_timeout) {
run_billboard(copy_buffer);

if (button_pressed()) {
Expand Down Expand Up @@ -113,10 +113,10 @@ int button_led_prompt(const char * prompt, const bool *states) {

// prompt with text showing, no cycle waiting for a response
// but cancelable with a button press
void title_prompt(const char * title, byte times, int show_panel_leds) {
void title_prompt(const char * title, byte times, bool show_panel_leds) {
unsigned long time = millis();
unsigned long timeout_time = time + PROMPT_TIMEOUT;
unsigned long sleep_timeout = time + SLEEP_TIMEOUT;
unsigned long idle_timeout = time + IDLE_TIMEOUT;

if (show_panel_leds)
panel_leds.begin(millis(), TITLE_PANEL_LEDS_STYLE, TITLE_PANEL_LEDS_SHOW_TIME, TITLE_PANEL_LEDS_BLANK_TIME);
Expand All @@ -134,7 +134,7 @@ void title_prompt(const char * title, byte times, int show_panel_leds) {
all_leds.deactivate_leds(true);

// breaking out of the loop is handled by the display call
while ((time = millis()) < sleep_timeout) {
while ((time = millis()) < idle_timeout) {
if (display.loop_scroll_string(time, title, DISPLAY_SHOW_TIME, DISPLAY_SCROLL_TIME)) {
if (show_panel_leds)
panel_leds.step(time);
Expand Down
3 changes: 3 additions & 0 deletions src/seeding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "seeding.h"

RandomSeed<RANDOM_SEED_PIN> randomizer;
13 changes: 10 additions & 3 deletions src/slot_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "prompts.h"
#include "timeouts.h"
#include "utils.h"
#include "seeding.h"
#include "slot_game.h"

byte choice1, choice2, choice3;
Expand Down Expand Up @@ -39,6 +40,7 @@ void slots_round(bool rude){
if(running1){
running1 = disp1.loop_scroll_string(time, text, SLOTS_SHOW_TIME, SLOTS_SCROLL_TIME);
if(!running1){
randomizer.randomize();
choice1 = random(NUM_WORDS);
disp1.show_string(words[choice1]);
}
Expand All @@ -47,6 +49,7 @@ void slots_round(bool rude){
if(running2){
running2 = disp2.loop_scroll_string(time, text, SLOTS_SHOW_TIME, SLOTS_SCROLL_TIME);
if(!running2){
randomizer.randomize();
choice2 = random(NUM_WORDS);
disp2.show_string(words[choice2]);
}
Expand All @@ -55,6 +58,7 @@ void slots_round(bool rude){
if(running3){
running3 = disp3.loop_scroll_string(time, text, SLOTS_SHOW_TIME, SLOTS_SCROLL_TIME);
if(!running3){
randomizer.randomize();
choice3 = random(NUM_WORDS);
disp3.show_string(words[choice3]);
}
Expand All @@ -81,8 +85,11 @@ bool jackpot_words_chosen(byte word1, byte word2, byte word3){
void slots_game(){
title_prompt(load_f_string(F("Silly Slots")), TITLE_SHOW_TIMES, true);

randomizer.randomize();
byte jackpot_choice1 = random(NUM_WORDS);
randomizer.randomize();
byte jackpot_choice2 = random(NUM_WORDS);
randomizer.randomize();
byte jackpot_choice3 = random(NUM_WORDS);

bool rude;
Expand All @@ -103,10 +110,10 @@ void slots_game(){
break;
}

unsigned long sleep_timeout = millis() + SLEEP_TIMEOUT;
unsigned long idle_timeout = millis() + IDLE_TIMEOUT;
unsigned long time;

while((time = millis()) < sleep_timeout){
while((time = millis()) < idle_timeout){
bet_amounts[BET_ALL] = purse;
sprintf(display_buffer, load_f_string(F("Bet %s Back")), standard_bet_str(current_bet));
const bool states[] = {false, true, false, false};
Expand All @@ -129,7 +136,7 @@ void slots_game(){
return;
}

sleep_timeout = millis() + SLEEP_TIMEOUT;
idle_timeout = millis() + IDLE_TIMEOUT;

int win = 0;
bool jackpot = false;
Expand Down
6 changes: 4 additions & 2 deletions src/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool decrement_timer(byte &second, byte &minute, byte &hour, int seconds, int mi
// TODO timer mode should time out per sleep timeout after no activity
void timer_prompt(byte seconds, byte minutes, byte hours) {
unsigned long time = millis();
unsigned long sleep_timeout = time + SLEEP_TIMEOUT;
unsigned long idle_timeout = time + IDLE_TIMEOUT;

// unsigned long next_second = millis() + 1000;
unsigned long next_second = 0;
Expand All @@ -56,7 +56,7 @@ void timer_prompt(byte seconds, byte minutes, byte hours) {
render_timer_string(timer_second, timer_minute, timer_hour, running);
display.show_string(display_buffer);

while ((time = millis()) < sleep_timeout) {
while ((time = millis()) < idle_timeout) {
if (running && time >= next_second) {
if (going_up)
increment_timer(timer_second, timer_minute, timer_hour);
Expand All @@ -76,6 +76,8 @@ void timer_prompt(byte seconds, byte minutes, byte hours) {
if (long_press_state == 1) {
return;
} else {
idle_timeout = time + IDLE_TIMEOUT;

if (button_states[GREEN_ID]) {
if (!running) {
running = true;
Expand Down

0 comments on commit 02346b9

Please sign in to comment.