root / trunk / code / projects / colonet / utilities / robot_wireless_relay / oled.c @ 13
History | View | Annotate | Download (8.6 KB)
1 | 13 | emarinel | #ifdef FFPP
|
---|---|---|---|
2 | #include "firefly+_lib.h" |
||
3 | /******************************************************************************/
|
||
4 | /* OLED example. 8 bit mode */
|
||
5 | /* Spark Fun Electronics 5/3/06 OSO */
|
||
6 | /******************************************************************************/
|
||
7 | /* */
|
||
8 | /* STOLEN and hacked to work with SPI, AVR */
|
||
9 | /******************************************************************************/
|
||
10 | |||
11 | // data bus for LCD, pins on port 0
|
||
12 | //#define D0 16
|
||
13 | //#define D1 17
|
||
14 | //#define D2 18
|
||
15 | //#define D3 19
|
||
16 | //#define D4 20
|
||
17 | //#define D5 21
|
||
18 | //#define D6 22
|
||
19 | //#define D7 23
|
||
20 | |||
21 | // OLED data port
|
||
22 | //#define LCD_DATA 0x00FF0000
|
||
23 | |||
24 | // other OLED pins
|
||
25 | //#define LCD_RW 0x00000080
|
||
26 | #define LCD_RS PB4 // Command/Data |
||
27 | //#define LCD_RD 0x00008000
|
||
28 | #define LCD_RSTB PE2 // reset line |
||
29 | #define LCD_CS PB0
|
||
30 | |||
31 | |||
32 | |||
33 | void drawcircle(char col, char row, char radius, int lcolor, int fcolor) { |
||
34 | write_c(0x86);
|
||
35 | write_d(col); |
||
36 | write_d(row); |
||
37 | write_d(radius); |
||
38 | write_d(lcolor>>8);
|
||
39 | write_d(lcolor&0xf);
|
||
40 | write_d(fcolor>>8);
|
||
41 | write_d(fcolor&0xf);
|
||
42 | } |
||
43 | |||
44 | void drawrect(char scol, char srow, char ecol, char erow, int lcolor, int fcolor) { |
||
45 | write_c(0x84);
|
||
46 | write_d((ecol>scol)?scol:ecol); |
||
47 | write_d((erow>srow)?srow:erow); |
||
48 | write_d((ecol<scol)?scol:ecol); |
||
49 | write_d((erow<srow)?srow:erow); |
||
50 | write_d(lcolor>>8);
|
||
51 | write_d(lcolor&0xf);
|
||
52 | write_d(fcolor>>8);
|
||
53 | write_d(fcolor&0xf);
|
||
54 | } |
||
55 | |||
56 | void drawline(char scol, char srow, char ecol, char erow, int lcolor) { |
||
57 | write_c(0x83);
|
||
58 | write_d(scol); |
||
59 | write_d(srow); |
||
60 | write_d(ecol); |
||
61 | write_d(erow); |
||
62 | write_d(lcolor>>8);
|
||
63 | write_d(lcolor&0xf);
|
||
64 | } |
||
65 | |||
66 | void drawcopy(char scol, char srow, char ecol, char erow, char ncol, char nrow) { |
||
67 | write_c(0x8A);
|
||
68 | write_d((ecol>scol)?scol:ecol); |
||
69 | write_d((erow>srow)?srow:erow); |
||
70 | write_d((ecol<scol)?scol:ecol); |
||
71 | write_d((erow<srow)?srow:erow); |
||
72 | write_d(ncol); |
||
73 | write_d(nrow); |
||
74 | } |
||
75 | |||
76 | void drawfill(char fill) { |
||
77 | write_c(0x92); // fill enable command |
||
78 | write_d(0x01&fill);
|
||
79 | } |
||
80 | |||
81 | void drawclearwindow(char scol, char srow, char ecol, char erow) { |
||
82 | write_c(0x8e); // clear window command |
||
83 | write_d(scol); |
||
84 | write_d(srow); |
||
85 | write_d(ecol); |
||
86 | write_d(erow); |
||
87 | } |
||
88 | |||
89 | inline void drawclear(void) { |
||
90 | drawclearwindow(0,0,130,130); |
||
91 | } |
||
92 | |||
93 | void drawscroll(char hoff, char srow, char rows, char speed) { |
||
94 | write_c(0x96);
|
||
95 | write_d(hoff); |
||
96 | write_d(srow); |
||
97 | write_d((srow+rows>132)?132:rows); |
||
98 | write_d(0x00);
|
||
99 | write_d(speed&0x3);
|
||
100 | |||
101 | } |
||
102 | |||
103 | inline void drawscrollstart(void) { |
||
104 | write_c(0x9F);
|
||
105 | } |
||
106 | |||
107 | inline void drawscrollstop(void) { |
||
108 | write_c(0x9E);
|
||
109 | } |
||
110 | |||
111 | #define ROBOSIZE 4 |
||
112 | |||
113 | #include "rangefinder.h" |
||
114 | |||
115 | void drawIR(void) { |
||
116 | /* char d;
|
||
117 | enable_IR();
|
||
118 | drawclear();
|
||
119 | delay_ms(500);
|
||
120 | drawfill(1);
|
||
121 | |||
122 | delay_ms(10);
|
||
123 | |||
124 | drawcircle(64,64-ROBOSIZE,ROBOSIZE,C_BLUE,C_BLUE);
|
||
125 | delay_ms(20);
|
||
126 | drawcircle(64,64+ROBOSIZE,ROBOSIZE,C_BLUE,C_BLUE);
|
||
127 | delay_ms(20);
|
||
128 | drawrect(64-ROBOSIZE,64-ROBOSIZE,64+ROBOSIZE,64+ROBOSIZE,C_BLUE,C_BLUE);
|
||
129 | delay_ms(20);
|
||
130 | |||
131 | while(1){
|
||
132 | |||
133 | d=(convert_IR_distance(read_IR_val(1))-100)>>4;
|
||
134 | if(d>63-ROBOSIZE*2){
|
||
135 | d=0;//63-ROBOSIZE*2;
|
||
136 | }
|
||
137 | // delay_ms(100);
|
||
138 | |||
139 | drawclearwindow(63, 0, 65, 63-ROBOSIZE*2);
|
||
140 | drawline(64,63-ROBOSIZE*2-d,64,63-ROBOSIZE*2,C_RED);
|
||
141 | delay_ms(5);
|
||
142 | |||
143 | |||
144 | |||
145 | |||
146 | |||
147 | d=(convert_IR_distance(read_IR_val(0))-100)/11;
|
||
148 | if(d>62-ROBOSIZE){
|
||
149 | d=0;//64-ROBOSIZE*2;
|
||
150 | }
|
||
151 | // delay_ms(100);
|
||
152 | |||
153 | drawclearwindow(0, 0, 63-ROBOSIZE, 63-ROBOSIZE);
|
||
154 | delay_ms(5);
|
||
155 | drawline(63-ROBOSIZE-d,63-ROBOSIZE-d,63-ROBOSIZE,63-ROBOSIZE,C_RED);
|
||
156 | //delay_ms(25);
|
||
157 | delay_ms(5);
|
||
158 | |||
159 | |||
160 | |||
161 | // d=(convert_IR_distance(read_IR_val(2))-100)/11;
|
||
162 | // if(d>64){
|
||
163 | // d=0;//64-ROBOSIZE*2;
|
||
164 | // }
|
||
165 | // delay_ms(100);
|
||
166 | |||
167 | // drawclearwindow(65+ROBOSIZE,0, 128,128);
|
||
168 | // delay_ms(5);
|
||
169 | // drawline(128-(65+ROBOSIZE+d),128-(65+ROBOSIZE-d),65+ROBOSIZE,(65-ROBOSIZE),C_RED);
|
||
170 | // drawline(64,64-d,64+d,64,C_RED);
|
||
171 | //delay_ms(25);
|
||
172 | // delay_ms(5);
|
||
173 | |||
174 | |||
175 | |||
176 | |||
177 | |||
178 | |||
179 | d=(convert_IR_distance(read_IR_val(4))-100)>>4;
|
||
180 | if(d>64-ROBOSIZE){
|
||
181 | d=0;//64-ROBOSIZE;
|
||
182 | }
|
||
183 | // delay_ms(100);
|
||
184 | |||
185 | drawclearwindow(0, 63+ROBOSIZE, 63-ROBOSIZE, 65+ROBOSIZE);
|
||
186 | drawline(64-ROBOSIZE-d,64+ROBOSIZE,63-ROBOSIZE,64+ROBOSIZE,C_RED);
|
||
187 | delay_ms(5);
|
||
188 | |||
189 | d=(convert_IR_distance(read_IR_val(3))-100)>>4;
|
||
190 | if(d>128-65-ROBOSIZE){
|
||
191 | d=0;//128-65-ROBOSIZE;
|
||
192 | }
|
||
193 | // delay_ms(100);
|
||
194 | |||
195 | drawclearwindow(65+ROBOSIZE, 63+ROBOSIZE, 127, 65+ROBOSIZE);
|
||
196 | drawline(65+ROBOSIZE,64+ROBOSIZE,65+ROBOSIZE+d,64+ROBOSIZE,C_RED);
|
||
197 | delay_ms(5);
|
||
198 | |||
199 | }
|
||
200 | */
|
||
201 | } |
||
202 | |||
203 | void crazycircle (void) { |
||
204 | int i;
|
||
205 | // draw 100 random circles
|
||
206 | for(i = 0;i < 100;i++){ |
||
207 | /* write_c(0x86); // draw circle command
|
||
208 | write_d(rand() % 130);
|
||
209 | write_d(rand() % 130);
|
||
210 | write_d(rand() % 64);
|
||
211 | write_d(rand());
|
||
212 | write_d(rand());
|
||
213 | write_d(rand());
|
||
214 | write_d(rand());
|
||
215 | */
|
||
216 | drawcircle(rand() % 130,rand() % 130,rand() %64, rand(),rand()); |
||
217 | |||
218 | delay_ms(10);
|
||
219 | } |
||
220 | } |
||
221 | |||
222 | void crazyrect (void) { |
||
223 | int i;
|
||
224 | // draw 100 random circles
|
||
225 | for(i = 0;i < 100;i++){ |
||
226 | |||
227 | drawrect(rand() % 130,rand() % 130,rand() % 130,rand() % 130, rand(),rand()); |
||
228 | |||
229 | delay_ms(10);
|
||
230 | } |
||
231 | } |
||
232 | |||
233 | void OLEDtest (void) { |
||
234 | //int i = 0;
|
||
235 | |||
236 | // Initialize
|
||
237 | //Initialize();
|
||
238 | OLED_init(); |
||
239 | |||
240 | delay_ms(120);
|
||
241 | |||
242 | drawclear(); |
||
243 | |||
244 | delay_ms(100);
|
||
245 | |||
246 | drawfill(1);
|
||
247 | |||
248 | delay_ms(10);
|
||
249 | |||
250 | // crazycircle();
|
||
251 | |||
252 | // delay_ms(500);
|
||
253 | |||
254 | // drawscroll(1,0,50,1);
|
||
255 | //drawscrollstart();
|
||
256 | //delay_ms(1000);
|
||
257 | //drawscrollstop();
|
||
258 | //drawcopy(0,0,100,100,50,50);
|
||
259 | // delay_ms(100);
|
||
260 | // drawfill(0);
|
||
261 | //drawcircle(64,64,16,C_RED,C_GREEN);
|
||
262 | // drawcircle(64,64,15,C_RED,C_GREEN);
|
||
263 | // drawcircle(64,64,14,C_RED,C_GREEN);
|
||
264 | |||
265 | drawIR(); |
||
266 | /*
|
||
267 | // write directly to ram, this fills up bottom 1/3 of display with color pattern
|
||
268 | write_c(0x5c);
|
||
269 | for (i = 0; i < 2000; i++){
|
||
270 | write_c(0x5c);
|
||
271 | write_d(i);
|
||
272 | write_d(i);
|
||
273 | write_d(i);
|
||
274 | }
|
||
275 | */
|
||
276 | } |
||
277 | |||
278 | /**********************************************************
|
||
279 | Initialize
|
||
280 | **********************************************************/
|
||
281 | |||
282 | void OLED_init(void) |
||
283 | { |
||
284 | // Setup SPI here
|
||
285 | SPCR = 0x5D; // enable SPI, master, SPI mode 3 |
||
286 | DDRB |= 0x06; // enable MOSI and SCK as outputs |
||
287 | |||
288 | LCD_out(0);
|
||
289 | DC(0);
|
||
290 | CS(0);
|
||
291 | Reset_SSD1339(); |
||
292 | write_c(0xa0); // Set Re-map / Color Depth |
||
293 | write_d(0x34);//0xb4); // 262K 8bit R->G->B |
||
294 | write_c(0xa1); // Set display start line |
||
295 | write_d(0x00); // 00h start |
||
296 | //write_c(0xa2); // Set display offset
|
||
297 | //write_d(0x80); // 80h start
|
||
298 | write_c(0xA6); // Normal display |
||
299 | write_c(0xad); // Set Master Configuration |
||
300 | write_d(0x8e); // DC-DC off & external VcomH voltage & external pre-charge voltage |
||
301 | write_c(0xb0); // Power saving mode |
||
302 | write_d(0x05);
|
||
303 | write_c(0xb1); // Set pre & dis_charge |
||
304 | write_d(0x11); // pre=1h dis=1h |
||
305 | write_c(0xb3); // clock & frequency |
||
306 | write_d(0xf0); // clock=Divser+1 frequency=fh |
||
307 | write_c(0xbb); // Set pre-charge voltage of color A B C |
||
308 | write_d(0xff); // color A was 1c |
||
309 | write_d(0xff); // color B was 1c |
||
310 | write_d(0xff); // color C was 1c |
||
311 | write_c(0xbe); // Set VcomH |
||
312 | write_d(0x1f); // |
||
313 | write_c(0xc1); // Set contrast current for A B C |
||
314 | write_d(0xCa); // Color A was AA |
||
315 | write_d(0xD4); // Color B was B4 |
||
316 | write_d(0xF8); // Color C was C8 |
||
317 | write_c(0xc7); // Set master contrast |
||
318 | write_d(0x0f); // no change |
||
319 | write_c(0xca); // Duty |
||
320 | write_d(0x7f); // 127+1 |
||
321 | write_c(0xaf); // Display on |
||
322 | } |
||
323 | |||
324 | void Reset_SSD1339(void) |
||
325 | { |
||
326 | RES(0);
|
||
327 | delay_ms(100);
|
||
328 | RES(1);
|
||
329 | } |
||
330 | |||
331 | void write_c(unsigned char out_command) |
||
332 | { |
||
333 | DC(0);CS(0); |
||
334 | //delay_ms(1);
|
||
335 | |||
336 | LCD_out(out_command); |
||
337 | // delay_ms(1);
|
||
338 | |||
339 | DC(1);
|
||
340 | // potentially add NOP
|
||
341 | CS(1);
|
||
342 | |||
343 | } |
||
344 | |||
345 | void write_d(unsigned char out_data) |
||
346 | { |
||
347 | DC(1);CS(0); |
||
348 | // delay_ms(1);
|
||
349 | |||
350 | LCD_out(out_data); |
||
351 | // delay_ms(1);
|
||
352 | DC(1);
|
||
353 | //potentially add NOP
|
||
354 | CS(1);
|
||
355 | } |
||
356 | |||
357 | // these functions set / clear pins for LCD control lines. they accecpt a 0 or 1
|
||
358 | void DC(char stat) |
||
359 | { |
||
360 | DDRB |= _BV(LCD_RS); // RS is P0.30, set to output
|
||
361 | if (stat)
|
||
362 | PORTB |= _BV(LCD_RS); |
||
363 | else
|
||
364 | PORTB &= ~(_BV(LCD_RS)); |
||
365 | } |
||
366 | |||
367 | void RES(char stat) |
||
368 | { |
||
369 | DDRE |= _BV(LCD_RSTB); // RSTB is P0.7, set to output
|
||
370 | if (stat)
|
||
371 | PORTE |= _BV(LCD_RSTB); |
||
372 | else
|
||
373 | PORTE &= ~(_BV(LCD_RSTB)); |
||
374 | } |
||
375 | |||
376 | void CS(char stat) |
||
377 | { |
||
378 | DDRB |= _BV(LCD_CS); // RSTB is P0.7, set to output
|
||
379 | if (stat)
|
||
380 | PORTB |= _BV(LCD_CS); |
||
381 | else
|
||
382 | PORTB &= ~(_BV(LCD_CS)); |
||
383 | } |
||
384 | |||
385 | // send to the LCD
|
||
386 | void LCD_out(unsigned char cmd) |
||
387 | { |
||
388 | SPDR = cmd; |
||
389 | // could also do this via interrupts
|
||
390 | loop_until_bit_is_set(SPSR, SPIF); |
||
391 | } |
||
392 | |||
393 | |||
394 | #endif |