Cleaned up for RGB/BGR selection
[hackover2013-badge-firmware.git] / drivers / lcd / bitmap / ssd1306 / ssd1306.c
1 /**************************************************************************/
2 /*!
3 @file ssd1306.c
4 @author K. Townsend (microBuilder.eu)
5
6 @section DESCRIPTION
7
8 Driver for 128x64 OLED display based on the SSD1306 controller.
9
10 This driver is based on the SSD1306 Library from Limor Fried
11 (Adafruit Industries) at: https://github.com/adafruit/SSD1306
12
13 @section LICENSE
14
15 Software License Agreement (BSD License)
16
17 Copyright (c) 2012, microBuilder SARL
18 All rights reserved.
19
20 Redistribution and use in source and binary forms, with or without
21 modification, are permitted provided that the following conditions are met:
22 1. Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
24 2. Redistributions in binary form must reproduce the above copyright
25 notice, this list of conditions and the following disclaimer in the
26 documentation and/or other materials provided with the distribution.
27 3. Neither the name of the copyright holders nor the
28 names of its contributors may be used to endorse or promote products
29 derived from this software without specific prior written permission.
30
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
32 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
35 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42 /**************************************************************************/
43 #include <string.h>
44
45 #include "ssd1306.h"
46
47 #include "core/gpio/gpio.h"
48 #include "core/systick/systick.h"
49 #include "drivers/lcd/smallfonts.h"
50
51 void ssd1306SendByte(uint8_t byte);
52
53 #define CMD(c) do { gpioSetValue( SSD1306_CS_PORT, SSD1306_CS_PIN, 1 ); \
54 gpioSetValue( SSD1306_DC_PORT, SSD1306_DC_PIN, 0 ); \
55 gpioSetValue( SSD1306_CS_PORT, SSD1306_CS_PIN, 0 ); \
56 ssd1306SendByte( c ); \
57 gpioSetValue( SSD1306_CS_PORT, SSD1306_CS_PIN, 1 ); \
58 } while (0);
59 #define DATA(c) do { gpioSetValue( SSD1306_CS_PORT, SSD1306_CS_PIN, 1 ); \
60 gpioSetValue( SSD1306_DC_PORT, SSD1306_DC_PIN, 1 ); \
61 gpioSetValue( SSD1306_CS_PORT, SSD1306_CS_PIN, 0 ); \
62 ssd1306SendByte( c ); \
63 gpioSetValue( SSD1306_CS_PORT, SSD1306_CS_PIN, 1 ); \
64 } while (0);
65 #define DELAY(mS) do { systickDelay( mS / CFG_SYSTICK_DELAY_IN_MS ); } while(0);
66
67 uint8_t _ssd1306buffer[SSD1306_LCDWIDTH * SSD1306_LCDHEIGHT / 8];
68
69 /**************************************************************************/
70 /* Private Methods */
71 /**************************************************************************/
72
73 /**************************************************************************/
74 /*!
75 @brief Simulates an SPI write using GPIO
76
77 @param[in] byte
78 The byte to send
79 */
80 /**************************************************************************/
81 void ssd1306SendByte(uint8_t byte)
82 {
83 int8_t i;
84
85 // Make sure clock pin starts high
86 gpioSetValue(SSD1306_SCLK_PORT, SSD1306_SCLK_PIN, 1);
87
88 // Write from MSB to LSB
89 for (i=7; i>=0; i--)
90 {
91 // Set clock pin low
92 gpioSetValue(SSD1306_SCLK_PORT, SSD1306_SCLK_PIN, 0);
93 // Set data pin high or low depending on the value of the current bit
94 gpioSetValue(SSD1306_SDAT_PORT, SSD1306_SDAT_PIN, byte & (1 << i) ? 1 : 0);
95 // Set clock pin high
96 gpioSetValue(SSD1306_SCLK_PORT, SSD1306_SCLK_PIN, 1);
97 }
98 }
99
100 /**************************************************************************/
101 /*!
102 @brief Draws a single graphic character using the supplied font
103 */
104 /**************************************************************************/
105 static void ssd1306DrawChar(uint16_t x, uint16_t y, uint8_t c, struct FONT_DEF font)
106 {
107 uint8_t col, column[font.u8Width];
108
109 // Make sure we are exceeding the display limits
110 // This also gets checked in ssd1306DrawPixel, but if we start
111 // outside the limits we can avoid some unecessary work at the outset
112 if ((x > SSD1306_LCDWIDTH) || (y > SSD1306_LCDHEIGHT))
113 return;
114
115 // Check if the requested character is available
116 if ((c >= font.u8FirstChar) && (c <= font.u8LastChar))
117 {
118 // Retrieve appropriate columns from font data
119 for (col = 0; col < font.u8Width; col++)
120 {
121 column[col] = font.au8FontTable[((c - 32) * font.u8Width) + col]; // Get first column of appropriate character
122 }
123 }
124 else
125 {
126 // Requested character is not available in this font ... send a space instead
127 for (col = 0; col < font.u8Width; col++)
128 {
129 column[col] = 0xFF; // Send solid space
130 }
131 }
132
133 // Render each column
134 uint16_t xoffset, yoffset;
135 for (xoffset = 0; xoffset < font.u8Width; xoffset++)
136 {
137 for (yoffset = 0; yoffset < (font.u8Height + 1); yoffset++)
138 {
139 uint8_t bit = 0x00;
140 bit = (column[xoffset] << (8 - (yoffset + 1))); // Shift current row bit left
141 bit = (bit >> 7); // Shift current row but right (results in 0x01 for black, and 0x00 for white)
142 if (bit)
143 {
144 ssd1306DrawPixel(x + xoffset, y + yoffset);
145 }
146 }
147 }
148 }
149
150 /**************************************************************************/
151 /* Public Methods */
152 /**************************************************************************/
153
154 /**************************************************************************/
155 /*!
156 @brief Initialises the SSD1306 LCD display
157 */
158 /**************************************************************************/
159 void ssd1306Init(uint8_t vccstate)
160 {
161 // Set all pins to output
162 gpioSetDir(SSD1306_SCLK_PORT, SSD1306_SCLK_PIN, gpioDirection_Output);
163 gpioSetDir(SSD1306_SDAT_PORT, SSD1306_SDAT_PIN, gpioDirection_Output);
164 gpioSetDir(SSD1306_DC_PORT, SSD1306_DC_PIN, gpioDirection_Output);
165 gpioSetDir(SSD1306_RST_PORT, SSD1306_RST_PIN, gpioDirection_Output);
166 gpioSetDir(SSD1306_CS_PORT, SSD1306_CS_PIN, gpioDirection_Output);
167
168 // Reset the LCD
169 gpioSetValue(SSD1306_RST_PORT, SSD1306_RST_PIN, 1);
170 DELAY(1);
171 gpioSetValue(SSD1306_RST_PORT, SSD1306_RST_PIN, 0);
172 DELAY(10);
173 gpioSetValue(SSD1306_RST_PORT, SSD1306_RST_PIN, 1);
174
175 #if defined SSD1306_128_32
176 // Init sequence taken from datasheet for UG-2832HSWEG04 (128x32 OLED module)
177 CMD(SSD1306_DISPLAYOFF); // 0xAE
178 CMD(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
179 CMD(0x80); // the suggested ratio 0x80
180 CMD(SSD1306_SETMULTIPLEX); // 0xA8
181 CMD(0x1F);
182 CMD(SSD1306_SETDISPLAYOFFSET); // 0xD3
183 CMD(0x0); // no offset
184 CMD(SSD1306_SETSTARTLINE | 0x0); // line #0
185 CMD(SSD1306_CHARGEPUMP); // 0x8D
186 if (vccstate == SSD1306_EXTERNALVCC)
187 { CMD(0x10) }
188 else
189 { CMD(0x14) }
190 CMD(SSD1306_SEGREMAP | 0x1);
191 CMD(SSD1306_COMSCANDEC);
192 CMD(SSD1306_SETCOMPINS); // 0xDA
193 CMD(0x02);
194 CMD(SSD1306_SETCONTRAST); // 0x81
195 if (vccstate == SSD1306_EXTERNALVCC)
196 { CMD(0x9F) }
197 else
198 { CMD(0xCF) }
199 CMD(SSD1306_SETPRECHARGE); // 0xd9
200 if (vccstate == SSD1306_EXTERNALVCC)
201 { CMD(0x22) }
202 else
203 { CMD(0xF1) }
204 CMD(SSD1306_SETVCOMDETECT); // 0xDB
205 CMD(0x40); // 0x20 is default?
206 CMD(SSD1306_DISPLAYALLON_RESUME); // 0xA4
207 CMD(SSD1306_NORMALDISPLAY); // 0xA6
208 #endif
209
210 #if defined SSD1306_128_64
211 // Init sequence taken from datasheet for UG-2864HSWEG01 (128x64 OLED module)
212 CMD(SSD1306_DISPLAYOFF); // 0xAE
213 CMD(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5
214 CMD(0x80); // the suggested ratio 0x80
215 CMD(SSD1306_SETMULTIPLEX); // 0xA8
216 CMD(0x3F);
217 CMD(SSD1306_SETDISPLAYOFFSET); // 0xD3
218 CMD(0x0); // no offset
219 CMD(SSD1306_SETSTARTLINE | 0x0); // line #0
220 CMD(SSD1306_CHARGEPUMP); // 0x8D
221 if (vccstate == SSD1306_EXTERNALVCC)
222 { CMD(0x10) }
223 else
224 { CMD(0x14) }
225 CMD(SSD1306_SEGREMAP | 0x1);
226 CMD(SSD1306_COMSCANDEC);
227 CMD(SSD1306_SETCOMPINS); // 0xDA
228 CMD(0x12);
229 CMD(SSD1306_SETCONTRAST); // 0x81
230 if (vccstate == SSD1306_EXTERNALVCC)
231 { CMD(0x9F) }
232 else
233 { CMD(0xCF) }
234 CMD(SSD1306_SETPRECHARGE); // 0xd9
235 if (vccstate == SSD1306_EXTERNALVCC)
236 { CMD(0x22) }
237 else
238 { CMD(0xF1) }
239 CMD(SSD1306_SETVCOMDETECT); // 0xDB
240 CMD(0x40); // 0x20 is default?
241 CMD(SSD1306_DISPLAYALLON_RESUME); // 0xA4
242 CMD(SSD1306_NORMALDISPLAY); // 0xA6
243 #endif
244
245 // Enabled the OLED panel
246 CMD(SSD1306_DISPLAYON);
247 }
248
249 /**************************************************************************/
250 /*!
251 @brief Draws a single pixel in image buffer
252
253 @param[in] x
254 The x position (0..127)
255 @param[in] y
256 The y position (0..63)
257 */
258 /**************************************************************************/
259 void ssd1306DrawPixel(uint8_t x, uint8_t y)
260 {
261 if ((x >= SSD1306_LCDWIDTH) || (y >= SSD1306_LCDHEIGHT))
262 return;
263
264 _ssd1306buffer[x+ (y/8)*SSD1306_LCDWIDTH] |= (1 << y%8);
265 }
266
267 /**************************************************************************/
268 /*!
269 @brief Clears a single pixel in image buffer
270
271 @param[in] x
272 The x position (0..127)
273 @param[in] y
274 The y position (0..63)
275 */
276 /**************************************************************************/
277 void ssd1306ClearPixel(uint8_t x, uint8_t y)
278 {
279 if ((x >= SSD1306_LCDWIDTH) || (y >= SSD1306_LCDHEIGHT))
280 return;
281
282 _ssd1306buffer[x+ (y/8)*SSD1306_LCDWIDTH] &= ~(1 << y%8);
283 }
284
285 /**************************************************************************/
286 /*!
287 @brief Gets the value (1 or 0) of the specified pixel from the buffer
288
289 @param[in] x
290 The x position (0..127)
291 @param[in] y
292 The y position (0..63)
293
294 @return 1 if the pixel is enabled, 0 if disabled
295 */
296 /**************************************************************************/
297 uint8_t ssd1306GetPixel(uint8_t x, uint8_t y)
298 {
299 if ((x >= SSD1306_LCDWIDTH) || (y >=SSD1306_LCDHEIGHT)) return 0;
300 return _ssd1306buffer[x+ (y/8)*SSD1306_LCDWIDTH] & (1 << y%8) ? 1 : 0;
301 }
302
303 /**************************************************************************/
304 /*!
305 @brief Clears the screen
306 */
307 /**************************************************************************/
308 void ssd1306ClearScreen()
309 {
310 memset(_ssd1306buffer, 0, 1024);
311 }
312
313 /**************************************************************************/
314 /*!
315 @brief Renders the contents of the pixel buffer on the LCD
316 */
317 /**************************************************************************/
318 void ssd1306Refresh(void)
319 {
320 CMD(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0
321 CMD(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0
322 CMD(SSD1306_SETSTARTLINE | 0x0); // line #0
323
324 uint16_t i;
325 for (i=0; i<1024; i++)
326 {
327 DATA(_ssd1306buffer[i]);
328 }
329 }
330
331 /**************************************************************************/
332 /*!
333 @brief Draws a string using the supplied font data.
334
335 @param[in] x
336 Starting x co-ordinate
337 @param[in] y
338 Starting y co-ordinate
339 @param[in] text
340 The string to render
341 @param[in] font
342 Pointer to the FONT_DEF to use when drawing the string
343
344 @section Example
345
346 @code
347
348 #include "drivers/lcd/bitmap/ssd1306/ssd1306.h"
349 #include "drivers/lcd/smallfonts.h"
350
351 // Configure the pins and initialise the LCD screen
352 ssd1306Init(SSD1306_INTERNALVCC);
353
354 // Render some text on the screen
355 ssd1306DrawString(1, 10, "5x8 System", Font_System5x8);
356 ssd1306DrawString(1, 20, "7x8 System", Font_System7x8);
357
358 // Refresh the screen to see the results
359 ssd1306Refresh();
360
361 @endcode
362 */
363 /**************************************************************************/
364 void ssd1306DrawString(uint16_t x, uint16_t y, char* text, struct FONT_DEF font)
365 {
366 uint8_t l;
367 for (l = 0; l < strlen(text); l++)
368 {
369 ssd1306DrawChar(x + (l * (font.u8Width + 1)), y, text[l], font);
370 }
371 }
372
373 /**************************************************************************/
374 /*!
375 @brief Shifts the contents of the frame buffer up the specified
376 number of pixels
377
378 @param[in] height
379 The number of pixels to shift the frame buffer up, leaving
380 a blank space at the bottom of the frame buffer x pixels
381 high
382
383 @section Example
384
385 @code
386
387 #include "drivers/lcd/bitmap/ssd1306/ssd1306.h"
388 #include "drivers/lcd/smallfonts.h"
389
390 // Configure the pins and initialise the LCD screen
391 ssd1306Init();
392
393 // Enable the backlight
394 ssd1306BLEnable();
395
396 // Continually write some text, scrolling upward one line each time
397 while (1)
398 {
399 // Shift the buffer up 8 pixels (adjust for font-height)
400 ssd1306ShiftFrameBuffer(8);
401 // Render some text on the screen with different fonts
402 ssd1306DrawString(1, 56, "INSERT TEXT HERE", Font_System5x8);
403 // Refresh the screen to see the results
404 ssd1306Refresh();
405 // Wait a bit before writing the next line
406 systickDelay(1000);
407 }
408
409 @endcode
410 */
411 /**************************************************************************/
412 void ssd1306ShiftFrameBuffer( uint8_t height )
413 {
414 if (height == 0) return;
415 if (height >= SSD1306_LCDHEIGHT)
416 {
417 // Clear the entire frame buffer
418 ssd1306ClearScreen();
419 return;
420 }
421
422 // This is horribly inefficient, but at least easy to understand
423 // In a production environment, this should be significantly optimised
424
425 uint8_t y, x;
426 for (y = 0; y < SSD1306_LCDHEIGHT; y++)
427 {
428 for (x = 0; x < SSD1306_LCDWIDTH; x++)
429 {
430 if ((SSD1306_LCDHEIGHT - 1) - y > height)
431 {
432 // Shift height from further ahead in the buffer
433 ssd1306GetPixel(x, y + height) ? ssd1306DrawPixel(x, y) : ssd1306ClearPixel(x, y);
434 }
435 else
436 {
437 // Clear the entire line
438 ssd1306ClearPixel(x, y);
439 }
440 }
441 }
442 }
This page took 0.089787 seconds and 5 git commands to generate.