1 /**
2 * \file
3 *
4 * \brief Haven Display UG 2832HSWEG04 display glue code for display controller
5 *
6 * Copyright (c) 2013-2015 Atmel Corporation. All rights reserved.
7 *
8 * \asf_license_start
9 *
10 * \page License
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 *
22 * 3. The name of Atmel may not be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * 4. This software may only be redistributed and used in connection with an
26 * Atmel microcontroller product.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 *
40 * \asf_license_stop
41 *
42 */
43 /*
44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45 */
46 #include "gfx_mono_ug_2832hsweg04.h"
47
48 /* If we are using a serial interface without readback, use framebuffer */
49
50 #ifdef CONFIG_SSD1306_FRAMEBUFFER
51 static uint8_t framebuffer[GFX_MONO_LCD_FRAMEBUFFER_SIZE];
52 #endif
53
54 /**
55 * \brief Initialize SSD1306 controller and LCD display.
56 * It will also write the graphic controller RAM to all zeroes.
57 *
58 * \note This function will clear the contents of the display.
59 */
gfx_mono_ssd1306_init(void)60 void gfx_mono_ssd1306_init(void)
61 {
62 uint8_t page;
63 uint8_t column;
64
65 #ifdef CONFIG_SSD1306_FRAMEBUFFER
66 gfx_mono_set_framebuffer(framebuffer);
67 #endif
68
69 /* Initialize the low-level display controller. */
70 ssd1306_init();
71
72 /* Set display to output data from line 0 */
73 ssd1306_set_display_start_line_address(0);
74
75 /* Clear the contents of the display.
76 * If using a framebuffer (SPI interface) it will both clear the
77 * controller memory and the framebuffer.
78 */
79 for (page = 0; page < GFX_MONO_LCD_PAGES; page++) {
80 for (column = 0; column < GFX_MONO_LCD_WIDTH; column++) {
81 gfx_mono_ssd1306_put_byte(page, column, 0x00, true);
82 }
83 }
84 }
85
86 #ifdef CONFIG_SSD1306_FRAMEBUFFER
87 /**
88 * \brief Put framebuffer to LCD controller
89 *
90 * This function will output the complete framebuffer from RAM to the
91 * LCD controller.
92 *
93 * \note This is done automatically if using the graphic primitives. Only
94 * needed if you are manipulating the framebuffer directly in your code.
95 */
gfx_mono_ssd1306_put_framebuffer(void)96 void gfx_mono_ssd1306_put_framebuffer(void)
97 {
98 uint8_t page;
99
100 for (page = 0; page < GFX_MONO_LCD_PAGES; page++) {
101 ssd1306_set_page_address(page);
102 ssd1306_set_column_address(0);
103 gfx_mono_ssd1306_put_page(framebuffer
104 + (page * GFX_MONO_LCD_WIDTH), page, 0,
105 GFX_MONO_LCD_WIDTH);
106 }
107 }
108 #endif
109
110 /**
111 * \brief Draw pixel to screen
112 *
113 * \param[in] x X coordinate of the pixel
114 * \param[in] y Y coordinate of the pixel
115 * \param[in] color Pixel operation
116 *
117 * The following will set the pixel at x=10,y=10:
118 * \code
119 gfx_mono_ssd1306_draw_pixel(10, 10, GFX_PIXEL_SET);
120 \endcode
121 * The following example will clear the pixel at x=10,y=10:
122 * \code
123 gfx_mono_ssd1306_draw_pixel(10, 10, GFX_PIXEL_CLR);
124 \endcode
125 * And the following will toggle the pixel at x=10,y=10:
126 * \code
127 gfx_mono_ssd1306_draw_pixel(10, 10, GFX_PIXEL_XOR);
128 \endcode
129 */
gfx_mono_ssd1306_draw_pixel(gfx_coord_t x,gfx_coord_t y,gfx_coord_t color)130 void gfx_mono_ssd1306_draw_pixel(gfx_coord_t x, gfx_coord_t y,
131 gfx_coord_t color)
132 {
133 uint8_t page;
134 uint8_t pixel_mask;
135 uint8_t pixel_value;
136
137 /* Discard pixels drawn outside the screen */
138 if ((x > GFX_MONO_LCD_WIDTH - 1) || (y > GFX_MONO_LCD_HEIGHT - 1)) {
139 return;
140 }
141
142 page = y / GFX_MONO_LCD_PIXELS_PER_BYTE;
143 pixel_mask = (1 << (y - (page * 8)));
144
145 /*
146 * Read the page containing the pixel in interest, then perform the
147 * requested action on this pixel before writing the page back to the
148 * display.
149 */
150 pixel_value = gfx_mono_get_byte(page, x);
151
152 switch (color) {
153 case GFX_PIXEL_SET:
154 pixel_value |= pixel_mask;
155 break;
156
157 case GFX_PIXEL_CLR:
158 pixel_value &= ~pixel_mask;
159 break;
160
161 case GFX_PIXEL_XOR:
162 pixel_value ^= pixel_mask;
163 break;
164
165 default:
166 break;
167 }
168
169 gfx_mono_put_byte(page, x, pixel_value);
170 }
171
172 /**
173 * \brief Get the pixel value at x,y
174 *
175 * \param[in] x X coordinate of pixel
176 * \param[in] y Y coordinate of pixel
177 * \return Non zero value if pixel is set.
178 *
179 * The following example will read the pixel value from x=10,y=10:
180 * \code
181 pixelval = gfx_mono_ssd1306_get_pixel(10,10);
182 \endcode
183 */
gfx_mono_ssd1306_get_pixel(gfx_coord_t x,gfx_coord_t y)184 uint8_t gfx_mono_ssd1306_get_pixel(gfx_coord_t x, gfx_coord_t y)
185 {
186 uint8_t page;
187 uint8_t pixel_mask;
188
189 if ((x > GFX_MONO_LCD_WIDTH - 1) || (y > GFX_MONO_LCD_HEIGHT - 1)) {
190 return 0;
191 }
192
193 page = y / GFX_MONO_LCD_PIXELS_PER_BYTE;
194 pixel_mask = (1 << (y - (page * 8)));
195
196 return gfx_mono_get_byte(page, x) & pixel_mask;
197 }
198
199 /**
200 * \brief Put a page from RAM to display controller.
201 *
202 * If the controller is accessed by the SPI interface, we can not read
203 * back data from the LCD controller RAM. Because of this all data that is
204 * written to the LCD controller in this mode is also written to a framebuffer
205 * in MCU RAM.
206 *
207 * \param[in] data Pointer to data to be written
208 * \param[in] page Page address
209 * \param[in] column Offset into page (x coordinate)
210 * \param[in] width Number of bytes to be written.
211 *
212 * The following example will write 32 bytes from data_buf to the page 0,
213 * column 10. This will place data_buf in the rectangle x1=10,y1=0,x2=42,y2=8
214 * (10 pixels from the upper left corner of the screen):
215 * \code
216 gfx_mono_ssd1306_put_page(data_buf, 0, 10, 32);
217 \endcode
218 */
gfx_mono_ssd1306_put_page(gfx_mono_color_t * data,gfx_coord_t page,gfx_coord_t column,gfx_coord_t width)219 void gfx_mono_ssd1306_put_page(gfx_mono_color_t *data, gfx_coord_t page,
220 gfx_coord_t column, gfx_coord_t width)
221 {
222 #ifdef CONFIG_SSD1306_FRAMEBUFFER
223 gfx_mono_framebuffer_put_page(data, page, column, width);
224 #endif
225 ssd1306_set_page_address(page);
226 ssd1306_set_column_address(column);
227
228 do {
229 ssd1306_write_data(*data++);
230 } while (--width);
231 }
232
233 /**
234 * \brief Read a page from the LCD controller
235 *
236 * If the LCD controller is accessed by the SPI interface we cannot read
237 * data directly from the controller. In that case we will read the data from
238 * the local framebuffer instead.
239 *
240 * \param[in] data Pointer where to store the read data
241 * \param[in] page Page address
242 * \param[in] column Offset into page (x coordinate)
243 * \param[in] width Number of bytes to be read
244 *
245 * The following example will read back the first 128 bytes (first page) from
246 * the display memory:
247 * \code
248 gfx_mono_ssd1306_get_page(read_buffer, 0, 0, 128);
249 \endcode
250 */
gfx_mono_ssd1306_get_page(gfx_mono_color_t * data,gfx_coord_t page,gfx_coord_t column,gfx_coord_t width)251 void gfx_mono_ssd1306_get_page(gfx_mono_color_t *data, gfx_coord_t page,
252 gfx_coord_t column, gfx_coord_t width)
253 {
254 #ifdef CONFIG_SSD1306_FRAMEBUFFER
255 gfx_mono_framebuffer_get_page(data, page, column, width);
256 #else
257 ssd1306_set_page_address(page);
258 ssd1306_set_column_address(column);
259
260 do {
261 *data++ = ssd1306_read_data();
262 } while (--width);
263 #endif
264 }
265
266 /**
267 * \brief Put a byte to the display controller RAM
268 *
269 * If the LCD controller is accessed by the SPI interface we will also put the
270 * data to the local framebuffer.
271 *
272 * \param[in] page Page address
273 * \param[in] column Page offset (x coordinate)
274 * \param[in] data Data to be written
275 * \param[in] force Forces the write
276 *
277 * This example will put the value 0xFF to the first byte in the display memory
278 * setting a 8 pixel high column of pixels in the upper left corner of the
279 * display.
280 * \code
281 gfx_mono_ssd1306_put_byte(0, 0, 0xFF, false);
282 \endcode
283 */
gfx_mono_ssd1306_put_byte(gfx_coord_t page,gfx_coord_t column,uint8_t data,bool force)284 void gfx_mono_ssd1306_put_byte(gfx_coord_t page, gfx_coord_t column,
285 uint8_t data, bool force)
286 {
287 #ifdef CONFIG_SSD1306_FRAMEBUFFER
288 if (!force && data == gfx_mono_framebuffer_get_byte(page, column)) {
289 return;
290 }
291 gfx_mono_framebuffer_put_byte(page, column, data);
292 #endif
293
294 ssd1306_set_page_address(page);
295 ssd1306_set_column_address(column);
296
297 ssd1306_write_data(data);
298 }
299
300 /**
301 * \brief Get a byte from the display controller RAM
302 *
303 * If the LCD controller is accessed by the SPI interface we cannot read the
304 * data. In this case return the data from the local framebuffer instead.
305 *
306 * \param[in] page Page address
307 * \param[in] column Page offset (x coordinate)
308 * \return data from LCD controller or framebuffer.
309 *
310 * The following code will read the first byte from the display memory or the
311 * local framebuffer if direct read is not possible. The data represents the
312 * pixels from x = 0 and y = 0 to y = 7.
313 * \code
314 data = gfx_mono_ssd1306_get_byte(0, 0);
315 \endcode
316 */
gfx_mono_ssd1306_get_byte(gfx_coord_t page,gfx_coord_t column)317 uint8_t gfx_mono_ssd1306_get_byte(gfx_coord_t page, gfx_coord_t column)
318 {
319 #ifdef CONFIG_SSD1306_FRAMEBUFFER
320 return gfx_mono_framebuffer_get_byte(page, column);
321
322 #else
323 ssd1306_set_page_address(page);
324 ssd1306_set_column_address(column);
325
326 return ssd1306_read_data();
327
328 #endif
329 }
330
331 /**
332 * \brief Read/Modify/Write a byte on the display controller
333 *
334 * This function will read the byte from the display controller (or the
335 * framebuffer if we cannot read directly from the controller) and
336 * do a mask operation on the byte according to the pixel operation selected
337 * by the color argument and the pixel mask provided.
338 *
339 * \param[in] page Page address
340 * \param[in] column Page offset (x coordinate)
341 * \param[in] pixel_mask Mask for pixel operation
342 * \param[in] color Pixel operation
343 *
344 * A small example that will XOR the first byte of display memory with 0xAA
345 * \code
346 gfx_mono_ssd1306_mask_byte(0,0,0xAA,GFX_PIXEL_XOR);
347 \endcode
348 */
gfx_mono_ssd1306_mask_byte(gfx_coord_t page,gfx_coord_t column,gfx_mono_color_t pixel_mask,gfx_mono_color_t color)349 void gfx_mono_ssd1306_mask_byte(gfx_coord_t page, gfx_coord_t column,
350 gfx_mono_color_t pixel_mask, gfx_mono_color_t color)
351 {
352 gfx_mono_color_t temp = gfx_mono_get_byte(page, column);
353
354 switch (color) {
355 case GFX_PIXEL_SET:
356 temp |= pixel_mask;
357 break;
358
359 case GFX_PIXEL_CLR:
360 temp &= ~pixel_mask;
361 break;
362
363 case GFX_PIXEL_XOR:
364 temp ^= pixel_mask;
365 break;
366
367 default:
368 break;
369 }
370
371 gfx_mono_put_byte(page, column, temp);
372 }
373