Skip to content

Commit d29cbd6

Browse files
committedSep 3, 2015
Initial Commit
1 parent cdea012 commit d29cbd6

File tree

7 files changed

+724
-0
lines changed

7 files changed

+724
-0
lines changed
 

‎README.md

+26
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
# Arduino-Dice-Board
2+
Arduino Dice Switchboard
3+
4+
5+
Description
6+
------------
7+
Dice board software for use with the TM1637 7-Segment Display and the Arduino Uno. Rolls the selected dice based on the switch and displays it on screen. This uses external libraries to handle the display module.
8+
9+
Installation
10+
------------
11+
If using the arduino IDE:
12+
Copy TM1637-master to your arduino libraries, make sure that it appears as a contributed library
13+
14+
On the circuit board:
15+
Set up the display CLK & DIO to pins 8 & 9, respectively.
16+
Wire inputs to digital pins 2 through 7.
17+
18+
Schematic
19+
------------
20+
Coming Soon
21+
22+
Usage
23+
-------------
24+
Pressing a switch will display the selected dice on screen. Releasing the switch will then display a random number based on the selection.
25+
26+
27+
Thanks to avishorp for the great [library](https://github.com/avishorp/TM1637) they provided for the TM1637.

‎TM1637-master/LICENSE

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
GNU LESSER GENERAL PUBLIC LICENSE
2+
Version 3, 29 June 2007
3+
4+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5+
Everyone is permitted to copy and distribute verbatim copies
6+
of this license document, but changing it is not allowed.
7+
8+
9+
This version of the GNU Lesser General Public License incorporates
10+
the terms and conditions of version 3 of the GNU General Public
11+
License, supplemented by the additional permissions listed below.
12+
13+
0. Additional Definitions.
14+
15+
As used herein, "this License" refers to version 3 of the GNU Lesser
16+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
17+
General Public License.
18+
19+
"The Library" refers to a covered work governed by this License,
20+
other than an Application or a Combined Work as defined below.
21+
22+
An "Application" is any work that makes use of an interface provided
23+
by the Library, but which is not otherwise based on the Library.
24+
Defining a subclass of a class defined by the Library is deemed a mode
25+
of using an interface provided by the Library.
26+
27+
A "Combined Work" is a work produced by combining or linking an
28+
Application with the Library. The particular version of the Library
29+
with which the Combined Work was made is also called the "Linked
30+
Version".
31+
32+
The "Minimal Corresponding Source" for a Combined Work means the
33+
Corresponding Source for the Combined Work, excluding any source code
34+
for portions of the Combined Work that, considered in isolation, are
35+
based on the Application, and not on the Linked Version.
36+
37+
The "Corresponding Application Code" for a Combined Work means the
38+
object code and/or source code for the Application, including any data
39+
and utility programs needed for reproducing the Combined Work from the
40+
Application, but excluding the System Libraries of the Combined Work.
41+
42+
1. Exception to Section 3 of the GNU GPL.
43+
44+
You may convey a covered work under sections 3 and 4 of this License
45+
without being bound by section 3 of the GNU GPL.
46+
47+
2. Conveying Modified Versions.
48+
49+
If you modify a copy of the Library, and, in your modifications, a
50+
facility refers to a function or data to be supplied by an Application
51+
that uses the facility (other than as an argument passed when the
52+
facility is invoked), then you may convey a copy of the modified
53+
version:
54+
55+
a) under this License, provided that you make a good faith effort to
56+
ensure that, in the event an Application does not supply the
57+
function or data, the facility still operates, and performs
58+
whatever part of its purpose remains meaningful, or
59+
60+
b) under the GNU GPL, with none of the additional permissions of
61+
this License applicable to that copy.
62+
63+
3. Object Code Incorporating Material from Library Header Files.
64+
65+
The object code form of an Application may incorporate material from
66+
a header file that is part of the Library. You may convey such object
67+
code under terms of your choice, provided that, if the incorporated
68+
material is not limited to numerical parameters, data structure
69+
layouts and accessors, or small macros, inline functions and templates
70+
(ten or fewer lines in length), you do both of the following:
71+
72+
a) Give prominent notice with each copy of the object code that the
73+
Library is used in it and that the Library and its use are
74+
covered by this License.
75+
76+
b) Accompany the object code with a copy of the GNU GPL and this license
77+
document.
78+
79+
4. Combined Works.
80+
81+
You may convey a Combined Work under terms of your choice that,
82+
taken together, effectively do not restrict modification of the
83+
portions of the Library contained in the Combined Work and reverse
84+
engineering for debugging such modifications, if you also do each of
85+
the following:
86+
87+
a) Give prominent notice with each copy of the Combined Work that
88+
the Library is used in it and that the Library and its use are
89+
covered by this License.
90+
91+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
92+
document.
93+
94+
c) For a Combined Work that displays copyright notices during
95+
execution, include the copyright notice for the Library among
96+
these notices, as well as a reference directing the user to the
97+
copies of the GNU GPL and this license document.
98+
99+
d) Do one of the following:
100+
101+
0) Convey the Minimal Corresponding Source under the terms of this
102+
License, and the Corresponding Application Code in a form
103+
suitable for, and under terms that permit, the user to
104+
recombine or relink the Application with a modified version of
105+
the Linked Version to produce a modified Combined Work, in the
106+
manner specified by section 6 of the GNU GPL for conveying
107+
Corresponding Source.
108+
109+
1) Use a suitable shared library mechanism for linking with the
110+
Library. A suitable mechanism is one that (a) uses at run time
111+
a copy of the Library already present on the user's computer
112+
system, and (b) will operate properly with a modified version
113+
of the Library that is interface-compatible with the Linked
114+
Version.
115+
116+
e) Provide Installation Information, but only if you would otherwise
117+
be required to provide such information under section 6 of the
118+
GNU GPL, and only to the extent that such information is
119+
necessary to install and execute a modified version of the
120+
Combined Work produced by recombining or relinking the
121+
Application with a modified version of the Linked Version. (If
122+
you use option 4d0, the Installation Information must accompany
123+
the Minimal Corresponding Source and Corresponding Application
124+
Code. If you use option 4d1, you must provide the Installation
125+
Information in the manner specified by section 6 of the GNU GPL
126+
for conveying Corresponding Source.)
127+
128+
5. Combined Libraries.
129+
130+
You may place library facilities that are a work based on the
131+
Library side by side in a single library together with other library
132+
facilities that are not Applications and are not covered by this
133+
License, and convey such a combined library under terms of your
134+
choice, if you do both of the following:
135+
136+
a) Accompany the combined library with a copy of the same work based
137+
on the Library, uncombined with any other library facilities,
138+
conveyed under the terms of this License.
139+
140+
b) Give prominent notice with the combined library that part of it
141+
is a work based on the Library, and explaining where to find the
142+
accompanying uncombined form of the same work.
143+
144+
6. Revised Versions of the GNU Lesser General Public License.
145+
146+
The Free Software Foundation may publish revised and/or new versions
147+
of the GNU Lesser General Public License from time to time. Such new
148+
versions will be similar in spirit to the present version, but may
149+
differ in detail to address new problems or concerns.
150+
151+
Each version is given a distinguishing version number. If the
152+
Library as you received it specifies that a certain numbered version
153+
of the GNU Lesser General Public License "or any later version"
154+
applies to it, you have the option of following the terms and
155+
conditions either of that published version or of any later version
156+
published by the Free Software Foundation. If the Library as you
157+
received it does not specify a version number of the GNU Lesser
158+
General Public License, you may choose any version of the GNU Lesser
159+
General Public License ever published by the Free Software Foundation.
160+
161+
If the Library as you received it specifies that a proxy can decide
162+
whether future versions of the GNU Lesser General Public License shall
163+
apply, that proxy's public statement of acceptance of any version is
164+
permanent authorization for you to choose that version for the
165+
Library.

