forked from casartar/MacherDaachBadgeFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.ino
180 lines (155 loc) · 4.72 KB
/
display.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
void display() {
// LED Matrix multiplexing
static uint8_t state = 0;
if (state==0) {
digitalWrite(LED_X[7], ROW_DISABLE);
} else {
digitalWrite(LED_X[state-1], ROW_DISABLE);
}
//digitalWrite(LED_Y1, !matrix[0][state]);
setRow(~matrix[state]);
digitalWrite(LED_X[state], ROW_ENABLE);
if (state == 7) {
state = 0;
} else {
state += 1;
}
// Debouncing push buttons
static uint8_t debounce_timer_1 = 0;
static uint8_t debounce_timer_2 = 0;
uint8_t val;
if (debounce_timer_1 > 0) {
debounce_timer_1--;
} else {
val = digitalRead(button_1_Pin);
if ((button_1_state == BUTTON_INACTIVE) && (val == LOW)){
debounce_timer_1 = TIME_50_MS; // 50ms
button_1_state = BUTTON_PRESSED;
} else if (button_1_state == BUTTON_PRESSED){ // aka 50MS later ...
if (val == LOW){
button_1_state = BUTTON_HELD; // now, this counts
debounce_timer_1 = TIME_20_MS; // --> read button every 20ms
} else {
button_1_state = BUTTON_INACTIVE; // nope, just a glitch
}
} else if (button_1_state == BUTTON_HELD){
if (val == HIGH){
debounce_timer_1 = TIME_50_MS; // 50ms
button_1_state = BUTTON_RELEASED;
} else {
debounce_timer_1 = TIME_20_MS; // --> read button only every 20ms
}
} else if (button_1_state == BUTTON_RELEASED){
if (val == HIGH){
button_1_state = BUTTON_INACTIVE; // yep, it's released
} else {
button_1_state = BUTTON_HELD; // nope, just a glitch
}
}
}
if (debounce_timer_2 > 0) {
debounce_timer_2--;
} else {
val = digitalRead(button_2_Pin);
if ((button_2_state == BUTTON_INACTIVE) && (val == LOW)){
debounce_timer_2 = TIME_50_MS; // 50ms
button_2_state = BUTTON_PRESSED;
} else if (button_2_state == BUTTON_PRESSED){ // aka 50MS later ...
if (val == LOW){
button_2_state = BUTTON_HELD; // now, this counts
debounce_timer_2 = TIME_20_MS; // --> read button every 20ms while on hold
} else {
button_2_state = BUTTON_INACTIVE; // nope, just a glitch
}
} else if (button_2_state == BUTTON_HELD){
if (val == HIGH){
debounce_timer_2 = TIME_50_MS; // 50ms
button_2_state = BUTTON_RELEASED;
} else {
debounce_timer_2 = TIME_20_MS; // --> read button only every 20ms while on hold
}
} else if (button_2_state == BUTTON_RELEASED){
if (val == HIGH){
button_2_state = BUTTON_INACTIVE; // yep, it's released
} else {
button_2_state = BUTTON_HELD; // nope, just a glitch
}
}
}
static uint16_t mode_switch_timer = 0;
if (button_1_state == BUTTON_HELD
&& button_2_state == BUTTON_HELD){
mode_switch_timer++;
if (mode_switch_timer == SWITCH_TIME * TIME_1_S) {
reqModeSwitch = 1;
}
}
else{
mode_switch_timer = 0;
}
// Countdown
if (countdown > 0) countdown--;
}
// Helper functions
void clear_matrix_immediatly(){
memset(matrix, 0, 8*sizeof(*matrix));
x = 0;
y = 0;
}
// Helper functions
void clear_matrix_immediatly_without_reset(){
memset(matrix, 0, 8*sizeof(*matrix));
}
void matrixSetPixel(byte x, byte y, bool value){
bitWrite(matrix[x], y, value);
}
bool matrixGetPixel(byte x, byte y){
return bitRead(matrix[y], x);
}
void matrixShiftUp() {
for (uint8_t y=0; y<8; y++) {
matrix[y]<<=1;
}
}
void matrixShiftDown() {
for (uint8_t y=0; y<8; y++) {
matrix[y]>>=1;
}
}
void matrixShiftLeft(uint8_t newColumn) {
for (uint8_t y=0; y<7; y++) {
matrix[y]=matrix[y+1];
}
matrix[7]= newColumn;
}
void matrixShiftRight(uint8_t newColumn) {
for (uint8_t y=0; y<7; y++) {
matrix[y+1]=matrix[y];
}
matrix[0]= newColumn;
}
void setRow(uint8_t values){
uint8_t portcValues = values & B00111111; //remove the last 2 Bits from value
uint8_t portbValues = values & B11000000; //remove the first 6 Bits from value
PORTC = PORTC & B11000000; //reset PORTC but not PC6 and PC7
PORTC = portcValues | PORTC; // final PORTC values
PORTB = PORTB & B11001111; //reset PORTB but only PB4 and PB5
portbValues = portbValues >> 2 ;
PORTB = portbValues | PORTB; // final PORTB values
}
void displayCharacter(const byte* image) {
// this is just bad and needs fixing!
displayCharacterOffset(image, 0, 0);
}
void displayCharacterOffset(const byte* image, int8_t x, int8_t y) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if(((j+x) >= 0 && (j+x) < 8) && ((i+y) >= 0 && (i+y) < 8)){
// set pixel
//matrix[8-(i+y)] = pgm_read_byte_near(image + i);
matrixSetPixel(j+x,7-(i+y), bitRead(pgm_read_byte_near(image + i),7-j));
//matrix[i+y][j+x] = bitRead(image[i],7-j);
}
}
}
}