You are on page 1of 6

1 /*********************************************************************

2 This is an example sketch for our Monochrome Nokia 5110 LCD Displays
3
4 Pick one up today in the adafruit shop!
5 ------> http://www.adafruit.com/products/338
6
7 These displays use SPI to communicate, 4 or 5 pins are required to
8 interface
9
10 Adafruit invests time and resources providing this open source code,
11 please support Adafruit and open-source hardware by purchasing
12 products from Adafruit!
13
14 Written by Limor Fried/Ladyada for Adafruit Industries.
15 BSD license, check license.txt for more information
16 All text above, and the splash screen must be included in any redistribution
17 *********************************************************************/
18
19 #include <SPI.h>
20 #include <Adafruit_GFX.h>
21 #include <Adafruit_PCD8544.h>
22
23 // Software SPI (slower updates, more flexible pin options):
24 // pin 7 - Serial clock out (SCLK)
25 // pin 6 - Serial data out (DIN)
26 // pin 5 - Data/Command select (D/C)
27 // pin 4 - LCD chip select (CS)
28 // pin 3 - LCD reset (RST)
29 #if defined (__STM32F1__) || defined (ARDUINO_ARCH_STM32)
30 Adafruit_PCD8544 display = Adafruit_PCD8544(PB12, PA8, PB15, PB14, PB13);
31 #else
32 Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
33 #endif
34
35 // Hardware SPI (faster, but must use certain hardware pins):
36 // SCK is LCD serial clock (SCLK) - this is pin 13 on Arduino Uno
37 // MOSI is LCD DIN - this is pin 11 on an Arduino Uno
38 // pin 5 - Data/Command select (D/C)
39 // pin 4 - LCD chip select (CS)
40 // pin 3 - LCD reset (RST)
41 // For the STM32F1:
42 // MOSI - on PA7 (Maple Mini: also known as pin 4)
43 // SCK - on PA5 (Maple Mini: also known as pin 6)
44 //
45 //#if defined (__STM32F1__) || defined (ARDUINO_ARCH_STM32)
46 //Adafruit_PCD8544 display = Adafruit_PCD8544(PB15, PB14, PB13);
47 //#else
48 //Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3);
49 //#endif
50 // Note with hardware SPI MISO and SS pins aren't used but will still be read
51 // and written to during SPI transfer. Be careful sharing these pins!
52
53 #define NUMFLAKES 10
54 #define XPOS 0
55 #define YPOS 1
56 #define DELTAY 2
57
58
59 #define LOGO16_GLCD_HEIGHT 16
60 #define LOGO16_GLCD_WIDTH 16
61
62 static const unsigned char PROGMEM logo16_glcd_bmp[] =
63 { B00000000, B11000000,
64 B00000001, B11000000,
65 B00000001, B11000000,
66 B00000011, B11100000,
67 B11110011, B11100000,
68 B11111110, B11111000,
69 B01111110, B11111111,
70 B00110011, B10011111,
71 B00011111, B11111100,
72 B00001101, B01110000,
73 B00011011, B10100000,
74 B00111111, B11100000,
75 B00111111, B11110000,
76 B01111100, B11110000,
77 B01110000, B01110000,
78 B00000000, B00110000 };
79
80 void setup() {
81 Serial.begin(9600);
82
83 display.begin();
84 // init done
85
86 // you can change the contrast around to adapt the display
87 // for the best viewing!
88 display.setContrast(50);
89
90 display.display(); // show splashscreen
91 delay(2000);
92 display.clearDisplay(); // clears the screen and buffer
93
94 // draw a single pixel
95 display.drawPixel(10, 10, BLACK);
96 display.display();
97 delay(2000);
98 display.clearDisplay();
99
100 // draw many lines
101 testdrawline();
102 display.display();
103 delay(2000);
104 display.clearDisplay();
105
106 // draw rectangles
107 testdrawrect();
108 display.display();
109 delay(2000);
110 display.clearDisplay();
111
112 // draw multiple rectangles
113 testfillrect();
114 display.display();
115 delay(2000);
116 display.clearDisplay();
117
118 // draw mulitple circles
119 testdrawcircle();
120 display.display();
121 delay(2000);
122 display.clearDisplay();
123
124 // draw a circle, 10 pixel radius
125 display.fillCircle(display.width()/2, display.height()/2, 10, BLACK);
126 display.display();
127 delay(2000);
128 display.clearDisplay();
129
130 testdrawroundrect();
131 delay(2000);
132 display.clearDisplay();
133
134 testfillroundrect();
135 delay(2000);
136 display.clearDisplay();
137
138 testdrawtriangle();
139 delay(2000);
140 display.clearDisplay();
141
142 testfilltriangle();
143 delay(2000);
144 display.clearDisplay();
145
146 // draw the first ~12 characters in the font
147 testdrawchar();
148 display.display();
149 delay(2000);
150 display.clearDisplay();
151
152 // text display tests
153 display.setTextSize(1);
154 display.setTextColor(BLACK);
155 display.setCursor(0,0);
156 display.println("Hello, world!");
157 display.setTextColor(WHITE, BLACK); // 'inverted' text
158 display.println(3.141592);
159 display.setTextSize(2);
160 display.setTextColor(BLACK);
161 display.print("0x"); display.println(0xDEADBEEF, HEX);
162 display.display();
163 delay(2000);
164
165 // rotation example
166 display.clearDisplay();
167 display.setRotation(1); // rotate 90 degrees counter clockwise, can also use
values of 2 and 3 to go further.
168 display.setTextSize(1);
169 display.setTextColor(BLACK);
170 display.setCursor(0,0);
171 display.println("Rotation");
172 display.setTextSize(2);
173 display.println("Example!");
174 display.display();
175 delay(2000);
176
177 // revert back to no rotation
178 display.setRotation(0);
179
180 // miniature bitmap display
181 display.clearDisplay();
182 display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1);
183 display.display();
184
185 // invert the display
186 display.invertDisplay(true);
187 delay(1000);
188 display.invertDisplay(false);
189 delay(1000);
190
191 // draw a bitmap icon and 'animate' movement
192 testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_WIDTH, LOGO16_GLCD_HEIGHT);
193 }
194
195
196 void loop() {
197
198 }
199
200
201 void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
202 uint8_t icons[NUMFLAKES][3];
203 randomSeed(666); // whatever seed
204
205 // initialize
206 for (uint8_t f=0; f< NUMFLAKES; f++) {
207 icons[f][XPOS] = random(display.width());
208 icons[f][YPOS] = 0;
209 icons[f][DELTAY] = random(5) + 1;
210
211 Serial.print("x: ");
212 Serial.print(icons[f][XPOS], DEC);
213 Serial.print(" y: ");
214 Serial.print(icons[f][YPOS], DEC);
215 Serial.print(" dy: ");
216 Serial.println(icons[f][DELTAY], DEC);
217 }
218
219 while (1) {
220 // draw each icon
221 for (uint8_t f=0; f< NUMFLAKES; f++) {
222 display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h,
BLACK);
223 }
224 display.display();
225 delay(200);
226
227 // then erase it + move it
228 for (uint8_t f=0; f< NUMFLAKES; f++) {
229 display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h,
WHITE);
230 // move it
231 icons[f][YPOS] += icons[f][DELTAY];
232 // if its gone, reinit
233 if (icons[f][YPOS] > display.height()) {
234 icons[f][XPOS] = random(display.width());
235 icons[f][YPOS] = 0;
236 icons[f][DELTAY] = random(5) + 1;
237 }
238 }
239 }
240 }
241
242
243 void testdrawchar(void) {
244 display.setTextSize(1);
245 display.setTextColor(BLACK);
246 display.setCursor(0,0);
247
248 for (uint8_t i=0; i < 168; i++) {
249 if (i == '\n') continue;
250 display.write(i);
251 //if ((i > 0) && (i % 14 == 0))
252 //display.println();
253 }
254 display.display();
255 }
256
257 void testdrawcircle(void) {
258 for (int16_t i=0; i<display.height(); i+=2) {
259 display.drawCircle(display.width()/2, display.height()/2, i, BLACK);
260 display.display();
261 }
262 }
263
264 void testfillrect(void) {
265 uint8_t color = 1;
266 for (int16_t i=0; i<display.height()/2; i+=3) {
267 // alternate colors
268 display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
269 display.display();
270 color++;
271 }
272 }
273
274 void testdrawtriangle(void) {
275 for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5) {
276 display.drawTriangle(display.width()/2, display.height()/2-i,
277 display.width()/2-i, display.height()/2+i,
278 display.width()/2+i, display.height()/2+i, BLACK);
279 display.display();
280 }
281 }
282
283 void testfilltriangle(void) {
284 uint8_t color = BLACK;
285 for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) {
286 display.fillTriangle(display.width()/2, display.height()/2-i,
287 display.width()/2-i, display.height()/2+i,
288 display.width()/2+i, display.height()/2+i, color);
289 if (color == WHITE) color = BLACK;
290 else color = WHITE;
291 display.display();
292 }
293 }
294
295 void testdrawroundrect(void) {
296 for (int16_t i=0; i<display.height()/2-2; i+=2) {
297 display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i,
display.height()/4, BLACK);
298 display.display();
299 }
300 }
301
302 void testfillroundrect(void) {
303 uint8_t color = BLACK;
304 for (int16_t i=0; i<display.height()/2-2; i+=2) {
305 display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i,
display.height()/4, color);
306 if (color == WHITE) color = BLACK;
307 else color = WHITE;
308 display.display();
309 }
310 }
311
312 void testdrawrect(void) {
313 for (int16_t i=0; i<display.height()/2; i+=2) {
314 display.drawRect(i, i, display.width()-2*i, display.height()-2*i, BLACK);
315 display.display();
316 }
317 }
318
319 void testdrawline() {
320 for (int16_t i=0; i<display.width(); i+=4) {
321 display.drawLine(0, 0, i, display.height()-1, BLACK);
322 display.display();
323 }
324 for (int16_t i=0; i<display.height(); i+=4) {
325 display.drawLine(0, 0, display.width()-1, i, BLACK);
326 display.display();
327 }
328 delay(250);
329
330 display.clearDisplay();
331 for (int16_t i=0; i<display.width(); i+=4) {
332 display.drawLine(0, display.height()-1, i, 0, BLACK);
333 display.display();
334 }
335 for (int8_t i=display.height()-1; i>=0; i-=4) {
336 display.drawLine(0, display.height()-1, display.width()-1, i, BLACK);
337 display.display();
338 }
339 delay(250);
340
341 display.clearDisplay();
342 for (int16_t i=display.width()-1; i>=0; i-=4) {
343 display.drawLine(display.width()-1, display.height()-1, i, 0, BLACK);
344 display.display();
345 }
346 for (int16_t i=display.height()-1; i>=0; i-=4) {
347 display.drawLine(display.width()-1, display.height()-1, 0, i, BLACK);
348 display.display();
349 }
350 delay(250);
351
352 display.clearDisplay();
353 for (int16_t i=0; i<display.height(); i+=4) {
354 display.drawLine(display.width()-1, 0, 0, i, BLACK);
355 display.display();
356 }
357 for (int16_t i=0; i<display.width(); i+=4) {
358 display.drawLine(display.width()-1, 0, i, display.height()-1, BLACK);
359 display.display();
360 }
361 delay(250);
362 }
363

You might also like