| 1 | /* |
| 2 | |
| 3 | u8g2_hvline.c |
| 4 | |
| 5 | Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) |
| 6 | |
| 7 | Copyright (c) 2016, olikraus@gmail.com |
| 8 | All rights reserved. |
| 9 | |
| 10 | Redistribution and use in source and binary forms, with or without modification, |
| 11 | are permitted provided that the following conditions are met: |
| 12 | |
| 13 | * Redistributions of source code must retain the above copyright notice, this list |
| 14 | of conditions and the following disclaimer. |
| 15 | |
| 16 | * Redistributions in binary form must reproduce the above copyright notice, this |
| 17 | list of conditions and the following disclaimer in the documentation and/or other |
| 18 | materials provided with the distribution. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 21 | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 22 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 30 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 32 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | |
| 34 | |
| 35 | Calltree |
| 36 | |
| 37 | void u8g2_DrawHVLine(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
| 38 | u8g2->cb->draw_l90 |
| 39 | u8g2_draw_hv_line_2dir |
| 40 | u8g2->ll_hvline(u8g2, x, y, len, dir); |
| 41 | |
| 42 | |
| 43 | */ |
| 44 | |
| 45 | #include "u8g2.h" |
| 46 | #include <assert.h> |
| 47 | |
| 48 | /*==========================================================*/ |
| 49 | /* intersection procedure */ |
| 50 | |
| 51 | /* |
| 52 | Description: |
| 53 | clip range from pos a (included) with line len (a+len excluded) agains c (included) to d (excluded) |
| 54 | Assumptions: |
| 55 | len > 0 |
| 56 | c <= d (this is not checked) |
| 57 | will return 0 if there is no intersection and if a > b |
| 58 | |
| 59 | */ |
| 60 | |
| 61 | static uint8_t u8g2_clip_intersection2(u8g2_uint_t *ap, u8g2_uint_t *len, u8g2_uint_t c, u8g2_uint_t d) |
| 62 | { |
| 63 | u8g2_uint_t a = *ap; |
| 64 | u8g2_uint_t b; |
| 65 | b = a; |
| 66 | b += *len; |
| 67 | |
| 68 | /* |
| 69 | Description: |
| 70 | clip range from a (included) to b (excluded) agains c (included) to d (excluded) |
| 71 | Assumptions: |
| 72 | a <= b (violation is checked and handled correctly) |
| 73 | c <= d (this is not checked) |
| 74 | will return 0 if there is no intersection and if a > b |
| 75 | |
| 76 | optimized clipping: c is set to 0 --> 27 Oct 2018: again removed the c==0 assumption |
| 77 | |
| 78 | replaced by uint8_t u8g2_clip_intersection2 |
| 79 | */ |
| 80 | |
| 81 | /* handle the a>b case correctly. If code and time is critical, this could */ |
| 82 | /* be removed completly (be aware about memory curruption for wrong */ |
| 83 | /* arguments) or return 0 for a>b (will lead to skipped lines for wrong */ |
| 84 | /* arguments) */ |
| 85 | |
| 86 | /* removing the following if clause completly may lead to memory corruption of a>b */ |
| 87 | if ( a > b ) |
| 88 | { |
| 89 | /* replacing this if with a simple "return 0;" will not handle the case with negative a */ |
| 90 | if ( a < d ) |
| 91 | { |
| 92 | b = d; |
| 93 | b--; |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | a = c; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* from now on, the asumption a <= b is ok */ |
| 102 | |
| 103 | if ( a >= d ) |
| 104 | return 0; |
| 105 | if ( b <= c ) |
| 106 | return 0; |
| 107 | if ( a < c ) |
| 108 | a = c; |
| 109 | if ( b > d ) |
| 110 | b = d; |
| 111 | |
| 112 | *ap = a; |
| 113 | b -= a; |
| 114 | *len = b; |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | /*==========================================================*/ |
| 121 | /* draw procedures */ |
| 122 | |
| 123 | /* |
| 124 | x,y Upper left position of the line within the pixel buffer |
| 125 | len length of the line in pixel, len must not be 0 |
| 126 | dir 0: horizontal line (left to right) |
| 127 | 1: vertical line (top to bottom) |
| 128 | This function first adjusts the y position to the local buffer. Then it |
| 129 | will clip the line and call u8g2_draw_low_level_hv_line() |
| 130 | |
| 131 | */ |
| 132 | void u8g2_draw_hv_line_2dir(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
| 133 | { |
| 134 | |
| 135 | /* clipping happens before the display rotation */ |
| 136 | |
| 137 | /* transform to pixel buffer coordinates */ |
| 138 | y -= u8g2->pixel_curr_row; |
| 139 | |
| 140 | u8g2->ll_hvline(u8g2, x, y, len, dir); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /* |
| 145 | This is the toplevel function for the hv line draw procedures. |
| 146 | This function should be called by the user. |
| 147 | |
| 148 | "dir" may have 4 directions: 0 (left to right), 1, 2, 3 (down up) |
| 149 | */ |
| 150 | void u8g2_DrawHVLine(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, uint8_t dir) |
| 151 | { |
| 152 | /* Make a call to the callback function (e.g. u8g2_draw_l90_r0). */ |
| 153 | /* The callback may rotate the hv line */ |
| 154 | /* after rotation this will call u8g2_draw_hv_line_4dir() */ |
| 155 | |
| 156 | #ifdef U8G2_WITH_CLIP_WINDOW_SUPPORT |
| 157 | if ( u8g2->is_page_clip_window_intersection != 0 ) |
| 158 | #endif /* U8G2_WITH_CLIP_WINDOW_SUPPORT */ |
| 159 | if ( len != 0 ) |
| 160 | { |
| 161 | |
| 162 | /* convert to two directions */ |
| 163 | if ( len > 1 ) |
| 164 | { |
| 165 | if ( dir == 2 ) |
| 166 | { |
| 167 | x -= len; |
| 168 | x++; |
| 169 | } |
| 170 | else if ( dir == 3 ) |
| 171 | { |
| 172 | y -= len; |
| 173 | y++; |
| 174 | } |
| 175 | } |
| 176 | dir &= 1; |
| 177 | |
| 178 | /* clip against the user window */ |
| 179 | if ( dir == 0 ) |
| 180 | { |
| 181 | if ( y < u8g2->user_y0 ) |
| 182 | return; |
| 183 | if ( y >= u8g2->user_y1 ) |
| 184 | return; |
| 185 | if ( u8g2_clip_intersection2(&x, &len, u8g2->user_x0, u8g2->user_x1) == 0 ) |
| 186 | return; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | if ( x < u8g2->user_x0 ) |
| 191 | return; |
| 192 | if ( x >= u8g2->user_x1 ) |
| 193 | return; |
| 194 | if ( u8g2_clip_intersection2(&y, &len, u8g2->user_y0, u8g2->user_y1) == 0 ) |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | u8g2->cb->draw_l90(u8g2, x, y, len, dir); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void u8g2_DrawHLine(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len) |
| 204 | { |
| 205 | // #ifdef U8G2_WITH_INTERSECTION |
| 206 | // if ( u8g2_IsIntersection(u8g2, x, y, x+len, y+1) == 0 ) |
| 207 | // return; |
| 208 | // #endif /* U8G2_WITH_INTERSECTION */ |
| 209 | u8g2_DrawHVLine(u8g2, x, y, len, 0); |
| 210 | } |
| 211 | |
| 212 | void u8g2_DrawVLine(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len) |
| 213 | { |
| 214 | // #ifdef U8G2_WITH_INTERSECTION |
| 215 | // if ( u8g2_IsIntersection(u8g2, x, y, x+1, y+len) == 0 ) |
| 216 | // return; |
| 217 | // #endif /* U8G2_WITH_INTERSECTION */ |
| 218 | u8g2_DrawHVLine(u8g2, x, y, len, 1); |
| 219 | } |
| 220 | |
| 221 | void u8g2_DrawPixel(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y) |
| 222 | { |
| 223 | #ifdef U8G2_WITH_INTERSECTION |
| 224 | if ( y < u8g2->user_y0 ) |
| 225 | return; |
| 226 | if ( y >= u8g2->user_y1 ) |
| 227 | return; |
| 228 | if ( x < u8g2->user_x0 ) |
| 229 | return; |
| 230 | if ( x >= u8g2->user_x1 ) |
| 231 | return; |
| 232 | #endif /* U8G2_WITH_INTERSECTION */ |
| 233 | u8g2_DrawHVLine(u8g2, x, y, 1, 0); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | Assign the draw color for all drawing functions. |
| 238 | color may be 0 or 1. The actual color is defined by the display. |
| 239 | With color = 1 the drawing function will set the display memory to 1. |
| 240 | For OLEDs this ususally means, that the pixel is enabled and the LED |
| 241 | at the pixel is turned on. |
| 242 | On an LCD it usually means that the LCD segment of the pixel is enabled, |
| 243 | which absorbs the light. |
| 244 | For eInk/ePaper it means black ink. |
| 245 | |
| 246 | 7 Jan 2017: Allow color value 2 for XOR operation. |
| 247 | |
| 248 | */ |
| 249 | void u8g2_SetDrawColor(u8g2_t *u8g2, uint8_t color) |
| 250 | { |
| 251 | u8g2->draw_color = color; /* u8g2_SetDrawColor: just assign the argument */ |
| 252 | if ( color >= 3 ) |
| 253 | u8g2->draw_color = 1; /* u8g2_SetDrawColor: make color as one if arg is invalid */ |
| 254 | } |
| 255 | |