1 /**************************************************************************/
4 @author K. Townsend (www.adafruit.com)
8 Driver for S6B33B6X 128x128 pixel RGB OLED displays.
10 This driver uses a 3 or 4-pin SPI interface and 16-bit RGB565 colours.
14 Software License Agreement (BSD License)
16 Copyright (c) 2012, Adafruit Industries
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions are met:
21 1. Redistributions of source code must retain the above copyright
22 notice, this list of conditions and the following disclaimer.
23 2. Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
26 3. Neither the name of the copyright holders nor the
27 names of its contributors may be used to endorse or promote products
28 derived from this software without specific prior written permission.
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
31 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
34 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 /**************************************************************************/
43 #include "core/systick/systick.h"
45 static volatile lcdOrientation_t lcdOrientation
= LCD_ORIENTATION_PORTRAIT
;
46 static lcdProperties_t s6b33b6xProperties
= { 128, 128, false, false, false, false, false };
48 /*************************************************/
50 /*************************************************/
52 #define CMD(c) do { CLR_DC; CLR_CS; s6b33b6xSendByte( c, 1 ); SET_CS; } while (0)
53 #define DATA(c) do { SET_DC; CLR_CS; s6b33b6xSendByte( c, 0 ); SET_CS; } while (0)
54 #define DELAY(ms) do { systickDelay( ms / CFG_SYSTICK_DELAY_IN_MS ); } while(0)
56 /**************************************************************************/
58 @brief Simulates an SPI write using GPIO.
63 1 if this is a command, 0 if it is data
65 /**************************************************************************/
66 void s6b33b6xSendByte(uint8_t byte
, uint8_t command
)
70 // Make sure clock pin starts high (CPOL = 1)
73 #if defined S6B33B6X_BUS_SPI3
83 // Read on rising edge (CPHA = 1)
91 // Set data pin high or low depending on the value of the current bit
92 if((byte
& 0x80) == 0x80)
100 // Set clock pin high (CPHA = 1, read on rising edge)
106 /**************************************************************************/
108 @brief Sets the cursor to the specified X/Y position
110 /**************************************************************************/
111 void s6b33b6xSetCursor(uint8_t x
, uint8_t y
)
113 if ((x
>= s6b33b6xProperties
.width
) || (y
>= s6b33b6xProperties
.height
))
116 CMD(S6B33B6X_CMD_ROWADDRESSAREASET
);
117 CMD(2+y
); // Start Address
118 CMD(129); // End Address (129)
120 CMD(S6B33B6X_CMD_COLUMNADDRESSAREASET
);
121 CMD(2+x
); // Start Address
122 CMD(129); // End Address (129)
125 /*************************************************/
127 /*************************************************/
129 /**************************************************************************/
131 @brief Configures any pins or HW and initialises the LCD controller
133 /**************************************************************************/
138 // Make sure CS starts high
141 // Make sure clock pin starts high (CPOL = 1)
144 // Set all pins to output
145 gpioSetDir(S6B33B6X_SCK_PORT
, S6B33B6X_SCK_PIN
, gpioDirection_Output
);
146 gpioSetDir(S6B33B6X_SID_PORT
, S6B33B6X_SID_PIN
, gpioDirection_Output
);
147 gpioSetDir(S6B33B6X_RST_PORT
, S6B33B6X_RST_PIN
, gpioDirection_Output
);
148 gpioSetDir(S6B33B6X_CS_PORT
, S6B33B6X_CS_PIN
, gpioDirection_Output
);
150 #if !defined S6B33B6X_BUS_SPI3
151 gpioSetDir(S6B33B6X_DC_PORT
, S6B33B6X_DC_PIN
, gpioDirection_Output
);
163 CMD(S6B33B6X_CMD_STANDBYMODEOFF
);
165 CMD(S6B33B6X_CMD_OSCILLATIONMODESET
);
167 CMD(S6B33B6X_CMD_DCDCCONTROL
);
169 CMD(S6B33B6X_CMD_DCDCAMPONOFFSET
);
172 CMD(S6B33B6X_CMD_TEMPCOMPENSATIONSET
);
175 CMD(S6B33B6X_CMD_RAMSKIPAREASET
);
177 CMD(S6B33B6X_CMD_SPECIFIEDDISPLAYPATTERN
);
179 CMD(S6B33B6X_CMD_DRIVEROUTPUTMODESET
);
181 CMD(S6B33B6X_CMD_DCDCCLOCKDIVISIONSET
);
183 CMD(S6B33B6X_CMD_ADDRESSINGMODESET
);
185 CMD(S6B33B6X_CMD_ROWVECTORMODESET
);
187 CMD(S6B33B6X_CMD_ENTRYMODESET
);
189 CMD(S6B33B6X_CMD_ROWADDRESSAREASET
);
192 CMD(S6B33B6X_CMD_COLUMNADDRESSAREASET
);
195 CMD(S6B33B6X_CMD_NBLOCKINVERSIONSET
);
197 CMD(S6B33B6X_CMD_CONTRASTCONTROL
);
203 CMD(S6B33B6X_CMD_PARTIALDISPLAYMODESET
);
205 CMD(S6B33B6X_CMD_PARTIALDISPLAYSTARTLINE
);
207 CMD(S6B33B6X_CMD_PARTIALDISPLAYENDLINE
);
214 CMD(S6B33B6X_CMD_DISPLAYON
);
218 lcdFillRGB(COLOR_RED
);
221 /**************************************************************************/
223 @brief Enables or disables the LCD backlight
225 /**************************************************************************/
226 void lcdBacklight(bool state
)
228 // ToDo: Add BL pin and toggle here
231 /**************************************************************************/
233 @brief Renders a simple test pattern on the LCD
235 /**************************************************************************/
239 s6b33b6xSetCursor(0, 0);
245 if(i
>111){DATA(COLOR_WHITE
>>8);DATA((uint8_t)COLOR_WHITE
);}
246 else if(i
>95){DATA(COLOR_BLUE
>>8);DATA((uint8_t)COLOR_BLUE
);}
247 else if(i
>79){DATA(COLOR_GREEN
>>8);DATA((uint8_t)COLOR_GREEN
);}
248 else if(i
>63){DATA(COLOR_CYAN
>>8);DATA((uint8_t)COLOR_CYAN
);}
249 else if(i
>47){DATA(COLOR_RED
>>8);DATA((uint8_t)COLOR_RED
);}
250 else if(i
>31){DATA(COLOR_MAGENTA
>>8);DATA((uint8_t)COLOR_MAGENTA
);}
251 else if(i
>15){DATA(COLOR_YELLOW
>>8);DATA((uint8_t)COLOR_YELLOW
);}
252 else {DATA(COLOR_BLACK
>>8);DATA((uint8_t)COLOR_BLACK
);}
257 /**************************************************************************/
259 @brief Fills the LCD with the specified 16-bit color
261 /**************************************************************************/
262 void lcdFillRGB(uint16_t data
)
266 s6b33b6xSetCursor(0, 0);
278 /**************************************************************************/
280 @brief Draws a single pixel at the specified X/Y location
282 /**************************************************************************/
283 void lcdDrawPixel(uint16_t x
, uint16_t y
, uint16_t color
)
285 if ((x
>= s6b33b6xProperties
.width
) || (y
>= s6b33b6xProperties
.height
))
288 s6b33b6xSetCursor((uint8_t)x
, (uint8_t)y
);
293 /**************************************************************************/
295 @brief Draws an array of consecutive RGB565 pixels (much
296 faster than addressing each pixel individually)
298 /**************************************************************************/
299 void lcdDrawPixels(uint16_t x
, uint16_t y
, uint16_t *data
, uint32_t len
)
304 /**************************************************************************/
306 @brief Optimised routine to draw a horizontal line faster than
307 setting individual pixels
309 /**************************************************************************/
310 void lcdDrawHLine(uint16_t x0
, uint16_t x1
, uint16_t y
, uint16_t color
)
315 /**************************************************************************/
317 @brief Optimised routine to draw a vertical line faster than
318 setting individual pixels
320 /**************************************************************************/
321 void lcdDrawVLine(uint16_t x
, uint16_t y0
, uint16_t y1
, uint16_t color
)
326 /**************************************************************************/
328 @brief Gets the 16-bit color of the pixel at the specified location
330 /**************************************************************************/
331 uint16_t lcdGetPixel(uint16_t x
, uint16_t y
)
337 /**************************************************************************/
339 @brief Sets the LCD orientation to horizontal and vertical
341 /**************************************************************************/
342 void lcdSetOrientation(lcdOrientation_t orientation
)
347 /**************************************************************************/
349 @brief Gets the current screen orientation (horizontal or vertical)
351 /**************************************************************************/
352 lcdOrientation_t
lcdGetOrientation(void)
354 return lcdOrientation
;
357 /**************************************************************************/
359 @brief Gets the width in pixels of the LCD screen (varies depending
360 on the current screen orientation)
362 /**************************************************************************/
363 uint16_t lcdGetWidth(void)
365 return s6b33b6xProperties
.width
;
368 /**************************************************************************/
370 @brief Gets the height in pixels of the LCD screen (varies depending
371 on the current screen orientation)
373 /**************************************************************************/
374 uint16_t lcdGetHeight(void)
376 return s6b33b6xProperties
.height
;
379 /**************************************************************************/
381 @brief Scrolls the contents of the LCD screen vertically the
382 specified number of pixels using a HW optimised routine
384 /**************************************************************************/
385 void lcdScroll(int16_t pixels
, uint16_t fillColor
)
390 /**************************************************************************/
392 @brief Gets the controller's 16-bit (4 hexdigit) ID
394 /**************************************************************************/
395 uint16_t lcdGetControllerID(void)
400 /**************************************************************************/
402 @brief Returns the LCDs 'lcdProperties_t' that describes the LCDs
403 generic capabilities and dimensions
405 /**************************************************************************/
406 lcdProperties_t
lcdGetProperties(void)
408 return s6b33b6xProperties
;