1 /**
2  * \file
3  *
4  * \brief Haven Display C12832 A1Z display glue code for display controller
5  *
6  * Copyright (c) 2011-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_c12832_a1z.h"
47 
48 /* If we are using a serial interface without readback, use framebuffer */
49 #ifdef ST7565R_SERIAL_INTERFACE
50 # define CONFIG_ST7565R_FRAMEBUFFER
51 #endif
52 
53 #ifdef CONFIG_ST7565R_FRAMEBUFFER
54 static uint8_t framebuffer[GFX_MONO_LCD_FRAMEBUFFER_SIZE];
55 #endif
56 
57 /**
58  * \brief Initialize ST7565R controller and LCD display.
59  * It will also write the graphic controller RAM to all zeroes.
60  *
61  * \note This function will clear the contents of the display.
62  */
gfx_mono_st7565r_init(void)63 void gfx_mono_st7565r_init(void)
64 {
65 	uint8_t page;
66 	uint8_t column;
67 
68 #ifdef CONFIG_ST7565R_FRAMEBUFFER
69 	gfx_mono_set_framebuffer(framebuffer);
70 #endif
71 
72 	/* Initialize the low-level display controller. */
73 	st7565r_init();
74 
75 	/* Set display to output data from line 0 */
76 	st7565r_set_display_start_line_address(0);
77 
78 	/* Clear the contents of the display.
79 	 * If using a framebuffer (SPI interface) it will both clear the
80 	 * controller memory and the framebuffer.
81 	 */
82 	for (page = 0; page < GFX_MONO_LCD_PAGES; page++) {
83 		for (column = 0; column < GFX_MONO_LCD_WIDTH; column++) {
84 			gfx_mono_put_byte(page, column, 0x00);
85 		}
86 	}
87 }
88 
89 /**
90  * \brief Put framebuffer to LCD controller
91  *
92  * This function will output the complete framebuffer from RAM to the
93  * LCD controller.
94  *
95  * \note This is done automatically if using the graphic primitives. Only
96  * needed if you are manipulating the framebuffer directly in your code.
97  */
gfx_mono_st7565r_put_framebuffer(void)98 void gfx_mono_st7565r_put_framebuffer(void)
99 {
100 	uint8_t page;
101 
102 	for (page = 0; page < GFX_MONO_LCD_PAGES; page++) {
103 		st7565r_set_page_address(page);
104 		st7565r_set_column_address(0);
105 		gfx_mono_st7565r_put_page(framebuffer
106 				+ (page * GFX_MONO_LCD_WIDTH), page, 0,
107 				GFX_MONO_LCD_WIDTH);
108 	}
109 }
110 
111 /**
112  * \brief Draw pixel to screen
113  *
114  * \param[in] x         X coordinate of the pixel
115  * \param[in] y         Y coordinate of the pixel
116  * \param[in] color     Pixel operation
117  *
118  * The following will set the pixel at x=10,y=10:
119  * \code
120 	gfx_mono_st7565r_draw_pixel(10, 10, GFX_PIXEL_SET);
121 \endcode
122  * The following example will clear the pixel at x=10,y=10:
123  * \code
124 	gfx_mono_st7565r_draw_pixel(10, 10, GFX_PIXEL_CLR);
125 \endcode
126  * And the following will toggle the pixel at x=10,y=10:
127  * \code
128 	gfx_mono_st7565r_draw_pixel(10, 10, GFX_PIXEL_XOR);
129 \endcode
130  */
gfx_mono_st7565r_draw_pixel(gfx_coord_t x,gfx_coord_t y,gfx_coord_t color)131 void gfx_mono_st7565r_draw_pixel(gfx_coord_t x, gfx_coord_t y,
132 		gfx_coord_t color)
133 {
134 	uint8_t page;
135 	uint8_t pixel_mask;
136 	uint8_t pixel_value;
137 
138 	/* Discard pixels drawn outside the screen */
139 	if ((x > GFX_MONO_LCD_WIDTH - 1) || (y > GFX_MONO_LCD_HEIGHT - 1)) {
140 		return;
141 	}
142 
143 	page = y / GFX_MONO_LCD_PIXELS_PER_BYTE;
144 	pixel_mask = (1 << (y - (page * 8)));
145 
146 	/*
147 	 * Read the page containing the pixel in interest, then perform the
148 	 * requested action on this pixel before writing the page back to the
149 	 * display.
150 	 */
151 	pixel_value = gfx_mono_get_byte(page, x);
152 
153 	switch (color) {
154 	case GFX_PIXEL_SET:
155 		pixel_value |= pixel_mask;
156 		break;
157 
158 	case GFX_PIXEL_CLR:
159 		pixel_value &= ~pixel_mask;
160 		break;
161 
162 	case GFX_PIXEL_XOR:
163 		pixel_value ^= pixel_mask;
164 		break;
165 
166 	default:
167 		break;
168 	}
169 
170 	gfx_mono_put_byte(page, x, pixel_value);
171 }
172 
173 /**
174  * \brief Get the pixel value at x,y
175  *
176  * \param[in] x      X coordinate of pixel
177  * \param[in] y      Y coordinate of pixel
178  * \return Non zero value if pixel is set.
179  *
180  * The following example will read the pixel value from x=10,y=10:
181  * \code
182 	pixelval = gfx_mono_st7565r_get_pixel(10,10);
183 \endcode
184  */
gfx_mono_st7565r_get_pixel(gfx_coord_t x,gfx_coord_t y)185 uint8_t gfx_mono_st7565r_get_pixel(gfx_coord_t x, gfx_coord_t y)
186 {
187 	uint8_t page;
188 	uint8_t pixel_mask;
189 
190 	if ((x > GFX_MONO_LCD_WIDTH - 1) || (y > GFX_MONO_LCD_HEIGHT - 1)) {
191 		return 0;
192 	}
193 
194 	page = y / GFX_MONO_LCD_PIXELS_PER_BYTE;
195 	pixel_mask = (1 << (y - (page * 8)));
196 
197 	return gfx_mono_get_byte(page, x) & pixel_mask;
198 }
199 
200 /**
201  * \brief Put a page from RAM to display controller.
202  *
203  * If the controller is accessed by the SPI interface, we can not read
204  * back data from the LCD controller RAM. Because of this all data that is
205  * written to the LCD controller in this mode is also written to a framebuffer
206  * in MCU RAM.
207  *
208  * \param[in] data Pointer to data to be written
209  * \param[in] page Page address
210  * \param[in] column Offset into page (x coordinate)
211  * \param[in] width Number of bytes to be written.
212  *
213  * The following example will write 32 bytes from data_buf to the page 0,
214  * column 10. This will place data_buf in the rectangle x1=10,y1=0,x2=42,y2=8
215  * (10 pixels from the upper left corner of the screen):
216  * \code
217 	gfx_mono_st7565r_put_page(data_buf, 0, 10, 32);
218 \endcode
219  */
gfx_mono_st7565r_put_page(gfx_mono_color_t * data,gfx_coord_t page,gfx_coord_t column,gfx_coord_t width)220 void gfx_mono_st7565r_put_page(gfx_mono_color_t *data, gfx_coord_t page,
221 		gfx_coord_t column, gfx_coord_t width)
222 {
223 #ifdef CONFIG_ST7565R_FRAMEBUFFER
224 	gfx_mono_framebuffer_put_page(data, page, column, width);
225 #endif
226 	st7565r_set_page_address(page);
227 	st7565r_set_column_address(column);
228 
229 	do {
230 		st7565r_write_data(*data++);
231 	} while (--width);
232 }
233 
234 /**
235  * \brief Read a page from the LCD controller
236  *
237  * If the LCD controller is accessed by the SPI interface we cannot read
238  * data directly from the controller. In that case we will read the data from
239  * the local framebuffer instead.
240  *
241  * \param[in] data   Pointer where to store the read data
242  * \param[in] page   Page address
243  * \param[in] column Offset into page (x coordinate)
244  * \param[in] width  Number of bytes to be read
245  *
246  * The following example will read back the first 128 bytes (first page) from
247  * the display memory:
248  * \code
249 	gfx_mono_st7565r_get_page(read_buffer, 0, 0, 128);
250 \endcode
251  */
gfx_mono_st7565r_get_page(gfx_mono_color_t * data,gfx_coord_t page,gfx_coord_t column,gfx_coord_t width)252 void gfx_mono_st7565r_get_page(gfx_mono_color_t *data, gfx_coord_t page,
253 		gfx_coord_t column, gfx_coord_t width)
254 {
255 #ifdef CONFIG_ST7565R_FRAMEBUFFER
256 	gfx_mono_framebuffer_get_page(data, page, column, width);
257 #else
258 	st7565r_set_page_address(page);
259 	st7565r_set_column_address(column);
260 
261 	do {
262 		*data++ = st7565r_read_data();
263 	} while (--width);
264 #endif
265 }
266 
267 /**
268  * \brief Put a byte to the display controller RAM
269  *
270  * If the LCD controller is accessed by the SPI interface we will also put the
271  * data to the local framebuffer.
272  *
273  * \param[in] page Page address
274  * \param[in] column Page offset (x coordinate)
275  * \param[in] data Data to be written
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_st7565r_put_byte(0, 0, 0xFF);
282 \endcode
283  */
gfx_mono_st7565r_put_byte(gfx_coord_t page,gfx_coord_t column,uint8_t data)284 void gfx_mono_st7565r_put_byte(gfx_coord_t page, gfx_coord_t column,
285 		uint8_t data)
286 {
287 #ifdef CONFIG_ST7565R_FRAMEBUFFER
288 	gfx_mono_framebuffer_put_byte(page, column, data);
289 #endif
290 
291 	st7565r_set_page_address(page);
292 	st7565r_set_column_address(column);
293 
294 	st7565r_write_data(data);
295 }
296 
297 /**
298  * \brief Get a byte from the display controller RAM
299  *
300  * If the LCD controller is accessed by the SPI interface we cannot read the
301  * data. In this case return the data from the local framebuffer instead.
302  *
303  * \param page Page address
304  * \param column Page offset (x coordinate)
305  * \return data from LCD controller or framebuffer.
306  *
307  * The following code will read the first byte from the display memory or the
308  * local framebuffer if direct read is not possible. The data represents the
309  * pixels from x = 0 and y = 0 to y = 7.
310  * \code
311 	data = gfx_mono_st7565r_get_byte(0, 0);
312 \endcode
313  */
gfx_mono_st7565r_get_byte(gfx_coord_t page,gfx_coord_t column)314 uint8_t gfx_mono_st7565r_get_byte(gfx_coord_t page, gfx_coord_t column)
315 {
316 #ifdef CONFIG_ST7565R_FRAMEBUFFER
317 	return gfx_mono_framebuffer_get_byte(page, column);
318 
319 #else
320 	st7565r_set_page_address(page);
321 	st7565r_set_column_address(column);
322 
323 	return st7565r_read_data();
324 
325 #endif
326 }
327 
328 /**
329  * \brief Read/Modify/Write a byte on the display controller
330  *
331  * This function will read the byte from the display controller (or the
332  * framebuffer if we cannot read directly from the controller) and
333  * do a mask operation on the byte according to the pixel operation selected
334  * by the color argument and the pixel mask provided.
335  *
336  * \param[in] page Page address
337  * \param[in] column Page offset (x coordinate)
338  * \param[in] pixel_mask Mask for pixel operation
339  * \param[in] color Pixel operation
340  *
341  * A small example that will XOR the first byte of display memory with 0xAA
342  * \code
343 	gfx_mono_st7565r_mask_byte(0,0,0xAA,GFX_PIXEL_XOR);
344 \endcode
345  */
gfx_mono_st7565r_mask_byte(gfx_coord_t page,gfx_coord_t column,gfx_mono_color_t pixel_mask,gfx_mono_color_t color)346 void gfx_mono_st7565r_mask_byte(gfx_coord_t page, gfx_coord_t column,
347 		gfx_mono_color_t pixel_mask, gfx_mono_color_t color)
348 {
349 	gfx_mono_color_t temp = gfx_mono_get_byte(page, column);
350 
351 	switch (color) {
352 	case GFX_PIXEL_SET:
353 		temp |= pixel_mask;
354 		break;
355 
356 	case GFX_PIXEL_CLR:
357 		temp &= ~pixel_mask;
358 		break;
359 
360 	case GFX_PIXEL_XOR:
361 		temp ^= pixel_mask;
362 		break;
363 
364 	default:
365 		break;
366 	}
367 
368 	gfx_mono_put_byte(page, column, temp);
369 }
370