acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
Arduino_GFX.h (19752B)
1 /* 2 * start rewrite from: 3 * https://github.com/adafruit/Adafruit-GFX-Library.git 4 */ 5 #ifndef _ARDUINO_GFX_H_ 6 #define _ARDUINO_GFX_H_ 7 8 #include <Arduino.h> 9 #include <Print.h> 10 #include "Arduino_G.h" 11 #include "Arduino_DataBus.h" 12 13 #if !defined(ATTINY_CORE) 14 #include "gfxfont.h" 15 #endif // !defined(ATTINY_CORE) 16 17 #ifndef DEGTORAD 18 #define DEGTORAD 0.017453292519943295769236907684886F 19 #endif 20 21 #if __has_include(<U8g2lib.h>) 22 #include <U8g2lib.h> 23 #define U8G2_FONT_SUPPORT 24 #include "font/u8g2_font_chill7_h_cjk.h" 25 #include "font/u8g2_font_cubic11_h_cjk.h" 26 #include "font/u8g2_font_quan7_h_cjk.h" 27 #include "font/u8g2_font_unifont_h_utf8.h" 28 #include "font/u8g2_font_unifont_t_chinese.h" 29 #include "font/u8g2_font_unifont_t_chinese4.h" 30 #include "font/u8g2_font_unifont_t_cjk.h" 31 #endif 32 33 #define RGB565(r, g, b) ((((r)&0xF8) << 8) | (((g)&0xFC) << 3) | ((b) >> 3)) 34 #define RGB16TO24(c) ((((uint32_t)c & 0xF800) << 8) | ((c & 0x07E0) << 5) | ((c & 0x1F) << 3)) 35 36 #define RGB565_BLACK RGB565(0, 0, 0) 37 #define RGB565_NAVY RGB565(0, 0, 123) 38 #define RGB565_DARKGREEN RGB565(0, 125, 0) 39 #define RGB565_DARKCYAN RGB565(0, 125, 123) 40 #define RGB565_MAROON RGB565(123, 0, 0) 41 #define RGB565_PURPLE RGB565(123, 0, 123) 42 #define RGB565_OLIVE RGB565(123, 125, 0) 43 #define RGB565_LIGHTGREY RGB565(198, 195, 198) 44 #define RGB565_DARKGREY RGB565(123, 125, 123) 45 #define RGB565_BLUE RGB565(0, 0, 255) 46 #define RGB565_GREEN RGB565(0, 255, 0) 47 #define RGB565_CYAN RGB565(0, 255, 255) 48 #define RGB565_RED RGB565(255, 0, 0) 49 #define RGB565_MAGENTA RGB565(255, 0, 255) 50 #define RGB565_YELLOW RGB565(255, 255, 0) 51 #define RGB565_WHITE RGB565(255, 255, 255) 52 #define RGB565_ORANGE RGB565(255, 165, 0) 53 #define RGB565_GREENYELLOW RGB565(173, 255, 41) 54 #define RGB565_PINK RGB565(255, 130, 198) 55 56 // Color definitions 57 #ifndef DISABLE_COLOR_DEFINES 58 #define BLACK RGB565_BLACK 59 #define NAVY RGB565_NAVY 60 #define DARKGREEN RGB565_DARKGREEN 61 #define DARKCYAN RGB565_DARKCYAN 62 #define MAROON RGB565_MAROON 63 #define PURPLE RGB565_PURPLE 64 #define OLIVE RGB565_OLIVE 65 #define LIGHTGREY RGB565_LIGHTGREY 66 #define DARKGREY RGB565_DARKGREY 67 #define BLUE RGB565_BLUE 68 #define GREEN RGB565_GREEN 69 #define CYAN RGB565_CYAN 70 #define RED RGB565_RED 71 #define MAGENTA RGB565_MAGENTA 72 #define YELLOW RGB565_YELLOW 73 #define WHITE RGB565_WHITE 74 #define ORANGE RGB565_ORANGE 75 #define GREENYELLOW RGB565_GREENYELLOW 76 #define PINK RGB565_PINK 77 #endif 78 79 // Many (but maybe not all) non-AVR board installs define macros 80 // for compatibility with existing PROGMEM-reading AVR code. 81 // Do our own checks and defines here for good measure... 82 83 #ifndef pgm_read_byte 84 #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) 85 #endif 86 #ifndef pgm_read_word 87 #define pgm_read_word(addr) (*(const unsigned short *)(addr)) 88 #endif 89 #ifndef pgm_read_dword 90 #define pgm_read_dword(addr) (*(const unsigned long *)(addr)) 91 #endif 92 // workaround of a15 asm compile error 93 #ifdef ESP8266 94 #undef pgm_read_word 95 #define pgm_read_word(addr) (*(const unsigned short *)(addr)) 96 #endif 97 98 // Pointers are a peculiar case...typically 16-bit on AVR boards, 99 // 32 bits elsewhere. Try to accommodate both... 100 101 #if !defined(__INT_MAX__) || (__INT_MAX__ > 0xFFFF) 102 #define pgm_read_pointer(addr) ((void *)pgm_read_dword(addr)) 103 #else 104 #define pgm_read_pointer(addr) ((void *)pgm_read_word(addr)) 105 #endif 106 107 #ifndef _swap_uint8_t 108 #define _swap_uint8_t(a, b) \ 109 { \ 110 uint8_t t = a; \ 111 a = b; \ 112 b = t; \ 113 } 114 #endif 115 116 #ifndef _swap_int16_t 117 #define _swap_int16_t(a, b) \ 118 { \ 119 int16_t t = a; \ 120 a = b; \ 121 b = t; \ 122 } 123 #endif 124 125 #ifndef _diff 126 #define _diff(a, b) ((a > b) ? (a - b) : (b - a)) 127 #endif 128 129 #ifndef _ordered_in_range 130 #define _ordered_in_range(v, a, b) ((a <= v) && (v <= b)) 131 #endif 132 133 #ifndef _in_range 134 #define _in_range(v, a, b) ((a > b) ? _ordered_in_range(v, b, a) : _ordered_in_range(v, a, b)) 135 #endif 136 137 #if !defined(ATTINY_CORE) 138 INLINE GFXglyph *pgm_read_glyph_ptr(const GFXfont *gfxFont, uint8_t c) 139 { 140 #ifdef __AVR__ 141 return &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]); 142 #else 143 // expression in __AVR__ section may generate "dereferencing type-punned pointer will break strict-aliasing rules" warning 144 // In fact, on other platforms (such as STM32) there is no need to do this pointer magic as program memory may be read in a usual way 145 // So expression may be simplified 146 return gfxFont->glyph + c; 147 #endif //__AVR__ 148 } 149 150 INLINE uint8_t *pgm_read_bitmap_ptr(const GFXfont *gfxFont) 151 { 152 #ifdef __AVR__ 153 return (uint8_t *)pgm_read_pointer(&gfxFont->bitmap); 154 #else 155 // expression in __AVR__ section generates "dereferencing type-punned pointer will break strict-aliasing rules" warning 156 // In fact, on other platforms (such as STM32) there is no need to do this pointer magic as program memory may be read in a usual way 157 // So expression may be simplified 158 return gfxFont->bitmap; 159 #endif //__AVR__ 160 } 161 #endif // !defined(ATTINY_CORE) 162 163 /// A generic graphics superclass that can handle all sorts of drawing. At a minimum you can subclass and provide drawPixel(). At a maximum you can do a ton of overriding to optimize. Used for any/all Adafruit displays! 164 #if defined(LITTLE_FOOT_PRINT) 165 class Arduino_GFX : public Print 166 #else 167 class Arduino_GFX : public Print, public Arduino_G 168 #endif // !defined(LITTLE_FOOT_PRINT) 169 { 170 public: 171 Arduino_GFX(int16_t w, int16_t h); // Constructor 172 173 // This MUST be defined by the subclass: 174 virtual bool begin(int32_t speed = GFX_NOT_DEFINED) = 0; 175 virtual void writePixelPreclipped(int16_t x, int16_t y, uint16_t color) = 0; 176 177 // TRANSACTION API / CORE DRAW API 178 // These MAY be overridden by the subclass to provide device-specific 179 // optimized code. Otherwise 'generic' versions are used. 180 virtual void startWrite(); 181 virtual void writeFillRectPreclipped(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); 182 virtual void writeFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); 183 virtual void writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); 184 virtual void writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color); 185 virtual void endWrite(void); 186 187 // CONTROL API 188 // These MAY be overridden by the subclass to provide device-specific 189 // optimized code. Otherwise 'generic' versions are used. 190 virtual void setRotation(uint8_t r); 191 virtual void invertDisplay(bool i); 192 virtual void displayOn(); 193 virtual void displayOff(); 194 195 // BASIC DRAW API 196 // These MAY be overridden by the subclass to provide device-specific 197 // optimized code. Otherwise 'generic' versions are used. 198 // It's good to implement those, even if using transaction API 199 void writePixel(int16_t x, int16_t y, uint16_t color); 200 void drawPixel(int16_t x, int16_t y, uint16_t color); 201 void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); 202 void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); 203 void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); 204 void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); 205 void fillScreen(uint16_t color); 206 void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color); 207 void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); 208 void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color); 209 void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color); 210 void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); 211 void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); 212 void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color); 213 void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color); 214 void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color); 215 void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color); 216 void drawXBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color); 217 void drawGrayscaleBitmap(int16_t x, int16_t y, const uint8_t bitmap[], const uint8_t mask[], int16_t w, int16_t h); 218 void drawGrayscaleBitmap(int16_t x, int16_t y, uint8_t *bitmap, uint8_t *mask, int16_t w, int16_t h); 219 void draw16bitRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[], const uint8_t mask[], int16_t w, int16_t h); 220 void draw24bitRGBBitmap(int16_t x, int16_t y, const uint8_t bitmap[], const uint8_t mask[], int16_t w, int16_t h); 221 void draw24bitRGBBitmap(int16_t x, int16_t y, uint8_t *bitmap, uint8_t *mask, int16_t w, int16_t h); 222 void getTextBounds(const char *string, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h); 223 void getTextBounds(const __FlashStringHelper *s, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h); 224 void getTextBounds(const String &str, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h); 225 void setTextSize(uint8_t s); 226 void setTextSize(uint8_t sx, uint8_t sy); 227 void setTextSize(uint8_t sx, uint8_t sy, uint8_t pixel_margin); 228 229 #if !defined(ATTINY_CORE) 230 void setFont(const GFXfont *f = NULL); 231 #if defined(U8G2_FONT_SUPPORT) 232 void setFont(const uint8_t *font); 233 void setUTF8Print(bool isEnable); 234 uint16_t u8g2_font_get_word(const uint8_t *font, uint8_t offset); 235 uint8_t u8g2_font_decode_get_unsigned_bits(uint8_t cnt); 236 int8_t u8g2_font_decode_get_signed_bits(uint8_t cnt); 237 void u8g2_font_decode_len(uint8_t len, uint8_t is_foreground, uint16_t color, uint16_t bg); 238 #endif // defined(U8G2_FONT_SUPPORT) 239 virtual void flush(void); 240 #endif // !defined(ATTINY_CORE) 241 242 // adopt from LovyanGFX 243 void drawEllipse(int16_t x, int16_t y, int16_t rx, int16_t ry, uint16_t color); 244 void drawEllipseHelper(int32_t x, int32_t y, int32_t rx, int32_t ry, uint8_t cornername, uint16_t color); 245 void fillEllipse(int16_t x, int16_t y, int16_t rx, int16_t ry, uint16_t color); 246 void fillEllipseHelper(int32_t x, int32_t y, int32_t rx, int32_t ry, uint8_t cornername, int16_t delta, uint16_t color); 247 void drawArc(int16_t x, int16_t y, int16_t r1, int16_t r2, float start, float end, uint16_t color); 248 void fillArc(int16_t x, int16_t y, int16_t r1, int16_t r2, float start, float end, uint16_t color); 249 void fillArcHelper(int16_t cx, int16_t cy, int16_t oradius, int16_t iradius, float start, float end, uint16_t color); 250 251 // TFT optimization code, too big for ATMEL family 252 #if defined(LITTLE_FOOT_PRINT) 253 void writeSlashLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color); 254 void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color, uint16_t bg); 255 void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg); 256 void drawGrayscaleBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h); 257 void drawGrayscaleBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h); 258 void drawIndexedBitmap(int16_t x, int16_t y, uint8_t *bitmap, uint16_t *color_index, int16_t w, int16_t h); 259 void draw3bitRGBBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h); 260 void draw16bitRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, uint8_t *mask, int16_t w, int16_t h); 261 void draw16bitRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[], int16_t w, int16_t h); 262 void draw16bitRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, int16_t w, int16_t h); 263 void draw16bitBeRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, int16_t w, int16_t h); 264 void draw24bitRGBBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h); 265 void draw24bitRGBBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h); 266 void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg); 267 #else // !defined(LITTLE_FOOT_PRINT) 268 virtual void writeSlashLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color); 269 virtual void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color, uint16_t bg); 270 virtual void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg); 271 virtual void drawGrayscaleBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h); 272 virtual void drawGrayscaleBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h); 273 virtual void drawIndexedBitmap(int16_t x, int16_t y, uint8_t *bitmap, uint16_t *color_index, int16_t w, int16_t h); 274 virtual void draw3bitRGBBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h); 275 virtual void draw16bitRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, uint8_t *mask, int16_t w, int16_t h); 276 virtual void draw16bitRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[], int16_t w, int16_t h); 277 virtual void draw16bitRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, int16_t w, int16_t h); 278 virtual void draw16bitBeRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, int16_t w, int16_t h); 279 virtual void draw24bitRGBBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h); 280 virtual void draw24bitRGBBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h); 281 virtual void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg); 282 #endif // !defined(LITTLE_FOOT_PRINT) 283 284 /**********************************************************************/ 285 /*! 286 @brief Set text cursor location 287 @param x X coordinate in pixels 288 @param y Y coordinate in pixels 289 */ 290 /**********************************************************************/ 291 void setCursor(int16_t x, int16_t y) 292 { 293 cursor_x = x; 294 cursor_y = y; 295 } 296 297 /**********************************************************************/ 298 /*! 299 @brief Set text font color with transparant background 300 @param c 16-bit 5-6-5 Color to draw text with 301 @note For 'transparent' background, background and foreground 302 are set to same color rather than using a separate flag. 303 */ 304 /**********************************************************************/ 305 void setTextColor(uint16_t c) { textcolor = textbgcolor = c; } 306 307 /**********************************************************************/ 308 /*! 309 @brief Set text font color with custom background color 310 @param c 16-bit 5-6-5 Color to draw text with 311 @param bg 16-bit 5-6-5 Color to draw background/fill with 312 */ 313 /**********************************************************************/ 314 void setTextColor(uint16_t c, uint16_t bg) 315 { 316 textcolor = c; 317 textbgcolor = bg; 318 } 319 320 /**********************************************************************/ 321 /*! 322 @brief Set whether text that is too long for the screen width should 323 automatically wrap around to the next line (else clip right). 324 @param w true for wrapping, false for clipping 325 */ 326 /**********************************************************************/ 327 void setTextWrap(bool w) { wrap = w; } 328 329 virtual size_t write(uint8_t); 330 331 /************************************************************************/ 332 /*! 333 @brief Get width of the display, accounting for current rotation 334 @returns Width in pixels 335 */ 336 /************************************************************************/ 337 int16_t width(void) const { return _width; }; 338 339 /************************************************************************/ 340 /*! 341 @brief Get height of the display, accounting for current rotation 342 @returns Height in pixels 343 */ 344 /************************************************************************/ 345 int16_t height(void) const { return _height; } 346 347 /************************************************************************/ 348 /*! 349 @brief Get rotation setting for display 350 @returns 0 thru 3 corresponding to 4 cardinal rotations 351 */ 352 /************************************************************************/ 353 uint8_t getRotation(void) const { return _rotation; } 354 355 // get current cursor position (get rotation safe maximum values, 356 // using: width() for x, height() for y) 357 /************************************************************************/ 358 /*! 359 @brief Get text cursor X location 360 @returns X coordinate in pixels 361 */ 362 /************************************************************************/ 363 int16_t getCursorX(void) const { return cursor_x; } 364 365 /************************************************************************/ 366 /*! 367 @brief Get text cursor Y location 368 @returns Y coordinate in pixels 369 */ 370 /************************************************************************/ 371 int16_t getCursorY(void) const { return cursor_y; }; 372 373 /*! 374 @brief Given 8-bit red, green and blue values, return a 'packed' 375 16-bit color value in '565' RGB format (5 bits red, 6 bits 376 green, 5 bits blue). This is just a mathematical operation, 377 no hardware is touched. 378 @param red 8-bit red brightnesss (0 = off, 255 = max). 379 @param green 8-bit green brightnesss (0 = off, 255 = max). 380 @param blue 8-bit blue brightnesss (0 = off, 255 = max). 381 @return 'Packed' 16-bit color value (565 format). 382 */ 383 uint16_t color565(uint8_t red, uint8_t green, uint8_t blue) 384 { 385 return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3); 386 } 387 388 protected: 389 void charBounds(char c, int16_t *x, int16_t *y, int16_t *minx, int16_t *miny, int16_t *maxx, int16_t *maxy); 390 int16_t 391 _width, ///< Display width as modified by current rotation 392 _height, ///< Display height as modified by current rotation 393 _max_x, ///< x zero base bound (_width - 1) 394 _max_y, ///< y zero base bound (_height - 1) 395 cursor_x, ///< x location to start print()ing text 396 cursor_y; ///< y location to start print()ing text 397 uint16_t 398 textcolor, ///< 16-bit background color for print() 399 textbgcolor; ///< 16-bit text color for print() 400 uint8_t 401 textsize_x, ///< Desired magnification in X-axis of text to print() 402 textsize_y, ///< Desired magnification in Y-axis of text to print() 403 text_pixel_margin, ///< Margin for each text pixel 404 _rotation; ///< Display rotation (0 thru 3) 405 bool 406 wrap; ///< If set, 'wrap' text at right edge of display 407 #if !defined(ATTINY_CORE) 408 GFXfont *gfxFont; ///< Pointer to special font 409 #endif // !defined(ATTINY_CORE) 410 411 #if defined(U8G2_FONT_SUPPORT) 412 uint8_t *u8g2Font; 413 bool _enableUTF8Print = false; 414 uint8_t _utf8_state = 0; 415 uint16_t _encoding; 416 417 uint8_t _u8g2_glyph_cnt; 418 uint8_t _u8g2_bits_per_0; 419 uint8_t _u8g2_bits_per_1; 420 uint8_t _u8g2_bits_per_char_width; 421 uint8_t _u8g2_bits_per_char_height; 422 uint8_t _u8g2_bits_per_char_x; 423 uint8_t _u8g2_bits_per_char_y; 424 uint8_t _u8g2_bits_per_delta_x; 425 int8_t _u8g2_max_char_width; 426 int8_t _u8g2_max_char_height; 427 uint16_t _u8g2_start_pos_upper_A; 428 uint16_t _u8g2_start_pos_lower_a; 429 uint16_t _u8g2_start_pos_unicode; 430 uint8_t _u8g2_first_char; 431 432 uint8_t _u8g2_char_width; 433 uint8_t _u8g2_char_height; 434 int8_t _u8g2_char_x; 435 int8_t _u8g2_char_y; 436 int8_t _u8g2_delta_x; 437 438 int8_t _u8g2_dx; 439 int8_t _u8g2_dy; 440 uint16_t _u8g2_target_x; 441 uint16_t _u8g2_target_y; 442 443 const uint8_t *_u8g2_decode_ptr; 444 uint8_t _u8g2_decode_bit_pos; 445 #endif // defined(U8G2_FONT_SUPPORT) 446 447 #if defined(LITTLE_FOOT_PRINT) 448 int16_t 449 WIDTH, ///< This is the 'raw' display width - never changes 450 HEIGHT; ///< This is the 'raw' display height - never changes 451 #endif // defined(LITTLE_FOOT_PRINT) 452 }; 453 454 #endif // _ARDUINO_GFX_H_