1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
8 Driver for 128x64 OLED display based on the SSD1306 controller.
10 This driver is based on the SSD1306 Library from Limor Fried
11 (Adafruit Industries) at: https://github.com/adafruit/SSD1306
15 Software License Agreement (BSD License)
17 Copyright (c) 2012, microBuilder SARL
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.
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.
42 /**************************************************************************/
47 #include "core/gpio/gpio.h"
48 #include "core/systick/systick.h"
49 #include "drivers/lcd/smallfonts.h"
51 void ssd1306SendByte(uint8_t byte
);
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 ); \
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 ); \
65 #define DELAY(mS) do { systickDelay( mS / CFG_SYSTICK_DELAY_IN_MS ); } while(0);
67 uint8_t _ssd1306buffer
[SSD1306_LCDWIDTH
* SSD1306_LCDHEIGHT
/ 8];
69 /**************************************************************************/
71 /**************************************************************************/
73 /**************************************************************************/
75 @brief Simulates an SPI write using GPIO
80 /**************************************************************************/
81 void ssd1306SendByte(uint8_t byte
)
85 // Make sure clock pin starts high
86 gpioSetValue(SSD1306_SCLK_PORT
, SSD1306_SCLK_PIN
, 1);
88 // Write from MSB to LSB
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);
96 gpioSetValue(SSD1306_SCLK_PORT
, SSD1306_SCLK_PIN
, 1);
100 /**************************************************************************/
102 @brief Draws a single graphic character using the supplied font
104 /**************************************************************************/
105 static void ssd1306DrawChar(uint16_t x
, uint16_t y
, uint8_t c
, struct FONT_DEF font
)
107 uint8_t col
, column
[font
.u8Width
];
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
))
115 // Check if the requested character is available
116 if ((c
>= font
.u8FirstChar
) && (c
<= font
.u8LastChar
))
118 // Retrieve appropriate columns from font data
119 for (col
= 0; col
< font
.u8Width
; col
++)
121 column
[col
] = font
.au8FontTable
[((c
- 32) * font
.u8Width
) + col
]; // Get first column of appropriate character
126 // Requested character is not available in this font ... send a space instead
127 for (col
= 0; col
< font
.u8Width
; col
++)
129 column
[col
] = 0xFF; // Send solid space
133 // Render each column
134 uint16_t xoffset
, yoffset
;
135 for (xoffset
= 0; xoffset
< font
.u8Width
; xoffset
++)
137 for (yoffset
= 0; yoffset
< (font
.u8Height
+ 1); yoffset
++)
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)
144 ssd1306DrawPixel(x
+ xoffset
, y
+ yoffset
);
150 /**************************************************************************/
152 /**************************************************************************/
154 /**************************************************************************/
156 @brief Initialises the SSD1306 LCD display
158 /**************************************************************************/
159 void ssd1306Init(uint8_t vccstate
)
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
);
169 gpioSetValue(SSD1306_RST_PORT
, SSD1306_RST_PIN
, 1);
171 gpioSetValue(SSD1306_RST_PORT
, SSD1306_RST_PIN
, 0);
173 gpioSetValue(SSD1306_RST_PORT
, SSD1306_RST_PIN
, 1);
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
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
)
190 CMD(SSD1306_MEMORYMODE
); // 0x20
191 CMD(0x00); // 0x0 act like ks0108
192 CMD(SSD1306_SEGREMAP
| 0x1);
193 CMD(SSD1306_COMSCANDEC
);
194 CMD(SSD1306_SETCOMPINS
); // 0xDA
196 CMD(SSD1306_SETCONTRAST
); // 0x81
197 if (vccstate
== SSD1306_EXTERNALVCC
)
201 CMD(SSD1306_SETPRECHARGE
); // 0xd9
202 if (vccstate
== SSD1306_EXTERNALVCC
)
206 CMD(SSD1306_SETVCOMDETECT
); // 0xDB
208 CMD(SSD1306_DISPLAYALLON_RESUME
); // 0xA4
209 CMD(SSD1306_NORMALDISPLAY
); // 0xA6
212 #if defined SSD1306_128_64
213 // Init sequence taken from datasheet for UG-2864HSWEG01 (128x64 OLED module)
214 CMD(SSD1306_DISPLAYOFF
); // 0xAE
215 CMD(SSD1306_SETDISPLAYCLOCKDIV
); // 0xD5
216 CMD(0x80); // the suggested ratio 0x80
217 CMD(SSD1306_SETMULTIPLEX
); // 0xA8
219 CMD(SSD1306_SETDISPLAYOFFSET
); // 0xD3
220 CMD(0x0); // no offset
221 CMD(SSD1306_SETSTARTLINE
| 0x0); // line #0
222 CMD(SSD1306_CHARGEPUMP
); // 0x8D
223 if (vccstate
== SSD1306_EXTERNALVCC
)
227 CMD(SSD1306_MEMORYMODE
); // 0x20
228 CMD(0x00); // 0x0 act like ks0108
229 CMD(SSD1306_SEGREMAP
| 0x1);
230 CMD(SSD1306_COMSCANDEC
);
231 CMD(SSD1306_SETCOMPINS
); // 0xDA
233 CMD(SSD1306_SETCONTRAST
); // 0x81
234 if (vccstate
== SSD1306_EXTERNALVCC
)
238 CMD(SSD1306_SETPRECHARGE
); // 0xd9
239 if (vccstate
== SSD1306_EXTERNALVCC
)
243 CMD(SSD1306_SETVCOMDETECT
); // 0xDB
245 CMD(SSD1306_DISPLAYALLON_RESUME
); // 0xA4
246 CMD(SSD1306_NORMALDISPLAY
); // 0xA6
249 // Enabled the OLED panel
250 CMD(SSD1306_DISPLAYON
);
253 /**************************************************************************/
255 @brief Draws a single pixel in image buffer
258 The x position (0..127)
260 The y position (0..63)
262 /**************************************************************************/
263 void ssd1306DrawPixel(uint8_t x
, uint8_t y
)
265 if ((x
>= SSD1306_LCDWIDTH
) || (y
>= SSD1306_LCDHEIGHT
))
268 _ssd1306buffer
[x
+ (y
/8)*SSD1306_LCDWIDTH
] |= (1 << y
%8);
271 /**************************************************************************/
273 @brief Clears a single pixel in image buffer
276 The x position (0..127)
278 The y position (0..63)
280 /**************************************************************************/
281 void ssd1306ClearPixel(uint8_t x
, uint8_t y
)
283 if ((x
>= SSD1306_LCDWIDTH
) || (y
>= SSD1306_LCDHEIGHT
))
286 _ssd1306buffer
[x
+ (y
/8)*SSD1306_LCDWIDTH
] &= ~(1 << y
%8);
289 /**************************************************************************/
291 @brief Gets the value (1 or 0) of the specified pixel from the buffer
294 The x position (0..127)
296 The y position (0..63)
298 @return 1 if the pixel is enabled, 0 if disabled
300 /**************************************************************************/
301 uint8_t ssd1306GetPixel(uint8_t x
, uint8_t y
)
303 if ((x
>= SSD1306_LCDWIDTH
) || (y
>=SSD1306_LCDHEIGHT
)) return 0;
304 return _ssd1306buffer
[x
+ (y
/8)*SSD1306_LCDWIDTH
] & (1 << y
%8) ? 1 : 0;
307 /**************************************************************************/
309 @brief Clears the screen
311 /**************************************************************************/
312 void ssd1306ClearScreen()
314 memset(_ssd1306buffer
, 0, 1024);
317 /**************************************************************************/
319 @brief Renders the contents of the pixel buffer on the LCD
321 /**************************************************************************/
322 void ssd1306Refresh(void)
324 CMD(SSD1306_SETLOWCOLUMN
| 0x0); // low col = 0
325 CMD(SSD1306_SETHIGHCOLUMN
| 0x0); // hi col = 0
326 CMD(SSD1306_SETSTARTLINE
| 0x0); // line #0
329 for (i
=0; i
<1024; i
++)
331 DATA(_ssd1306buffer
[i
]);
335 /**************************************************************************/
337 @brief Draws a string using the supplied font data.
340 Starting x co-ordinate
342 Starting y co-ordinate
346 Pointer to the FONT_DEF to use when drawing the string
352 #include "drivers/lcd/bitmap/ssd1306/ssd1306.h"
353 #include "drivers/lcd/smallfonts.h"
355 // Configure the pins and initialise the LCD screen
356 ssd1306Init(SSD1306_INTERNALVCC);
358 // Render some text on the screen
359 ssd1306DrawString(1, 10, "5x8 System", Font_System5x8);
360 ssd1306DrawString(1, 20, "7x8 System", Font_System7x8);
362 // Refresh the screen to see the results
367 /**************************************************************************/
368 void ssd1306DrawString(uint16_t x
, uint16_t y
, char* text
, struct FONT_DEF font
)
371 for (l
= 0; l
< strlen(text
); l
++)
373 ssd1306DrawChar(x
+ (l
* (font
.u8Width
+ 1)), y
, text
[l
], font
);
377 /**************************************************************************/
379 @brief Shifts the contents of the frame buffer up the specified
383 The number of pixels to shift the frame buffer up, leaving
384 a blank space at the bottom of the frame buffer x pixels
391 #include "drivers/lcd/bitmap/ssd1306/ssd1306.h"
392 #include "drivers/lcd/smallfonts.h"
394 // Configure the pins and initialise the LCD screen
395 ssd1306Init(SSD1306_INTERNALVCC);
397 // Continually write some text, scrolling upward one line each time
400 // Shift the buffer up 8 pixels (adjust for font-height)
401 ssd1306ShiftFrameBuffer(8);
402 // Render some text on the screen with different fonts
403 ssd1306DrawString(1, 56, "INSERT TEXT HERE", Font_System5x8);
404 // Refresh the screen to see the results
406 // Wait a bit before writing the next line
412 /**************************************************************************/
413 void ssd1306ShiftFrameBuffer( uint8_t height
)
415 if (height
== 0) return;
416 if (height
>= SSD1306_LCDHEIGHT
)
418 // Clear the entire frame buffer
419 ssd1306ClearScreen();
423 // This is horribly inefficient, but at least easy to understand
424 // In a production environment, this should be significantly optimised
427 for (y
= 0; y
< SSD1306_LCDHEIGHT
; y
++)
429 for (x
= 0; x
< SSD1306_LCDWIDTH
; x
++)
431 if ((SSD1306_LCDHEIGHT
- 1) - y
> height
)
433 // Shift height from further ahead in the buffer
434 ssd1306GetPixel(x
, y
+ height
) ? ssd1306DrawPixel(x
, y
) : ssd1306ClearPixel(x
, y
);
438 // Clear the entire line
439 ssd1306ClearPixel(x
, y
);