-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.cpp
121 lines (90 loc) · 2.68 KB
/
display.cpp
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
#include "display.h"
Display::Display() {
memset(screen,0, sizeof(screen));
memset(output_ports, 0, sizeof(output_ports));
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
digitalWrite(latchPin, HIGH);
for (int i = FIRST_PIN; i <= LAST_PIN; i++) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
disable_rows();
out_byte(0, 0xff);
out_byte(1, 0xff);
}
bool* Display::getScreen() {
return &(screen[0][0]);
}
void Display::putPoint(int col, int row, boolean value) {
screen[col][row] = value;
}
boolean Display::getPoint(int col, int row) {
return screen[col][row];
}
void Display::clear() {
memset(screen,0, sizeof(screen));
}
volatile static int inside = 0;
void Display::printLine(int row_num) {
if (inside == 1) {
return;
}
inside=1;
byte binary[MEMORY_BLOCKS];
memset(binary,0, sizeof(binary));
for (int i = 0; i < COLS; i++) {
byte mask = (1 << (7 - (i % 8)));
if (screen[row_num][i] == true) {
binary[i / 8] |= mask;
}
else {
binary[i / 8] &= ~mask;
}
}
disable_rows();
for (int i = 0; i < MEMORY_BLOCKS; i++) {
out_byte(i, ~(binary[i])); // надо поставить 0 чтобы зажечь светодиод, т.к тут мы управляем катодом (-)
}
enable_row(row_num);
inside=0;
}
void Display::disable_rows() {
for (int i = 0; i < 3; i++) {
out_byte(FIRST_ROW_ADDR + i, 0xff);
}
}
void Display::enable_row(int r) {
int desired_block = r / ROW_BLOCK_SIZE;
out_byte(FIRST_ROW_ADDR + desired_block, ~(1 << (7-(r % ROW_BLOCK_SIZE)) ));
}
byte Display::reverse(byte b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
void Display::out_byte(int bank_number, byte b) {
// Serial.print(b, BIN);
// Serial.println(" to bank "+ String(bank_number));
if (output_ports[bank_number] == b) {
return;
}
output_ports[bank_number] = b;
set_addr_line(0);
b = reverse(b);
/* PORTB = (b >> 2);
PORTD &= B00111111;
PORTD |= (b & B11000000);
*/
for (int i = 0; i < 8; i++) {
digitalWrite( FIRST_PIN + i, (b & (1 << i)) ? HIGH : LOW);
}
set_addr_line(1 << bank_number);
}
void Display::set_addr_line(int addr_line) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, addr_line);
digitalWrite(latchPin, HIGH);
}