|
| 1 | +/* |
| 2 | + * This is the core graphics library for all our displays, providing a common |
| 3 | + * set of graphics primitives (points, lines, circles, etc.). It needs to be |
| 4 | + * paired with a hardware-specific library for each display device we carry |
| 5 | + * (to handle the lower-level functions). |
| 6 | + * |
| 7 | + * Adafruit invests time and resources providing this open source code, please |
| 8 | + * support Adafruit & open-source hardware by purchasing products from Adafruit! |
| 9 | + * |
| 10 | + * Copyright (c) 2013 Adafruit Industries. All rights reserved. |
| 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 | + * - Redistributions of source code must retain the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer. |
| 17 | + * - Redistributions in binary form must reproduce the above copyright notice, |
| 18 | + * this list of conditions and the following disclaimer in the documentation |
| 19 | + * and/or other materials provided with the distribution. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 25 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + * POSSIBILITY OF SUCH DAMAGE. |
| 32 | + * |
| 33 | + * Modified the AdaFruit library to be a C library, changed the font and |
| 34 | + * generally munged it in a variety of ways, creating a reasonably quick |
| 35 | + * and dirty way to put something "interesting" on the LCD display. |
| 36 | + * --Chuck McManis (2013, 2014) |
| 37 | + * |
| 38 | + */ |
| 39 | + |
| 40 | +#include <stdint.h> |
| 41 | +#include <math.h> |
| 42 | +#include <stdlib.h> |
| 43 | +#include "gfx.h" |
| 44 | + |
| 45 | +struct gfx_state __gfx_state; |
| 46 | + |
| 47 | +void |
| 48 | +gfx_draw_pixel(int x, int y, uint16_t color) |
| 49 | +{ |
| 50 | + if ((x < 0) || (x >= __gfx_state._width) || |
| 51 | + (y < 0) || (y >= __gfx_state._height)) { |
| 52 | + return; /* off screen so don't draw it */ |
| 53 | + } |
| 54 | + (__gfx_state.drawpixel)(x, y, color); |
| 55 | +} |
| 56 | +#define true 1 |
| 57 | + |
| 58 | +void |
| 59 | +gfx_init(void (*pixel_func)(int, int, uint16_t), int width, int height) |
| 60 | +{ |
| 61 | + __gfx_state._width = width; |
| 62 | + __gfx_state._height = height; |
| 63 | + __gfx_state.rotation = 0; |
| 64 | + __gfx_state.cursor_y = __gfx_state.cursor_x = 0; |
| 65 | + __gfx_state.textsize = 1; |
| 66 | + __gfx_state.textcolor = 0; |
| 67 | + __gfx_state.textbgcolor = 0xFFFF; |
| 68 | + __gfx_state.wrap = true; |
| 69 | + __gfx_state.drawpixel = pixel_func; |
| 70 | +} |
| 71 | + |
| 72 | +/* Bresenham's algorithm - thx wikpedia */ |
| 73 | +void gfx_draw_line(int16_t x0, int16_t y0, |
| 74 | + int16_t x1, int16_t y1, |
| 75 | + uint16_t color) |
| 76 | +{ |
| 77 | + int16_t steep = abs(y1 - y0) > abs(x1 - x0); |
| 78 | + if (steep) { |
| 79 | + swap(x0, y0); |
| 80 | + swap(x1, y1); |
| 81 | + } |
| 82 | + |
| 83 | + if (x0 > x1) { |
| 84 | + swap(x0, x1); |
| 85 | + swap(y0, y1); |
| 86 | + } |
| 87 | + |
| 88 | + int16_t dx, dy; |
| 89 | + dx = x1 - x0; |
| 90 | + dy = abs(y1 - y0); |
| 91 | + |
| 92 | + int16_t err = dx / 2; |
| 93 | + int16_t ystep; |
| 94 | + |
| 95 | + if (y0 < y1) { |
| 96 | + ystep = 1; |
| 97 | + } else { |
| 98 | + ystep = -1; |
| 99 | + } |
| 100 | + |
| 101 | + for (; x0 <= x1; x0++) { |
| 102 | + if (steep) { |
| 103 | + gfx_draw_pixel(y0, x0, color); |
| 104 | + } else { |
| 105 | + gfx_draw_pixel(x0, y0, color); |
| 106 | + } |
| 107 | + err -= dy; |
| 108 | + if (err < 0) { |
| 109 | + y0 += ystep; |
| 110 | + err += dx; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +void gfx_draw_fast_vline(int16_t x, int16_t y, |
| 116 | + int16_t h, uint16_t color) |
| 117 | +{ |
| 118 | + /* Update in subclasses if desired! */ |
| 119 | + gfx_draw_line(x, y, x, y + h - 1, color); |
| 120 | +} |
| 121 | + |
| 122 | +void gfx_fill_rect(int16_t x, int16_t y, int16_t w, int16_t h, |
| 123 | + uint16_t color) |
| 124 | +{ |
| 125 | + /* Update in subclasses if desired! */ |
| 126 | + int16_t i; |
| 127 | + for (i = x; i < x + w; i++) { |
| 128 | + gfx_draw_fast_vline(i, y, h, color); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +void gfx_fill_screen(uint16_t color) |
| 133 | +{ |
| 134 | + gfx_fill_rect(0, 0, __gfx_state._width, __gfx_state._height, color); |
| 135 | +} |
| 136 | + |
| 137 | +/* Return the size of the display (per current rotation) */ |
| 138 | +uint16_t gfx_width(void) |
| 139 | +{ |
| 140 | + return __gfx_state._width; |
| 141 | +} |
| 142 | + |
| 143 | +uint16_t gfx_height(void) |
| 144 | +{ |
| 145 | + return __gfx_state._height; |
| 146 | +} |
| 147 | + |
0 commit comments