‎TM1637-master/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
TM1637
2+
======
3+
Arduino library for TM1637 (LED Driver)
4+
5+
6+
Description
7+
-----------
8+
An Arduino library for 7-segment display modules based on the TM1637 chip, such as Seeed Studio's [Grove 4 digit display](http://www.seeedstudio.com/depot/grove-4digit-display-p-1198.html). The TM1637 chip also has keyboard input capability, but it's not implemented in this library.
9+
10+
Hardware Connection
11+
-------------------
12+
The display modules has two signal connection (and two power connections) which are CLK and DIO. These pins can be connected to any pair of digital pins on the Arduino. When an object is created, the pins should be configured. There is no limitaion on the number of instances used concurrently (as long as each instance has a pin pair of its own)
13+
14+
Installation
15+
------------
16+
The library is installed as any Arduino library, by copying the files into a directory on the library search path of the Arduino IDE
17+
18+
Usage
19+
-----
20+
The library provides a single class named TM1637Display. An instance of this class provides the following functions:
21+
22+
* `setSegments` - Sets the raw value of the segments of each digit
23+
* `showNumberDec` - Displays a decimal number
24+
* `setBrightness` - Sets the brightness of the display
25+
26+
The information given above is only a summary. Please refer to TM1637Display.h for more information. An example is included, demonstarting the operation of most of the functions.
27+

