The right button rolls 1 die, resulting in a 1-6. The left button rolls 2 dice, resulting in 2-12. I used the Chinese Character Bitmapper app I made to get the bitmaps for the numbers, and put them in an array so they could be called in a loop.
I don't know Chinese so it was interesting to learn the Chinese number system works differently than I am used to. Zero doesn't factor into it and 10 has its own character. Then 11 and 12 use the character for 10 combined with 1 and 2, respectively. So I chose to alternate 10 + 1 and 10 + 2 to represent those numbers. In order to prevent a delay from blocking the button detection while the 10 and 1 or 2 are alternating, I used a simple timer algorithm.
The other thing I learned to do is split a 2-digit int into separate digits, in order to display the 10s or 1s part of 11 and 12. I found out that getting the modulus 10 of the number worked to get the remainder in the ones place (byte digit = num % 10;).
#include <sinobit.h>
Sinobit matrix = Sinobit();
static const uint8_t digits[][24] PROGMEM {
{
// char bitmap of 零 ling 0
0xff, 0xe0,
0x04, 0x00,
0xff, 0xe0,
0xb5, 0xa0,
0x8e, 0x20,
0x31, 0x80,
0xdf, 0x60,
0x00, 0x00,
0xff, 0xe0,
0x04, 0x20,
0x04, 0xe0,
0x00, 0x00,
},
{
// char bitmap of 一 yi 1
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0xff, 0xe0,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
},
{
// char bitmap of 二 er 2
0x00, 0x00,
0x7f, 0xc0,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0xff, 0xe0,
0x00, 0x00,
0x00, 0x00,
},
{
// char bitmap of 三 san 3
0x00, 0x00,
0xff, 0xe0,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x7f, 0xc0,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0xff, 0xe0,
0x00, 0x00,
},
{
// char bitmap of 四 si 4
0xff, 0xe0,
0x91, 0x20,
0x91, 0x20,
0x91, 0x20,
0x91, 0x20,
0x91, 0x20,
0xa1, 0xe0,
0xc0, 0x20,
0x80, 0x20,
0x80, 0x20,
0xff, 0xe0,
0x00, 0x00,
},
{
// char bitmap of 五 wu 5
0xff, 0xe0,
0x08, 0x00,
0x08, 0x00,
0x08, 0x00,
0x7f, 0x80,
0x10, 0x80,
0x10, 0x80,
0x10, 0x80,
0x10, 0x80,
0x10, 0x80,
0xff, 0xe0,
0x00, 0x00,
},
{
// char bitmap of 六 liu 6
0x04, 0x00,
0x04, 0x00,
0xff, 0xe0,
0x00, 0x00,
0x00, 0x00,
0x11, 0x00,
0x11, 0x00,
0x20, 0x80,
0x20, 0x80,
0x40, 0x40,
0x80, 0x20,
0x00, 0x00,
},
{
// char bitmap of 七 qi 7
0x20, 0x00,
0x20, 0x00,
0x20, 0xe0,
0x2f, 0x00,
0xf0, 0x00,
0x20, 0x00,
0x20, 0x00,
0x20, 0x00,
0x20, 0x20,
0x20, 0x20,
0x3f, 0xe0,
0x00, 0x00,
},
{
// char bitmap of 八 ba 8
0x11, 0x00,
0x11, 0x00,
0x11, 0x00,
0x11, 0x00,
0x11, 0x00,
0x20, 0x80,
0x20, 0x80,
0x20, 0x80,
0x40, 0x40,
0x40, 0x40,
0x80, 0x20,
0x00, 0x00,
},
{
// char bitmap of 九 jiu 9
0x10, 0x00,
0x10, 0x00,
0xff, 0x00,
0x11, 0x00,
0x11, 0x00,
0x11, 0x00,
0x11, 0x20,
0x21, 0x20,
0x21, 0x20,
0x41, 0x20,
0x81, 0xe0,
0x00, 0x00,
},
{
// char bitmap of 十 shi 10
0x04, 0x00,
0x04, 0x00,
0x04, 0x00,
0x04, 0x00,
0x04, 0x00,
0xff, 0xe0,
0x04, 0x00,
0x04, 0x00,
0x04, 0x00,
0x04, 0x00,
0x04, 0x00,
0x00, 0x00,
}
};
int wait = 500;
int num = 0;
byte dice = 0;
int lastTime = 0;
boolean firstDigit = true;
void setup() {
matrix.begin();
pinMode(SINOBIT_BUTTON_A, INPUT);
pinMode(SINOBIT_BUTTON_B, INPUT);
matrix.clearScreen();
// Serial.begin(9600);
}
void loop() {
if (!digitalRead(SINOBIT_BUTTON_A)) {
dice = 1;
roll(dice);
}
if (!digitalRead(SINOBIT_BUTTON_B)) {
dice = 2;
roll(dice);
}
displayNum();
}
void roll(byte numDice) {
byte maxRoll = 0, minRoll = 0;
switch (numDice) {
case 1:
minRoll = 1;
maxRoll = 7;
break;
case 2:
minRoll = 2;
maxRoll = 11;
break;
}
float rollTime = 1;
float rollTimeChg = .1;
for (int i = 0; i < 60; i++) {
num = int(random(minRoll, maxRoll));
matrix.clearScreen();
displayNum();
delay(rollTime);
rollTimeChg *= 1.1;
rollTime += rollTimeChg;
}
switch (numDice) {
case 1:
minRoll = 1;
maxRoll = 7;
break;
case 2:
minRoll = 2;
maxRoll = 13;
break;
}
num = int(random(minRoll, maxRoll));
matrix.clearScreen();
displayNum();
// Serial.println(num);
}
void displayNum() {
if (num < 1) {
matrix.clearScreen();
} else if (num < 11) {
matrix.drawBitmap(0, 0, digits[num], 12, 12, 1);
matrix.writeScreen();
} else {
if (lastTime + wait < millis()) {
lastTime = millis();
firstDigit = !firstDigit;
matrix.clearScreen();
}
byte digit = num % 10;
if (firstDigit) {
matrix.drawBitmap(0, 0, digits[10], 12, 12, 1);
} else {
matrix.drawBitmap(0, 0, digits[digit], 12, 12, 1);
}
matrix.writeScreen();
}
}
blah
No comments :
Post a Comment