zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame^] | 1 | /** |
| 2 | \file glcd.c |
| 3 | \author Andy Gock |
| 4 | \brief Basic GLCD functions affecting bounding box manipulation, |
| 5 | clearing of screen and buffers, and basic scroll functions. |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | Copyright (c) 2012, Andy Gock |
| 10 | |
| 11 | All rights reserved. |
| 12 | |
| 13 | Redistribution and use in source and binary forms, with or without |
| 14 | modification, are permitted provided that the following conditions are met: |
| 15 | * Redistributions of source code must retain the above copyright |
| 16 | notice, this list of conditions and the following disclaimer. |
| 17 | * Redistributions in binary form must reproduce the above copyright |
| 18 | notice, this list of conditions and the following disclaimer in the |
| 19 | documentation and/or other materials provided with the distribution. |
| 20 | * Neither the name of Andy Gock nor the |
| 21 | names of its contributors may be used to endorse or promote products |
| 22 | derived from this software without specific prior written permission. |
| 23 | |
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 25 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | DISCLAIMED. IN NO EVENT SHALL ANDY GOCK BE LIABLE FOR ANY |
| 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | */ |
| 35 | |
| 36 | #include <string.h> |
| 37 | #include <stdio.h> |
| 38 | #include "glcd.h" |
| 39 | |
| 40 | /** \addtogroup GlobalVars Global Variables |
| 41 | * @{ |
| 42 | */ |
| 43 | |
| 44 | /** |
| 45 | * Screen buffer |
| 46 | * |
| 47 | * Requires at least one bit for every pixel (e.g 504 bytes for 48x84 LCD) |
| 48 | */ |
| 49 | uint8_t glcd_buffer[GLCD_LCD_WIDTH * GLCD_LCD_HEIGHT / 8]; |
| 50 | |
| 51 | /** |
| 52 | * Keeps track of bounding box of area on LCD which need to be |
| 53 | * updated next reresh cycle |
| 54 | */ |
| 55 | glcd_BoundingBox_t glcd_bbox; |
| 56 | |
| 57 | /** |
| 58 | * Pointer to screen buffer currently in use. |
| 59 | */ |
| 60 | uint8_t* glcd_buffer_selected; |
| 61 | |
| 62 | uint8_t glcd_disp_reverse_en = 0; |
| 63 | |
| 64 | /** |
| 65 | * Pointer to bounding box currently in use. |
| 66 | */ |
| 67 | glcd_BoundingBox_t* glcd_bbox_selected; |
| 68 | |
| 69 | /** @} */ |
| 70 | |
| 71 | void glcd_set_reverse_sta(uint8_t sta) |
| 72 | { |
| 73 | if(!sta) |
| 74 | glcd_disp_reverse_en = 0; |
| 75 | else |
| 76 | glcd_disp_reverse_en = 1; |
| 77 | } |
| 78 | |
| 79 | uint8_t glcd_get_reverse_sta(void) |
| 80 | { |
| 81 | return glcd_disp_reverse_en; |
| 82 | } |
| 83 | |
| 84 | void glcd_update_bbox(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax) |
| 85 | { |
| 86 | /* Keep and check bounding box within limits of LCD screen dimensions */ |
| 87 | if(xmin > (GLCD_LCD_WIDTH-1)) |
| 88 | { |
| 89 | xmin = GLCD_LCD_WIDTH-1; |
| 90 | } |
| 91 | if(xmax > (GLCD_LCD_WIDTH-1)) |
| 92 | { |
| 93 | xmax = GLCD_LCD_WIDTH-1; |
| 94 | } |
| 95 | |
| 96 | if(ymin > (GLCD_LCD_HEIGHT-1)) |
| 97 | { |
| 98 | ymin = GLCD_LCD_HEIGHT-1; |
| 99 | } |
| 100 | if(ymax > (GLCD_LCD_HEIGHT-1)) |
| 101 | { |
| 102 | ymax = GLCD_LCD_HEIGHT-1; |
| 103 | } |
| 104 | |
| 105 | /* Update the bounding box size */ |
| 106 | if(xmin < glcd_bbox_selected->x_min) |
| 107 | { |
| 108 | glcd_bbox_selected->x_min = xmin; |
| 109 | } |
| 110 | if(xmax > glcd_bbox_selected->x_max) |
| 111 | { |
| 112 | glcd_bbox_selected->x_max = xmax; |
| 113 | } |
| 114 | if(ymin < glcd_bbox_selected->y_min) |
| 115 | { |
| 116 | glcd_bbox_selected->y_min = ymin; |
| 117 | } |
| 118 | if(ymax > glcd_bbox_selected->y_max) |
| 119 | { |
| 120 | glcd_bbox_selected->y_max = ymax; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void glcd_reset_bbox() |
| 125 | { |
| 126 | /* Used after physically writing to the LCD */ |
| 127 | glcd_bbox_selected->x_min = GLCD_LCD_WIDTH - 1; |
| 128 | glcd_bbox_selected->x_max = 0; |
| 129 | glcd_bbox_selected->y_min = GLCD_LCD_HEIGHT -1; |
| 130 | glcd_bbox_selected->y_max = 0; |
| 131 | } |
| 132 | |
| 133 | void glcd_bbox_reset() |
| 134 | { |
| 135 | glcd_reset_bbox(); |
| 136 | } |
| 137 | |
| 138 | void glcd_bbox_refresh() |
| 139 | { |
| 140 | /* Marks bounding box as entire screen, so on next glcd_write(), it writes the entire buffer to the LCD */ |
| 141 | glcd_bbox_selected->x_min = 0; |
| 142 | glcd_bbox_selected->x_max = GLCD_LCD_WIDTH - 1; |
| 143 | glcd_bbox_selected->y_min = 0; |
| 144 | glcd_bbox_selected->y_max = GLCD_LCD_HEIGHT -1; |
| 145 | } |
| 146 | |
| 147 | void glcd_clear(void) |
| 148 | { |
| 149 | memset(glcd_buffer_selected, 0x00, GLCD_LCD_WIDTH * GLCD_LCD_HEIGHT / 8); |
| 150 | glcd_update_bbox(0,0,GLCD_LCD_WIDTH - 1,GLCD_LCD_HEIGHT - 1); |
| 151 | glcd_write(); |
| 152 | } |
| 153 | |
| 154 | void glcd_clear_buffer(void) |
| 155 | { |
| 156 | memset(glcd_buffer_selected, 0x00, GLCD_LCD_WIDTH * GLCD_LCD_HEIGHT / 8); |
| 157 | glcd_update_bbox(0,0,GLCD_LCD_WIDTH - 1,GLCD_LCD_HEIGHT - 1); |
| 158 | } |
| 159 | |
| 160 | void glcd_select_screen(uint8_t* buffer, glcd_BoundingBox_t* bbox) |
| 161 | { |
| 162 | glcd_buffer_selected = buffer; |
| 163 | glcd_bbox_selected = bbox; |
| 164 | } |
| 165 | |
| 166 | void glcd_scroll(int8_t x, int8_t y) |
| 167 | { |
| 168 | /** \todo Skeleton */ |
| 169 | |
| 170 | uint8_t row; |
| 171 | |
| 172 | for(row=0; row<(GLCD_LCD_HEIGHT / 8); row++) |
| 173 | { |
| 174 | uint8_t x; |
| 175 | for(x=0; x<GLCD_LCD_WIDTH; x++) |
| 176 | { |
| 177 | |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void glcd_scroll_line(void) |
| 183 | { |
| 184 | uint8_t y; |
| 185 | uint8_t number_of_rows = GLCD_LCD_HEIGHT / 8; |
| 186 | for(y=0; y<number_of_rows; y++) |
| 187 | { |
| 188 | if(y < (number_of_rows - 1)) |
| 189 | { |
| 190 | /* All lines except the last */ |
| 191 | memcpy(glcd_buffer_selected + y*GLCD_LCD_WIDTH, |
| 192 | glcd_buffer_selected + y*GLCD_LCD_WIDTH + GLCD_LCD_WIDTH, GLCD_LCD_WIDTH); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | /* Last line, clear it */ |
| 197 | memset(glcd_buffer_selected + (number_of_rows - 1)*GLCD_LCD_WIDTH, 0x00, GLCD_LCD_WIDTH); |
| 198 | } |
| 199 | } |
| 200 | glcd_update_bbox(0,0,GLCD_LCD_WIDTH - 1,GLCD_LCD_HEIGHT - 1); |
| 201 | } |
| 202 | |
| 203 | void glcd_init(void) |
| 204 | { |
| 205 | glcd_HW_init(); |
| 206 | |
| 207 | glcd_select_screen(glcd_buffer,&glcd_bbox); |
| 208 | glcd_clear(); |
| 209 | } |
| 210 | void glcd_write() |
| 211 | { |
| 212 | |
| 213 | uint8_t bank; |
| 214 | |
| 215 | for(bank = 0; bank < GLCD_NUMBER_OF_BANKS; bank++) |
| 216 | { |
| 217 | /* Each bank is a single row 8 bits tall */ |
| 218 | uint8_t column; |
| 219 | |
| 220 | if(glcd_bbox_selected->y_min >= (bank+1)*8) |
| 221 | { |
| 222 | continue; /* Skip the entire bank */ |
| 223 | } |
| 224 | |
| 225 | if(glcd_bbox_selected->y_max < bank*8) |
| 226 | { |
| 227 | break; /* No more banks need updating */ |
| 228 | } |
| 229 | |
| 230 | glcd_set_y_address(bank); |
| 231 | glcd_set_x_address(glcd_bbox_selected->x_min); |
| 232 | |
| 233 | for(column = glcd_bbox_selected->x_min; column <= glcd_bbox_selected->x_max; column++) |
| 234 | { |
| 235 | glcd_data(glcd_buffer_selected[GLCD_NUMBER_OF_COLS * bank + column]); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | glcd_reset_bbox(); |
| 240 | |
| 241 | } |
| 242 | |