Displays distance to start location on OLED screen.

Dependencies:   mbed

Committer:
iforce2d
Date:
Wed Mar 07 12:49:14 2018 +0000
Revision:
0:972874f31c98
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iforce2d 0:972874f31c98 1 /*
iforce2d 0:972874f31c98 2
iforce2d 0:972874f31c98 3 u8g_pbxh24.c
iforce2d 0:972874f31c98 4
iforce2d 0:972874f31c98 5 x lines per page, horizontal, 24 bits per pixel (true color modes)
iforce2d 0:972874f31c98 6
iforce2d 0:972874f31c98 7 Universal 8bit Graphics Library
iforce2d 0:972874f31c98 8
iforce2d 0:972874f31c98 9 Copyright (c) 2013, olikraus@gmail.com
iforce2d 0:972874f31c98 10 All rights reserved.
iforce2d 0:972874f31c98 11
iforce2d 0:972874f31c98 12 Redistribution and use in source and binary forms, with or without modification,
iforce2d 0:972874f31c98 13 are permitted provided that the following conditions are met:
iforce2d 0:972874f31c98 14
iforce2d 0:972874f31c98 15 * Redistributions of source code must retain the above copyright notice, this list
iforce2d 0:972874f31c98 16 of conditions and the following disclaimer.
iforce2d 0:972874f31c98 17
iforce2d 0:972874f31c98 18 * Redistributions in binary form must reproduce the above copyright notice, this
iforce2d 0:972874f31c98 19 list of conditions and the following disclaimer in the documentation and/or other
iforce2d 0:972874f31c98 20 materials provided with the distribution.
iforce2d 0:972874f31c98 21
iforce2d 0:972874f31c98 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
iforce2d 0:972874f31c98 23 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
iforce2d 0:972874f31c98 24 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
iforce2d 0:972874f31c98 25 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
iforce2d 0:972874f31c98 26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
iforce2d 0:972874f31c98 27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
iforce2d 0:972874f31c98 28 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
iforce2d 0:972874f31c98 29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
iforce2d 0:972874f31c98 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
iforce2d 0:972874f31c98 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
iforce2d 0:972874f31c98 32 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
iforce2d 0:972874f31c98 33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
iforce2d 0:972874f31c98 34 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
iforce2d 0:972874f31c98 35
iforce2d 0:972874f31c98 36
iforce2d 0:972874f31c98 37 struct _u8g_pb_t
iforce2d 0:972874f31c98 38 {
iforce2d 0:972874f31c98 39 u8g_page_t p;
iforce2d 0:972874f31c98 40 u8g_uint_t width;
iforce2d 0:972874f31c98 41 void *buf;
iforce2d 0:972874f31c98 42 };
iforce2d 0:972874f31c98 43 typedef struct _u8g_pb_t u8g_pb_t;
iforce2d 0:972874f31c98 44
iforce2d 0:972874f31c98 45
iforce2d 0:972874f31c98 46 uint8_t u8g_index_color_xh16_buf[2*WIDTH*PAGE_HEIGHT] U8G_NOCOMMON ;
iforce2d 0:972874f31c98 47 u8g_pb_t u8g_index_color_xh16_pb = { {PAGE_HEIGHT, HEIGHT, 0, 0, 0}, WIDTH, u8g_index_color_xh16_buf};
iforce2d 0:972874f31c98 48 u8g_dev_t name = { dev_fn, &u8g_index_color_xh16_pb , com_fn }
iforce2d 0:972874f31c98 49
iforce2d 0:972874f31c98 50 */
iforce2d 0:972874f31c98 51
iforce2d 0:972874f31c98 52 #include "u8g.h"
iforce2d 0:972874f31c98 53
iforce2d 0:972874f31c98 54 /*
iforce2d 0:972874f31c98 55 #define WIDTH_BITS 7
iforce2d 0:972874f31c98 56 #define WIDTH (1<<WIDTH_BITS)
iforce2d 0:972874f31c98 57 #define PAGE_HEIGHT_BITS 3
iforce2d 0:972874f31c98 58 #define PAGE_HEIGHT (1<<PAGE_HEIGHT_BITS)
iforce2d 0:972874f31c98 59 */
iforce2d 0:972874f31c98 60
iforce2d 0:972874f31c98 61 void u8g_pbxh24_Clear(u8g_pb_t *b)
iforce2d 0:972874f31c98 62 {
iforce2d 0:972874f31c98 63 uint8_t *ptr = (uint8_t *)b->buf;
iforce2d 0:972874f31c98 64 uint8_t *end_ptr = ptr;
iforce2d 0:972874f31c98 65 uint8_t cnt = b->p.page_height;
iforce2d 0:972874f31c98 66 do
iforce2d 0:972874f31c98 67 {
iforce2d 0:972874f31c98 68 end_ptr += b->width*3;
iforce2d 0:972874f31c98 69 cnt--;
iforce2d 0:972874f31c98 70 } while( cnt > 0 );
iforce2d 0:972874f31c98 71 do
iforce2d 0:972874f31c98 72 {
iforce2d 0:972874f31c98 73 *ptr++ = 0;
iforce2d 0:972874f31c98 74 } while( ptr != end_ptr );
iforce2d 0:972874f31c98 75 }
iforce2d 0:972874f31c98 76
iforce2d 0:972874f31c98 77
iforce2d 0:972874f31c98 78 void u8g_pbxh24_Init(u8g_pb_t *b, void *buf, u8g_uint_t width)
iforce2d 0:972874f31c98 79 {
iforce2d 0:972874f31c98 80 b->buf = buf;
iforce2d 0:972874f31c98 81 b->width = width;
iforce2d 0:972874f31c98 82 u8g_pbxh24_Clear(b);
iforce2d 0:972874f31c98 83 }
iforce2d 0:972874f31c98 84
iforce2d 0:972874f31c98 85 #ifdef OBSOLETE
iforce2d 0:972874f31c98 86 static void u8g_pbxh24_set_pixel(u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y, uint8_t r, uint8_t g, uint8_t b)
iforce2d 0:972874f31c98 87 {
iforce2d 0:972874f31c98 88 uint16_t tmp;
iforce2d 0:972874f31c98 89 uint8_t *ptr = b->buf;
iforce2d 0:972874f31c98 90 y -= b->p.page_y0;
iforce2d 0:972874f31c98 91 tmp = y;
iforce2d 0:972874f31c98 92 tmp *= b->width;
iforce2d 0:972874f31c98 93 tmp += x;
iforce2d 0:972874f31c98 94 tmp *= 3;
iforce2d 0:972874f31c98 95 ptr += tmp;
iforce2d 0:972874f31c98 96 *ptr = r;
iforce2d 0:972874f31c98 97 ptr++;
iforce2d 0:972874f31c98 98 *ptr = g;
iforce2d 0:972874f31c98 99 ptr++;
iforce2d 0:972874f31c98 100 *ptr = b;
iforce2d 0:972874f31c98 101 }
iforce2d 0:972874f31c98 102 #endif
iforce2d 0:972874f31c98 103
iforce2d 0:972874f31c98 104 /*
iforce2d 0:972874f31c98 105 intensity
iforce2d 0:972874f31c98 106 0..3 intensity value
iforce2d 0:972874f31c98 107 4 replace color
iforce2d 0:972874f31c98 108 */
iforce2d 0:972874f31c98 109 static void u8g_pbxh24_set_tpixel(u8g_pb_t *b, u8g_uint_t x, u8g_uint_t y, uint8_t red, uint8_t green, uint8_t blue, uint8_t intensity)
iforce2d 0:972874f31c98 110 {
iforce2d 0:972874f31c98 111 uint16_t tmp;
iforce2d 0:972874f31c98 112 uint8_t *ptr = b->buf;
iforce2d 0:972874f31c98 113
iforce2d 0:972874f31c98 114 if ( intensity == 0 )
iforce2d 0:972874f31c98 115 return;
iforce2d 0:972874f31c98 116
iforce2d 0:972874f31c98 117 y -= b->p.page_y0;
iforce2d 0:972874f31c98 118 tmp = y;
iforce2d 0:972874f31c98 119 tmp *= b->width;
iforce2d 0:972874f31c98 120 tmp += x;
iforce2d 0:972874f31c98 121 tmp *= 3;
iforce2d 0:972874f31c98 122 ptr += tmp;
iforce2d 0:972874f31c98 123
iforce2d 0:972874f31c98 124 if ( intensity == 4 )
iforce2d 0:972874f31c98 125 {
iforce2d 0:972874f31c98 126 *ptr = red;
iforce2d 0:972874f31c98 127 ptr++;
iforce2d 0:972874f31c98 128 *ptr = green;
iforce2d 0:972874f31c98 129 ptr++;
iforce2d 0:972874f31c98 130 *ptr = blue;
iforce2d 0:972874f31c98 131 return;
iforce2d 0:972874f31c98 132 }
iforce2d 0:972874f31c98 133
iforce2d 0:972874f31c98 134 if ( intensity == 2 )
iforce2d 0:972874f31c98 135 {
iforce2d 0:972874f31c98 136 /*
iforce2d 0:972874f31c98 137 red = red/4 + red/2;
iforce2d 0:972874f31c98 138 green = green/4 + green/2;
iforce2d 0:972874f31c98 139 blue = blue/4 + blue/2;
iforce2d 0:972874f31c98 140 */
iforce2d 0:972874f31c98 141 red >>= 1;
iforce2d 0:972874f31c98 142 green >>= 1;
iforce2d 0:972874f31c98 143 blue >>= 1;
iforce2d 0:972874f31c98 144 }
iforce2d 0:972874f31c98 145 else if ( intensity == 1 )
iforce2d 0:972874f31c98 146 {
iforce2d 0:972874f31c98 147 red >>= 2;
iforce2d 0:972874f31c98 148 green >>= 2;
iforce2d 0:972874f31c98 149 blue >>= 2;
iforce2d 0:972874f31c98 150 }
iforce2d 0:972874f31c98 151
iforce2d 0:972874f31c98 152 if ( *ptr >= 255-red ) *ptr = 255;
iforce2d 0:972874f31c98 153 else *ptr += red;
iforce2d 0:972874f31c98 154 ptr++;
iforce2d 0:972874f31c98 155
iforce2d 0:972874f31c98 156 if ( *ptr >= 255-green ) *ptr = 255;
iforce2d 0:972874f31c98 157 else *ptr += green;
iforce2d 0:972874f31c98 158 ptr++;
iforce2d 0:972874f31c98 159
iforce2d 0:972874f31c98 160 if ( *ptr >= 255-blue ) *ptr = 255;
iforce2d 0:972874f31c98 161 else *ptr += blue;
iforce2d 0:972874f31c98 162
iforce2d 0:972874f31c98 163 /*
iforce2d 0:972874f31c98 164 if ( *ptr < red ) *ptr = red;
iforce2d 0:972874f31c98 165 ptr++;
iforce2d 0:972874f31c98 166 if ( *ptr < green ) *ptr = green;
iforce2d 0:972874f31c98 167 ptr++;
iforce2d 0:972874f31c98 168 if ( *ptr < blue ) *ptr = blue;
iforce2d 0:972874f31c98 169 */
iforce2d 0:972874f31c98 170
iforce2d 0:972874f31c98 171
iforce2d 0:972874f31c98 172 }
iforce2d 0:972874f31c98 173
iforce2d 0:972874f31c98 174 void u8g_pbxh24_SetTPixel(u8g_pb_t *b, const u8g_dev_arg_pixel_t * const arg_pixel, uint8_t intensity)
iforce2d 0:972874f31c98 175 {
iforce2d 0:972874f31c98 176 if ( arg_pixel->y < b->p.page_y0 )
iforce2d 0:972874f31c98 177 return;
iforce2d 0:972874f31c98 178 if ( arg_pixel->y > b->p.page_y1 )
iforce2d 0:972874f31c98 179 return;
iforce2d 0:972874f31c98 180 if ( arg_pixel->x >= b->width )
iforce2d 0:972874f31c98 181 return;
iforce2d 0:972874f31c98 182 u8g_pbxh24_set_tpixel(b, arg_pixel->x, arg_pixel->y, arg_pixel->color, arg_pixel->hi_color, arg_pixel->blue, intensity);
iforce2d 0:972874f31c98 183 }
iforce2d 0:972874f31c98 184
iforce2d 0:972874f31c98 185
iforce2d 0:972874f31c98 186 void u8g_pbxh24_Set8Pixel(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel)
iforce2d 0:972874f31c98 187 {
iforce2d 0:972874f31c98 188 register uint8_t pixel = arg_pixel->pixel;
iforce2d 0:972874f31c98 189 u8g_uint_t dx = 0;
iforce2d 0:972874f31c98 190 u8g_uint_t dy = 0;
iforce2d 0:972874f31c98 191
iforce2d 0:972874f31c98 192 switch( arg_pixel->dir )
iforce2d 0:972874f31c98 193 {
iforce2d 0:972874f31c98 194 case 0: dx++; break;
iforce2d 0:972874f31c98 195 case 1: dy++; break;
iforce2d 0:972874f31c98 196 case 2: dx--; break;
iforce2d 0:972874f31c98 197 case 3: dy--; break;
iforce2d 0:972874f31c98 198 }
iforce2d 0:972874f31c98 199
iforce2d 0:972874f31c98 200 do
iforce2d 0:972874f31c98 201 {
iforce2d 0:972874f31c98 202 if ( pixel & 128 )
iforce2d 0:972874f31c98 203 u8g_pbxh24_SetTPixel(b, arg_pixel, 4);
iforce2d 0:972874f31c98 204 arg_pixel->x += dx;
iforce2d 0:972874f31c98 205 arg_pixel->y += dy;
iforce2d 0:972874f31c98 206 pixel <<= 1;
iforce2d 0:972874f31c98 207 } while( pixel != 0 );
iforce2d 0:972874f31c98 208 }
iforce2d 0:972874f31c98 209
iforce2d 0:972874f31c98 210 void u8g_pbxh24_Set4TPixel(u8g_pb_t *b, u8g_dev_arg_pixel_t *arg_pixel)
iforce2d 0:972874f31c98 211 {
iforce2d 0:972874f31c98 212 register uint8_t pixel = arg_pixel->pixel;
iforce2d 0:972874f31c98 213 u8g_uint_t dx = 0;
iforce2d 0:972874f31c98 214 u8g_uint_t dy = 0;
iforce2d 0:972874f31c98 215
iforce2d 0:972874f31c98 216 switch( arg_pixel->dir )
iforce2d 0:972874f31c98 217 {
iforce2d 0:972874f31c98 218 case 0: dx++; break;
iforce2d 0:972874f31c98 219 case 1: dy++; break;
iforce2d 0:972874f31c98 220 case 2: dx--; break;
iforce2d 0:972874f31c98 221 case 3: dy--; break;
iforce2d 0:972874f31c98 222 }
iforce2d 0:972874f31c98 223
iforce2d 0:972874f31c98 224 do
iforce2d 0:972874f31c98 225 {
iforce2d 0:972874f31c98 226 u8g_pbxh24_SetTPixel(b, arg_pixel, pixel >> 6);
iforce2d 0:972874f31c98 227 arg_pixel->x += dx;
iforce2d 0:972874f31c98 228 arg_pixel->y += dy;
iforce2d 0:972874f31c98 229 pixel <<= 2;
iforce2d 0:972874f31c98 230 } while( pixel != 0 );
iforce2d 0:972874f31c98 231 }
iforce2d 0:972874f31c98 232
iforce2d 0:972874f31c98 233
iforce2d 0:972874f31c98 234 uint8_t u8g_dev_pbxh24_base_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, void *arg)
iforce2d 0:972874f31c98 235 {
iforce2d 0:972874f31c98 236 u8g_pb_t *pb = (u8g_pb_t *)(dev->dev_mem);
iforce2d 0:972874f31c98 237 switch(msg)
iforce2d 0:972874f31c98 238 {
iforce2d 0:972874f31c98 239 case U8G_DEV_MSG_SET_8PIXEL:
iforce2d 0:972874f31c98 240 if ( u8g_pb_Is8PixelVisible(pb, (u8g_dev_arg_pixel_t *)arg) )
iforce2d 0:972874f31c98 241 u8g_pbxh24_Set8Pixel(pb, (u8g_dev_arg_pixel_t *)arg);
iforce2d 0:972874f31c98 242 break;
iforce2d 0:972874f31c98 243 case U8G_DEV_MSG_SET_PIXEL:
iforce2d 0:972874f31c98 244 u8g_pbxh24_SetTPixel(pb, (u8g_dev_arg_pixel_t *)arg, 4);
iforce2d 0:972874f31c98 245 break;
iforce2d 0:972874f31c98 246 case U8G_DEV_MSG_SET_4TPIXEL:
iforce2d 0:972874f31c98 247 u8g_pbxh24_Set4TPixel(pb, (u8g_dev_arg_pixel_t *)arg);
iforce2d 0:972874f31c98 248 break;
iforce2d 0:972874f31c98 249 case U8G_DEV_MSG_SET_TPIXEL:
iforce2d 0:972874f31c98 250 u8g_pbxh24_SetTPixel(pb, (u8g_dev_arg_pixel_t *)arg, ((u8g_dev_arg_pixel_t *)arg)->pixel&3);
iforce2d 0:972874f31c98 251 break;
iforce2d 0:972874f31c98 252 case U8G_DEV_MSG_INIT:
iforce2d 0:972874f31c98 253 break;
iforce2d 0:972874f31c98 254 case U8G_DEV_MSG_STOP:
iforce2d 0:972874f31c98 255 break;
iforce2d 0:972874f31c98 256 case U8G_DEV_MSG_PAGE_FIRST:
iforce2d 0:972874f31c98 257 u8g_pbxh24_Clear(pb);
iforce2d 0:972874f31c98 258 u8g_page_First(&(pb->p));
iforce2d 0:972874f31c98 259 break;
iforce2d 0:972874f31c98 260 case U8G_DEV_MSG_PAGE_NEXT:
iforce2d 0:972874f31c98 261 if ( u8g_page_Next(&(pb->p)) == 0 )
iforce2d 0:972874f31c98 262 return 0;
iforce2d 0:972874f31c98 263 u8g_pbxh24_Clear(pb);
iforce2d 0:972874f31c98 264 break;
iforce2d 0:972874f31c98 265 #ifdef U8G_DEV_MSG_IS_BBX_INTERSECTION
iforce2d 0:972874f31c98 266 case U8G_DEV_MSG_IS_BBX_INTERSECTION:
iforce2d 0:972874f31c98 267 return u8g_pb_IsIntersection(pb, (u8g_dev_arg_bbx_t *)arg);
iforce2d 0:972874f31c98 268 #endif
iforce2d 0:972874f31c98 269 case U8G_DEV_MSG_GET_PAGE_BOX:
iforce2d 0:972874f31c98 270 u8g_pb_GetPageBox(pb, (u8g_box_t *)arg);
iforce2d 0:972874f31c98 271 break;
iforce2d 0:972874f31c98 272 case U8G_DEV_MSG_GET_WIDTH:
iforce2d 0:972874f31c98 273 *((u8g_uint_t *)arg) = pb->width;
iforce2d 0:972874f31c98 274 break;
iforce2d 0:972874f31c98 275 case U8G_DEV_MSG_GET_HEIGHT:
iforce2d 0:972874f31c98 276 *((u8g_uint_t *)arg) = pb->p.total_height;
iforce2d 0:972874f31c98 277 break;
iforce2d 0:972874f31c98 278 case U8G_DEV_MSG_SET_COLOR_ENTRY:
iforce2d 0:972874f31c98 279 break;
iforce2d 0:972874f31c98 280 case U8G_DEV_MSG_SET_XY_CB:
iforce2d 0:972874f31c98 281 break;
iforce2d 0:972874f31c98 282 case U8G_DEV_MSG_GET_MODE:
iforce2d 0:972874f31c98 283 return U8G_MODE_TRUECOLOR;
iforce2d 0:972874f31c98 284 }
iforce2d 0:972874f31c98 285 return 1;
iforce2d 0:972874f31c98 286 }
iforce2d 0:972874f31c98 287
iforce2d 0:972874f31c98 288