-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMDLEDShield.cpp
175 lines (159 loc) · 4.65 KB
/
SMDLEDShield.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
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
#include "SMDLEDShield.h"
EEMEM char _store[256];
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
for (int k=7; k>=0; k--) {
digitalWrite(myClockPin, 0);
if (myDataOut & (1 << k)) {
digitalWrite(myDataPin, 1);
} else {
digitalWrite(myDataPin, 0);
}
digitalWrite(myClockPin, 1);
digitalWrite(myDataPin, 0);
}
digitalWrite(myClockPin, 0);
}
PROGMEM const prog_int8_t SMDLEDShield::columns[] = {
0b01111111, 0b10111111, 0b11011111, 0b11101111,
0b11110111, 0b11111011, 0b11111101, 0b11111110
};
PROGMEM const prog_int8_t SMDLEDShield::charset[26][8] = {
{ 0x9, 0x9, 0x9, 0x9,
0xf, 0x9, 0x9, 0x6 }, // A
{ 0x7, 0x9, 0x9, 0x9,
0x7, 0x9, 0x9, 0x7 }, // B
{ 0x6, 0x9, 0x1, 0x1,
0x1, 0x1, 0x9, 0x6 }, // C
{ 0x7, 0x9, 0x9, 0x9,
0x9, 0x9, 0x9, 0x7 }, // D
{ 0xF, 0x1, 0x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EDShield::SMDLEDShield(const int _step) {
step = _step;
left_bit = 0;
eeprom_read_block(text, _store, 256);
message(text,false);
pinMode(SMDLatchPin, OUTPUT);
}
SMDLEDShield::SMDLEDShield(const int _step, const char *_text) {
step = _step;
left_bit = 0;
if(text)
message(_text);
pinMode(SMDLatchPin, OUTPUT);
}
void SMDLEDShield::message(const char *_text, bool store) {
int i, j;
int temp;
//save a private copy of the text string
strcpy(text, _text);
//save to eeprom
if (store)
eeprom_write_block(text, _store, strlen(text)+1);
buffer_width = strlen(_text)*4 + 16;
memset(buffer,0,128*8);
//Preprocess the message into font indeces
for(i=0; i< strlen(text); i++) {
if((64 < text[i]) && (text[i] < 91))//upper case
temp = text[i] - 65;
else if((96 < text[i]) && (text[i] < 123))//lower case
temp = text[i] - 97;
else //unmapped
temp = -1;
//Render the buffer
for(j=0; j<8; j++) {
buffer[j][(i/2)+1] |= (temp > -1)?pgm_read_byte(&(charset[(int)temp][j])) << 4*(i%2):0;
}
}
left_bit = 0;
//Render the initial window buffer
renderWindow();
}
void SMDLEDShield::scroll(direction d) {
shiftWindow(d);
renderWindow();
}
void SMDLEDShield::draw() {
int i;
for (int i = 0; i < 8; i++) {
//1. Disable all pixels (all rows low, all columns high)
digitalWrite(SMDLatchPin, 0);
shiftOut(SMDDataPin, SMDClockPin, 0x00);
shiftOut(SMDDataPin, SMDClockPin, 0x255);
digitalWrite(SMDLatchPin, 1);
//2. Push enabled rows (high) and enabled columns (low)
digitalWrite(SMDLatchPin, 0);
shiftOut(SMDDataPin, SMDClockPin, window[i]);
shiftOut(SMDDataPin, SMDClockPin, pgm_read_byte(&(columns[i])));
digitalWrite(SMDLatchPin, 1);
}
//Disable all pixels
digitalWrite(SMDLatchPin, 0);
shiftOut(SMDDataPin, SMDClockPin, 0x00);
shiftOut(SMDDataPin, SMDClockPin, 0x255);
digitalWrite(SMDLatchPin, 1);
}
/* private interface */
void SMDLEDShield::renderWindow() {
int i;
int byte_index, right_offset;
uint16_t *region;
byte_index = left_bit/8;
right_offset = left_bit%8;
for(i=0; i<8; i++) {
//capture the 16 bit region address
region = (uint16_t*)(&(buffer[i][byte_index]));
//dereference, shift right, and stuff the first byte into the window
window[i] = (uint8_t)(*region>>right_offset);
}
}
void SMDLEDShield::shiftWindow(direction d) {
left_bit = (left_bit + d*step)%buffer_width;
}