Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
u8g_pb8h1.c
00001 /* 00002 00003 u8g_pb8h1.c 00004 00005 8bit height monochrom (1 bit) page buffer 00006 byte has horizontal orientation 00007 00008 Universal 8bit Graphics Library 00009 00010 Copyright (c) 2011, olikraus@gmail.com 00011 All rights reserved. 00012 00013 Redistribution and use in source and binary forms, with or without modification, 00014 are permitted provided that the following conditions are met: 00015 00016 * Redistributions of source code must retain the above copyright notice, this list 00017 of conditions and the following disclaimer. 00018 00019 * Redistributions in binary form must reproduce the above copyright notice, this 00020 list of conditions and the following disclaimer in the documentation and/or other 00021 materials provided with the distribution. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 00024 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 00025 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00026 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00028 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00029 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00030 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00031 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00032 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00033 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00034 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 00035 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 00037 00038 total buffer size is limited to 256 bytes because of the calculation inside the set pixel procedure 00039 00040 23. Sep 2012: Bug with down procedure, see FPS 1st page --> fixed (bug located in u8g_clip.c) 00041 00042 */ 00043 00044 #include "u8g.h" 00045 #include <string.h> 00046 00047 #ifdef __unix__ 00048 #include <assert.h> 00049 #endif 00050 00051 /* NEW_CODE disabled, because the performance increase was too slow and not worth compared */ 00052 /* to the increase of code size */ 00053 /* #define NEW_CODE */ 00054 00055 #ifdef __unix__ 00056 void *u8g_buf_lower_limit; 00057 void *u8g_buf_upper_limit; 00058 #endif 00059 00060 void u8g_pb8h1_Init(u8g_pb_t *b, void *buf, u8g_uint_t width) U8G_NOINLINE; 00061 void u8g_pb8h1_set_pixel(u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y, uint8_t color_index) U8G_NOINLINE; 00062 void u8g_pb8h1_SetPixel(u8g_pb_t *b, const u8g_dev_arg_pixel_t * const arg_pixel) U8G_NOINLINE ; 00063 void u8g_pb8h1_Set8PixelStd(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel) U8G_NOINLINE; 00064 uint8_t u8g_dev_pb8h1_base_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg); 00065 00066 00067 #ifdef NEW_CODE 00068 struct u8g_pb_h1_struct 00069 { 00070 u8g_uint_t x; 00071 u8g_uint_t y; 00072 uint8_t *ptr; 00073 uint8_t mask; 00074 uint8_t line_byte_len; 00075 uint8_t cnt; 00076 }; 00077 00078 static uint8_t u8g_pb8h1_bitmask[8] = { 0x080, 0x040, 0x020, 0x010, 0x008, 0x004, 0x002, 0x001 }; 00079 00080 static void u8g_pb8h1_state_right(struct u8g_pb_h1_struct *s) U8G_NOINLINE; 00081 static void u8g_pb8h1_state_right(struct u8g_pb_h1_struct *s) 00082 { 00083 register u8g_uint_t x; 00084 x = s->x; 00085 x++; 00086 s->x = x; 00087 x &= 7; 00088 s->mask = u8g_pb8h1_bitmask[x]; 00089 if ( x == 0 ) 00090 s->ptr++; 00091 } 00092 00093 static void u8g_pb8h1_state_left(struct u8g_pb_h1_struct *s) 00094 { 00095 register u8g_uint_t x; 00096 x = s->x; 00097 x--; 00098 s->x = x; 00099 x &= 7; 00100 s->mask = u8g_pb8h1_bitmask[x]; 00101 if ( x == 7 ) 00102 s->ptr--; 00103 } 00104 00105 static void u8g_pb8h1_state_down(struct u8g_pb_h1_struct *s) 00106 { 00107 s->y++; 00108 s->ptr += s->line_byte_len; 00109 } 00110 00111 static void u8g_pb8h1_state_up(struct u8g_pb_h1_struct *s) 00112 { 00113 s->y--; 00114 s->ptr -= s->line_byte_len; 00115 } 00116 00117 static void u8g_pb8h1_state_init(struct u8g_pb_h1_struct *s, u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y) U8G_NOINLINE; 00118 static void u8g_pb8h1_state_init(struct u8g_pb_h1_struct *s, u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y) 00119 { 00120 u8g_uint_t tmp; 00121 00122 uint8_t *ptr = b->buf; 00123 00124 s->x = x; 00125 s->y = y; 00126 00127 y -= b->p.page_y0; 00128 00129 tmp = b->width; 00130 tmp >>= 3; 00131 s->line_byte_len = tmp; 00132 00133 /* assume negative y values, can be down to -7, subtract this from the pointer and add correction of 8 to y */ 00134 ptr -= tmp*8; 00135 y+=8; 00136 /* it is important that the result of tmp*y can be 16 bit value also for 8 bit mode */ 00137 ptr += tmp*y; 00138 00139 s->mask = u8g_pb8h1_bitmask[x & 7]; 00140 00141 /* assume negative x values (to -7), subtract 8 pixel from the pointer and add 8 to x */ 00142 ptr--; 00143 x += 8; 00144 x >>= 3; 00145 ptr += x; 00146 s->ptr = ptr; 00147 } 00148 00149 static void u8g_pb8h1_state_set_pixel(struct u8g_pb_h1_struct *s, uint8_t color_index) U8G_NOINLINE; 00150 static void u8g_pb8h1_state_set_pixel(struct u8g_pb_h1_struct *s, uint8_t color_index) 00151 { 00152 00153 #ifdef __unix__ 00154 assert( s->ptr >= u8g_buf_lower_limit ); 00155 assert( s->ptr < u8g_buf_upper_limit ); 00156 #endif 00157 00158 if ( color_index ) 00159 { 00160 *s->ptr |= s->mask; 00161 } 00162 else 00163 { 00164 uint8_t mask = s->mask; 00165 mask ^=0xff; 00166 *s->ptr &= mask; 00167 } 00168 } 00169 #endif 00170 00171 00172 void u8g_pb8h1_Init(u8g_pb_t *b, void *buf, u8g_uint_t width) 00173 { 00174 b->buf = buf; 00175 b->width = width; 00176 u8g_pb_Clear(b); 00177 } 00178 00179 /* limitation: total buffer must not exceed 256 bytes */ 00180 void u8g_pb8h1_set_pixel(u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y, uint8_t color_index) 00181 { 00182 #ifdef NEW_CODE 00183 struct u8g_pb_h1_struct s; 00184 u8g_pb8h1_state_init(&s, b, x, y); 00185 u8g_pb8h1_state_set_pixel(&s, color_index); 00186 00187 // u8g_pb8h1_state_up(&s); 00188 // if ( s.y > b->p.page_y1 ) 00189 // return; 00190 // if ( s.x > b->width ) 00191 // return; 00192 // u8g_pb8h1_state_set_pixel(&s, color_index); 00193 #else 00194 register uint8_t mask; 00195 u8g_uint_t tmp; 00196 uint8_t *ptr = b->buf; 00197 00198 y -= b->p.page_y0; 00199 tmp = b->width; 00200 tmp >>= 3; 00201 tmp *= (uint8_t)y; 00202 ptr += tmp; 00203 00204 mask = 0x080; 00205 mask >>= x & 7; 00206 x >>= 3; 00207 ptr += x; 00208 if ( color_index ) 00209 { 00210 *ptr |= mask; 00211 } 00212 else 00213 { 00214 mask ^=0xff; 00215 *ptr &= mask; 00216 } 00217 #endif 00218 } 00219 00220 00221 void u8g_pb8h1_SetPixel(u8g_pb_t *b, const u8g_dev_arg_pixel_t * const arg_pixel) 00222 { 00223 if ( arg_pixel->y < b->p.page_y0 ) 00224 return; 00225 if ( arg_pixel->y > b->p.page_y1 ) 00226 return; 00227 if ( arg_pixel->x >= b->width ) 00228 return; 00229 u8g_pb8h1_set_pixel(b, arg_pixel->x, arg_pixel->y, arg_pixel->color); 00230 } 00231 00232 void u8g_pb8h1_Set8PixelStd(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel) 00233 { 00234 register uint8_t pixel = arg_pixel->pixel; 00235 do 00236 { 00237 if ( pixel & 128 ) 00238 { 00239 u8g_pb8h1_SetPixel(b, arg_pixel); 00240 } 00241 switch( arg_pixel->dir ) 00242 { 00243 case 0: arg_pixel->x++; break; 00244 case 1: arg_pixel->y++; break; 00245 case 2: arg_pixel->x--; break; 00246 case 3: arg_pixel->y--; break; 00247 } 00248 pixel <<= 1; 00249 } while( pixel != 0 ); 00250 } 00251 00252 void u8g_pb8h1_Set8PixelOpt2(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel) 00253 { 00254 register uint8_t pixel = arg_pixel->pixel; 00255 u8g_uint_t dx = 0; 00256 u8g_uint_t dy = 0; 00257 00258 switch( arg_pixel->dir ) 00259 { 00260 case 0: dx++; break; 00261 case 1: dy++; break; 00262 case 2: dx--; break; 00263 case 3: dy--; break; 00264 } 00265 00266 do 00267 { 00268 if ( pixel & 128 ) 00269 u8g_pb8h1_SetPixel(b, arg_pixel); 00270 arg_pixel->x += dx; 00271 arg_pixel->y += dy; 00272 pixel <<= 1; 00273 } while( pixel != 0 ); 00274 } 00275 00276 #ifdef NEW_CODE 00277 static void u8g_pb8h1_Set8PixelState(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel) 00278 { 00279 register uint8_t pixel = arg_pixel->pixel; 00280 struct u8g_pb_h1_struct s; 00281 uint8_t cnt; 00282 u8g_pb8h1_state_init(&s, b, arg_pixel->x, arg_pixel->y); 00283 cnt = 8; 00284 switch( arg_pixel->dir ) 00285 { 00286 case 0: 00287 do 00288 { 00289 if ( s.x < b->width ) 00290 if ( pixel & 128 ) 00291 u8g_pb8h1_state_set_pixel(&s, arg_pixel->color); 00292 u8g_pb8h1_state_right(&s); 00293 pixel <<= 1; 00294 cnt--; 00295 } while( cnt > 0 && pixel != 0 ); 00296 break; 00297 case 1: 00298 do 00299 { 00300 if ( s.y >= b->p.page_y0 ) 00301 if ( s.y <= b->p.page_y1 ) 00302 if ( pixel & 128 ) 00303 u8g_pb8h1_state_set_pixel(&s, arg_pixel->color); 00304 u8g_pb8h1_state_down(&s); 00305 pixel <<= 1; 00306 cnt--; 00307 } while( cnt > 0 && pixel != 0 ); 00308 break; 00309 case 2: 00310 do 00311 { 00312 if ( s.x < b->width ) 00313 if ( pixel & 128 ) 00314 u8g_pb8h1_state_set_pixel(&s, arg_pixel->color); 00315 u8g_pb8h1_state_left(&s); 00316 pixel <<= 1; 00317 cnt--; 00318 } while( cnt > 0 && pixel != 0 ); 00319 break; 00320 case 3: 00321 do 00322 { 00323 if ( s.y >= b->p.page_y0 ) 00324 if ( s.y <= b->p.page_y1 ) 00325 if ( pixel & 128 ) 00326 u8g_pb8h1_state_set_pixel(&s, arg_pixel->color); 00327 u8g_pb8h1_state_up(&s); 00328 pixel <<= 1; 00329 cnt--; 00330 } while( cnt > 0 && pixel != 0 ); 00331 break; 00332 } 00333 } 00334 #endif 00335 00336 uint8_t u8g_dev_pb8h1_base_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg) 00337 { 00338 u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem); 00339 switch(msg) 00340 { 00341 case U8G_DEV_MSG_SET_8PIXEL: 00342 #ifdef NEW_CODE 00343 if ( u8g_pb_Is8PixelVisible(pb, (u8g_dev_arg_pixel_t *)arg) ) 00344 u8g_pb8h1_Set8PixelState(pb, (u8g_dev_arg_pixel_t *)arg); 00345 #else 00346 if ( u8g_pb_Is8PixelVisible(pb, (u8g_dev_arg_pixel_t *)arg) ) 00347 u8g_pb8h1_Set8PixelOpt2(pb, (u8g_dev_arg_pixel_t *)arg); 00348 #endif 00349 break; 00350 case U8G_DEV_MSG_SET_PIXEL: 00351 u8g_pb8h1_SetPixel(pb, (u8g_dev_arg_pixel_t *)arg); 00352 break; 00353 case U8G_DEV_MSG_INIT: 00354 break; 00355 case U8G_DEV_MSG_STOP: 00356 break; 00357 case U8G_DEV_MSG_PAGE_FIRST: 00358 u8g_pb_Clear(pb); 00359 u8g_page_First(&(pb->p)); 00360 break; 00361 case U8G_DEV_MSG_PAGE_NEXT: 00362 if ( u8g_page_Next(&(pb->p)) == 0 ) 00363 return 0; 00364 u8g_pb_Clear(pb); 00365 break; 00366 #ifdef U8G_DEV_MSG_IS_BBX_INTERSECTION 00367 case U8G_DEV_MSG_IS_BBX_INTERSECTION: 00368 return u8g_pb_IsIntersection(pb, (u8g_dev_arg_bbx_t *)arg); 00369 #endif 00370 case U8G_DEV_MSG_GET_PAGE_BOX: 00371 u8g_pb_GetPageBox(pb, (u8g_box_t *)arg); 00372 break; 00373 case U8G_DEV_MSG_GET_WIDTH: 00374 *((u8g_uint_t *)arg) = pb->width; 00375 break; 00376 case U8G_DEV_MSG_GET_HEIGHT: 00377 *((u8g_uint_t *)arg) = pb->p.total_height; 00378 break; 00379 case U8G_DEV_MSG_SET_COLOR_ENTRY: 00380 break; 00381 case U8G_DEV_MSG_SET_XY_CB: 00382 break; 00383 case U8G_DEV_MSG_GET_MODE: 00384 return U8G_MODE_BW; 00385 } 00386 return 1; 00387 } 00388 00389 00390
Generated on Tue Jul 12 2022 17:30:58 by
1.7.2