‎TM1637-master/TM1637Display.cpp

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
2+
// Author: avishorp@gmail.com
3+
//
4+
// This library is free software; you can redistribute it and/or
5+
// modify it under the terms of the GNU Lesser General Public
6+
// License as published by the Free Software Foundation; either
7+
// version 2.1 of the License, or (at your option) any later version.
8+
//
9+
// This library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
// Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public
15+
// License along with this library; if not, write to the Free Software
16+
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
18+
extern "C" {
19+
#include <stdlib.h>
20+
#include <string.h>
21+
#include <inttypes.h>
22+
}
23+
24+
#include <TM1637Display.h>
25+
#include <Arduino.h>
26+
27+
#define TM1637_I2C_COMM1 0x40
28+
#define TM1637_I2C_COMM2 0xC0
29+
#define TM1637_I2C_COMM3 0x80
30+
31+
//
32+
// A
33+
// ---
34+
// F | | B
35+
// -G-
36+
// E | | C
37+
// ---
38+
// D
39+
const uint8_t digitToSegment[] = {
40+
// XGFEDCBA
41+
0b00111111, // 0
42+
0b00000110, // 1
43+
0b01011011, // 2
44+
0b01001111, // 3
45+
0b01100110, // 4
46+
0b01101101, // 5
47+
0b01111101, // 6
48+
0b00000111, // 7
49+
0b01111111, // 8
50+
0b01101111, // 9
51+
0b01110111, // A
52+
0b01111100, // b
53+
0b00111001, // C
54+
0b01011110, // d
55+
0b01111001, // E
56+
0b01110001 // F
57+
};
58+
59+
60+
TM1637Display::TM1637Display(uint8_t pinClk, uint8_t pinDIO)
61+
{
62+
// Copy the pin numbers
63+
m_pinClk = pinClk;
64+
m_pinDIO = pinDIO;
65+
66+
// Set the pin direction and default value.
67+
// Both pins are set as inputs, allowing the pull-up resistors to pull them up
68+
pinMode(m_pinClk, INPUT);
69+
pinMode(m_pinDIO,INPUT);
70+
digitalWrite(m_pinClk, LOW);
71+
digitalWrite(m_pinDIO, LOW);
72+
}
73+
74+
void TM1637Display::setBrightness(uint8_t brightness)
75+
{
76+
m_brightness = brightness;
77+
}
78+
79+
void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos)
80+
{
81+
// Write COMM1
82+
start();
83+
writeByte(TM1637_I2C_COMM1);
84+
stop();
85+
86+
// Write COMM2 + first digit address
87+
start();
88+
writeByte(TM1637_I2C_COMM2 + (pos & 0x03));
89+
90+
// Write the data bytes
91+
for (uint8_t k=0; k < length; k++)
92+
writeByte(segments[k]);
93+
94+
stop();
95+
96+
// Write COMM3 + brightness
97+
start();
98+
writeByte(TM1637_I2C_COMM3 + (m_brightness & 0x0f));
99+
stop();
100+
}
101+
102+
void TM1637Display::showNumberDec(int num, bool leading_zero, uint8_t length, uint8_t pos)
103+
{
104+
uint8_t digits[4];
105+
const static int divisors[] = { 1, 10, 100, 1000 };
106+
bool leading = true;
107+
108+
for(int8_t k = 0; k < 4; k++) {
109+
int divisor = divisors[4 - 1 - k];
110+
int d = num / divisor;
111+
112+
if (d == 0) {
113+
if (leading_zero || !leading || (k == 3))
114+
digits[k] = encodeDigit(d);
115+
else
116+
digits[k] = 0;
117+
}
118+
else {
119+
digits[k] = encodeDigit(d);
120+
num -= d * divisor;
121+
leading = false;
122+
}
123+
}
124+
125+
setSegments(digits + (4 - length), length, pos);
126+
}
127+
128+
void TM1637Display::bitDelay()
129+
{
130+
delayMicroseconds(50);
131+
}
132+
133+
void TM1637Display::start()
134+
{
135+
pinMode(m_pinDIO, OUTPUT);
136+
bitDelay();
137+
}
138+
139+
void TM1637Display::stop()
140+
{
141+
pinMode(m_pinDIO, OUTPUT);
142+
bitDelay();
143+
pinMode(m_pinClk, INPUT);
144+
bitDelay();
145+
pinMode(m_pinDIO, INPUT);
146+
bitDelay();
147+
}
148+
149+
bool TM1637Display::writeByte(uint8_t b)
150+
{
151+
uint8_t data = b;
152+
153+
// 8 Data Bits
154+
for(uint8_t i = 0; i < 8; i++) {
155+
// CLK low
156+
pinMode(m_pinClk, OUTPUT);
157+
bitDelay();
158+
159+
// Set data bit
160+
if (data & 0x01)
161+
pinMode(m_pinDIO, INPUT);
162+
else
163+
pinMode(m_pinDIO, OUTPUT);
164+
165+
bitDelay();
166+
167+
// CLK high
168+
pinMode(m_pinClk, INPUT);
169+
bitDelay();
170+
data = data >> 1;
171+
}
172+
173+
// Wait for acknowledge
174+
// CLK to zero
175+
pinMode(m_pinClk, OUTPUT);
176+
pinMode(m_pinDIO, INPUT);
177+
bitDelay();
178+
179+
// CLK to high
180+
pinMode(m_pinClk, INPUT);
181+
bitDelay();
182+
uint8_t ack = digitalRead(m_pinDIO);
183+
if (ack == 0)
184+
pinMode(m_pinDIO, OUTPUT);
185+
186+
187+
bitDelay();
188+
pinMode(m_pinClk, OUTPUT);
189+
bitDelay();
190+
191+
return ack;
192+
}
193+
194+
uint8_t TM1637Display::encodeDigit(uint8_t digit)
195+
{
196+
return digitToSegment[digit & 0x0f];
197+
}
198+
199+
200+

