-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSharpMemoryLcd.cpp
More file actions
281 lines (266 loc) · 8.36 KB
/
SharpMemoryLcd.cpp
File metadata and controls
281 lines (266 loc) · 8.36 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "SharpMemoryLcd.h"
#include <avr/pgmspace.h>
// for Leonardo and Micro, use these pins
#ifdef __AVR_ATmega32U4__
static const int DISP = 2;
static const int EXTC = 7;
#else // ATmega 328 (Uno etc)
static const int DISP = 2;
static const int EXTC = 3;
#endif
static const int EXTM = 4; //this can just be pulled high on your PCB if you need the pin
static const int SI = 11;
static const int SCS = 12;
static const int SCLK = 13;
#define ARRAYSIZE 1152
const unsigned char ARDUINOBMP[ARRAYSIZE] PROGMEM = {
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,31,128,255,255,255,255,3,240,255,63,
252,255,3,0,252,255,255,127,0,0,255,63,
252,255,0,0,224,255,255,31,0,0,252,63,
252,63,0,0,192,255,255,7,0,0,248,63,
252,15,0,0,0,255,255,1,0,0,224,63,
252,7,0,0,0,254,255,0,0,0,192,63,
252,3,0,0,0,248,127,0,0,0,128,63,
252,1,192,63,0,240,63,0,248,7,0,63,
252,0,248,255,1,224,31,0,254,63,0,62,
252,0,252,255,7,192,15,128,255,127,0,60,
124,0,255,255,15,192,7,224,255,255,1,60,
124,128,255,255,31,128,3,240,255,255,3,56,
60,192,255,255,127,0,1,248,255,255,7,56,
60,192,255,255,255,0,0,252,255,255,15,48,
28,224,255,255,255,0,0,254,31,255,15,48,
28,224,255,255,255,1,0,255,31,255,31,48,
28,240,255,255,255,3,128,255,31,255,31,32,
28,240,255,255,255,7,128,255,31,255,63,32,
12,240,255,255,255,7,192,255,15,254,63,32,
12,248,15,0,254,15,224,255,0,224,63,32,
12,248,15,0,254,15,224,255,0,224,63,32,
12,248,15,0,254,15,224,255,0,224,63,32,
12,248,255,255,255,15,192,255,15,254,63,32,
12,240,255,255,255,7,192,255,31,255,63,32,
28,240,255,255,255,3,128,255,31,255,31,32,
28,240,255,255,255,3,0,255,31,255,31,48,
28,224,255,255,255,1,0,255,31,255,31,48,
28,224,255,255,255,0,0,254,255,255,15,48,
60,192,255,255,127,0,0,252,255,255,7,56,
60,128,255,255,63,0,3,248,255,255,3,56,
124,0,255,255,31,128,3,240,255,255,1,60,
124,0,254,255,7,192,7,192,255,255,0,60,
252,0,248,255,3,224,15,0,255,127,0,62,
252,1,224,127,0,240,31,0,252,15,0,63,
252,3,0,6,0,248,63,0,192,0,0,63,
252,7,0,0,0,252,127,0,0,0,192,63,
252,15,0,0,0,254,255,1,0,0,224,63,
252,31,0,0,128,255,255,3,0,0,240,63,
252,127,0,0,224,255,255,15,0,0,248,63,
252,255,1,0,248,255,255,63,0,0,255,63,
252,255,7,0,254,255,255,255,1,192,255,63,
252,255,255,240,255,255,255,255,31,254,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
252,255,255,255,255,255,255,255,255,255,255,63,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0
};
SHARPMEMORYLCD::SHARPMEMORYLCD(int16_t w, int16_t h) : Adafruit_GFX(w, h) {
pinMode(DISP, OUTPUT);
pinMode(EXTC, OUTPUT);
pinMode(EXTM, OUTPUT); //this can be ignored and you can just pull the pin high on your PCB
pinMode(SI, OUTPUT);
pinMode(SCS, OUTPUT);
pinMode(SCLK, OUTPUT);
#ifdef __AVR_ATmega32U4__
TCCR4B = TCCR4B & 0b11111000 | 0x07; //this sets the EXTC timer on pin 3. you need to change this to move pins
#else
TCCR2B = TCCR2B & 0b11111000 | 0x07; //this sets the EXTC timer on pin 3. you need to change this to move pins
#endif
// constructor(96, 96);
LcdClearBuffer();
}
SHARPMEMORYLCD::~SHARPMEMORYLCD(){/*nothing to destruct*/}
void SHARPMEMORYLCD::LcdInitialize() {
digitalWrite(EXTM,HIGH);
digitalWrite(DISP,HIGH);
delayMicroseconds(50);
}
void SHARPMEMORYLCD::LcdClearBuffer() {
for(int x = 0;x < ARRAYSIZE;x++) {
_buffer[x] = 255;
}
_invert = 0;
}
void SHARPMEMORYLCD::LcdAllClearMode() {
int i;
digitalWrite(SCS,HIGH);
LcdSetMode(LOW, HIGH);
for(i = 0; i < 14; i++){ //Send 14 bits of dummy data
digitalWrite(SI,LOW);
delayMicroseconds(3);
LcdPost(-2);
}
digitalWrite(SCS,LOW);
LcdClearBuffer();
}
void SHARPMEMORYLCD::LcdSetMode(int M0, int M2) {
int i;
digitalWrite(SI,M0);
LcdPost();
digitalWrite(SI,HIGH);
LcdPost();
digitalWrite(SI,M2);
LcdPost();
for(i = 0; i < 5; i++){ //Send 5 bits of dummy data
digitalWrite(SI,LOW);
LcdPost();
}
}
void SHARPMEMORYLCD::LcdSendPixels(int num, int state) {
int i;
for(i = 0; i < num; i++){
digitalWrite(SI,state);
LcdPost(0);
}
}
void SHARPMEMORYLCD::LcdSetLineAddress(int line) {
int i;
unsigned char tmp;
for (i = 0; i < 8; i++){
tmp = (line & 1);
if (tmp == 1){
digitalWrite(SI,HIGH);
LcdPost(0);
} else {
digitalWrite(SI,LOW);
LcdPost(0);
}
line = line >> 1;
}
}
void SHARPMEMORYLCD::LcdPrintImage(void)
{
LcdPrintImage(0,ARRAYSIZE);
}
void SHARPMEMORYLCD::LcdPrintImage(int bitmap, int arraySize)
{
int i,b;
unsigned char LineCount;
char temp;
LineCount = 1;
digitalWrite(SCS,HIGH);
LcdSetMode(HIGH,LOW);
LcdSetLineAddress(LineCount);
for(i = 0; i < arraySize; i++){
if(bitmap == 0)
temp = pgm_read_byte(&ARDUINOBMP[i]);
else if(bitmap == -1)
temp = _buffer[i];
if(_invert == 1) {
temp = ~temp;
}
LcdSendByte(temp);
if((i+1) % 12 == 0){
LcdSendPixels(8,0);
LineCount++;
if(LineCount < 97)
LcdSetLineAddress(LineCount);
}
}
delayMicroseconds(1);
digitalWrite(SCS,LOW);
}
void SHARPMEMORYLCD::LcdPost() {
LcdPost(1);
}
void SHARPMEMORYLCD::LcdPost(int delayTime) {
digitalWrite(SCLK,HIGH);
if(delayTime > 0) {
delay(delayTime);
}
if(delayTime < 0) {
delayMicroseconds(delayTime);
}
digitalWrite(SCLK,LOW);
}
void SHARPMEMORYLCD::LcdStartEXTC() {
analogWrite(EXTC,127);
}
void SHARPMEMORYLCD::LcdStopEXTC() {
analogWrite(EXTC,0);
}
void SHARPMEMORYLCD::LcdSendByte(char byteToSend) {
for(int b = 0; b < 8; b++){
if(byteToSend & 1){
digitalWrite(SI,HIGH);
delayMicroseconds(1);
LcdPost(0);
} else {
digitalWrite(SI,LOW);
delayMicroseconds(1);
LcdPost(0);
}
byteToSend = byteToSend >> 1;
}
}
void SHARPMEMORYLCD::LcdPrintBuffer() {
LcdPrintImage(-1,ARRAYSIZE);
}
void SHARPMEMORYLCD::invert(uint8_t i) {
_invert = i;
}
void SHARPMEMORYLCD::drawPixel(int16_t x, int16_t y, uint16_t color) {
int xloc = ((y*12))+x/8;
int shift = (x % 8);
if (color)
_buffer[xloc] |= (1 << shift);
else
_buffer[xloc] &= ~(1 << shift);
}