-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLCD.cpp
More file actions
236 lines (207 loc) · 5.44 KB
/
LCD.cpp
File metadata and controls
236 lines (207 loc) · 5.44 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
#include <Arduino.h>
// "XC4630d" LCD code from this demo https://www.jaycar.co.nz/rfid-keypad NOT the code on the part's download page https://www.jaycar.co.nz/240x320-lcd-touch-screen-for-arduino/p/XC4630
#include "XC4630d.h"
#include "LCD.h"
LCD lcd;
#define LCD_RD B00000001
#define LCD_WR B00000010
#define LCD_RS B00000100
#define LCD_CS B00001000
#define LCD_RST B00010000
// Preserves 10,11,12 & 13 on B
#define FastCmdByte(c) PORTC = LCD_RST | LCD_RD; PORTD = c & B11111100; PORTB = (PORTB & B11111100) | (c & B00000011);PORTC |= LCD_WR; PORTC |= LCD_RS;
#define FastCmd(c) FastCmdByte(0); FastCmdByte(c);
void LCD::init()
{
// reset
delay(250);
pinMode(A4, OUTPUT);
digitalWrite(A4, LOW);
delay(100);
digitalWrite(A4, HIGH);
delay(500);
XC4630_init();
ChipSelect(true);
#ifdef ROTATION_USB_DOWN
XC4630_rotate(3);
#else
XC4630_rotate(1);
#endif
_rotation = XC4630_orientation;
_width = XC4630_width;
_height = XC4630_height;
SERIALISE_COMMENT("*** START");
SERIALISE_INIT(_width,_height,1);
}
void LCD::ChipSelect(bool select)
{
if (select)
PORTC &= ~LCD_CS;
else
PORTC |= LCD_CS;
}
unsigned long LCD::beginFill(int x, int y, int w, int h)
{
SERIALISE_BEGINFILL(x, y, w, h);
#ifdef ROTATION_USB_DOWN
int x2 = x + w - 1;
int y2 = y + h - 1;
FastCmd(0x50); //set x bounds
FastData(x >> 8);
FastData(x);
FastCmd(0x51); //set x bounds
FastData(x2 >> 8);
FastData(x2);
FastCmd(0x52); //set y bounds
FastData(y >> 8);
FastData(y);
FastCmd(0x53); //set y bounds
FastData(y2 >> 8);
FastData(y2);
FastCmd(0x20); //set x pos
FastData(x >> 8);
FastData(x);
FastCmd(0x21); //set y pos
FastData(y >> 8);
FastData(y);
FastCmd(0x22); // Write Data to GRAM
#else
int x2 = x + w - 1;
int y2 = y + h - 1;
int tmp = x;
x = _width - x2 - 1;
x2 = _width - tmp - 1;
tmp = y;
y = _height - y2 - 1;
y2 = _height - tmp - 1;
FastCmd(0x50); //set x bounds
FastData(x >> 8);
FastData(x);
FastCmd(0x51); //set x bounds
FastData(x2 >> 8);
FastData(x2);
FastCmd(0x52); //set y bounds
FastData(y >> 8);
FastData(y);
FastCmd(0x53); //set y bounds
FastData(y2 >> 8);
FastData(y2);
FastCmd(0x20); //set x pos
FastData(x2 >> 8);
FastData(x2);
FastCmd(0x21); //set y pos
FastData(y2 >> 8);
FastData(y2);
FastCmd(0x22); // Write Data to GRAM
#endif
unsigned long count = w;
count *= h;
return count;
}
void LCD::setWindow(int x, int y, int w, int h)
{
// just defines the window
#ifdef ROTATION_USB_DOWN
int x2 = x + w - 1;
int y2 = y + h - 1;
FastCmd(0x50); //set x bounds
FastData(x >> 8);
FastData(x);
FastCmd(0x51); //set x bounds
FastData(x2 >> 8);
FastData(x2);
FastCmd(0x52); //set y bounds
FastData(y >> 8);
FastData(y);
FastCmd(0x53); //set y bounds
FastData(y2 >> 8);
FastData(y2);
#else
int x2 = x + w - 1;
int y2 = y + h - 1;
int tmp = x;
x = _width - x2 - 1;
x2 = _width - tmp - 1;
tmp = y;
y = _height - y2 - 1;
y2 = _height - tmp - 1;
FastCmd(0x50); //set x bounds
FastData(x >> 8);
FastData(x);
FastCmd(0x51); //set x bounds
FastData(x2 >> 8);
FastData(x2);
FastCmd(0x52); //set y bounds
FastData(y >> 8);
FastData(y);
FastCmd(0x53); //set y bounds
FastData(y2 >> 8);
FastData(y2);
#endif
}
void LCD::setPosition(int x, int y, int w, int h)
{
// just defines the cursor (pixel write) position
SERIALISE_BEGINFILL(x, y, w, h); // is this right?
#ifdef ROTATION_USB_DOWN
FastCmd(0x20); //set x pos
FastData(x >> 8);
FastData(x);
FastCmd(0x21); //set y pos
FastData(y >> 8);
FastData(y);
FastCmd(0x22); // Write Data to GRAM
#else
int x2 = x + w - 1;
int y2 = y + h - 1;
int tmp = x;
x = _width - x2 - 1;
x2 = _width - tmp - 1;
tmp = y;
y = _height - y2 - 1;
y2 = _height - tmp - 1;
FastCmd(0x20); //set x pos
FastData(x2 >> 8);
FastData(x2);
FastCmd(0x21); //set y pos
FastData(y2 >> 8);
FastData(y2);
FastCmd(0x22); // Write Data to GRAM
#endif
}
void LCD::fillColour(unsigned long count, word colour)
{
SERIALISE_FILLCOLOUR(count, colour);
// fill with full 16-bit colour
byte h1 = (colour >> 8) & B11111100;
byte l1 = (colour >> 8) & B00000011;
byte h2 = colour & B11111100;
byte l2 = colour & B00000011;
while (count--)
{
FastData2(h1, l1);
FastData2(h2, l2);
}
}
void LCD::fillByte(unsigned long count, byte colour)
{
SERIALISE_FILLBYTE(count, colour);
// fill with just one byte, i.e. 0/black or 255/white, or other, for pastels
PORTD = colour & B11111100;
PORTB = (PORTB & B11111100) | (colour & B00000011);
while (count--)
{
ToggleDataWR;
ToggleDataWR;
}
}
bool LCD::isTouch(int x, int y, int w,int h)
{
return XC4630_istouch(x,y,x+w,y+h);
}
bool LCD::getTouch(int& x, int& y)
{
x = XC4630_touchx();
y = XC4630_touchy();
return x >= 0 && x >= 0;
}