‎TM1637-master/TM1637Display.h

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Author: avishorp@gmail.com
2+
//
3+
// This library is free software; you can redistribute it and/or
4+
// modify it under the terms of the GNU Lesser General Public
5+
// License as published by the Free Software Foundation; either
6+
// version 2.1 of the License, or (at your option) any later version.
7+
//
8+
// This library is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
// Lesser General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public
14+
// License along with this library; if not, write to the Free Software
15+
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
17+
#ifndef __TM1637DISPLAY__
18+
#define __TM1637DISPLAY__
19+
20+
#include <inttypes.h>
21+
22+
#define SEG_A 0b00000001
23+
#define SEG_B 0b00000010
24+
#define SEG_C 0b00000100
25+
#define SEG_D 0b00001000
26+
#define SEG_E 0b00010000
27+
#define SEG_F 0b00100000
28+
#define SEG_G 0b01000000
29+
30+
class TM1637Display {
31+
32+
public:
33+
//! Initialize a TM1637Display object, setting the clock and
34+
//! data pins.
35+
//!
36+
//! @param pinClk - The number of the digital pin connected to the clock pin of the module
37+
//! @param pinDIO - The number of the digital pin connected to the DIO pin of the module
38+
TM1637Display(uint8_t pinClk, uint8_t pinDIO);
39+
40+
//! Sets the brightness of the display.
41+
//!
42+
//! The setting takes effect when a command is given to change the data being
43+
//! displayed.
44+
//!
45+
//! @param brightness A number from 0 (lowes brightness) to 7 (highest brightness)
46+
void setBrightness(uint8_t brightness);
47+
48+
//! Display arbitrary data on the module
49+
//!
50+
//! This function receives raw segment values as input and displays them. The segment data
51+
//! is given as a byte array, each byte corresponding to a single digit. Within each byte,
52+
//! bit 0 is segment A, bit 1 is segment B etc.
53+
//! The function may either set the entire display or any desirable part on its own. The first
54+
//! digit is given by the @ref pos argument with 0 being the leftmost digit. The @ref length
55+
//! argument is the number of digits to be set. Other digits are not affected.
56+
//!
57+
//! @param segments An array of size @ref length containing the raw segment values
58+
//! @param length The number of digits to be modified
59+
//! @param pos The position from which to start the modification (0 - leftmost, 3 - rightmost)
60+
void setSegments(const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0);
61+
62+
//! Displayes a decimal number
63+
//!
64+
//! Dispalyes the given argument as a decimal number
65+
//!
66+
//! @param num The number to be shown
67+
//! @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are
68+
//! blank
69+
//! @param length The number of digits to set. The user must ensure that the number to be shown
70+
//! fits to the number of digits requested (for example, if two digits are to be displayed,
71+
//! the number must be between 0 to 99)
72+
//! @param pos The position least significant digit (0 - leftmost, 3 - rightmost)
73+
void showNumberDec(int num, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
74+
75+
//! Translate a single digit into 7 segment code
76+
//!
77+
//! The method accepts a number between 0 - 15 and converts it to the
78+
//! code required to display the number on a 7 segment display.
79+
//! Numbers between 10-15 are converted to hexadecimal digits (A-F)
80+
//!
81+
//! @param digit A number between 0 to 15
82+
//! @return A code representing the 7 segment image of the digit (LSB - segment A;
83+
//! bit 6 - segment G; bit 7 - always zero)
84+
uint8_t encodeDigit(uint8_t digit);
85+
86+
protected:
87+
void bitDelay();
88+
89+
void start();
90+
91+
void stop();
92+
93+
bool writeByte(uint8_t b);
94+
95+
private:
96+
uint8_t m_pinClk;
97+
uint8_t m_pinDIO;
98+
uint8_t m_brightness;
99+
};
100+
101+
#endif // __TM1637DISPLAY__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <Arduino.h>
2+
#include <TM1637Display.h>
3+
4+
// Module connection pins (Digital Pins)
5+
#define CLK 2
6+
#define DIO 3
7+
8+
// The amount of time (in milliseconds) between tests
9+
#define TEST_DELAY 2000
10+
11+
const uint8_t SEG_DONE[] = {
12+
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
13+
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O
14+
SEG_C | SEG_E | SEG_G, // n
15+
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E
16+
};
17+
18+
TM1637Display display(CLK, DIO);
19+
20+
void setup()
21+
{
22+
}
23+
24+
void loop()
25+
{
26+
int k;
27+
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
28+
display.setBrightness(0x0f);
29+
30+
// All segments on
31+
display.setSegments(data);
32+
delay(TEST_DELAY);
33+
34+
// Selectively set different digits
35+
data[0] = 0b01001001;
36+
data[1] = display.encodeDigit(1);
37+
data[2] = display.encodeDigit(2);
38+
data[3] = display.encodeDigit(3);
39+
40+
for(k = 3; k >= 0; k--) {
41+
display.setSegments(data, 1, k);
42+
delay(TEST_DELAY);
43+
}
44+
45+
display.setSegments(data+2, 2, 2);
46+
delay(TEST_DELAY);
47+
48+
display.setSegments(data+2, 2, 1);
49+
delay(TEST_DELAY);
50+
51+
display.setSegments(data+1, 3, 1);
52+
delay(TEST_DELAY);
53+
54+
55+
// Show decimal numbers with/without leading zeros
56+
bool lz = false;
57+
for (uint8_t z = 0; z < 2; z++) {
58+
for(k = 0; k < 10000; k += k*4 + 7) {
59+
display.showNumberDec(k, lz);
60+
delay(TEST_DELAY);
61+
}
62+
lz = true;
63+
}
64+
65+
// Show decimal number whose length is smaller than 4
66+
for(k = 0; k < 4; k++)
67+
data[k] = 0;
68+
display.setSegments(data);
69+
70+
display.showNumberDec(153, false, 3, 1);
71+
delay(TEST_DELAY);
72+
display.showNumberDec(22, false, 2, 2);
73+
delay(TEST_DELAY);
74+
display.showNumberDec(0, true, 1, 3);
75+
delay(TEST_DELAY);
76+
display.showNumberDec(0, true, 1, 2);
77+
delay(TEST_DELAY);
78+
display.showNumberDec(0, true, 1, 1);
79+
delay(TEST_DELAY);
80+
display.showNumberDec(0, true, 1, 0);
81+
delay(TEST_DELAY);
82+
83+
// Brightness Test
84+
for(k = 0; k < 4; k++)
85+
data[k] = 0xff;
86+
for(k = 0; k < 16; k++) {
87+
display.setBrightness(k);
88+
display.setSegments(data);
89+
delay(TEST_DELAY);
90+
}
91+
92+
// Done!
93+
display.setSegments(SEG_DONE);
94+
95+
while(1);
96+
}

‎main.ino

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

0 commit comments

Comments
 (0)
Please sign in to comment.