-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ino
109 lines (93 loc) · 2.04 KB
/
main.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <TM1637Display.h>
#define PINDTWENTY 7
#define PINDTWELVE 6
#define PINDTEN 5
#define PINDEIGHT 4
#define PINDSIX 3
#define PINDTFOUR 2
#define PINCLK 9
#define PINDIO 8
#define NO_SELECTION 0
#define SWITCH_DEFAULT -1
TM1637Display Display;
int Selected;
const uint8_t LetterD[] = {
0b01011110
};
const uint8_t LoadingFrames[] = {
SEG_F,
SEG_A,
SEG_B,
SEG_C,
SEG_D,
SEG_E
};
const uint8_t BlankScreen[] = {
0x00,
0x00,
0x00,
0x00
};
void setup()
{
Display = new TM1637Display(PINCLK, PINDIO)
Display.setBrightness(0x0f);
clearScreen();
Selected = NO_SELECTION;
randomSeed(analogRead(0));
}
void loop()
{
requestAnimationFrame(millis());
}
void requestAnimationFrame(long mtime)
{
int currentDice = getSelectedDice();
//Switch status same as last frame
if (currentDice == Selected)
{
Display.setSegments(LetterD + 0, 1, 0);
Display.showNumberDec(Selected, false, (Selected > 9) ? 2 : 1, 1);
loadingIndicator(mtime);
}
//Switch status changed
else if (currentDice != SWITCH_DEFAULT)
{
clearScreen();
Selected = currentDice;
}
//Switch released and selection was made
else if (Selected > NO_SELECTION)
{
Display.showNumberDec(random(Selected) + 1, false, 4);
Selected = NO_SELECTION;
}
}
int getSelectedDice()
{
int dice = checkDice(PINDTWENTY, 20);
dice = dice == SWITCH_DEFAULT ? checkDice(PINDTWELVE, 12) : dice;
dice = dice == SWITCH_DEFAULT ? checkDice(PINDTEN, 10) : dice;
dice = dice == SWITCH_DEFAULT ? checkDice(PINDEIGHT, 8) : dice;
dice = dice == SWITCH_DEFAULT ? checkDice(PINDSIX, 6) : dice;
dice = dice == SWITCH_DEFAULT ? checkDice(PINDFOUR, 4) : dice;
return dice;
}
int checkDice(int pin, int range)
{
int switchState = digitalRead(pin);
if (switchState == HIGH)
{
return range;
}
return SWITCH_DEFAULT;
}
void clearScreen()
{
Display.setSegments(BlankScreen + 0);
}
void loadingIndicator(long mtime)
{
int frame = (mtime / (1000 / 30)) % 6;
Display.setSegments(LoadingFrames + frame, 1, 3);
}