-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotmatrix.ino
132 lines (122 loc) · 3.46 KB
/
dotmatrix.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "Dot.h"
const int DIRECT[] = {1, 2, 3, 4, 5, 6};
const int REGISTER[] = {7, 8, 9, 10, 11, 12}; //bに登録する
const int ANODE[] = {1, 3, 10, 7, 8};
const int CATHODE[] = {12, 11, 2, 9, 4, 5, 6}; //0で通電
const int BASE_BIT = B001011;
const int REGISTER_PIN_HEAD = 7;
void resetDotMatrix () {
digitalWrite(MAT[1], 0);
digitalWrite(MAT[2], 1);
digitalWrite(MAT[3], 0);
digitalWrite(MAT[4], 1);
digitalWrite(MAT[5], 1);
digitalWrite(MAT[6], 1);
reg(BASE_BIT);
}
void makeDisplay(Dot (*_clear)[COL], Dot (*_failure)[COL]) {
int _rowC[] = {0, 1, 3, 4, 2, 0, 1, 3, 4, 0, 0, 1, 2, 3, 4, 0};
int _colC[] = {0, 0, 0, 0, 1, 2, 2, 2, 2, 4, 5, 5, 5, 5, 5, 6};
for (int i=0; i<16; i++) {
_clear[_rowC[i]][_colC[i]].setOpen();
}
int _rowF[] = {0, 1, 2, 3, 4, 0, 4, 0, 1, 2, 3, 4, 0, 2, 3, 4, 0, 4, 0, 1, 2, 3, 4};
int _colF[] = {0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 6};
for (int i=0; i<22; i++) {
_failure[_rowF[i]][_colF[i]].setOpen();
}
}
void dotMatrix (Dot (*arrays)[COL], int count) {
int b = BASE_BIT;
b ^= writeAnode(ANODE[count]);
for (int j=0; j<COL; j++) {
b ^= writeCathode(CATHODE[j], arrays[count][j]);
}
reg(b);
}
int writeAnode (int pin) {
if (isValue(DIRECT, pin)) {
digitalWrite(MAT[pin], 1);
return 0;
}
else if (isValue(REGISTER, pin)) {
int result = B100000;
result = result >> (pin-REGISTER_PIN_HEAD);
return result;
}
}
int writeCathode (int pin, Dot dot) {
if (isValue(DIRECT, pin)) {
if (dot.getIsSelected()) {
if ((millis()%1000 <= 899)) digitalWrite(MAT[pin], 0);
else digitalWrite(MAT[pin], 1);
}
else if (dot.getIsFlag()) {
if (millis()%500 <= 249) digitalWrite(MAT[pin], 0);
else digitalWrite(MAT[pin], 1);
}
else if (dot.getState()==OPEN) {
digitalWrite(MAT[pin], 0);
}
else {
digitalWrite(MAT[pin], 1);
}
return 0;
}
else if (isValue(REGISTER, pin)) {
int result;
if (dot.getIsSelected()) {
if (millis()%1000 <= 899) result = B100000;
else result = B000000;
}
else if (dot.getIsFlag()) {
if (millis()%500 <= 249) result = B100000;
else result = B000000;
}
else if (dot.getState()==OPEN) {
result = B100000;
}
else {
result = B000000;
}
result >>= (pin-REGISTER_PIN_HEAD);
return result;
}
}
void reg (int b) {
digitalWrite(REG_LATCH, LOW);
shiftOut(REG_SER, REG_CLK, LSBFIRST, b);
digitalWrite(REG_LATCH, HIGH);
}
bool isValue (int *array, int value) {
for (int i=0; i<NUM_PIN_ONESIDE; i++) {
if (array[i]==value) {
return true;
}
}
return false;
}
void printField (Dot (*arrays)[COL]) {
for (int i=0; i<ROW; i++) {
for (int j=COL-1; j>=0; j--) {
if (arrays[i][j].getIsSelected()) {
Serial.print("SE");
}
else if (arrays[i][j].getIsFlag()) {
Serial.print("FL");
}
else if (arrays[i][j].getState()==OPEN) {
Serial.print("OP");
}
else if (arrays[i][j].getState()==CLOSE) {
Serial.print("CL");
}
else if (arrays[i][j].getState()==MINE) {
Serial.print("MI");
}
Serial.print(" ");
}
Serial.println("");
}
Serial.println("============================");
}