если ты имеешь в виду птичку с лева в верху то делал... сторочка myGLCD.setBackColor(BLACK); делает затирание с другими цветами, но по чему не работает с черным.... вот это странно. ставлю RED и фон у цифр красный, цифры в png, но не моргает и затирается, а если BLACK то фон у цифр ясно что черный, но цифры не в png а белые и продолжают налипать, ПОЧЕМУ...... только с черным такое? вот в чем вопрос, по идеи черный должен работать как и другие цвета.....
Черный - это гашение всех rgb - может оно как то криво на аппаратном уровне работает. Сделай фон НЕ ЧЕРНЫМ для всех экранов проекта. Тогда правки во фруктовой библиотеке отменить надо скорее всего.
myGLCD.setTextColor(WHITE, BLACK);...... не моргает, не затирает
так .....myGLCD.setBackColor(BLACK);
myGLCD.setTextColor(WHITE, BLACK);......... не моргает, не затирает
наоборот ......... myGLCD.setTextColor(WHITE, BLACK);
myGLCD.setBackColor(BLACK);.........заголовки белые , данных нет
меняем цвет
myGLCD.setTextColor(WHITE, RED);....... не мигает, не затирает, фон черный
так ставим ....myGLCD.setBackColor(RED);
myGLCD.setTextColor(WHITE, BLACK);.......моргает, затирает, фон красный, цифры белые
так ставим......myGLCD.setTextColor(WHITE, BLACK);
myGLCD.setBackColor(RED);........ моргает, затирает, фон красный, цифры png
так и еще встертил на просторах инета что во фрукте
Функция фонового цвета обычно использовалась с «классическим» шрифтом для перезаписи старого содержимого экрана новыми данными.Это работает только потому, что эти символы имеют одинаковый размер;это неразумно делать с пропорционально разнесенными шрифтами с символами разных размеров (которые могут перекрываться).
стандартный шрифт который вообще не указывает только размер
myGLCD.setTextSize(3);
myGLCD.setTextColor(WHITE, BLACK);
и даже с myGLCD.setColor(WHITE);
работает отлично не моргает и затирает.
как я понял с описания выше, затирание со стандартным шрифтом происходит потому что он квадратные семитричные символы имеет, а мне они не подходят, мне нужны выше и ужи, да и он как из майн крафта , уродлив. вот и вся проблема. вообщем библа фрукта это та еще дрянь
Подозреваю что не ту либу тянет компилятор ! Просто удали файл либы с моими правками и проверь - ругнётся или нет ? В правках ЖЕСТКО указано 0xF800 (красный)
стапе. есть такая же библа Adafruit-GFX1. ща уберу. убрал теперь ругается. как ее переключить теперь на эту на Adafruit-GFX? ща гляну может версия старая. 1.5.6 последняя. ща перекачаю и заменю
короче оно использовало обе библы. но в одиночку 1.5.6 ругается. выкинул вернул старую она 1.5.3. теперь только одна , можно пробовать править вот(
ты правил 1.5.6 а она по ходу брала с 1.5.3. вот и не было результата)
/*
This is the core graphics library for all our displays, providing a common
set of graphics primitives (points, lines, circles, etc.). It needs to be
paired with a hardware-specific library for each display device we carry
(to handle the lower-level functions).
Adafruit invests time and resources providing this open source code, please
support Adafruit & open-source hardware by purchasing products from Adafruit!
Copyright (c) 2013 Adafruit Industries. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "Adafruit_GFX.h"
#include "glcdfont.c"
#ifdef __AVR__
#include <avr/pgmspace.h>
#elif defined(ESP8266) || defined(ESP32)
#include <pgmspace.h>
#endif
// Many (but maybe not all) non-AVR board installs define macros
// for compatibility with existing PROGMEM-reading AVR code.
// Do our own checks and defines here for good measure...
#ifndef pgm_read_byte
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#endif
#ifndef pgm_read_word
#define pgm_read_word(addr) (*(const unsigned short *)(addr))
#endif
#ifndef pgm_read_dword
#define pgm_read_dword(addr) (*(const unsigned long *)(addr))
#endif
// Pointers are a peculiar case...typically 16-bit on AVR boards,
// 32 bits elsewhere. Try to accommodate both...
#if !defined(__INT_MAX__) || (__INT_MAX__ > 0xFFFF)
#define pgm_read_pointer(addr) ((void *)pgm_read_dword(addr))
#else
#define pgm_read_pointer(addr) ((void *)pgm_read_word(addr))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
#endif
/**************************************************************************/
/*!
@brief Instatiate a GFX context for graphics! Can only be done by a superclass
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h):
WIDTH(w), HEIGHT(h)
{
_width = WIDTH;
_height = HEIGHT;
rotation = 0;
cursor_y = cursor_x = 0;
textsize = 1;
textcolor = textbgcolor = 0xFFFF;
wrap = true;
_cp437 = false;
gfxFont = NULL;
}
/**************************************************************************/
/*!
@brief Write a line. Bresenham's algorithm - thx wikpedia
@param x0 Start point x coordinate
@param y0 Start point y coordinate
@param x1 End point x coordinate
@param y1 End point y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
uint16_t color) {
#if defined(ESP8266)
yield();
#endif
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
_swap_int16_t(x0, y0);
_swap_int16_t(x1, y1);
}
if (x0 > x1) {
_swap_int16_t(x0, x1);
_swap_int16_t(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for (; x0<=x1; x0++) {
if (steep) {
writePixel(y0, x0, color);
} else {
writePixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
/**************************************************************************/
/*!
@brief Start a display-writing routine, overwrite in subclasses.
*/
/**************************************************************************/
void Adafruit_GFX::startWrite(){
}
/**************************************************************************/
/*!
@brief Write a pixel, overwrite in subclasses if startWrite is defined!
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writePixel(int16_t x, int16_t y, uint16_t color){
drawPixel(x, y, color);
}
/**************************************************************************/
/*!
@brief Write a perfectly vertical line, overwrite in subclasses if startWrite is defined!
@param x Top-most x coordinate
@param y Top-most y coordinate
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color) {
// Overwrite in subclasses if startWrite is defined!
// Can be just writeLine(x, y, x, y+h-1, color);
// or writeFillRect(x, y, 1, h, color);
drawFastVLine(x, y, h, color);
}
/**************************************************************************/
/*!
@brief Write a perfectly horizontal line, overwrite in subclasses if startWrite is defined!
@param x Left-most x coordinate
@param y Left-most y coordinate
@param w Width in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
// Overwrite in subclasses if startWrite is defined!
// Example: writeLine(x, y, x+w-1, y, color);
// or writeFillRect(x, y, w, 1, color);
drawFastHLine(x, y, w, color);
}
/**************************************************************************/
/*!
@brief Write a rectangle completely with one color, overwrite in subclasses if startWrite is defined!
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
// Overwrite in subclasses if desired!
fillRect(x,y,w,h,color);
}
/**************************************************************************/
/*!
@brief End a display-writing routine, overwrite in subclasses if startWrite is defined!
*/
/**************************************************************************/
void Adafruit_GFX::endWrite(){
}
/**************************************************************************/
/*!
@brief Draw a perfectly vertical line (this is often optimized in a subclass!)
@param x Top-most x coordinate
@param y Top-most y coordinate
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color) {
startWrite();
writeLine(x, y, x, y+h-1, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a perfectly horizontal line (this is often optimized in a subclass!)
@param x Left-most x coordinate
@param y Left-most y coordinate
@param w Width in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
startWrite();
writeLine(x, y, x+w-1, y, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Fill a rectangle completely with one color. Update in subclasses if desired!
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
startWrite();
for (int16_t i=x; i<x+w; i++) {
writeFastVLine(i, y, h, color);
}
endWrite();
}
/**************************************************************************/
/*!
@brief Fill the screen completely with one color. Update in subclasses if desired!
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color);
}
/**************************************************************************/
/*!
@brief Draw a line
@param x0 Start point x coordinate
@param y0 Start point y coordinate
@param x1 End point x coordinate
@param y1 End point y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
uint16_t color) {
// Update in subclasses if desired!
if(x0 == x1){
if(y0 > y1) _swap_int16_t(y0, y1);
drawFastVLine(x0, y0, y1 - y0 + 1, color);
} else if(y0 == y1){
if(x0 > x1) _swap_int16_t(x0, x1);
drawFastHLine(x0, y0, x1 - x0 + 1, color);
} else {
startWrite();
writeLine(x0, y0, x1, y1, color);
endWrite();
}
}
/**************************************************************************/
/*!
@brief Draw a circle outline
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
#if defined(ESP8266)
yield();
#endif
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
startWrite();
writePixel(x0 , y0+r, color);
writePixel(x0 , y0-r, color);
writePixel(x0+r, y0 , color);
writePixel(x0-r, y0 , color);
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
writePixel(x0 + x, y0 + y, color);
writePixel(x0 - x, y0 + y, color);
writePixel(x0 + x, y0 - y, color);
writePixel(x0 - x, y0 - y, color);
writePixel(x0 + y, y0 + x, color);
writePixel(x0 - y, y0 + x, color);
writePixel(x0 + y, y0 - x, color);
writePixel(x0 - y, y0 - x, color);
}
endWrite();
}
/**************************************************************************/
/*!
@brief Quarter-circle drawer, used to do circles and roundrects
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param cornername Mask bit #1 or bit #2 to indicate which quarters of the circle we're doing
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
int16_t r, uint8_t cornername, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x4) {
writePixel(x0 + x, y0 + y, color);
writePixel(x0 + y, y0 + x, color);
}
if (cornername & 0x2) {
writePixel(x0 + x, y0 - y, color);
writePixel(x0 + y, y0 - x, color);
}
if (cornername & 0x8) {
writePixel(x0 - y, y0 + x, color);
writePixel(x0 - x, y0 + y, color);
}
if (cornername & 0x1) {
writePixel(x0 - y, y0 - x, color);
writePixel(x0 - x, y0 - y, color);
}
}
}
/**************************************************************************/
/*!
@brief Draw a circle with filled color
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
startWrite();
writeFastVLine(x0, y0-r, 2*r+1, color);
fillCircleHelper(x0, y0, r, 3, 0, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Quarter-circle drawer with fill, used for circles and roundrects
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param corners Mask bits indicating which quarters we're doing
@param delta Offset from center-point, used for round-rects
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
uint8_t corners, int16_t delta, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
int16_t px = x;
int16_t py = y;
delta++; // Avoid some +1's in the loop
while(x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
// These checks avoid double-drawing certain lines, important
// for the SSD1306 library which has an INVERT drawing mode.
if(x < (y + 1)) {
if(corners & 1) writeFastVLine(x0+x, y0-y, 2*y+delta, color);
if(corners & 2) writeFastVLine(x0-x, y0-y, 2*y+delta, color);
}
if(y != py) {
if(corners & 1) writeFastVLine(x0+py, y0-px, 2*px+delta, color);
if(corners & 2) writeFastVLine(x0-py, y0-px, 2*px+delta, color);
py = y;
}
px = x;
}
}
/**************************************************************************/
/*!
@brief Draw a rectangle with no fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
startWrite();
writeFastHLine(x, y, w, color);
writeFastHLine(x, y+h-1, w, color);
writeFastVLine(x, y, h, color);
writeFastVLine(x+w-1, y, h, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a rounded rectangle with no fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param r Radius of corner rounding
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r, uint16_t color) {
int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis
if(r > max_radius) r = max_radius;
// smarter version
startWrite();
writeFastHLine(x+r , y , w-2*r, color); // Top
writeFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
writeFastVLine(x , y+r , h-2*r, color); // Left
writeFastVLine(x+w-1, y+r , h-2*r, color); // Right
// draw four corners
drawCircleHelper(x+r , y+r , r, 1, color);
drawCircleHelper(x+w-r-1, y+r , r, 2, color);
drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
drawCircleHelper(x+r , y+h-r-1, r, 8, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a rounded rectangle with fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param r Radius of corner rounding
@param color 16-bit 5-6-5 Color to draw/fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r, uint16_t color) {
int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis
if(r > max_radius) r = max_radius;
// smarter version
startWrite();
writeFillRect(x+r, y, w-2*r, h, color);
// draw four corners
fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a triangle with no fill color
@param x0 Vertex #0 x coordinate
@param y0 Vertex #0 y coordinate
@param x1 Vertex #1 x coordinate
@param y1 Vertex #1 y coordinate
@param x2 Vertex #2 x coordinate
@param y2 Vertex #2 y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0,
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
drawLine(x0, y0, x1, y1, color);
drawLine(x1, y1, x2, y2, color);
drawLine(x2, y2, x0, y0, color);
}
/**************************************************************************/
/*!
@brief Draw a triangle with color-fill
@param x0 Vertex #0 x coordinate
@param y0 Vertex #0 y coordinate
@param x1 Vertex #1 x coordinate
@param y1 Vertex #1 y coordinate
@param x2 Vertex #2 x coordinate
@param y2 Vertex #2 y coordinate
@param color 16-bit 5-6-5 Color to fill/draw with
*/
/**************************************************************************/
void Adafruit_GFX::fillTriangle(int16_t x0, int16_t y0,
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
int16_t a, b, y, last;
// Sort coordinates by Y order (y2 >= y1 >= y0)
if (y0 > y1) {
_swap_int16_t(y0, y1); _swap_int16_t(x0, x1);
}
if (y1 > y2) {
_swap_int16_t(y2, y1); _swap_int16_t(x2, x1);
}
if (y0 > y1) {
_swap_int16_t(y0, y1); _swap_int16_t(x0, x1);
}
startWrite();
if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
a = b = x0;
if(x1 < a) a = x1;
else if(x1 > b) b = x1;
if(x2 < a) a = x2;
else if(x2 > b) b = x2;
writeFastHLine(a, y0, b-a+1, color);
endWrite();
return;
}
int16_t
dx01 = x1 - x0,
dy01 = y1 - y0,
dx02 = x2 - x0,
dy02 = y2 - y0,
dx12 = x2 - x1,
dy12 = y2 - y1;
int32_t
sa = 0,
sb = 0;
// For upper part of triangle, find scanline crossings for segments
// 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
// is included here (and second loop will be skipped, avoiding a /0
// error there), otherwise scanline y1 is skipped here and handled
// in the second loop...which also avoids a /0 error here if y0=y1
// (flat-topped triangle).
if(y1 == y2) last = y1; // Include y1 scanline
else last = y1-1; // Skip it
for(y=y0; y<=last; y++) {
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
/* longhand:
a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if(a > b) _swap_int16_t(a,b);
writeFastHLine(a, y, b-a+1, color);
}
// For lower part of triangle, find scanline crossings for segments
// 0-2 and 1-2. This loop is skipped if y1=y2.
sa = (int32_t)dx12 * (y - y1);
sb = (int32_t)dx02 * (y - y0);
for(; y<=y2; y++) {
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
/* longhand:
a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if(a > b) _swap_int16_t(a,b);
writeFastHLine(a, y, b-a+1, color);
}
endWrite();
}
// BITMAP / XBITMAP / GRAYSCALE / RGB BITMAP FUNCTIONS ---------------------
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent).
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
if(byte & 0x80) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h,
uint16_t color, uint16_t bg) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
writePixel(x+i, y, (byte & 0x80) ? color : bg);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent).
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
if(byte & 0x80) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
writePixel(x+i, y, (byte & 0x80) ? color : bg);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw PROGMEM-resident XBitMap Files (*.xbm), exported from GIMP.
Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
C Array can be directly used with this function.
There is no RAM-resident version of this function; if generating bitmaps
in RAM, use the format defined by drawBitmap() and call that instead.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
*/
/**************************************************************************/
void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte >>= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
// Nearly identical to drawBitmap(), only the bit order
// is reversed here (left-to-right = LSB to MSB):
if(byte & 0x01) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y) pos.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, (uint8_t)pgm_read_byte(&bitmap[j * w + i]));
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 8-bit image (grayscale) at the specified (x,y) pos.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 8-bit image (grayscale) with a 1-bit mask
(set bits = opaque, unset bits = clear) at the specified (x,y) position.
BOTH buffers (grayscale and mask) must be PROGMEM-resident.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param mask byte array with mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], const uint8_t mask[],
int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&mask[j * bw + i / 8]);
if(byte & 0x80) {
writePixel(x+i, y, (uint8_t)pgm_read_byte(&bitmap[j * w + i]));
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 8-bit image (grayscale) with a 1-bit mask
(set bits = opaque, unset bits = clear) at the specified (x,y) position.
BOTH buffers (grayscale and mask) must be RAM-residentt, no mix-and-match
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param mask byte array with mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
uint8_t *bitmap, uint8_t *mask, int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = mask[j * bw + i / 8];
if(byte & 0x80) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
const uint16_t bitmap[], int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, pgm_read_word(&bitmap[j * w + i]));
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
uint16_t *bitmap, int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (color and mask) must be PROGMEM-resident. For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param mask byte array with monochrome mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
const uint16_t bitmap[], const uint8_t mask[],
int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&mask[j * bw + i / 8]);
if(byte & 0x80) {
writePixel(x+i, y, pgm_read_word(&bitmap[j * w + i]));
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (color and mask) must be RAM-resident. For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param mask byte array with monochrome mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
uint16_t *bitmap, uint8_t *mask, int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = mask[j * bw + i / 8];
if(byte & 0x80) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
}
endWrite();
}
// TEXT- AND CHARACTER-HANDLING FUNCTIONS ----------------------------------
// Draw a character
/**************************************************************************/
/*!
@brief Draw a single character
@param x Bottom left corner x coordinate
@param y Bottom left corner y coordinate
@param c The 8-bit font-indexed character (likely ascii)
@param color 16-bit 5-6-5 Color to draw chraracter with
@param bg 16-bit 5-6-5 Color to fill background with (if same as color, no background)
@param size Font magnification level, 1 is 'original' size
*/
/**************************************************************************/
void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
uint16_t color, uint16_t bg, uint8_t size) {
if(!gfxFont) { // 'Classic' built-in font
if((x >= _width) || // Clip right
(y >= _height) || // Clip bottom
((x + 6 * size - 1) < 0) || // Clip left
((y + 8 * size - 1) < 0)) // Clip top
return;
if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior
startWrite();
for(int8_t i=0; i<5; i++ ) { // Char bitmap = 5 columns
uint8_t line = pgm_read_byte(&font[c * 5 + i]);
for(int8_t j=0; j<8; j++, line >>= 1) {
if(line & 1) {
if(size == 1)
writePixel(x+i, y+j, color);
else
writeFillRect(x+i*size, y+j*size, size, size, color);
} else if(bg != color) {
if(size == 1)
writePixel(x+i, y+j, bg);
else
writeFillRect(x+i*size, y+j*size, size, size, bg);
}
}
}
if(bg != color) { // If opaque, draw vertical line for last column
if(size == 1) writeFastVLine(x+5, y, 8, bg);
else writeFillRect(x+5*size, y, size, 8*size, bg);
}
endWrite();
} else { // Custom font
// Character is assumed previously filtered by write() to eliminate
// newlines, returns, non-printable characters, etc. Calling
// drawChar() directly with 'bad' characters of font may cause mayhem!
c -= (uint8_t)pgm_read_byte(&gfxFont->first);
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
uint8_t *bitmap = (uint8_t *)pgm_read_pointer(&gfxFont->bitmap);
uint16_t bo = pgm_read_word(&glyph->bitmapOffset);
uint8_t w = pgm_read_byte(&glyph->width),
h = pgm_read_byte(&glyph->height);
int8_t xo = pgm_read_byte(&glyph->xOffset),
yo = pgm_read_byte(&glyph->yOffset);
uint8_t xx, yy, bits = 0, bit = 0;
int16_t xo16 = 0, yo16 = 0;
if(size > 1) {
xo16 = xo;
yo16 = yo;
}
// Todo: Add character clipping here
// NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS.
// THIS IS ON PURPOSE AND BY DESIGN. The background color feature
// has typically been used with the 'classic' font to overwrite old
// screen contents with new data. This ONLY works because the
// characters are a uniform size; it's not a sensible thing to do with
// proportionally-spaced fonts with glyphs of varying sizes (and that
// may overlap). To replace previously-drawn text when using a custom
// font, use the getTextBounds() function to determine the smallest
// rectangle encompassing a string, erase the area with fillRect(),
// then draw new text. This WILL infortunately 'blink' the text, but
// is unavoidable. Drawing 'background' pixels will NOT fix this,
// only creates a new set of problems. Have an idea to work around
// this (a canvas object type for MCUs that can afford the RAM and
// displays supporting setAddrWindow() and pushColors()), but haven't
// implemented this yet.
startWrite();
for(yy=0; yy<h; yy++) {
for(xx=0; xx<w; xx++) {
if(!(bit++ & 7)) {
bits = pgm_read_byte(&bitmap[bo++]);
}
if(bits & 0x80) {
if(size == 1) {
writePixel(x+xo+xx, y+yo+yy, color);
} else {
writeFillRect(x+(xo16+xx)*size, y+(yo16+yy)*size,
size, size, color);
}
}
bits <<= 1;
}
}
endWrite();
} // End classic vs custom font
}
/**************************************************************************/
/*!
@brief Print one byte/character of data, used to support print()
@param c The 8-bit ascii character to write
*/
/**************************************************************************/
size_t Adafruit_GFX::write(uint8_t c) {
if(!gfxFont) { // 'Classic' built-in font
if(c == '\n') { // Newline?
cursor_x = 0; // Reset x to zero,
cursor_y += textsize * 8; // advance y one line
} else if(c != '\r') { // Ignore carriage returns
if(wrap && ((cursor_x + textsize * 6) > _width)) { // Off right?
cursor_x = 0; // Reset x to zero,
cursor_y += textsize * 8; // advance y one line
}
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
cursor_x += textsize * 6; // Advance x one char
}
} else { // Custom font
if(c == '\n') {
cursor_x = 0;
cursor_y += (int16_t)textsize *
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
} else if(c != '\r') {
uint8_t first = pgm_read_byte(&gfxFont->first);
if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) {
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(
&gfxFont->glyph))[c - first]);
uint8_t w = pgm_read_byte(&glyph->width),
h = pgm_read_byte(&glyph->height);
if((w > 0) && (h > 0)) { // Is there an associated bitmap?
int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
if(wrap && ((cursor_x + textsize * (xo + w)) > _width)) {
cursor_x = 0;
cursor_y += (int16_t)textsize *
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
}
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
}
cursor_x += (uint8_t)pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize;
}
}
}
return 1;
}
/**************************************************************************/
/*!
@brief Set text 'magnification' size. Each increase in s makes 1 pixel that much bigger.
@param s Desired text size. 1 is default 6x8, 2 is 12x16, 3 is 18x24, etc
*/
/**************************************************************************/
void Adafruit_GFX::setTextSize(uint8_t s) {
textsize = (s > 0) ? s : 1;
}
/**************************************************************************/
/*!
@brief Set rotation setting for display
@param x 0 thru 3 corresponding to 4 cardinal rotations
*/
/**************************************************************************/
void Adafruit_GFX::setRotation(uint8_t x) {
rotation = (x & 3);
switch(rotation) {
case 0:
case 2:
_width = WIDTH;
_height = HEIGHT;
break;
case 1:
case 3:
_width = HEIGHT;
_height = WIDTH;
break;
}
}
/**************************************************************************/
/*!
@brief Set the font to display when print()ing, either custom or default
@param f The GFXfont object, if NULL use built in 6x8 font
*/
/**************************************************************************/
void Adafruit_GFX::setFont(const GFXfont *f) {
if(f) { // Font struct pointer passed in?
if(!gfxFont) { // And no current font struct?
// Switching from classic to new font behavior.
// Move cursor pos down 6 pixels so it's on baseline.
cursor_y += 6;
}
} else if(gfxFont) { // NULL passed. Current font struct defined?
// Switching from new to classic font behavior.
// Move cursor pos up 6 pixels so it's at top-left of char.
cursor_y -= 6;
}
gfxFont = (GFXfont *)f;
}
/**************************************************************************/
/*!
@brief Helper to determine size of a character with current font/size.
Broke this out as it's used by both the PROGMEM- and RAM-resident getTextBounds() functions.
@param c The ascii character in question
@param x Pointer to x location of character
@param y Pointer to y location of character
@param minx Minimum clipping value for X
@param miny Minimum clipping value for Y
@param maxx Maximum clipping value for X
@param maxy Maximum clipping value for Y
*/
/**************************************************************************/
void Adafruit_GFX::charBounds(char c, int16_t *x, int16_t *y,
int16_t *minx, int16_t *miny, int16_t *maxx, int16_t *maxy) {
if(gfxFont) {
if(c == '\n') { // Newline?
*x = 0; // Reset x to zero, advance y by one line
*y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
} else if(c != '\r') { // Not a carriage return; is normal char
uint8_t first = pgm_read_byte(&gfxFont->first),
last = pgm_read_byte(&gfxFont->last);
if((c >= first) && (c <= last)) { // Char present in this font?
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(
&gfxFont->glyph))[c - first]);
uint8_t gw = pgm_read_byte(&glyph->width),
gh = pgm_read_byte(&glyph->height),
xa = pgm_read_byte(&glyph->xAdvance);
int8_t xo = pgm_read_byte(&glyph->xOffset),
yo = pgm_read_byte(&glyph->yOffset);
if(wrap && ((*x+(((int16_t)xo+gw)*textsize)) > _width)) {
*x = 0; // Reset x to zero, advance y by one line
*y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
}
int16_t ts = (int16_t)textsize,
x1 = *x + xo * ts,
y1 = *y + yo * ts,
x2 = x1 + gw * ts - 1,
y2 = y1 + gh * ts - 1;
if(x1 < *minx) *minx = x1;
if(y1 < *miny) *miny = y1;
if(x2 > *maxx) *maxx = x2;
if(y2 > *maxy) *maxy = y2;
*x += xa * ts;
}
}
} else { // Default font
if(c == '\n') { // Newline?
*x = 0; // Reset x to zero,
*y += textsize * 8; // advance y one line
// min/max x/y unchaged -- that waits for next 'normal' character
} else if(c != '\r') { // Normal char; ignore carriage returns
if(wrap && ((*x + textsize * 6) > _width)) { // Off right?
*x = 0; // Reset x to zero,
*y += textsize * 8; // advance y one line
}
int x2 = *x + textsize * 6 - 1, // Lower-right pixel of char
y2 = *y + textsize * 8 - 1;
if(x2 > *maxx) *maxx = x2; // Track max x, y
if(y2 > *maxy) *maxy = y2;
if(*x < *minx) *minx = *x; // Track min x, y
if(*y < *miny) *miny = *y;
*x += textsize * 6; // Advance x one char
}
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The ascii string to measure
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const char *str, int16_t x, int16_t y,
int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
uint8_t c; // Current character
*x1 = x;
*y1 = y;
*w = *h = 0;
int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;
while((c = *str++))
charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);
if(maxx >= minx) {
*x1 = minx;
*w = maxx - minx + 1;
}
if(maxy >= miny) {
*y1 = miny;
*h = maxy - miny + 1;
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The ascii string to measure (as an arduino String() class)
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const String &str, int16_t x, int16_t y,
int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
if (str.length() != 0) {
getTextBounds(const_cast<char*>(str.c_str()), x, y, x1, y1, w, h);
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a PROGMEM string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The flash-memory ascii string to measure
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str,
int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
uint8_t *s = (uint8_t *)str, c;
*x1 = x;
*y1 = y;
*w = *h = 0;
int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;
while((c = pgm_read_byte(s++)))
charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);
if(maxx >= minx) {
*x1 = minx;
*w = maxx - minx + 1;
}
if(maxy >= miny) {
*y1 = miny;
*h = maxy - miny + 1;
}
}
/**************************************************************************/
/*!
@brief Invert the display (ideally using built-in hardware command)
@param i True if you want to invert, false to make 'normal'
*/
/**************************************************************************/
void Adafruit_GFX::invertDisplay(boolean i) {
// Do nothing, must be subclassed if supported by hardware
}
/***************************************************************************/
/**************************************************************************/
/*!
@brief Create a simple drawn button UI element
*/
/**************************************************************************/
Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
_gfx = 0;
}
/**************************************************************************/
/*!
@brief Initialize button with our desired color/size/settings
@param gfx Pointer to our display so we can draw to it!
@param x The X coordinate of the center of the button
@param y The Y coordinate of the center of the button
@param w Width of the buttton
@param h Height of the buttton
@param outline Color of the outline (16-bit 5-6-5 standard)
@param fill Color of the button fill (16-bit 5-6-5 standard)
@param textcolor Color of the button label (16-bit 5-6-5 standard)
@param label Ascii string of the text inside the button
@param textsize The font magnification of the label text
*/
/**************************************************************************/
// Classic initButton() function: pass center & size
void Adafruit_GFX_Button::initButton(
Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
uint16_t outline, uint16_t fill, uint16_t textcolor,
char *label, uint8_t textsize)
{
// Tweak arguments and pass to the newer initButtonUL() function...
initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill,
textcolor, label, textsize);
}
/**************************************************************************/
/*!
@brief Initialize button with our desired color/size/settings, with upper-left coordinates
@param gfx Pointer to our display so we can draw to it!
@param x1 The X coordinate of the Upper-Left corner of the button
@param y1 The Y coordinate of the Upper-Left corner of the button
@param w Width of the buttton
@param h Height of the buttton
@param outline Color of the outline (16-bit 5-6-5 standard)
@param fill Color of the button fill (16-bit 5-6-5 standard)
@param textcolor Color of the button label (16-bit 5-6-5 standard)
@param label Ascii string of the text inside the button
@param textsize The font magnification of the label text
*/
/**************************************************************************/
void Adafruit_GFX_Button::initButtonUL(
Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
uint16_t outline, uint16_t fill, uint16_t textcolor,
char *label, uint8_t textsize)
{
_x1 = x1;
_y1 = y1;
_w = w;
_h = h;
_outlinecolor = outline;
_fillcolor = fill;
_textcolor = textcolor;
_textsize = textsize;
_gfx = gfx;
strncpy(_label, label, 9);
}
/**************************************************************************/
/*!
@brief Draw the button on the screen
@param inverted Whether to draw with fill/text swapped to indicate 'pressed'
*/
/**************************************************************************/
void Adafruit_GFX_Button::drawButton(boolean inverted) {
uint16_t fill, outline, text;
if(!inverted) {
fill = _fillcolor;
outline = _outlinecolor;
text = _textcolor;
} else {
fill = _textcolor;
outline = _outlinecolor;
text = _fillcolor;
}
uint8_t r = min(_w, _h) / 4; // Corner radius
_gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill);
_gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline);
_gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize),
_y1 + (_h/2) - (4 * _textsize));
_gfx->setTextColor(text);
_gfx->setTextSize(_textsize);
_gfx->print(_label);
}
/**************************************************************************/
/*!
@brief Helper to let us know if a coordinate is within the bounds of the button
@param x The X coordinate to check
@param y The Y coordinate to check
@returns True if within button graphics outline
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
return ((x >= _x1) && (x < (int16_t) (_x1 + _w)) &&
(y >= _y1) && (y < (int16_t) (_y1 + _h)));
}
/**************************************************************************/
/*!
@brief Query whether the button was pressed since we last checked state
@returns True if was not-pressed before, now is.
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
/**************************************************************************/
/*!
@brief Query whether the button was released since we last checked state
@returns True if was pressed before, now is not.
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }
// -------------------------------------------------------------------------
// GFXcanvas1, GFXcanvas8 and GFXcanvas16 (currently a WIP, don't get too
// comfy with the implementation) provide 1-, 8- and 16-bit offscreen
// canvases, the address of which can be passed to drawBitmap() or
// pushColors() (the latter appears only in a couple of GFX-subclassed TFT
// libraries at this time). This is here mostly to help with the recently-
// added proportionally-spaced fonts; adds a way to refresh a section of the
// screen without a massive flickering clear-and-redraw...but maybe you'll
// find other uses too. VERY RAM-intensive, since the buffer is in MCU
// memory and not the display driver...GXFcanvas1 might be minimally useful
// on an Uno-class board, but this and the others are much more likely to
// require at least a Mega or various recent ARM-type boards (recommended,
// as the text+bitmap draw can be pokey). GFXcanvas1 requires 1 bit per
// pixel (rounded up to nearest byte per scanline), GFXcanvas8 is 1 byte
// per pixel (no scanline pad), and GFXcanvas16 uses 2 bytes per pixel (no
// scanline pad).
// NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.
/**************************************************************************/
/*!
@brief Instatiate a GFX 1-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint16_t bytes = ((w + 7) / 8) * h;
if((buffer = (uint8_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas1::~GFXcanvas1(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas1::drawPixel(int16_t x, int16_t y, uint16_t color) {
#ifdef __AVR__
// Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR
static const uint8_t PROGMEM
GFXsetBit[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 },
GFXclrBit[] = { 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE };
#endif
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
uint8_t *ptr = &buffer[(x / 8) + y * ((WIDTH + 7) / 8)];
#ifdef __AVR__
if(color) *ptr |= pgm_read_byte(&GFXsetBit[x & 7]);
else *ptr &= pgm_read_byte(&GFXclrBit[x & 7]);
#else
if(color) *ptr |= 0x80 >> (x & 7);
else *ptr &= ~(0x80 >> (x & 7));
#endif
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas1::fillScreen(uint16_t color) {
if(buffer) {
uint16_t bytes = ((WIDTH + 7) / 8) * HEIGHT;
memset(buffer, color ? 0xFF : 0x00, bytes);
}
}
/**************************************************************************/
/*!
@brief Instatiate a GFX 8-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas8::GFXcanvas8(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint32_t bytes = w * h;
if((buffer = (uint8_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas8::~GFXcanvas8(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas8::drawPixel(int16_t x, int16_t y, uint16_t color) {
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
buffer[x + y * WIDTH] = color;
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas8::fillScreen(uint16_t color) {
if(buffer) {
memset(buffer, color, WIDTH * HEIGHT);
}
}
void GFXcanvas8::writeFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
if((x >= _width) || (y < 0) || (y >= _height)) return;
int16_t x2 = x + w - 1;
if(x2 < 0) return;
// Clip left/right
if(x < 0) {
x = 0;
w = x2 + 1;
}
if(x2 >= _width) w = _width - x;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
memset(buffer + y * WIDTH + x, color, w);
}
/**************************************************************************/
/*!
@brief Instatiate a GFX 16-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint32_t bytes = w * h * 2;
if((buffer = (uint16_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas16::~GFXcanvas16(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas16::drawPixel(int16_t x, int16_t y, uint16_t color) {
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
buffer[x + y * WIDTH] = color;
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas16::fillScreen(uint16_t color) {
if(buffer) {
uint8_t hi = color >> 8, lo = color & 0xFF;
if(hi == lo) {
memset(buffer, lo, WIDTH * HEIGHT * 2);
} else {
uint32_t i, pixels = WIDTH * HEIGHT;
for(i=0; i<pixels; i++) buffer[i] = color;
}
}
}
/**************************************************************************/
/*!
@brief Reverses the "endian-ness" of each 16-bit pixel within the
canvas; little-endian to big-endian, or big-endian to little.
Most microcontrollers (such as SAMD) are little-endian, while
most displays tend toward big-endianness. All the drawing
functions (including RGB bitmap drawing) take care of this
automatically, but some specialized code (usually involving
DMA) can benefit from having pixel data already in the
display-native order. Note that this does NOT convert to a
SPECIFIC endian-ness, it just flips the bytes within each word.
*/
/**************************************************************************/
void GFXcanvas16::byteSwap(void) {
if(buffer) {
uint32_t i, pixels = WIDTH * HEIGHT;
for(i=0; i<pixels; i++) buffer[i] = __builtin_bswap16(buffer[i]);
}
}
к стати по этому оно просило полный адрес к файлу фонта так как он там его не находил
/*
This is the core graphics library for all our displays, providing a common
set of graphics primitives (points, lines, circles, etc.). It needs to be
paired with a hardware-specific library for each display device we carry
(to handle the lower-level functions).
Adafruit invests time and resources providing this open source code, please
support Adafruit & open-source hardware by purchasing products from Adafruit!
Copyright (c) 2013 Adafruit Industries. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "Adafruit_GFX.h"
#include "glcdfont.c"
#ifdef __AVR__
#include <avr/pgmspace.h>
#elif defined(ESP8266) || defined(ESP32)
#include <pgmspace.h>
#endif
// Many (but maybe not all) non-AVR board installs define macros
// for compatibility with existing PROGMEM-reading AVR code.
// Do our own checks and defines here for good measure...
#ifndef pgm_read_byte
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#endif
#ifndef pgm_read_word
#define pgm_read_word(addr) (*(const unsigned short *)(addr))
#endif
#ifndef pgm_read_dword
#define pgm_read_dword(addr) (*(const unsigned long *)(addr))
#endif
// Pointers are a peculiar case...typically 16-bit on AVR boards,
// 32 bits elsewhere. Try to accommodate both...
#if !defined(__INT_MAX__) || (__INT_MAX__ > 0xFFFF)
#define pgm_read_pointer(addr) ((void *)pgm_read_dword(addr))
#else
#define pgm_read_pointer(addr) ((void *)pgm_read_word(addr))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
#endif
/**************************************************************************/
/*!
@brief Instatiate a GFX context for graphics! Can only be done by a superclass
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h):
WIDTH(w), HEIGHT(h)
{
_width = WIDTH;
_height = HEIGHT;
rotation = 0;
cursor_y = cursor_x = 0;
textsize = 1;
textcolor = textbgcolor = 0xFFFF;
wrap = true;
_cp437 = false;
gfxFont = NULL;
}
/**************************************************************************/
/*!
@brief Write a line. Bresenham's algorithm - thx wikpedia
@param x0 Start point x coordinate
@param y0 Start point y coordinate
@param x1 End point x coordinate
@param y1 End point y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
uint16_t color) {
#if defined(ESP8266)
yield();
#endif
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
_swap_int16_t(x0, y0);
_swap_int16_t(x1, y1);
}
if (x0 > x1) {
_swap_int16_t(x0, x1);
_swap_int16_t(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for (; x0<=x1; x0++) {
if (steep) {
writePixel(y0, x0, color);
} else {
writePixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
/**************************************************************************/
/*!
@brief Start a display-writing routine, overwrite in subclasses.
*/
/**************************************************************************/
void Adafruit_GFX::startWrite(){
}
/**************************************************************************/
/*!
@brief Write a pixel, overwrite in subclasses if startWrite is defined!
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writePixel(int16_t x, int16_t y, uint16_t color){
drawPixel(x, y, color);
}
/**************************************************************************/
/*!
@brief Write a perfectly vertical line, overwrite in subclasses if startWrite is defined!
@param x Top-most x coordinate
@param y Top-most y coordinate
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color) {
// Overwrite in subclasses if startWrite is defined!
// Can be just writeLine(x, y, x, y+h-1, color);
// or writeFillRect(x, y, 1, h, color);
drawFastVLine(x, y, h, color);
}
/**************************************************************************/
/*!
@brief Write a perfectly horizontal line, overwrite in subclasses if startWrite is defined!
@param x Left-most x coordinate
@param y Left-most y coordinate
@param w Width in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
// Overwrite in subclasses if startWrite is defined!
// Example: writeLine(x, y, x+w-1, y, color);
// or writeFillRect(x, y, w, 1, color);
drawFastHLine(x, y, w, color);
}
/**************************************************************************/
/*!
@brief Write a rectangle completely with one color, overwrite in subclasses if startWrite is defined!
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
// Overwrite in subclasses if desired!
fillRect(x,y,w,h,color);
}
/**************************************************************************/
/*!
@brief End a display-writing routine, overwrite in subclasses if startWrite is defined!
*/
/**************************************************************************/
void Adafruit_GFX::endWrite(){
}
/**************************************************************************/
/*!
@brief Draw a perfectly vertical line (this is often optimized in a subclass!)
@param x Top-most x coordinate
@param y Top-most y coordinate
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color) {
startWrite();
writeLine(x, y, x, y+h-1, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a perfectly horizontal line (this is often optimized in a subclass!)
@param x Left-most x coordinate
@param y Left-most y coordinate
@param w Width in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
startWrite();
writeLine(x, y, x+w-1, y, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Fill a rectangle completely with one color. Update in subclasses if desired!
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
startWrite();
for (int16_t i=x; i<x+w; i++) {
writeFastVLine(i, y, h, color);
}
endWrite();
}
/**************************************************************************/
/*!
@brief Fill the screen completely with one color. Update in subclasses if desired!
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color);
}
/**************************************************************************/
/*!
@brief Draw a line
@param x0 Start point x coordinate
@param y0 Start point y coordinate
@param x1 End point x coordinate
@param y1 End point y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
uint16_t color) {
// Update in subclasses if desired!
if(x0 == x1){
if(y0 > y1) _swap_int16_t(y0, y1);
drawFastVLine(x0, y0, y1 - y0 + 1, color);
} else if(y0 == y1){
if(x0 > x1) _swap_int16_t(x0, x1);
drawFastHLine(x0, y0, x1 - x0 + 1, color);
} else {
startWrite();
writeLine(x0, y0, x1, y1, color);
endWrite();
}
}
/**************************************************************************/
/*!
@brief Draw a circle outline
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
#if defined(ESP8266)
yield();
#endif
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
startWrite();
writePixel(x0 , y0+r, color);
writePixel(x0 , y0-r, color);
writePixel(x0+r, y0 , color);
writePixel(x0-r, y0 , color);
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
writePixel(x0 + x, y0 + y, color);
writePixel(x0 - x, y0 + y, color);
writePixel(x0 + x, y0 - y, color);
writePixel(x0 - x, y0 - y, color);
writePixel(x0 + y, y0 + x, color);
writePixel(x0 - y, y0 + x, color);
writePixel(x0 + y, y0 - x, color);
writePixel(x0 - y, y0 - x, color);
}
endWrite();
}
/**************************************************************************/
/*!
@brief Quarter-circle drawer, used to do circles and roundrects
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param cornername Mask bit #1 or bit #2 to indicate which quarters of the circle we're doing
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
int16_t r, uint8_t cornername, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x4) {
writePixel(x0 + x, y0 + y, color);
writePixel(x0 + y, y0 + x, color);
}
if (cornername & 0x2) {
writePixel(x0 + x, y0 - y, color);
writePixel(x0 + y, y0 - x, color);
}
if (cornername & 0x8) {
writePixel(x0 - y, y0 + x, color);
writePixel(x0 - x, y0 + y, color);
}
if (cornername & 0x1) {
writePixel(x0 - y, y0 - x, color);
writePixel(x0 - x, y0 - y, color);
}
}
}
/**************************************************************************/
/*!
@brief Draw a circle with filled color
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
startWrite();
writeFastVLine(x0, y0-r, 2*r+1, color);
fillCircleHelper(x0, y0, r, 3, 0, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Quarter-circle drawer with fill, used for circles and roundrects
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param corners Mask bits indicating which quarters we're doing
@param delta Offset from center-point, used for round-rects
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
uint8_t corners, int16_t delta, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
int16_t px = x;
int16_t py = y;
delta++; // Avoid some +1's in the loop
while(x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
// These checks avoid double-drawing certain lines, important
// for the SSD1306 library which has an INVERT drawing mode.
if(x < (y + 1)) {
if(corners & 1) writeFastVLine(x0+x, y0-y, 2*y+delta, color);
if(corners & 2) writeFastVLine(x0-x, y0-y, 2*y+delta, color);
}
if(y != py) {
if(corners & 1) writeFastVLine(x0+py, y0-px, 2*px+delta, color);
if(corners & 2) writeFastVLine(x0-py, y0-px, 2*px+delta, color);
py = y;
}
px = x;
}
}
/**************************************************************************/
/*!
@brief Draw a rectangle with no fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
startWrite();
writeFastHLine(x, y, w, color);
writeFastHLine(x, y+h-1, w, color);
writeFastVLine(x, y, h, color);
writeFastVLine(x+w-1, y, h, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a rounded rectangle with no fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param r Radius of corner rounding
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r, uint16_t color) {
int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis
if(r > max_radius) r = max_radius;
// smarter version
startWrite();
writeFastHLine(x+r , y , w-2*r, color); // Top
writeFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
writeFastVLine(x , y+r , h-2*r, color); // Left
writeFastVLine(x+w-1, y+r , h-2*r, color); // Right
// draw four corners
drawCircleHelper(x+r , y+r , r, 1, color);
drawCircleHelper(x+w-r-1, y+r , r, 2, color);
drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
drawCircleHelper(x+r , y+h-r-1, r, 8, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a rounded rectangle with fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param r Radius of corner rounding
@param color 16-bit 5-6-5 Color to draw/fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r, uint16_t color) {
int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis
if(r > max_radius) r = max_radius;
// smarter version
startWrite();
writeFillRect(x+r, y, w-2*r, h, color);
// draw four corners
fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a triangle with no fill color
@param x0 Vertex #0 x coordinate
@param y0 Vertex #0 y coordinate
@param x1 Vertex #1 x coordinate
@param y1 Vertex #1 y coordinate
@param x2 Vertex #2 x coordinate
@param y2 Vertex #2 y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0,
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
drawLine(x0, y0, x1, y1, color);
drawLine(x1, y1, x2, y2, color);
drawLine(x2, y2, x0, y0, color);
}
/**************************************************************************/
/*!
@brief Draw a triangle with color-fill
@param x0 Vertex #0 x coordinate
@param y0 Vertex #0 y coordinate
@param x1 Vertex #1 x coordinate
@param y1 Vertex #1 y coordinate
@param x2 Vertex #2 x coordinate
@param y2 Vertex #2 y coordinate
@param color 16-bit 5-6-5 Color to fill/draw with
*/
/**************************************************************************/
void Adafruit_GFX::fillTriangle(int16_t x0, int16_t y0,
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
int16_t a, b, y, last;
// Sort coordinates by Y order (y2 >= y1 >= y0)
if (y0 > y1) {
_swap_int16_t(y0, y1); _swap_int16_t(x0, x1);
}
if (y1 > y2) {
_swap_int16_t(y2, y1); _swap_int16_t(x2, x1);
}
if (y0 > y1) {
_swap_int16_t(y0, y1); _swap_int16_t(x0, x1);
}
startWrite();
if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
a = b = x0;
if(x1 < a) a = x1;
else if(x1 > b) b = x1;
if(x2 < a) a = x2;
else if(x2 > b) b = x2;
writeFastHLine(a, y0, b-a+1, color);
endWrite();
return;
}
int16_t
dx01 = x1 - x0,
dy01 = y1 - y0,
dx02 = x2 - x0,
dy02 = y2 - y0,
dx12 = x2 - x1,
dy12 = y2 - y1;
int32_t
sa = 0,
sb = 0;
// For upper part of triangle, find scanline crossings for segments
// 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
// is included here (and second loop will be skipped, avoiding a /0
// error there), otherwise scanline y1 is skipped here and handled
// in the second loop...which also avoids a /0 error here if y0=y1
// (flat-topped triangle).
if(y1 == y2) last = y1; // Include y1 scanline
else last = y1-1; // Skip it
for(y=y0; y<=last; y++) {
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
/* longhand:
a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if(a > b) _swap_int16_t(a,b);
writeFastHLine(a, y, b-a+1, color);
}
// For lower part of triangle, find scanline crossings for segments
// 0-2 and 1-2. This loop is skipped if y1=y2.
sa = (int32_t)dx12 * (y - y1);
sb = (int32_t)dx02 * (y - y0);
for(; y<=y2; y++) {
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
/* longhand:
a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if(a > b) _swap_int16_t(a,b);
writeFastHLine(a, y, b-a+1, color);
}
endWrite();
}
// BITMAP / XBITMAP / GRAYSCALE / RGB BITMAP FUNCTIONS ---------------------
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent).
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
if(byte & 0x80) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h,
uint16_t color, uint16_t bg) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
writePixel(x+i, y, (byte & 0x80) ? color : bg);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent).
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
if(byte & 0x80) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
writePixel(x+i, y, (byte & 0x80) ? color : bg);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw PROGMEM-resident XBitMap Files (*.xbm), exported from GIMP.
Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
C Array can be directly used with this function.
There is no RAM-resident version of this function; if generating bitmaps
in RAM, use the format defined by drawBitmap() and call that instead.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
*/
/**************************************************************************/
void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte >>= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
// Nearly identical to drawBitmap(), only the bit order
// is reversed here (left-to-right = LSB to MSB):
if(byte & 0x01) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y) pos.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, (uint8_t)pgm_read_byte(&bitmap[j * w + i]));
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 8-bit image (grayscale) at the specified (x,y) pos.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 8-bit image (grayscale) with a 1-bit mask
(set bits = opaque, unset bits = clear) at the specified (x,y) position.
BOTH buffers (grayscale and mask) must be PROGMEM-resident.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param mask byte array with mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], const uint8_t mask[],
int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&mask[j * bw + i / 8]);
if(byte & 0x80) {
writePixel(x+i, y, (uint8_t)pgm_read_byte(&bitmap[j * w + i]));
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 8-bit image (grayscale) with a 1-bit mask
(set bits = opaque, unset bits = clear) at the specified (x,y) position.
BOTH buffers (grayscale and mask) must be RAM-residentt, no mix-and-match
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param mask byte array with mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
uint8_t *bitmap, uint8_t *mask, int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = mask[j * bw + i / 8];
if(byte & 0x80) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
const uint16_t bitmap[], int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, pgm_read_word(&bitmap[j * w + i]));
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
uint16_t *bitmap, int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (color and mask) must be PROGMEM-resident. For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param mask byte array with monochrome mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
const uint16_t bitmap[], const uint8_t mask[],
int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&mask[j * bw + i / 8]);
if(byte & 0x80) {
writePixel(x+i, y, pgm_read_word(&bitmap[j * w + i]));
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (color and mask) must be RAM-resident. For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param mask byte array with monochrome mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
uint16_t *bitmap, uint8_t *mask, int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = mask[j * bw + i / 8];
if(byte & 0x80) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
}
endWrite();
}
// TEXT- AND CHARACTER-HANDLING FUNCTIONS ----------------------------------
// Draw a character
/**************************************************************************/
/*!
@brief Draw a single character
@param x Bottom left corner x coordinate
@param y Bottom left corner y coordinate
@param c The 8-bit font-indexed character (likely ascii)
@param color 16-bit 5-6-5 Color to draw chraracter with
@param bg 16-bit 5-6-5 Color to fill background with (if same as color, no background)
@param size Font magnification level, 1 is 'original' size
*/
/**************************************************************************/
void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
uint16_t color, uint16_t bg, uint8_t size) {
if(!gfxFont) { // 'Classic' built-in font
if((x >= _width) || // Clip right
(y >= _height) || // Clip bottom
((x + 6 * size - 1) < 0) || // Clip left
((y + 8 * size - 1) < 0)) // Clip top
return;
if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior
startWrite();
for(int8_t i=0; i<5; i++ ) { // Char bitmap = 5 columns
uint8_t line = pgm_read_byte(&font[c * 5 + i]);
for(int8_t j=0; j<8; j++, line >>= 1) {
if(line & 1) {
if(size == 1)
writePixel(x+i, y+j, color);
else
writeFillRect(x+i*size, y+j*size, size, size, color);
} else if(bg != color) {
if(size == 1)
writePixel(x+i, y+j, bg);
else
writeFillRect(x+i*size, y+j*size, size, size, bg);
}
}
}
if(bg != color) { // If opaque, draw vertical line for last column
if(size == 1) writeFastVLine(x+5, y, 8, bg);
else writeFillRect(x+5*size, y, size, 8*size, bg);
}
endWrite();
} else { // Custom font
// Character is assumed previously filtered by write() to eliminate
// newlines, returns, non-printable characters, etc. Calling
// drawChar() directly with 'bad' characters of font may cause mayhem!
c -= (uint8_t)pgm_read_byte(&gfxFont->first);
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
uint8_t *bitmap = (uint8_t *)pgm_read_pointer(&gfxFont->bitmap);
uint16_t bo = pgm_read_word(&glyph->bitmapOffset);
uint8_t w = pgm_read_byte(&glyph->width),
h = pgm_read_byte(&glyph->height);
int8_t xo = pgm_read_byte(&glyph->xOffset),
yo = pgm_read_byte(&glyph->yOffset);
uint8_t xx, yy, bits = 0, bit = 0;
int16_t xo16 = 0, yo16 = 0;
if(size > 1) {
xo16 = xo;
yo16 = yo;
}
// Todo: Add character clipping here
// NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS.
// THIS IS ON PURPOSE AND BY DESIGN. The background color feature
// has typically been used with the 'classic' font to overwrite old
// screen contents with new data. This ONLY works because the
// characters are a uniform size; it's not a sensible thing to do with
// proportionally-spaced fonts with glyphs of varying sizes (and that
// may overlap). To replace previously-drawn text when using a custom
// font, use the getTextBounds() function to determine the smallest
// rectangle encompassing a string, erase the area with fillRect(),
// then draw new text. This WILL infortunately 'blink' the text, but
// is unavoidable. Drawing 'background' pixels will NOT fix this,
// only creates a new set of problems. Have an idea to work around
// this (a canvas object type for MCUs that can afford the RAM and
// displays supporting setAddrWindow() and pushColors()), but haven't
// implemented this yet.
startWrite();
for(yy=0; yy<h; yy++) {
for(xx=0; xx<w; xx++) {
if(!(bit++ & 7)) {
bits = pgm_read_byte(&bitmap[bo++]);
}
if(bits & 0x80) {
if(size == 1) {
writePixel(x+xo+xx, y+yo+yy, color);
} else {
writeFillRect(x+(xo16+xx)*size, y+(yo16+yy)*size,
size, size, color);
}
} else {
if(size == 1) {
writePixel(x+xo+xx, y+yo+yy, bg);
} else {
writeFillRect(x+(xo16+xx)*size, y+(yo16+yy)*size,
size, size, bg);
}
}
bits <<= 1;
}
}
endWrite();
} // End classic vs custom font
}
/**************************************************************************/
/*!
@brief Print one byte/character of data, used to support print()
@param c The 8-bit ascii character to write
*/
/**************************************************************************/
size_t Adafruit_GFX::write(uint8_t c) {
if(!gfxFont) { // 'Classic' built-in font
if(c == '\n') { // Newline?
cursor_x = 0; // Reset x to zero,
cursor_y += textsize * 8; // advance y one line
} else if(c != '\r') { // Ignore carriage returns
if(wrap && ((cursor_x + textsize * 6) > _width)) { // Off right?
cursor_x = 0; // Reset x to zero,
cursor_y += textsize * 8; // advance y one line
}
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
cursor_x += textsize * 6; // Advance x one char
}
} else { // Custom font
if(c == '\n') {
cursor_x = 0;
cursor_y += (int16_t)textsize *
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
} else if(c != '\r') {
uint8_t first = pgm_read_byte(&gfxFont->first);
if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) {
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(
&gfxFont->glyph))[c - first]);
uint8_t w = pgm_read_byte(&glyph->width),
h = pgm_read_byte(&glyph->height);
if((w > 0) && (h > 0)) { // Is there an associated bitmap?
int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
if(wrap && ((cursor_x + textsize * (xo + w)) > _width)) {
cursor_x = 0;
cursor_y += (int16_t)textsize *
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
}
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
}
cursor_x += (uint8_t)pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize;
}
}
}
return 1;
}
/**************************************************************************/
/*!
@brief Set text 'magnification' size. Each increase in s makes 1 pixel that much bigger.
@param s Desired text size. 1 is default 6x8, 2 is 12x16, 3 is 18x24, etc
*/
/**************************************************************************/
void Adafruit_GFX::setTextSize(uint8_t s) {
textsize = (s > 0) ? s : 1;
}
/**************************************************************************/
/*!
@brief Set rotation setting for display
@param x 0 thru 3 corresponding to 4 cardinal rotations
*/
/**************************************************************************/
void Adafruit_GFX::setRotation(uint8_t x) {
rotation = (x & 3);
switch(rotation) {
case 0:
case 2:
_width = WIDTH;
_height = HEIGHT;
break;
case 1:
case 3:
_width = HEIGHT;
_height = WIDTH;
break;
}
}
/**************************************************************************/
/*!
@brief Set the font to display when print()ing, either custom or default
@param f The GFXfont object, if NULL use built in 6x8 font
*/
/**************************************************************************/
void Adafruit_GFX::setFont(const GFXfont *f) {
if(f) { // Font struct pointer passed in?
if(!gfxFont) { // And no current font struct?
// Switching from classic to new font behavior.
// Move cursor pos down 6 pixels so it's on baseline.
cursor_y += 6;
}
} else if(gfxFont) { // NULL passed. Current font struct defined?
// Switching from new to classic font behavior.
// Move cursor pos up 6 pixels so it's at top-left of char.
cursor_y -= 6;
}
gfxFont = (GFXfont *)f;
}
/**************************************************************************/
/*!
@brief Helper to determine size of a character with current font/size.
Broke this out as it's used by both the PROGMEM- and RAM-resident getTextBounds() functions.
@param c The ascii character in question
@param x Pointer to x location of character
@param y Pointer to y location of character
@param minx Minimum clipping value for X
@param miny Minimum clipping value for Y
@param maxx Maximum clipping value for X
@param maxy Maximum clipping value for Y
*/
/**************************************************************************/
void Adafruit_GFX::charBounds(char c, int16_t *x, int16_t *y,
int16_t *minx, int16_t *miny, int16_t *maxx, int16_t *maxy) {
if(gfxFont) {
if(c == '\n') { // Newline?
*x = 0; // Reset x to zero, advance y by one line
*y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
} else if(c != '\r') { // Not a carriage return; is normal char
uint8_t first = pgm_read_byte(&gfxFont->first),
last = pgm_read_byte(&gfxFont->last);
if((c >= first) && (c <= last)) { // Char present in this font?
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(
&gfxFont->glyph))[c - first]);
uint8_t gw = pgm_read_byte(&glyph->width),
gh = pgm_read_byte(&glyph->height),
xa = pgm_read_byte(&glyph->xAdvance);
int8_t xo = pgm_read_byte(&glyph->xOffset),
yo = pgm_read_byte(&glyph->yOffset);
if(wrap && ((*x+(((int16_t)xo+gw)*textsize)) > _width)) {
*x = 0; // Reset x to zero, advance y by one line
*y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
}
int16_t ts = (int16_t)textsize,
x1 = *x + xo * ts,
y1 = *y + yo * ts,
x2 = x1 + gw * ts - 1,
y2 = y1 + gh * ts - 1;
if(x1 < *minx) *minx = x1;
if(y1 < *miny) *miny = y1;
if(x2 > *maxx) *maxx = x2;
if(y2 > *maxy) *maxy = y2;
*x += xa * ts;
}
}
} else { // Default font
if(c == '\n') { // Newline?
*x = 0; // Reset x to zero,
*y += textsize * 8; // advance y one line
// min/max x/y unchaged -- that waits for next 'normal' character
} else if(c != '\r') { // Normal char; ignore carriage returns
if(wrap && ((*x + textsize * 6) > _width)) { // Off right?
*x = 0; // Reset x to zero,
*y += textsize * 8; // advance y one line
}
int x2 = *x + textsize * 6 - 1, // Lower-right pixel of char
y2 = *y + textsize * 8 - 1;
if(x2 > *maxx) *maxx = x2; // Track max x, y
if(y2 > *maxy) *maxy = y2;
if(*x < *minx) *minx = *x; // Track min x, y
if(*y < *miny) *miny = *y;
*x += textsize * 6; // Advance x one char
}
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The ascii string to measure
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const char *str, int16_t x, int16_t y,
int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
uint8_t c; // Current character
*x1 = x;
*y1 = y;
*w = *h = 0;
int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;
while((c = *str++))
charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);
if(maxx >= minx) {
*x1 = minx;
*w = maxx - minx + 1;
}
if(maxy >= miny) {
*y1 = miny;
*h = maxy - miny + 1;
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The ascii string to measure (as an arduino String() class)
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const String &str, int16_t x, int16_t y,
int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
if (str.length() != 0) {
getTextBounds(const_cast<char*>(str.c_str()), x, y, x1, y1, w, h);
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a PROGMEM string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The flash-memory ascii string to measure
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str,
int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
uint8_t *s = (uint8_t *)str, c;
*x1 = x;
*y1 = y;
*w = *h = 0;
int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;
while((c = pgm_read_byte(s++)))
charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);
if(maxx >= minx) {
*x1 = minx;
*w = maxx - minx + 1;
}
if(maxy >= miny) {
*y1 = miny;
*h = maxy - miny + 1;
}
}
/**************************************************************************/
/*!
@brief Invert the display (ideally using built-in hardware command)
@param i True if you want to invert, false to make 'normal'
*/
/**************************************************************************/
void Adafruit_GFX::invertDisplay(boolean i) {
// Do nothing, must be subclassed if supported by hardware
}
/***************************************************************************/
/**************************************************************************/
/*!
@brief Create a simple drawn button UI element
*/
/**************************************************************************/
Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
_gfx = 0;
}
/**************************************************************************/
/*!
@brief Initialize button with our desired color/size/settings
@param gfx Pointer to our display so we can draw to it!
@param x The X coordinate of the center of the button
@param y The Y coordinate of the center of the button
@param w Width of the buttton
@param h Height of the buttton
@param outline Color of the outline (16-bit 5-6-5 standard)
@param fill Color of the button fill (16-bit 5-6-5 standard)
@param textcolor Color of the button label (16-bit 5-6-5 standard)
@param label Ascii string of the text inside the button
@param textsize The font magnification of the label text
*/
/**************************************************************************/
// Classic initButton() function: pass center & size
void Adafruit_GFX_Button::initButton(
Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
uint16_t outline, uint16_t fill, uint16_t textcolor,
char *label, uint8_t textsize)
{
// Tweak arguments and pass to the newer initButtonUL() function...
initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill,
textcolor, label, textsize);
}
/**************************************************************************/
/*!
@brief Initialize button with our desired color/size/settings, with upper-left coordinates
@param gfx Pointer to our display so we can draw to it!
@param x1 The X coordinate of the Upper-Left corner of the button
@param y1 The Y coordinate of the Upper-Left corner of the button
@param w Width of the buttton
@param h Height of the buttton
@param outline Color of the outline (16-bit 5-6-5 standard)
@param fill Color of the button fill (16-bit 5-6-5 standard)
@param textcolor Color of the button label (16-bit 5-6-5 standard)
@param label Ascii string of the text inside the button
@param textsize The font magnification of the label text
*/
/**************************************************************************/
void Adafruit_GFX_Button::initButtonUL(
Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
uint16_t outline, uint16_t fill, uint16_t textcolor,
char *label, uint8_t textsize)
{
_x1 = x1;
_y1 = y1;
_w = w;
_h = h;
_outlinecolor = outline;
_fillcolor = fill;
_textcolor = textcolor;
_textsize = textsize;
_gfx = gfx;
strncpy(_label, label, 9);
}
/**************************************************************************/
/*!
@brief Draw the button on the screen
@param inverted Whether to draw with fill/text swapped to indicate 'pressed'
*/
/**************************************************************************/
void Adafruit_GFX_Button::drawButton(boolean inverted) {
uint16_t fill, outline, text;
if(!inverted) {
fill = _fillcolor;
outline = _outlinecolor;
text = _textcolor;
} else {
fill = _textcolor;
outline = _outlinecolor;
text = _fillcolor;
}
uint8_t r = min(_w, _h) / 4; // Corner radius
_gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill);
_gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline);
_gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize),
_y1 + (_h/2) - (4 * _textsize));
_gfx->setTextColor(text);
_gfx->setTextSize(_textsize);
_gfx->print(_label);
}
/**************************************************************************/
/*!
@brief Helper to let us know if a coordinate is within the bounds of the button
@param x The X coordinate to check
@param y The Y coordinate to check
@returns True if within button graphics outline
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
return ((x >= _x1) && (x < (int16_t) (_x1 + _w)) &&
(y >= _y1) && (y < (int16_t) (_y1 + _h)));
}
/**************************************************************************/
/*!
@brief Query whether the button was pressed since we last checked state
@returns True if was not-pressed before, now is.
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
/**************************************************************************/
/*!
@brief Query whether the button was released since we last checked state
@returns True if was pressed before, now is not.
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }
// -------------------------------------------------------------------------
// GFXcanvas1, GFXcanvas8 and GFXcanvas16 (currently a WIP, don't get too
// comfy with the implementation) provide 1-, 8- and 16-bit offscreen
// canvases, the address of which can be passed to drawBitmap() or
// pushColors() (the latter appears only in a couple of GFX-subclassed TFT
// libraries at this time). This is here mostly to help with the recently-
// added proportionally-spaced fonts; adds a way to refresh a section of the
// screen without a massive flickering clear-and-redraw...but maybe you'll
// find other uses too. VERY RAM-intensive, since the buffer is in MCU
// memory and not the display driver...GXFcanvas1 might be minimally useful
// on an Uno-class board, but this and the others are much more likely to
// require at least a Mega or various recent ARM-type boards (recommended,
// as the text+bitmap draw can be pokey). GFXcanvas1 requires 1 bit per
// pixel (rounded up to nearest byte per scanline), GFXcanvas8 is 1 byte
// per pixel (no scanline pad), and GFXcanvas16 uses 2 bytes per pixel (no
// scanline pad).
// NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.
/**************************************************************************/
/*!
@brief Instatiate a GFX 1-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint16_t bytes = ((w + 7) / 8) * h;
if((buffer = (uint8_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas1::~GFXcanvas1(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas1::drawPixel(int16_t x, int16_t y, uint16_t color) {
#ifdef __AVR__
// Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR
static const uint8_t PROGMEM
GFXsetBit[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 },
GFXclrBit[] = { 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE };
#endif
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
uint8_t *ptr = &buffer[(x / 8) + y * ((WIDTH + 7) / 8)];
#ifdef __AVR__
if(color) *ptr |= pgm_read_byte(&GFXsetBit[x & 7]);
else *ptr &= pgm_read_byte(&GFXclrBit[x & 7]);
#else
if(color) *ptr |= 0x80 >> (x & 7);
else *ptr &= ~(0x80 >> (x & 7));
#endif
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas1::fillScreen(uint16_t color) {
if(buffer) {
uint16_t bytes = ((WIDTH + 7) / 8) * HEIGHT;
memset(buffer, color ? 0xFF : 0x00, bytes);
}
}
/**************************************************************************/
/*!
@brief Instatiate a GFX 8-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas8::GFXcanvas8(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint32_t bytes = w * h;
if((buffer = (uint8_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas8::~GFXcanvas8(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas8::drawPixel(int16_t x, int16_t y, uint16_t color) {
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
buffer[x + y * WIDTH] = color;
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas8::fillScreen(uint16_t color) {
if(buffer) {
memset(buffer, color, WIDTH * HEIGHT);
}
}
void GFXcanvas8::writeFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
if((x >= _width) || (y < 0) || (y >= _height)) return;
int16_t x2 = x + w - 1;
if(x2 < 0) return;
// Clip left/right
if(x < 0) {
x = 0;
w = x2 + 1;
}
if(x2 >= _width) w = _width - x;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
memset(buffer + y * WIDTH + x, color, w);
}
/**************************************************************************/
/*!
@brief Instatiate a GFX 16-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint32_t bytes = w * h * 2;
if((buffer = (uint16_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas16::~GFXcanvas16(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas16::drawPixel(int16_t x, int16_t y, uint16_t color) {
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
buffer[x + y * WIDTH] = color;
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas16::fillScreen(uint16_t color) {
if(buffer) {
uint8_t hi = color >> 8, lo = color & 0xFF;
if(hi == lo) {
memset(buffer, lo, WIDTH * HEIGHT * 2);
} else {
uint32_t i, pixels = WIDTH * HEIGHT;
for(i=0; i<pixels; i++) buffer[i] = color;
}
}
}
/**************************************************************************/
/*!
@brief Reverses the "endian-ness" of each 16-bit pixel within the
canvas; little-endian to big-endian, or big-endian to little.
Most microcontrollers (such as SAMD) are little-endian, while
most displays tend toward big-endianness. All the drawing
functions (including RGB bitmap drawing) take care of this
automatically, but some specialized code (usually involving
DMA) can benefit from having pixel data already in the
display-native order. Note that this does NOT convert to a
SPECIFIC endian-ness, it just flips the bytes within each word.
*/
/**************************************************************************/
void GFXcanvas16::byteSwap(void) {
if(buffer) {
uint32_t i, pixels = WIDTH * HEIGHT;
for(i=0; i<pixels; i++) buffer[i] = __builtin_bswap16(buffer[i]);
}
}
ругается на не совместимость с мега 2560 чет такое. так вот теперь реакция пошла:). это в сетупе myGLCD.setTextColor(WHITE, BLACK); . затирание идет но остается на фоне цифр мелкий не дотертый мусор. и цифра например обороты были 800 потом 1200 то 1 остается а за остальными мелкий не дотертый мусор. Подправил везде цвета, все ок. Не знаю че ты там на колдовал но затирание идет, но остается мелкий не дотерный мусор с зади цифр, и не вытерает первую цифру при увеличении цисла в 4ох на оборотах. я думаю со скоростью таже история будет, тока при увеличении до 3ех значного, 3ех значная улетает в право на надпись км/ч и там застывает.
в сетуп теперь нет нужды кидать цвет, кинул в пару мест где нужно и все что нужно стало красны и что нужно белым.
И не могу понять как прописать линимя цвет в Adafruit_GFX.cpp четко написано
/*
This is the core graphics library for all our displays, providing a common
set of graphics primitives (points, lines, circles, etc.). It needs to be
paired with a hardware-specific library for each display device we carry
(to handle the lower-level functions).
Adafruit invests time and resources providing this open source code, please
support Adafruit & open-source hardware by purchasing products from Adafruit!
Copyright (c) 2013 Adafruit Industries. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "Adafruit_GFX.h"
#include "glcdfont.c"
#ifdef __AVR__
#include <avr/pgmspace.h>
#elif defined(ESP8266) || defined(ESP32)
#include <pgmspace.h>
#endif
// Many (but maybe not all) non-AVR board installs define macros
// for compatibility with existing PROGMEM-reading AVR code.
// Do our own checks and defines here for good measure...
#ifndef pgm_read_byte
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#endif
#ifndef pgm_read_word
#define pgm_read_word(addr) (*(const unsigned short *)(addr))
#endif
#ifndef pgm_read_dword
#define pgm_read_dword(addr) (*(const unsigned long *)(addr))
#endif
// Pointers are a peculiar case...typically 16-bit on AVR boards,
// 32 bits elsewhere. Try to accommodate both...
#if !defined(__INT_MAX__) || (__INT_MAX__ > 0xFFFF)
#define pgm_read_pointer(addr) ((void *)pgm_read_dword(addr))
#else
#define pgm_read_pointer(addr) ((void *)pgm_read_word(addr))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
#endif
/**************************************************************************/
/*!
@brief Instatiate a GFX context for graphics! Can only be done by a superclass
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h):
WIDTH(w), HEIGHT(h)
{
_width = WIDTH;
_height = HEIGHT;
rotation = 0;
cursor_y = cursor_x = 0;
textsize = 1;
textcolor = textbgcolor = 0xFFFF;
wrap = true;
_cp437 = false;
gfxFont = NULL;
}
/**************************************************************************/
/*!
@brief Write a line. Bresenham's algorithm - thx wikpedia
@param x0 Start point x coordinate
@param y0 Start point y coordinate
@param x1 End point x coordinate
@param y1 End point y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
uint16_t color) {
#if defined(ESP8266)
yield();
#endif
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
_swap_int16_t(x0, y0);
_swap_int16_t(x1, y1);
}
if (x0 > x1) {
_swap_int16_t(x0, x1);
_swap_int16_t(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for (; x0<=x1; x0++) {
if (steep) {
writePixel(y0, x0, color);
} else {
writePixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
/**************************************************************************/
/*!
@brief Start a display-writing routine, overwrite in subclasses.
*/
/**************************************************************************/
void Adafruit_GFX::startWrite(){
}
/**************************************************************************/
/*!
@brief Write a pixel, overwrite in subclasses if startWrite is defined!
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writePixel(int16_t x, int16_t y, uint16_t color){
drawPixel(x, y, color);
}
/**************************************************************************/
/*!
@brief Write a perfectly vertical line, overwrite in subclasses if startWrite is defined!
@param x Top-most x coordinate
@param y Top-most y coordinate
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color) {
// Overwrite in subclasses if startWrite is defined!
// Can be just writeLine(x, y, x, y+h-1, color);
// or writeFillRect(x, y, 1, h, color);
drawFastVLine(x, y, h, color);
}
/**************************************************************************/
/*!
@brief Write a perfectly horizontal line, overwrite in subclasses if startWrite is defined!
@param x Left-most x coordinate
@param y Left-most y coordinate
@param w Width in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
// Overwrite in subclasses if startWrite is defined!
// Example: writeLine(x, y, x+w-1, y, color);
// or writeFillRect(x, y, w, 1, color);
drawFastHLine(x, y, w, color);
}
/**************************************************************************/
/*!
@brief Write a rectangle completely with one color, overwrite in subclasses if startWrite is defined!
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
// Overwrite in subclasses if desired!
fillRect(x,y,w,h,color);
}
/**************************************************************************/
/*!
@brief End a display-writing routine, overwrite in subclasses if startWrite is defined!
*/
/**************************************************************************/
void Adafruit_GFX::endWrite(){
}
/**************************************************************************/
/*!
@brief Draw a perfectly vertical line (this is often optimized in a subclass!)
@param x Top-most x coordinate
@param y Top-most y coordinate
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color) {
startWrite();
writeLine(x, y, x, y+h-1, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a perfectly horizontal line (this is often optimized in a subclass!)
@param x Left-most x coordinate
@param y Left-most y coordinate
@param w Width in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
startWrite();
writeLine(x, y, x+w-1, y, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Fill a rectangle completely with one color. Update in subclasses if desired!
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
startWrite();
for (int16_t i=x; i<x+w; i++) {
writeFastVLine(i, y, h, color);
}
endWrite();
}
/**************************************************************************/
/*!
@brief Fill the screen completely with one color. Update in subclasses if desired!
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color);
}
/**************************************************************************/
/*!
@brief Draw a line
@param x0 Start point x coordinate
@param y0 Start point y coordinate
@param x1 End point x coordinate
@param y1 End point y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
uint16_t color) {
// Update in subclasses if desired!
if(x0 == x1){
if(y0 > y1) _swap_int16_t(y0, y1);
drawFastVLine(x0, y0, y1 - y0 + 1, color);
} else if(y0 == y1){
if(x0 > x1) _swap_int16_t(x0, x1);
drawFastHLine(x0, y0, x1 - x0 + 1, color);
} else {
startWrite();
writeLine(x0, y0, x1, y1, color);
endWrite();
}
}
/**************************************************************************/
/*!
@brief Draw a circle outline
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
#if defined(ESP8266)
yield();
#endif
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
startWrite();
writePixel(x0 , y0+r, color);
writePixel(x0 , y0-r, color);
writePixel(x0+r, y0 , color);
writePixel(x0-r, y0 , color);
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
writePixel(x0 + x, y0 + y, color);
writePixel(x0 - x, y0 + y, color);
writePixel(x0 + x, y0 - y, color);
writePixel(x0 - x, y0 - y, color);
writePixel(x0 + y, y0 + x, color);
writePixel(x0 - y, y0 + x, color);
writePixel(x0 + y, y0 - x, color);
writePixel(x0 - y, y0 - x, color);
}
endWrite();
}
/**************************************************************************/
/*!
@brief Quarter-circle drawer, used to do circles and roundrects
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param cornername Mask bit #1 or bit #2 to indicate which quarters of the circle we're doing
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
int16_t r, uint8_t cornername, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x4) {
writePixel(x0 + x, y0 + y, color);
writePixel(x0 + y, y0 + x, color);
}
if (cornername & 0x2) {
writePixel(x0 + x, y0 - y, color);
writePixel(x0 + y, y0 - x, color);
}
if (cornername & 0x8) {
writePixel(x0 - y, y0 + x, color);
writePixel(x0 - x, y0 + y, color);
}
if (cornername & 0x1) {
writePixel(x0 - y, y0 - x, color);
writePixel(x0 - x, y0 - y, color);
}
}
}
/**************************************************************************/
/*!
@brief Draw a circle with filled color
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
startWrite();
writeFastVLine(x0, y0-r, 2*r+1, color);
fillCircleHelper(x0, y0, r, 3, 0, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Quarter-circle drawer with fill, used for circles and roundrects
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param corners Mask bits indicating which quarters we're doing
@param delta Offset from center-point, used for round-rects
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
uint8_t corners, int16_t delta, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
int16_t px = x;
int16_t py = y;
delta++; // Avoid some +1's in the loop
while(x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
// These checks avoid double-drawing certain lines, important
// for the SSD1306 library which has an INVERT drawing mode.
if(x < (y + 1)) {
if(corners & 1) writeFastVLine(x0+x, y0-y, 2*y+delta, color);
if(corners & 2) writeFastVLine(x0-x, y0-y, 2*y+delta, color);
}
if(y != py) {
if(corners & 1) writeFastVLine(x0+py, y0-px, 2*px+delta, color);
if(corners & 2) writeFastVLine(x0-py, y0-px, 2*px+delta, color);
py = y;
}
px = x;
}
}
/**************************************************************************/
/*!
@brief Draw a rectangle with no fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
startWrite();
writeFastHLine(x, y, w, color);
writeFastHLine(x, y+h-1, w, color);
writeFastVLine(x, y, h, color);
writeFastVLine(x+w-1, y, h, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a rounded rectangle with no fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param r Radius of corner rounding
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r, uint16_t color) {
int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis
if(r > max_radius) r = max_radius;
// smarter version
startWrite();
writeFastHLine(x+r , y , w-2*r, color); // Top
writeFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
writeFastVLine(x , y+r , h-2*r, color); // Left
writeFastVLine(x+w-1, y+r , h-2*r, color); // Right
// draw four corners
drawCircleHelper(x+r , y+r , r, 1, color);
drawCircleHelper(x+w-r-1, y+r , r, 2, color);
drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
drawCircleHelper(x+r , y+h-r-1, r, 8, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a rounded rectangle with fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param r Radius of corner rounding
@param color 16-bit 5-6-5 Color to draw/fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r, uint16_t color) {
int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis
if(r > max_radius) r = max_radius;
// smarter version
startWrite();
writeFillRect(x+r, y, w-2*r, h, color);
// draw four corners
fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a triangle with no fill color
@param x0 Vertex #0 x coordinate
@param y0 Vertex #0 y coordinate
@param x1 Vertex #1 x coordinate
@param y1 Vertex #1 y coordinate
@param x2 Vertex #2 x coordinate
@param y2 Vertex #2 y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0,
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
drawLine(x0, y0, x1, y1, color);
drawLine(x1, y1, x2, y2, color);
drawLine(x2, y2, x0, y0, color);
}
/**************************************************************************/
/*!
@brief Draw a triangle with color-fill
@param x0 Vertex #0 x coordinate
@param y0 Vertex #0 y coordinate
@param x1 Vertex #1 x coordinate
@param y1 Vertex #1 y coordinate
@param x2 Vertex #2 x coordinate
@param y2 Vertex #2 y coordinate
@param color 16-bit 5-6-5 Color to fill/draw with
*/
/**************************************************************************/
void Adafruit_GFX::fillTriangle(int16_t x0, int16_t y0,
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
int16_t a, b, y, last;
// Sort coordinates by Y order (y2 >= y1 >= y0)
if (y0 > y1) {
_swap_int16_t(y0, y1); _swap_int16_t(x0, x1);
}
if (y1 > y2) {
_swap_int16_t(y2, y1); _swap_int16_t(x2, x1);
}
if (y0 > y1) {
_swap_int16_t(y0, y1); _swap_int16_t(x0, x1);
}
startWrite();
if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
a = b = x0;
if(x1 < a) a = x1;
else if(x1 > b) b = x1;
if(x2 < a) a = x2;
else if(x2 > b) b = x2;
writeFastHLine(a, y0, b-a+1, color);
endWrite();
return;
}
int16_t
dx01 = x1 - x0,
dy01 = y1 - y0,
dx02 = x2 - x0,
dy02 = y2 - y0,
dx12 = x2 - x1,
dy12 = y2 - y1;
int32_t
sa = 0,
sb = 0;
// For upper part of triangle, find scanline crossings for segments
// 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
// is included here (and second loop will be skipped, avoiding a /0
// error there), otherwise scanline y1 is skipped here and handled
// in the second loop...which also avoids a /0 error here if y0=y1
// (flat-topped triangle).
if(y1 == y2) last = y1; // Include y1 scanline
else last = y1-1; // Skip it
for(y=y0; y<=last; y++) {
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
/* longhand:
a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if(a > b) _swap_int16_t(a,b);
writeFastHLine(a, y, b-a+1, color);
}
// For lower part of triangle, find scanline crossings for segments
// 0-2 and 1-2. This loop is skipped if y1=y2.
sa = (int32_t)dx12 * (y - y1);
sb = (int32_t)dx02 * (y - y0);
for(; y<=y2; y++) {
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
/* longhand:
a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if(a > b) _swap_int16_t(a,b);
writeFastHLine(a, y, b-a+1, color);
}
endWrite();
}
// BITMAP / XBITMAP / GRAYSCALE / RGB BITMAP FUNCTIONS ---------------------
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent).
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
if(byte & 0x80) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h,
uint16_t color, uint16_t bg) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
writePixel(x+i, y, (byte & 0x80) ? color : bg);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent).
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
if(byte & 0x80) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
writePixel(x+i, y, (byte & 0x80) ? color : bg);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw PROGMEM-resident XBitMap Files (*.xbm), exported from GIMP.
Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
C Array can be directly used with this function.
There is no RAM-resident version of this function; if generating bitmaps
in RAM, use the format defined by drawBitmap() and call that instead.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
*/
/**************************************************************************/
void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte >>= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
// Nearly identical to drawBitmap(), only the bit order
// is reversed here (left-to-right = LSB to MSB):
if(byte & 0x01) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y) pos.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, (uint8_t)pgm_read_byte(&bitmap[j * w + i]));
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 8-bit image (grayscale) at the specified (x,y) pos.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 8-bit image (grayscale) with a 1-bit mask
(set bits = opaque, unset bits = clear) at the specified (x,y) position.
BOTH buffers (grayscale and mask) must be PROGMEM-resident.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param mask byte array with mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], const uint8_t mask[],
int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&mask[j * bw + i / 8]);
if(byte & 0x80) {
writePixel(x+i, y, (uint8_t)pgm_read_byte(&bitmap[j * w + i]));
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 8-bit image (grayscale) with a 1-bit mask
(set bits = opaque, unset bits = clear) at the specified (x,y) position.
BOTH buffers (grayscale and mask) must be RAM-residentt, no mix-and-match
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param mask byte array with mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
uint8_t *bitmap, uint8_t *mask, int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = mask[j * bw + i / 8];
if(byte & 0x80) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
const uint16_t bitmap[], int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, pgm_read_word(&bitmap[j * w + i]));
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
uint16_t *bitmap, int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (color and mask) must be PROGMEM-resident. For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param mask byte array with monochrome mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
const uint16_t bitmap[], const uint8_t mask[],
int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&mask[j * bw + i / 8]);
if(byte & 0x80) {
writePixel(x+i, y, pgm_read_word(&bitmap[j * w + i]));
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (color and mask) must be RAM-resident. For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param mask byte array with monochrome mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
uint16_t *bitmap, uint8_t *mask, int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = mask[j * bw + i / 8];
if(byte & 0x80) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
}
endWrite();
}
// TEXT- AND CHARACTER-HANDLING FUNCTIONS ----------------------------------
// Draw a character
/**************************************************************************/
/*!
@brief Draw a single character
@param x Bottom left corner x coordinate
@param y Bottom left corner y coordinate
@param c The 8-bit font-indexed character (likely ascii)
@param color 16-bit 5-6-5 Color to draw chraracter with
@param bg 16-bit 5-6-5 Color to fill background with (if same as color, no background)
@param size Font magnification level, 1 is 'original' size
*/
/**************************************************************************/
void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
uint16_t color, uint16_t bg, uint8_t size) {
if(!gfxFont) { // 'Classic' built-in font
if((x >= _width) || // Clip right
(y >= _height) || // Clip bottom
((x + 6 * size - 1) < 0) || // Clip left
((y + 8 * size - 1) < 0)) // Clip top
return;
if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior
startWrite();
for(int8_t i=0; i<5; i++ ) { // Char bitmap = 5 columns
uint8_t line = pgm_read_byte(&font[c * 5 + i]);
for(int8_t j=0; j<8; j++, line >>= 1) {
if(line & 1) {
if(size == 1)
writePixel(x+i, y+j, color);
else
writeFillRect(x+i*size, y+j*size, size, size, color);
} else if(bg != color) {
if(size == 1)
writePixel(x+i, y+j, bg);
else
writeFillRect(x+i*size, y+j*size, size, size, bg);
}
}
}
if(bg != color) { // If opaque, draw vertical line for last column
if(size == 1) writeFastVLine(x+5, y, 8, bg);
else writeFillRect(x+5*size, y, size, 8*size, bg);
}
endWrite();
} else { // Custom font
// Character is assumed previously filtered by write() to eliminate
// newlines, returns, non-printable characters, etc. Calling
// drawChar() directly with 'bad' characters of font may cause mayhem!
c -= (uint8_t)pgm_read_byte(&gfxFont->first);
uint8_t ya = pgm_read_byte(&gfxFont->yAdvance);
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
uint8_t *bitmap = (uint8_t *)pgm_read_pointer(&gfxFont->bitmap);
uint16_t bo = pgm_read_word(&glyph->bitmapOffset);
uint8_t w = pgm_read_byte(&glyph->width),
h = pgm_read_byte(&glyph->height),
xa = pgm_read_byte(&glyph->xAdvance);
int8_t xo = pgm_read_byte(&glyph->xOffset),
yo = pgm_read_byte(&glyph->yOffset);
uint8_t xx, yy, bits = 0, bit = 0;
int16_t xo16 = 0, yo16 = 0;
uint16_t cn = bg;
if(size > 1) {
xo16 = xo;
yo16 = yo;
}
// Todo: Add character clipping here
// NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS.
// THIS IS ON PURPOSE AND BY DESIGN. The background color feature
// has typically been used with the 'classic' font to overwrite old
// screen contents with new data. This ONLY works because the
// characters are a uniform size; it's not a sensible thing to do with
// proportionally-spaced fonts with glyphs of varying sizes (and that
// may overlap). To replace previously-drawn text when using a custom
// font, use the getTextBounds() function to determine the smallest
// rectangle encompassing a string, erase the area with fillRect(),
// then draw new text. This WILL infortunately 'blink' the text, but
// is unavoidable. Drawing 'background' pixels will NOT fix this,
// only creates a new set of problems. Have an idea to work around
// this (a canvas object type for MCUs that can afford the RAM and
// displays supporting setAddrWindow() and pushColors()), but haven't
// implemented this yet.
startWrite();
for(yy=0; yy<ya; yy++) {
for(xx=0; xx<xa; xx++) {
cn = bg;
if ((yy>=(ya+yo))&&(yy<=(ya+yo+h))&&(xx>=xo)&&(xx<=(xo+w))) {
if(!(bit++ & 7)) {
bits = pgm_read_byte(&bitmap[bo++]);
}
if(bits & 0x80) cn = color;
}
if(size == 1) {
writePixel(x+xx, y-ya+yy, cn);
} else {
writeFillRect(x+xx*size, y-(ya-yy)*size, size, size, cn);
}
bits <<= 1;
}
}
endWrite();
} // End classic vs custom font
}
/**************************************************************************/
/*!
@brief Print one byte/character of data, used to support print()
@param c The 8-bit ascii character to write
*/
/**************************************************************************/
size_t Adafruit_GFX::write(uint8_t c) {
if(!gfxFont) { // 'Classic' built-in font
if(c == '\n') { // Newline?
cursor_x = 0; // Reset x to zero,
cursor_y += textsize * 8; // advance y one line
} else if(c != '\r') { // Ignore carriage returns
if(wrap && ((cursor_x + textsize * 6) > _width)) { // Off right?
cursor_x = 0; // Reset x to zero,
cursor_y += textsize * 8; // advance y one line
}
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
cursor_x += textsize * 6; // Advance x one char
}
} else { // Custom font
if(c == '\n') {
cursor_x = 0;
cursor_y += (int16_t)textsize *
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
} else if(c != '\r') {
uint8_t first = pgm_read_byte(&gfxFont->first);
if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) {
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(
&gfxFont->glyph))[c - first]);
uint8_t w = pgm_read_byte(&glyph->width),
h = pgm_read_byte(&glyph->height);
if((w > 0) && (h > 0)) { // Is there an associated bitmap?
int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
if(wrap && ((cursor_x + textsize * (xo + w)) > _width)) {
cursor_x = 0;
cursor_y += (int16_t)textsize *
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
}
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
}
cursor_x += (uint8_t)pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize;
}
}
}
return 1;
}
/**************************************************************************/
/*!
@brief Set text 'magnification' size. Each increase in s makes 1 pixel that much bigger.
@param s Desired text size. 1 is default 6x8, 2 is 12x16, 3 is 18x24, etc
*/
/**************************************************************************/
void Adafruit_GFX::setTextSize(uint8_t s) {
textsize = (s > 0) ? s : 1;
}
/**************************************************************************/
/*!
@brief Set rotation setting for display
@param x 0 thru 3 corresponding to 4 cardinal rotations
*/
/**************************************************************************/
void Adafruit_GFX::setRotation(uint8_t x) {
rotation = (x & 3);
switch(rotation) {
case 0:
case 2:
_width = WIDTH;
_height = HEIGHT;
break;
case 1:
case 3:
_width = HEIGHT;
_height = WIDTH;
break;
}
}
/**************************************************************************/
/*!
@brief Set the font to display when print()ing, either custom or default
@param f The GFXfont object, if NULL use built in 6x8 font
*/
/**************************************************************************/
void Adafruit_GFX::setFont(const GFXfont *f) {
if(f) { // Font struct pointer passed in?
if(!gfxFont) { // And no current font struct?
// Switching from classic to new font behavior.
// Move cursor pos down 6 pixels so it's on baseline.
cursor_y += 6;
}
} else if(gfxFont) { // NULL passed. Current font struct defined?
// Switching from new to classic font behavior.
// Move cursor pos up 6 pixels so it's at top-left of char.
cursor_y -= 6;
}
gfxFont = (GFXfont *)f;
}
/**************************************************************************/
/*!
@brief Helper to determine size of a character with current font/size.
Broke this out as it's used by both the PROGMEM- and RAM-resident getTextBounds() functions.
@param c The ascii character in question
@param x Pointer to x location of character
@param y Pointer to y location of character
@param minx Minimum clipping value for X
@param miny Minimum clipping value for Y
@param maxx Maximum clipping value for X
@param maxy Maximum clipping value for Y
*/
/**************************************************************************/
void Adafruit_GFX::charBounds(char c, int16_t *x, int16_t *y,
int16_t *minx, int16_t *miny, int16_t *maxx, int16_t *maxy) {
if(gfxFont) {
if(c == '\n') { // Newline?
*x = 0; // Reset x to zero, advance y by one line
*y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
} else if(c != '\r') { // Not a carriage return; is normal char
uint8_t first = pgm_read_byte(&gfxFont->first),
last = pgm_read_byte(&gfxFont->last);
if((c >= first) && (c <= last)) { // Char present in this font?
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(
&gfxFont->glyph))[c - first]);
uint8_t gw = pgm_read_byte(&glyph->width),
gh = pgm_read_byte(&glyph->height),
xa = pgm_read_byte(&glyph->xAdvance);
int8_t xo = pgm_read_byte(&glyph->xOffset),
yo = pgm_read_byte(&glyph->yOffset);
if(wrap && ((*x+(((int16_t)xo+gw)*textsize)) > _width)) {
*x = 0; // Reset x to zero, advance y by one line
*y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
}
int16_t ts = (int16_t)textsize,
x1 = *x + xo * ts,
y1 = *y + yo * ts,
x2 = x1 + gw * ts - 1,
y2 = y1 + gh * ts - 1;
if(x1 < *minx) *minx = x1;
if(y1 < *miny) *miny = y1;
if(x2 > *maxx) *maxx = x2;
if(y2 > *maxy) *maxy = y2;
*x += xa * ts;
}
}
} else { // Default font
if(c == '\n') { // Newline?
*x = 0; // Reset x to zero,
*y += textsize * 8; // advance y one line
// min/max x/y unchaged -- that waits for next 'normal' character
} else if(c != '\r') { // Normal char; ignore carriage returns
if(wrap && ((*x + textsize * 6) > _width)) { // Off right?
*x = 0; // Reset x to zero,
*y += textsize * 8; // advance y one line
}
int x2 = *x + textsize * 6 - 1, // Lower-right pixel of char
y2 = *y + textsize * 8 - 1;
if(x2 > *maxx) *maxx = x2; // Track max x, y
if(y2 > *maxy) *maxy = y2;
if(*x < *minx) *minx = *x; // Track min x, y
if(*y < *miny) *miny = *y;
*x += textsize * 6; // Advance x one char
}
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The ascii string to measure
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const char *str, int16_t x, int16_t y,
int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
uint8_t c; // Current character
*x1 = x;
*y1 = y;
*w = *h = 0;
int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;
while((c = *str++))
charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);
if(maxx >= minx) {
*x1 = minx;
*w = maxx - minx + 1;
}
if(maxy >= miny) {
*y1 = miny;
*h = maxy - miny + 1;
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The ascii string to measure (as an arduino String() class)
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const String &str, int16_t x, int16_t y,
int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
if (str.length() != 0) {
getTextBounds(const_cast<char*>(str.c_str()), x, y, x1, y1, w, h);
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a PROGMEM string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The flash-memory ascii string to measure
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str,
int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
uint8_t *s = (uint8_t *)str, c;
*x1 = x;
*y1 = y;
*w = *h = 0;
int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;
while((c = pgm_read_byte(s++)))
charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);
if(maxx >= minx) {
*x1 = minx;
*w = maxx - minx + 1;
}
if(maxy >= miny) {
*y1 = miny;
*h = maxy - miny + 1;
}
}
/**************************************************************************/
/*!
@brief Invert the display (ideally using built-in hardware command)
@param i True if you want to invert, false to make 'normal'
*/
/**************************************************************************/
void Adafruit_GFX::invertDisplay(boolean i) {
// Do nothing, must be subclassed if supported by hardware
}
/***************************************************************************/
/**************************************************************************/
/*!
@brief Create a simple drawn button UI element
*/
/**************************************************************************/
Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
_gfx = 0;
}
/**************************************************************************/
/*!
@brief Initialize button with our desired color/size/settings
@param gfx Pointer to our display so we can draw to it!
@param x The X coordinate of the center of the button
@param y The Y coordinate of the center of the button
@param w Width of the buttton
@param h Height of the buttton
@param outline Color of the outline (16-bit 5-6-5 standard)
@param fill Color of the button fill (16-bit 5-6-5 standard)
@param textcolor Color of the button label (16-bit 5-6-5 standard)
@param label Ascii string of the text inside the button
@param textsize The font magnification of the label text
*/
/**************************************************************************/
// Classic initButton() function: pass center & size
void Adafruit_GFX_Button::initButton(
Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
uint16_t outline, uint16_t fill, uint16_t textcolor,
char *label, uint8_t textsize)
{
// Tweak arguments and pass to the newer initButtonUL() function...
initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill,
textcolor, label, textsize);
}
/**************************************************************************/
/*!
@brief Initialize button with our desired color/size/settings, with upper-left coordinates
@param gfx Pointer to our display so we can draw to it!
@param x1 The X coordinate of the Upper-Left corner of the button
@param y1 The Y coordinate of the Upper-Left corner of the button
@param w Width of the buttton
@param h Height of the buttton
@param outline Color of the outline (16-bit 5-6-5 standard)
@param fill Color of the button fill (16-bit 5-6-5 standard)
@param textcolor Color of the button label (16-bit 5-6-5 standard)
@param label Ascii string of the text inside the button
@param textsize The font magnification of the label text
*/
/**************************************************************************/
void Adafruit_GFX_Button::initButtonUL(
Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
uint16_t outline, uint16_t fill, uint16_t textcolor,
char *label, uint8_t textsize)
{
_x1 = x1;
_y1 = y1;
_w = w;
_h = h;
_outlinecolor = outline;
_fillcolor = fill;
_textcolor = textcolor;
_textsize = textsize;
_gfx = gfx;
strncpy(_label, label, 9);
}
/**************************************************************************/
/*!
@brief Draw the button on the screen
@param inverted Whether to draw with fill/text swapped to indicate 'pressed'
*/
/**************************************************************************/
void Adafruit_GFX_Button::drawButton(boolean inverted) {
uint16_t fill, outline, text;
if(!inverted) {
fill = _fillcolor;
outline = _outlinecolor;
text = _textcolor;
} else {
fill = _textcolor;
outline = _outlinecolor;
text = _fillcolor;
}
uint8_t r = min(_w, _h) / 4; // Corner radius
_gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill);
_gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline);
_gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize),
_y1 + (_h/2) - (4 * _textsize));
_gfx->setTextColor(text);
_gfx->setTextSize(_textsize);
_gfx->print(_label);
}
/**************************************************************************/
/*!
@brief Helper to let us know if a coordinate is within the bounds of the button
@param x The X coordinate to check
@param y The Y coordinate to check
@returns True if within button graphics outline
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
return ((x >= _x1) && (x < (int16_t) (_x1 + _w)) &&
(y >= _y1) && (y < (int16_t) (_y1 + _h)));
}
/**************************************************************************/
/*!
@brief Query whether the button was pressed since we last checked state
@returns True if was not-pressed before, now is.
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
/**************************************************************************/
/*!
@brief Query whether the button was released since we last checked state
@returns True if was pressed before, now is not.
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }
// -------------------------------------------------------------------------
// GFXcanvas1, GFXcanvas8 and GFXcanvas16 (currently a WIP, don't get too
// comfy with the implementation) provide 1-, 8- and 16-bit offscreen
// canvases, the address of which can be passed to drawBitmap() or
// pushColors() (the latter appears only in a couple of GFX-subclassed TFT
// libraries at this time). This is here mostly to help with the recently-
// added proportionally-spaced fonts; adds a way to refresh a section of the
// screen without a massive flickering clear-and-redraw...but maybe you'll
// find other uses too. VERY RAM-intensive, since the buffer is in MCU
// memory and not the display driver...GXFcanvas1 might be minimally useful
// on an Uno-class board, but this and the others are much more likely to
// require at least a Mega or various recent ARM-type boards (recommended,
// as the text+bitmap draw can be pokey). GFXcanvas1 requires 1 bit per
// pixel (rounded up to nearest byte per scanline), GFXcanvas8 is 1 byte
// per pixel (no scanline pad), and GFXcanvas16 uses 2 bytes per pixel (no
// scanline pad).
// NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.
/**************************************************************************/
/*!
@brief Instatiate a GFX 1-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint16_t bytes = ((w + 7) / 8) * h;
if((buffer = (uint8_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas1::~GFXcanvas1(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas1::drawPixel(int16_t x, int16_t y, uint16_t color) {
#ifdef __AVR__
// Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR
static const uint8_t PROGMEM
GFXsetBit[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 },
GFXclrBit[] = { 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE };
#endif
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
uint8_t *ptr = &buffer[(x / 8) + y * ((WIDTH + 7) / 8)];
#ifdef __AVR__
if(color) *ptr |= pgm_read_byte(&GFXsetBit[x & 7]);
else *ptr &= pgm_read_byte(&GFXclrBit[x & 7]);
#else
if(color) *ptr |= 0x80 >> (x & 7);
else *ptr &= ~(0x80 >> (x & 7));
#endif
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas1::fillScreen(uint16_t color) {
if(buffer) {
uint16_t bytes = ((WIDTH + 7) / 8) * HEIGHT;
memset(buffer, color ? 0xFF : 0x00, bytes);
}
}
/**************************************************************************/
/*!
@brief Instatiate a GFX 8-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas8::GFXcanvas8(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint32_t bytes = w * h;
if((buffer = (uint8_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas8::~GFXcanvas8(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas8::drawPixel(int16_t x, int16_t y, uint16_t color) {
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
buffer[x + y * WIDTH] = color;
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas8::fillScreen(uint16_t color) {
if(buffer) {
memset(buffer, color, WIDTH * HEIGHT);
}
}
void GFXcanvas8::writeFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
if((x >= _width) || (y < 0) || (y >= _height)) return;
int16_t x2 = x + w - 1;
if(x2 < 0) return;
// Clip left/right
if(x < 0) {
x = 0;
w = x2 + 1;
}
if(x2 >= _width) w = _width - x;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
memset(buffer + y * WIDTH + x, color, w);
}
/**************************************************************************/
/*!
@brief Instatiate a GFX 16-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint32_t bytes = w * h * 2;
if((buffer = (uint16_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas16::~GFXcanvas16(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas16::drawPixel(int16_t x, int16_t y, uint16_t color) {
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
buffer[x + y * WIDTH] = color;
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas16::fillScreen(uint16_t color) {
if(buffer) {
uint8_t hi = color >> 8, lo = color & 0xFF;
if(hi == lo) {
memset(buffer, lo, WIDTH * HEIGHT * 2);
} else {
uint32_t i, pixels = WIDTH * HEIGHT;
for(i=0; i<pixels; i++) buffer[i] = color;
}
}
}
/**************************************************************************/
/*!
@brief Reverses the "endian-ness" of each 16-bit pixel within the
canvas; little-endian to big-endian, or big-endian to little.
Most microcontrollers (such as SAMD) are little-endian, while
most displays tend toward big-endianness. All the drawing
functions (including RGB bitmap drawing) take care of this
automatically, but some specialized code (usually involving
DMA) can benefit from having pixel data already in the
display-native order. Note that this does NOT convert to a
SPECIFIC endian-ness, it just flips the bytes within each word.
*/
/**************************************************************************/
void GFXcanvas16::byteSwap(void) {
if(buffer) {
uint32_t i, pixels = WIDTH * HEIGHT;
for(i=0; i<pixels; i++) buffer[i] = __builtin_bswap16(buffer[i]);
}
}
там текст который всю жизнь мне выдавало с самого начала проэкта. ща скомпилил и теже заметки вот они
вот с ошибкой
In file included from C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino:3:0:
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\MCUFRIEND_kbv/UTFTGLUE.h:13:2: warning: #warning @@@@@@@@@@@@@@@@@@@@@@@@@ <UTFTGLUE.h> @@@@@@@@@@@@@@@@@@@@@@@@@@ [-Wcpp]
#warning @@@@@@@@@@@@@@@@@@@@@@@@@ <UTFTGLUE.h> @@@@@@@@@@@@@@@@@@@@@@@@@@
^
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino:9:40: warning: extra tokens at end of #include directive
#include "Fonts/Chosence_Bold16pt7b.h";
^
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino:19:38: warning: large integer implicitly truncated to unsigned type [-Woverflow]
UTFTGLUE myGLCD(0x1581,A2,A1,A3,A4,A0); //all dummy args
^
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino:91:5: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
} ;
^
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino:91:5: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino:91:5: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino:91:5: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino:91:5: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino: In function 'void setup()':
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino:168:39: warning: invalid conversion from 'const GFXfont*' to 'GFXfont*' [-fpermissive]
myGLCD.setFont(&Chosence_Bold16pt7b);
^
In file included from C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_161137\sketch_jul30a.ino:3:0:
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\MCUFRIEND_kbv/UTFTGLUE.h:111:10: note: initializing argument 1 of 'void UTFTGLUE::setFont(GFXfont*)'
void setFont(GFXfont* font) {
^
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Adafruit_GFX\Adafruit_GFX.cpp:1116:5: error: stray '\321' in program
сс = bg;
^
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Adafruit_GFX\Adafruit_GFX.cpp:1116:5: error: stray '\201' in program
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Adafruit_GFX\Adafruit_GFX.cpp:1116:5: error: stray '\321' in program
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Adafruit_GFX\Adafruit_GFX.cpp:1116:5: error: stray '\201' in program
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Adafruit_GFX\Adafruit_GFX.cpp: In member function 'void Adafruit_GFX::drawChar(int16_t, int16_t, unsigned char, uint16_t, uint16_t, uint8_t)':
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Adafruit_GFX\Adafruit_GFX.cpp:1116:10: error: expected primary-expression before '=' token
сс = bg;
^
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Adafruit_GFX\Adafruit_GFX.cpp:1117:66: error: expected ')' before '{' token
if ((yy>=(ya+yo))&&(yy<=((ya+yo+h))&&(xx>=xo)&&(xx<=(xo+w))) {
^
Несколько библиотек найдено для "Adafruit_GFX.h"
Используется: C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Adafruit_GFX
Не используется: C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Adafruit_TFTLCD
Несколько библиотек найдено для "SPI.h"
Используется: C:\Users\ViktorY\Desktop\arduino-1.8.9\hardware\arduino\avr\libraries\SPI
Не используется: C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\SPI
Несколько библиотек найдено для "EEPROM.h"
Используется: C:\Users\ViktorY\Desktop\arduino-1.8.9\hardware\arduino\avr\libraries\EEPROM
Не используется: C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\EEPROM
Несколько библиотек найдено для "Wire.h"
Используется: C:\Users\ViktorY\Desktop\arduino-1.8.9\hardware\arduino\avr\libraries\Wire
Не используется: C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Wire
Несколько библиотек найдено для "SoftwareSerial.h"
Используется: C:\Users\ViktorY\Desktop\arduino-1.8.9\hardware\arduino\avr\libraries\SoftwareSerial
Не используется: C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\SoftwareSerial
exit status 1
Ошибка компиляции для платы Arduino/Genuino Mega or Mega 2560.
вот без ошибки
In file included from C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino:3:0:
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\MCUFRIEND_kbv/UTFTGLUE.h:13:2: warning: #warning @@@@@@@@@@@@@@@@@@@@@@@@@ <UTFTGLUE.h> @@@@@@@@@@@@@@@@@@@@@@@@@@ [-Wcpp]
#warning @@@@@@@@@@@@@@@@@@@@@@@@@ <UTFTGLUE.h> @@@@@@@@@@@@@@@@@@@@@@@@@@
^
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino:9:40: warning: extra tokens at end of #include directive
#include "Fonts/Chosence_Bold16pt7b.h";
^
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino:19:38: warning: large integer implicitly truncated to unsigned type [-Woverflow]
UTFTGLUE myGLCD(0x1581,A2,A1,A3,A4,A0); //all dummy args
^
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino:91:5: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
} ;
^
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino:91:5: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino:91:5: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino:91:5: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino:91:5: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino: In function 'void setup()':
C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino:168:39: warning: invalid conversion from 'const GFXfont*' to 'GFXfont*' [-fpermissive]
myGLCD.setFont(&Chosence_Bold16pt7b);
^
In file included from C:\Users\ViktorY\AppData\Local\Temp\arduino_modified_sketch_730675\sketch_jul30a.ino:3:0:
C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\MCUFRIEND_kbv/UTFTGLUE.h:111:10: note: initializing argument 1 of 'void UTFTGLUE::setFont(GFXfont*)'
void setFont(GFXfont* font) {
^
Скетч использует 42300 байт (16%) памяти устройства. Всего доступно 253952 байт.
Глобальные переменные используют 1528 байт (18%) динамической памяти, оставляя 6664 байт для локальных переменных. Максимум: 8192 байт.
я покопаю фруктовую либу и выложу к утру - где то я с математикой накосячил.
Результат: (но это именно для Gobold_Bold14pt7b шрифта первого размера)
/*
This is the core graphics library for all our displays, providing a common
set of graphics primitives (points, lines, circles, etc.). It needs to be
paired with a hardware-specific library for each display device we carry
(to handle the lower-level functions).
Adafruit invests time and resources providing this open source code, please
support Adafruit & open-source hardware by purchasing products from Adafruit!
Copyright (c) 2013 Adafruit Industries. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "Adafruit_GFX.h"
#include "glcdfont.c"
#ifdef __AVR__
#include <avr/pgmspace.h>
#elif defined(ESP8266) || defined(ESP32)
#include <pgmspace.h>
#endif
// Many (but maybe not all) non-AVR board installs define macros
// for compatibility with existing PROGMEM-reading AVR code.
// Do our own checks and defines here for good measure...
#ifndef pgm_read_byte
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#endif
#ifndef pgm_read_word
#define pgm_read_word(addr) (*(const unsigned short *)(addr))
#endif
#ifndef pgm_read_dword
#define pgm_read_dword(addr) (*(const unsigned long *)(addr))
#endif
// Pointers are a peculiar case...typically 16-bit on AVR boards,
// 32 bits elsewhere. Try to accommodate both...
#if !defined(__INT_MAX__) || (__INT_MAX__ > 0xFFFF)
#define pgm_read_pointer(addr) ((void *)pgm_read_dword(addr))
#else
#define pgm_read_pointer(addr) ((void *)pgm_read_word(addr))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
#endif
/**************************************************************************/
/*!
@brief Instatiate a GFX context for graphics! Can only be done by a superclass
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h):
WIDTH(w), HEIGHT(h)
{
_width = WIDTH;
_height = HEIGHT;
rotation = 0;
cursor_y = cursor_x = 0;
textsize = 1;
textcolor = textbgcolor = 0xFFFF;
wrap = true;
_cp437 = false;
gfxFont = NULL;
}
/**************************************************************************/
/*!
@brief Write a line. Bresenham's algorithm - thx wikpedia
@param x0 Start point x coordinate
@param y0 Start point y coordinate
@param x1 End point x coordinate
@param y1 End point y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
uint16_t color) {
#if defined(ESP8266)
yield();
#endif
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
_swap_int16_t(x0, y0);
_swap_int16_t(x1, y1);
}
if (x0 > x1) {
_swap_int16_t(x0, x1);
_swap_int16_t(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for (; x0<=x1; x0++) {
if (steep) {
writePixel(y0, x0, color);
} else {
writePixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
/**************************************************************************/
/*!
@brief Start a display-writing routine, overwrite in subclasses.
*/
/**************************************************************************/
void Adafruit_GFX::startWrite(){
}
/**************************************************************************/
/*!
@brief Write a pixel, overwrite in subclasses if startWrite is defined!
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writePixel(int16_t x, int16_t y, uint16_t color){
drawPixel(x, y, color);
}
/**************************************************************************/
/*!
@brief Write a perfectly vertical line, overwrite in subclasses if startWrite is defined!
@param x Top-most x coordinate
@param y Top-most y coordinate
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color) {
// Overwrite in subclasses if startWrite is defined!
// Can be just writeLine(x, y, x, y+h-1, color);
// or writeFillRect(x, y, 1, h, color);
drawFastVLine(x, y, h, color);
}
/**************************************************************************/
/*!
@brief Write a perfectly horizontal line, overwrite in subclasses if startWrite is defined!
@param x Left-most x coordinate
@param y Left-most y coordinate
@param w Width in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
// Overwrite in subclasses if startWrite is defined!
// Example: writeLine(x, y, x+w-1, y, color);
// or writeFillRect(x, y, w, 1, color);
drawFastHLine(x, y, w, color);
}
/**************************************************************************/
/*!
@brief Write a rectangle completely with one color, overwrite in subclasses if startWrite is defined!
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
// Overwrite in subclasses if desired!
fillRect(x,y,w,h,color);
}
/**************************************************************************/
/*!
@brief End a display-writing routine, overwrite in subclasses if startWrite is defined!
*/
/**************************************************************************/
void Adafruit_GFX::endWrite(){
}
/**************************************************************************/
/*!
@brief Draw a perfectly vertical line (this is often optimized in a subclass!)
@param x Top-most x coordinate
@param y Top-most y coordinate
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color) {
startWrite();
writeLine(x, y, x, y+h-1, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a perfectly horizontal line (this is often optimized in a subclass!)
@param x Left-most x coordinate
@param y Left-most y coordinate
@param w Width in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
startWrite();
writeLine(x, y, x+w-1, y, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Fill a rectangle completely with one color. Update in subclasses if desired!
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
startWrite();
for (int16_t i=x; i<x+w; i++) {
writeFastVLine(i, y, h, color);
}
endWrite();
}
/**************************************************************************/
/*!
@brief Fill the screen completely with one color. Update in subclasses if desired!
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color);
}
/**************************************************************************/
/*!
@brief Draw a line
@param x0 Start point x coordinate
@param y0 Start point y coordinate
@param x1 End point x coordinate
@param y1 End point y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
uint16_t color) {
// Update in subclasses if desired!
if(x0 == x1){
if(y0 > y1) _swap_int16_t(y0, y1);
drawFastVLine(x0, y0, y1 - y0 + 1, color);
} else if(y0 == y1){
if(x0 > x1) _swap_int16_t(x0, x1);
drawFastHLine(x0, y0, x1 - x0 + 1, color);
} else {
startWrite();
writeLine(x0, y0, x1, y1, color);
endWrite();
}
}
/**************************************************************************/
/*!
@brief Draw a circle outline
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
#if defined(ESP8266)
yield();
#endif
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
startWrite();
writePixel(x0 , y0+r, color);
writePixel(x0 , y0-r, color);
writePixel(x0+r, y0 , color);
writePixel(x0-r, y0 , color);
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
writePixel(x0 + x, y0 + y, color);
writePixel(x0 - x, y0 + y, color);
writePixel(x0 + x, y0 - y, color);
writePixel(x0 - x, y0 - y, color);
writePixel(x0 + y, y0 + x, color);
writePixel(x0 - y, y0 + x, color);
writePixel(x0 + y, y0 - x, color);
writePixel(x0 - y, y0 - x, color);
}
endWrite();
}
/**************************************************************************/
/*!
@brief Quarter-circle drawer, used to do circles and roundrects
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param cornername Mask bit #1 or bit #2 to indicate which quarters of the circle we're doing
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
int16_t r, uint8_t cornername, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x4) {
writePixel(x0 + x, y0 + y, color);
writePixel(x0 + y, y0 + x, color);
}
if (cornername & 0x2) {
writePixel(x0 + x, y0 - y, color);
writePixel(x0 + y, y0 - x, color);
}
if (cornername & 0x8) {
writePixel(x0 - y, y0 + x, color);
writePixel(x0 - x, y0 + y, color);
}
if (cornername & 0x1) {
writePixel(x0 - y, y0 - x, color);
writePixel(x0 - x, y0 - y, color);
}
}
}
/**************************************************************************/
/*!
@brief Draw a circle with filled color
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
startWrite();
writeFastVLine(x0, y0-r, 2*r+1, color);
fillCircleHelper(x0, y0, r, 3, 0, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Quarter-circle drawer with fill, used for circles and roundrects
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param r Radius of circle
@param corners Mask bits indicating which quarters we're doing
@param delta Offset from center-point, used for round-rects
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
uint8_t corners, int16_t delta, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
int16_t px = x;
int16_t py = y;
delta++; // Avoid some +1's in the loop
while(x < y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
// These checks avoid double-drawing certain lines, important
// for the SSD1306 library which has an INVERT drawing mode.
if(x < (y + 1)) {
if(corners & 1) writeFastVLine(x0+x, y0-y, 2*y+delta, color);
if(corners & 2) writeFastVLine(x0-x, y0-y, 2*y+delta, color);
}
if(y != py) {
if(corners & 1) writeFastVLine(x0+py, y0-px, 2*px+delta, color);
if(corners & 2) writeFastVLine(x0-py, y0-px, 2*px+delta, color);
py = y;
}
px = x;
}
}
/**************************************************************************/
/*!
@brief Draw a rectangle with no fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
startWrite();
writeFastHLine(x, y, w, color);
writeFastHLine(x, y+h-1, w, color);
writeFastVLine(x, y, h, color);
writeFastVLine(x+w-1, y, h, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a rounded rectangle with no fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param r Radius of corner rounding
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r, uint16_t color) {
int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis
if(r > max_radius) r = max_radius;
// smarter version
startWrite();
writeFastHLine(x+r , y , w-2*r, color); // Top
writeFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
writeFastVLine(x , y+r , h-2*r, color); // Left
writeFastVLine(x+w-1, y+r , h-2*r, color); // Right
// draw four corners
drawCircleHelper(x+r , y+r , r, 1, color);
drawCircleHelper(x+w-r-1, y+r , r, 2, color);
drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
drawCircleHelper(x+r , y+h-r-1, r, 8, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a rounded rectangle with fill color
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param w Width in pixels
@param h Height in pixels
@param r Radius of corner rounding
@param color 16-bit 5-6-5 Color to draw/fill with
*/
/**************************************************************************/
void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r, uint16_t color) {
int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis
if(r > max_radius) r = max_radius;
// smarter version
startWrite();
writeFillRect(x+r, y, w-2*r, h, color);
// draw four corners
fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a triangle with no fill color
@param x0 Vertex #0 x coordinate
@param y0 Vertex #0 y coordinate
@param x1 Vertex #1 x coordinate
@param y1 Vertex #1 y coordinate
@param x2 Vertex #2 x coordinate
@param y2 Vertex #2 y coordinate
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0,
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
drawLine(x0, y0, x1, y1, color);
drawLine(x1, y1, x2, y2, color);
drawLine(x2, y2, x0, y0, color);
}
/**************************************************************************/
/*!
@brief Draw a triangle with color-fill
@param x0 Vertex #0 x coordinate
@param y0 Vertex #0 y coordinate
@param x1 Vertex #1 x coordinate
@param y1 Vertex #1 y coordinate
@param x2 Vertex #2 x coordinate
@param y2 Vertex #2 y coordinate
@param color 16-bit 5-6-5 Color to fill/draw with
*/
/**************************************************************************/
void Adafruit_GFX::fillTriangle(int16_t x0, int16_t y0,
int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
int16_t a, b, y, last;
// Sort coordinates by Y order (y2 >= y1 >= y0)
if (y0 > y1) {
_swap_int16_t(y0, y1); _swap_int16_t(x0, x1);
}
if (y1 > y2) {
_swap_int16_t(y2, y1); _swap_int16_t(x2, x1);
}
if (y0 > y1) {
_swap_int16_t(y0, y1); _swap_int16_t(x0, x1);
}
startWrite();
if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
a = b = x0;
if(x1 < a) a = x1;
else if(x1 > b) b = x1;
if(x2 < a) a = x2;
else if(x2 > b) b = x2;
writeFastHLine(a, y0, b-a+1, color);
endWrite();
return;
}
int16_t
dx01 = x1 - x0,
dy01 = y1 - y0,
dx02 = x2 - x0,
dy02 = y2 - y0,
dx12 = x2 - x1,
dy12 = y2 - y1;
int32_t
sa = 0,
sb = 0;
// For upper part of triangle, find scanline crossings for segments
// 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
// is included here (and second loop will be skipped, avoiding a /0
// error there), otherwise scanline y1 is skipped here and handled
// in the second loop...which also avoids a /0 error here if y0=y1
// (flat-topped triangle).
if(y1 == y2) last = y1; // Include y1 scanline
else last = y1-1; // Skip it
for(y=y0; y<=last; y++) {
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
/* longhand:
a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if(a > b) _swap_int16_t(a,b);
writeFastHLine(a, y, b-a+1, color);
}
// For lower part of triangle, find scanline crossings for segments
// 0-2 and 1-2. This loop is skipped if y1=y2.
sa = (int32_t)dx12 * (y - y1);
sb = (int32_t)dx02 * (y - y0);
for(; y<=y2; y++) {
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
/* longhand:
a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if(a > b) _swap_int16_t(a,b);
writeFastHLine(a, y, b-a+1, color);
}
endWrite();
}
// BITMAP / XBITMAP / GRAYSCALE / RGB BITMAP FUNCTIONS ---------------------
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent).
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
if(byte & 0x80) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h,
uint16_t color, uint16_t bg) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
writePixel(x+i, y, (byte & 0x80) ? color : bg);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent).
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
if(byte & 0x80) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
writePixel(x+i, y, (byte & 0x80) ? color : bg);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw PROGMEM-resident XBitMap Files (*.xbm), exported from GIMP.
Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
C Array can be directly used with this function.
There is no RAM-resident version of this function; if generating bitmaps
in RAM, use the format defined by drawBitmap() and call that instead.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
*/
/**************************************************************************/
void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte >>= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
// Nearly identical to drawBitmap(), only the bit order
// is reversed here (left-to-right = LSB to MSB):
if(byte & 0x01) writePixel(x+i, y, color);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y) pos.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, (uint8_t)pgm_read_byte(&bitmap[j * w + i]));
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 8-bit image (grayscale) at the specified (x,y) pos.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 8-bit image (grayscale) with a 1-bit mask
(set bits = opaque, unset bits = clear) at the specified (x,y) position.
BOTH buffers (grayscale and mask) must be PROGMEM-resident.
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param mask byte array with mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], const uint8_t mask[],
int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&mask[j * bw + i / 8]);
if(byte & 0x80) {
writePixel(x+i, y, (uint8_t)pgm_read_byte(&bitmap[j * w + i]));
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 8-bit image (grayscale) with a 1-bit mask
(set bits = opaque, unset bits = clear) at the specified (x,y) position.
BOTH buffers (grayscale and mask) must be RAM-residentt, no mix-and-match
Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param mask byte array with mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
uint8_t *bitmap, uint8_t *mask, int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = mask[j * bw + i / 8];
if(byte & 0x80) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
const uint16_t bitmap[], int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, pgm_read_word(&bitmap[j * w + i]));
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
uint16_t *bitmap, int16_t w, int16_t h) {
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (color and mask) must be PROGMEM-resident. For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param mask byte array with monochrome mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
const uint16_t bitmap[], const uint8_t mask[],
int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(&mask[j * bw + i / 8]);
if(byte & 0x80) {
writePixel(x+i, y, pgm_read_word(&bitmap[j * w + i]));
}
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (color and mask) must be RAM-resident. For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param mask byte array with monochrome mask bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
uint16_t *bitmap, uint8_t *mask, int16_t w, int16_t h) {
int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
uint8_t byte = 0;
startWrite();
for(int16_t j=0; j<h; j++, y++) {
for(int16_t i=0; i<w; i++ ) {
if(i & 7) byte <<= 1;
else byte = mask[j * bw + i / 8];
if(byte & 0x80) {
writePixel(x+i, y, bitmap[j * w + i]);
}
}
}
endWrite();
}
// TEXT- AND CHARACTER-HANDLING FUNCTIONS ----------------------------------
// Draw a character
/**************************************************************************/
/*!
@brief Draw a single character
@param x Bottom left corner x coordinate
@param y Bottom left corner y coordinate
@param c The 8-bit font-indexed character (likely ascii)
@param color 16-bit 5-6-5 Color to draw chraracter with
@param bg 16-bit 5-6-5 Color to fill background with (if same as color, no background)
@param size Font magnification level, 1 is 'original' size
*/
/**************************************************************************/
void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
uint16_t color, uint16_t bg, uint8_t size) {
if(!gfxFont) { // 'Classic' built-in font
if((x >= _width) || // Clip right
(y >= _height) || // Clip bottom
((x + 6 * size - 1) < 0) || // Clip left
((y + 8 * size - 1) < 0)) // Clip top
return;
if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior
startWrite();
for(int8_t i=0; i<5; i++ ) { // Char bitmap = 5 columns
uint8_t line = pgm_read_byte(&font[c * 5 + i]);
for(int8_t j=0; j<8; j++, line >>= 1) {
if(line & 1) {
if(size == 1)
writePixel(x+i, y+j, color);
else
writeFillRect(x+i*size, y+j*size, size, size, color);
} else if(bg != color) {
if(size == 1)
writePixel(x+i, y+j, bg);
else
writeFillRect(x+i*size, y+j*size, size, size, bg);
}
}
}
if(bg != color) { // If opaque, draw vertical line for last column
if(size == 1) writeFastVLine(x+5, y, 8, bg);
else writeFillRect(x+5*size, y, size, 8*size, bg);
}
endWrite();
} else { // Custom font
// Character is assumed previously filtered by write() to eliminate
// newlines, returns, non-printable characters, etc. Calling
// drawChar() directly with 'bad' characters of font may cause mayhem!
c -= (uint8_t)pgm_read_byte(&gfxFont->first);
uint8_t ya = pgm_read_byte(&gfxFont->yAdvance);
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
uint8_t *bitmap = (uint8_t *)pgm_read_pointer(&gfxFont->bitmap);
uint16_t zz,bo = pgm_read_word(&glyph->bitmapOffset);
uint8_t w = pgm_read_byte(&glyph->width),
h = pgm_read_byte(&glyph->height),
xa = pgm_read_byte(&glyph->xAdvance);
int8_t xo = pgm_read_byte(&glyph->xOffset),
yo = pgm_read_byte(&glyph->yOffset);
uint8_t xx, yy, bits = 0, bit = 0;
int16_t xo16 = 0, yo16 = 0;
if(size > 1) {
xo16 = xo;
yo16 = yo;
}
// Todo: Add character clipping here
// NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS.
// THIS IS ON PURPOSE AND BY DESIGN. The background color feature
// has typically been used with the 'classic' font to overwrite old
// screen contents with new data. This ONLY works because the
// characters are a uniform size; it's not a sensible thing to do with
// proportionally-spaced fonts with glyphs of varying sizes (and that
// may overlap). To replace previously-drawn text when using a custom
// font, use the getTextBounds() function to determine the smallest
// rectangle encompassing a string, erase the area with fillRect(),
// then draw new text. This WILL infortunately 'blink' the text, but
// is unavoidable. Drawing 'background' pixels will NOT fix this,
// only creates a new set of problems. Have an idea to work around
// this (a canvas object type for MCUs that can afford the RAM and
// displays supporting setAddrWindow() and pushColors()), but haven't
// implemented this yet.
startWrite();
for(yy=0; yy<ya; yy++) {
for(xx=0; xx<xa; xx++) {
zz = bg;
if ((yy>=(30+yo))&&(yy<(30+yo+h))&&(xx>=xo)&&(xx<(xo+w))) {
if(!(bit++ & 7)) {
bits = pgm_read_byte(&bitmap[bo++]);
}
if(bits & 0x80) {
zz = color;
}
bits <<= 1;
}
writePixel(x+xx, y-30+yy, zz);
}
}
endWrite();
} // End classic vs custom font
}
/**************************************************************************/
/*!
@brief Print one byte/character of data, used to support print()
@param c The 8-bit ascii character to write
*/
/**************************************************************************/
size_t Adafruit_GFX::write(uint8_t c) {
if(!gfxFont) { // 'Classic' built-in font
if(c == '\n') { // Newline?
cursor_x = 0; // Reset x to zero,
cursor_y += textsize * 8; // advance y one line
} else if(c != '\r') { // Ignore carriage returns
if(wrap && ((cursor_x + textsize * 6) > _width)) { // Off right?
cursor_x = 0; // Reset x to zero,
cursor_y += textsize * 8; // advance y one line
}
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
cursor_x += textsize * 6; // Advance x one char
}
} else { // Custom font
if(c == '\n') {
cursor_x = 0;
cursor_y += (int16_t)textsize *
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
} else if(c != '\r') {
uint8_t first = pgm_read_byte(&gfxFont->first);
if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) {
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(
&gfxFont->glyph))[c - first]);
uint8_t w = pgm_read_byte(&glyph->width),
h = pgm_read_byte(&glyph->height);
if((w > 0) && (h > 0)) { // Is there an associated bitmap?
int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
if(wrap && ((cursor_x + textsize * (xo + w)) > _width)) {
cursor_x = 0;
cursor_y += (int16_t)textsize *
(uint8_t)pgm_read_byte(&gfxFont->yAdvance);
}
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
}
cursor_x += (uint8_t)pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize;
}
}
}
return 1;
}
/**************************************************************************/
/*!
@brief Set text 'magnification' size. Each increase in s makes 1 pixel that much bigger.
@param s Desired text size. 1 is default 6x8, 2 is 12x16, 3 is 18x24, etc
*/
/**************************************************************************/
void Adafruit_GFX::setTextSize(uint8_t s) {
textsize = (s > 0) ? s : 1;
}
/**************************************************************************/
/*!
@brief Set rotation setting for display
@param x 0 thru 3 corresponding to 4 cardinal rotations
*/
/**************************************************************************/
void Adafruit_GFX::setRotation(uint8_t x) {
rotation = (x & 3);
switch(rotation) {
case 0:
case 2:
_width = WIDTH;
_height = HEIGHT;
break;
case 1:
case 3:
_width = HEIGHT;
_height = WIDTH;
break;
}
}
/**************************************************************************/
/*!
@brief Set the font to display when print()ing, either custom or default
@param f The GFXfont object, if NULL use built in 6x8 font
*/
/**************************************************************************/
void Adafruit_GFX::setFont(const GFXfont *f) {
if(f) { // Font struct pointer passed in?
if(!gfxFont) { // And no current font struct?
// Switching from classic to new font behavior.
// Move cursor pos down 6 pixels so it's on baseline.
cursor_y += 6;
}
} else if(gfxFont) { // NULL passed. Current font struct defined?
// Switching from new to classic font behavior.
// Move cursor pos up 6 pixels so it's at top-left of char.
cursor_y -= 6;
}
gfxFont = (GFXfont *)f;
}
/**************************************************************************/
/*!
@brief Helper to determine size of a character with current font/size.
Broke this out as it's used by both the PROGMEM- and RAM-resident getTextBounds() functions.
@param c The ascii character in question
@param x Pointer to x location of character
@param y Pointer to y location of character
@param minx Minimum clipping value for X
@param miny Minimum clipping value for Y
@param maxx Maximum clipping value for X
@param maxy Maximum clipping value for Y
*/
/**************************************************************************/
void Adafruit_GFX::charBounds(char c, int16_t *x, int16_t *y,
int16_t *minx, int16_t *miny, int16_t *maxx, int16_t *maxy) {
if(gfxFont) {
if(c == '\n') { // Newline?
*x = 0; // Reset x to zero, advance y by one line
*y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
} else if(c != '\r') { // Not a carriage return; is normal char
uint8_t first = pgm_read_byte(&gfxFont->first),
last = pgm_read_byte(&gfxFont->last);
if((c >= first) && (c <= last)) { // Char present in this font?
GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(
&gfxFont->glyph))[c - first]);
uint8_t gw = pgm_read_byte(&glyph->width),
gh = pgm_read_byte(&glyph->height),
xa = pgm_read_byte(&glyph->xAdvance);
int8_t xo = pgm_read_byte(&glyph->xOffset),
yo = pgm_read_byte(&glyph->yOffset);
if(wrap && ((*x+(((int16_t)xo+gw)*textsize)) > _width)) {
*x = 0; // Reset x to zero, advance y by one line
*y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
}
int16_t ts = (int16_t)textsize,
x1 = *x + xo * ts,
y1 = *y + yo * ts,
x2 = x1 + gw * ts - 1,
y2 = y1 + gh * ts - 1;
if(x1 < *minx) *minx = x1;
if(y1 < *miny) *miny = y1;
if(x2 > *maxx) *maxx = x2;
if(y2 > *maxy) *maxy = y2;
*x += xa * ts;
}
}
} else { // Default font
if(c == '\n') { // Newline?
*x = 0; // Reset x to zero,
*y += textsize * 8; // advance y one line
// min/max x/y unchaged -- that waits for next 'normal' character
} else if(c != '\r') { // Normal char; ignore carriage returns
if(wrap && ((*x + textsize * 6) > _width)) { // Off right?
*x = 0; // Reset x to zero,
*y += textsize * 8; // advance y one line
}
int x2 = *x + textsize * 6 - 1, // Lower-right pixel of char
y2 = *y + textsize * 8 - 1;
if(x2 > *maxx) *maxx = x2; // Track max x, y
if(y2 > *maxy) *maxy = y2;
if(*x < *minx) *minx = *x; // Track min x, y
if(*y < *miny) *miny = *y;
*x += textsize * 6; // Advance x one char
}
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The ascii string to measure
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const char *str, int16_t x, int16_t y,
int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
uint8_t c; // Current character
*x1 = x;
*y1 = y;
*w = *h = 0;
int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;
while((c = *str++))
charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);
if(maxx >= minx) {
*x1 = minx;
*w = maxx - minx + 1;
}
if(maxy >= miny) {
*y1 = miny;
*h = maxy - miny + 1;
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The ascii string to measure (as an arduino String() class)
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const String &str, int16_t x, int16_t y,
int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
if (str.length() != 0) {
getTextBounds(const_cast<char*>(str.c_str()), x, y, x1, y1, w, h);
}
}
/**************************************************************************/
/*!
@brief Helper to determine size of a PROGMEM string with current font/size. Pass string and a cursor position, returns UL corner and W,H.
@param str The flash-memory ascii string to measure
@param x The current cursor X
@param y The current cursor Y
@param x1 The boundary X coordinate, set by function
@param y1 The boundary Y coordinate, set by function
@param w The boundary width, set by function
@param h The boundary height, set by function
*/
/**************************************************************************/
void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str,
int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
uint8_t *s = (uint8_t *)str, c;
*x1 = x;
*y1 = y;
*w = *h = 0;
int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;
while((c = pgm_read_byte(s++)))
charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);
if(maxx >= minx) {
*x1 = minx;
*w = maxx - minx + 1;
}
if(maxy >= miny) {
*y1 = miny;
*h = maxy - miny + 1;
}
}
/**************************************************************************/
/*!
@brief Invert the display (ideally using built-in hardware command)
@param i True if you want to invert, false to make 'normal'
*/
/**************************************************************************/
void Adafruit_GFX::invertDisplay(boolean i) {
// Do nothing, must be subclassed if supported by hardware
}
/***************************************************************************/
/**************************************************************************/
/*!
@brief Create a simple drawn button UI element
*/
/**************************************************************************/
Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
_gfx = 0;
}
/**************************************************************************/
/*!
@brief Initialize button with our desired color/size/settings
@param gfx Pointer to our display so we can draw to it!
@param x The X coordinate of the center of the button
@param y The Y coordinate of the center of the button
@param w Width of the buttton
@param h Height of the buttton
@param outline Color of the outline (16-bit 5-6-5 standard)
@param fill Color of the button fill (16-bit 5-6-5 standard)
@param textcolor Color of the button label (16-bit 5-6-5 standard)
@param label Ascii string of the text inside the button
@param textsize The font magnification of the label text
*/
/**************************************************************************/
// Classic initButton() function: pass center & size
void Adafruit_GFX_Button::initButton(
Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
uint16_t outline, uint16_t fill, uint16_t textcolor,
char *label, uint8_t textsize)
{
// Tweak arguments and pass to the newer initButtonUL() function...
initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill,
textcolor, label, textsize);
}
/**************************************************************************/
/*!
@brief Initialize button with our desired color/size/settings, with upper-left coordinates
@param gfx Pointer to our display so we can draw to it!
@param x1 The X coordinate of the Upper-Left corner of the button
@param y1 The Y coordinate of the Upper-Left corner of the button
@param w Width of the buttton
@param h Height of the buttton
@param outline Color of the outline (16-bit 5-6-5 standard)
@param fill Color of the button fill (16-bit 5-6-5 standard)
@param textcolor Color of the button label (16-bit 5-6-5 standard)
@param label Ascii string of the text inside the button
@param textsize The font magnification of the label text
*/
/**************************************************************************/
void Adafruit_GFX_Button::initButtonUL(
Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
uint16_t outline, uint16_t fill, uint16_t textcolor,
char *label, uint8_t textsize)
{
_x1 = x1;
_y1 = y1;
_w = w;
_h = h;
_outlinecolor = outline;
_fillcolor = fill;
_textcolor = textcolor;
_textsize = textsize;
_gfx = gfx;
strncpy(_label, label, 9);
}
/**************************************************************************/
/*!
@brief Draw the button on the screen
@param inverted Whether to draw with fill/text swapped to indicate 'pressed'
*/
/**************************************************************************/
void Adafruit_GFX_Button::drawButton(boolean inverted) {
uint16_t fill, outline, text;
if(!inverted) {
fill = _fillcolor;
outline = _outlinecolor;
text = _textcolor;
} else {
fill = _textcolor;
outline = _outlinecolor;
text = _fillcolor;
}
uint8_t r = min(_w, _h) / 4; // Corner radius
_gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill);
_gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline);
_gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize),
_y1 + (_h/2) - (4 * _textsize));
_gfx->setTextColor(text);
_gfx->setTextSize(_textsize);
_gfx->print(_label);
}
/**************************************************************************/
/*!
@brief Helper to let us know if a coordinate is within the bounds of the button
@param x The X coordinate to check
@param y The Y coordinate to check
@returns True if within button graphics outline
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
return ((x >= _x1) && (x < (int16_t) (_x1 + _w)) &&
(y >= _y1) && (y < (int16_t) (_y1 + _h)));
}
/**************************************************************************/
/*!
@brief Query whether the button was pressed since we last checked state
@returns True if was not-pressed before, now is.
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
/**************************************************************************/
/*!
@brief Query whether the button was released since we last checked state
@returns True if was pressed before, now is not.
*/
/**************************************************************************/
boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }
// -------------------------------------------------------------------------
// GFXcanvas1, GFXcanvas8 and GFXcanvas16 (currently a WIP, don't get too
// comfy with the implementation) provide 1-, 8- and 16-bit offscreen
// canvases, the address of which can be passed to drawBitmap() or
// pushColors() (the latter appears only in a couple of GFX-subclassed TFT
// libraries at this time). This is here mostly to help with the recently-
// added proportionally-spaced fonts; adds a way to refresh a section of the
// screen without a massive flickering clear-and-redraw...but maybe you'll
// find other uses too. VERY RAM-intensive, since the buffer is in MCU
// memory and not the display driver...GXFcanvas1 might be minimally useful
// on an Uno-class board, but this and the others are much more likely to
// require at least a Mega or various recent ARM-type boards (recommended,
// as the text+bitmap draw can be pokey). GFXcanvas1 requires 1 bit per
// pixel (rounded up to nearest byte per scanline), GFXcanvas8 is 1 byte
// per pixel (no scanline pad), and GFXcanvas16 uses 2 bytes per pixel (no
// scanline pad).
// NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.
/**************************************************************************/
/*!
@brief Instatiate a GFX 1-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint16_t bytes = ((w + 7) / 8) * h;
if((buffer = (uint8_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas1::~GFXcanvas1(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas1::drawPixel(int16_t x, int16_t y, uint16_t color) {
#ifdef __AVR__
// Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR
static const uint8_t PROGMEM
GFXsetBit[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 },
GFXclrBit[] = { 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE };
#endif
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
uint8_t *ptr = &buffer[(x / 8) + y * ((WIDTH + 7) / 8)];
#ifdef __AVR__
if(color) *ptr |= pgm_read_byte(&GFXsetBit[x & 7]);
else *ptr &= pgm_read_byte(&GFXclrBit[x & 7]);
#else
if(color) *ptr |= 0x80 >> (x & 7);
else *ptr &= ~(0x80 >> (x & 7));
#endif
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas1::fillScreen(uint16_t color) {
if(buffer) {
uint16_t bytes = ((WIDTH + 7) / 8) * HEIGHT;
memset(buffer, color ? 0xFF : 0x00, bytes);
}
}
/**************************************************************************/
/*!
@brief Instatiate a GFX 8-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas8::GFXcanvas8(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint32_t bytes = w * h;
if((buffer = (uint8_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas8::~GFXcanvas8(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas8::drawPixel(int16_t x, int16_t y, uint16_t color) {
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
buffer[x + y * WIDTH] = color;
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas8::fillScreen(uint16_t color) {
if(buffer) {
memset(buffer, color, WIDTH * HEIGHT);
}
}
void GFXcanvas8::writeFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color) {
if((x >= _width) || (y < 0) || (y >= _height)) return;
int16_t x2 = x + w - 1;
if(x2 < 0) return;
// Clip left/right
if(x < 0) {
x = 0;
w = x2 + 1;
}
if(x2 >= _width) w = _width - x;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
memset(buffer + y * WIDTH + x, color, w);
}
/**************************************************************************/
/*!
@brief Instatiate a GFX 16-bit canvas context for graphics
@param w Display width, in pixels
@param h Display height, in pixels
*/
/**************************************************************************/
GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
uint32_t bytes = w * h * 2;
if((buffer = (uint16_t *)malloc(bytes))) {
memset(buffer, 0, bytes);
}
}
/**************************************************************************/
/*!
@brief Delete the canvas, free memory
*/
/**************************************************************************/
GFXcanvas16::~GFXcanvas16(void) {
if(buffer) free(buffer);
}
/**************************************************************************/
/*!
@brief Draw a pixel to the canvas framebuffer
@param x x coordinate
@param y y coordinate
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas16::drawPixel(int16_t x, int16_t y, uint16_t color) {
if(buffer) {
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
int16_t t;
switch(rotation) {
case 1:
t = x;
x = WIDTH - 1 - y;
y = t;
break;
case 2:
x = WIDTH - 1 - x;
y = HEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = HEIGHT - 1 - t;
break;
}
buffer[x + y * WIDTH] = color;
}
}
/**************************************************************************/
/*!
@brief Fill the framebuffer completely with one color
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void GFXcanvas16::fillScreen(uint16_t color) {
if(buffer) {
uint8_t hi = color >> 8, lo = color & 0xFF;
if(hi == lo) {
memset(buffer, lo, WIDTH * HEIGHT * 2);
} else {
uint32_t i, pixels = WIDTH * HEIGHT;
for(i=0; i<pixels; i++) buffer[i] = color;
}
}
}
/**************************************************************************/
/*!
@brief Reverses the "endian-ness" of each 16-bit pixel within the
canvas; little-endian to big-endian, or big-endian to little.
Most microcontrollers (such as SAMD) are little-endian, while
most displays tend toward big-endianness. All the drawing
functions (including RGB bitmap drawing) take care of this
automatically, but some specialized code (usually involving
DMA) can benefit from having pixel data already in the
display-native order. Note that this does NOT convert to a
SPECIFIC endian-ness, it just flips the bytes within each word.
*/
/**************************************************************************/
void GFXcanvas16::byteSwap(void) {
if(buffer) {
uint32_t i, pixels = WIDTH * HEIGHT;
for(i=0; i<pixels; i++) buffer[i] = __builtin_bswap16(buffer[i]);
}
}
для оладки шрифта? наверное трудоемко, рисовать в ней весь шрифт. а почему именно для Gobold_Bold14pt7b, я вчера подобрал более подходящий шрифт, он и прорисовывается быстрее и цифры не вооружонным глазо раборчиво видно, а то гоболде 0869 не разборчивы, я вот этот подобрал Chosence_Bold16pt7b.
красавчик, все красиво затирает :). я и новый свой шрифт подложил, он чутка меньше, идеально работает. так а тепрь вот..... двоеточие почемуто мигает палочкой, при увеличении цифр до 4ех значно первая цифра не исчезает при падении до 3ох значной, ну и на последок данные обновляются каждые 2 секунды, в мониторе запросы так же затормозились до 1 запрос в 2 секунды (но это уже наврное по Макса чатси, тут уже нужно смотреть каким боком вывод данных и запросы связаны, почему вывод данных затормаживает запросы....) . Ты гений... за фишку с 1116 строкой спасибо, нужно запомнить..
Для ускорения надо сделать всем переменным копию с припиской last и обновлять только то что изменилось. С двоеточием - может оно так нарисовано в шрифте (потом посмотрю). С остающимся символом - надо использовать дополнительные возможности printNUM - с сделать что бы выводились нули, вместо первых цифр.
на страницах где меньше в половину инфы выводится достаточно быстро,первое, двоеточие вчера на этом шрифте были, второе, с ласт Маскс пробовал, говорит заметно мерцание, третье, printNUM уже испльзуется, чет перестала работать кнопка TouchCHECK, ну и не нашел пока что вариант дать линиям цвет и рамкам кнопок. вот сетч, пересмотри, может че увидешь
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <UTFTGLUE.h>//use GLUE class and constructor
#include "TouchScreen.h"
#include <stdint.h>
#include <SPI.h>
#include <EEPROM.h>
//MCUFRIEND_kbv tft;
#include "Fonts/Chosence_Bold16pt7b.h";
#define MINPRESSURE 200
#define MAXPRESSURE 1000
//pin 20 SCL , 21 SDA датчик реального времени
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
UTFTGLUE myGLCD(0x1581,A2,A1,A3,A4,A0); //all dummy args
const int XP = 6, XM = A2, YP = A1, YM = 7;
const int TS_LEFT = 136, TS_RT = 907, TS_TOP = 139, TS_BOT = 942;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
uint16_t ID;
int x, y;
char currentPage;
float h;
float t;
bool Dvoet = 0;
bool reNew = 0;
#define BLACK 0x0000
#define WHITE 0xFFFF
#define RED 0xF800
#define GREEN 0x07E0
#include <SoftwareSerial.h>
#define TX_PCM 13
SoftwareSerial K_LINE_PCM (12, TX_PCM); //RХ,TХ
#define PCM_address 0x11 // адрес PCM
#define DIAG_address 0xF1 // адрес диагностики
#define debugPCM // раскоментировать эту строку для отладки в Serial порту обмена с PCM
//#define debugGAUGE // раскоментировать эту строку для отладки в Serial порту обмена co щитком приборов
uint32_t curmillis = 0; // снимок текущего времени
uint32_t prevRequest = 0; // таймер периодических запосов на PCM
uint16_t RequestPeriod = 3500; // периодичность запросов на PCM
uint32_t prevRESETheader=0; // таймер сброса сообщения, если данные оборвались посередине сообщения
bool RESETheader_timer; // таймер сброса сообщения, если данные оборвались посередине сообщения
uint32_t prev_NOanswer=0; // таймер контроля неответов от ЭБУ после подачи запросов
bool NOanswer_timer = 0; // таймер контроля неответов от ЭБУ после подачи запросов
byte noanswers = 0; // количество подряд неответов от ЭБУ
uint32_t timerdelay = 0; // таймер ожидания байт (для успевания появления данных в буфере UART)
bool Delay = 0; // таймер ожидания байт (для успевания появления данных в буфере UART)
byte waitbyte_RX = 1; // задержка, мс для успевания появления данных в буфере RX
// (подрегулировать в зависимости от уровня жизнидеятельности на Марсе)
#define TIMER_DELAY Delay = 0; timerdelay = curmillis // включение этого таймера
byte delaybyte_TX = 1; // задержка между отправкой байт в запросах, мс
byte header = 0; // состояние заголовка
byte message_size = 0; // размер тела сообщения
byte j = 3; // инкремент
byte n = 3; // количество старт байт
const byte bufsize = 150; // размер буфера принятого сообщения
byte MessageRx_PCM [bufsize] = {0}; // буфер принятого сообщения
byte crc =0; // байт контрольной суммы
// возможные варианты запросов на ЭБУ:
enum REQUEST {
INIT,
PID,
DTCERASE,
DTCREAD,
PRESENT,
};
// текстовки запросов для отладки
char* textRequest[] = {
"INIT",
"PID_2101",
"DTC_ERASE",
"DTC_READ",
"PRESENT",
} ;
// сами запросы
byte PCM_Message_TX[][5] = {
{1, 0x81, 0,0,0}, // запрос инициализации
{2, 0x21,0x01, 0,0}, // запрос пид 2101
{3, 0x14,0xFF,0x00, 0}, // запрос на стирание ошибок
{4, 0x18,0x00,0xFF,0x00 }, // запрос на чтение ошибок
{1, 0x3E, 0,0,0}, // запрос присутствия
};
byte request = INIT; // переменная, показывающая какой запрос будем делать
//-------------------------------------------переменные бортовика
float L100M = 0; //расход на 100 км измеренный за поездку
float L100 = 0; //мгновенный расход литров на 100км
float LHor = 0; //мгновенный расход топлива литров в час
float L100SR = 0; //расход литров на 100км измеренный раз в интервал kmL
float L100SR_TFT = 0; // самый средний из расходов на 100км, он выводится на экран
int L100_Eeprom[11]= {10,10,10,10,10,10,10,10,10,10,10};
int FuelZamer[10]= {0}; // массив для измерения уровня (количества) топлива
int ZamerNumber = 0; // номер замера уровня (количества) топлива
int n_eeprom = 0; // текущий адрес ячейки еепром для записи расхода
int MAF = 0; //26,27 байты Sensor de flujo de aire en masa
float BoostPres = 0; //28,29 байты Presión de refuerzo
int RPM = 0; //32,33 байты Velocidad del motor
int EGRmg = 0; //34,35 байты Comando EGR (Comando de recirculación de gases de escape)
float BoostPresCom = 0; //38,39 байты Comando de presión de refuerzo
int Speed = 0; //44,45 байты Velocidad del vehículo
float DesaInjQua = 0; //50,51 байты Cantidad de inyección deseada
float InjQua = 0; //52,53 байты Cantidad de la inyección
float StaDaliv = 0; //54,55 байты Inicio de la entrega
int PumpRPM = 0; //56,57 байты Velocidad de la bomba
float EGRPul = 0; //62,63 байты Relación de impulsos EGR (recirculación de gases de escape
float SolenPul = 0; //64,65 байты Velocidad de solenoide de control de nivel de remolino Relación de impulsos
float SolenPre = 0; //70,71 байты Relación de impulsos Presión Electroválvula de presión
float DesInj = 0; //72,73 байты Inyección deseada Inicio
float ActInj = 0; //16,17 байты Inicio de la inyección real
int TempAir = 0; //74,75 байты Temperatura del aire de admisión
int Temp = 0; //14,15 байты Temperatura del refrigerante
int TempOil = 0; //18,19 байты Temperatura del aceite del motor
int TempFuel = 0; //58,59 байты Temperatura del combustible
//все что касается топлива
float Fuel = 0; //остаток топлива
float Fuel2 = 0; //остаток мгновенного топлива байт 16 , датчика в баке
int FuelIGN = 0; // количество топлвива в баке на момент включения зажигания
int Fuel_last = 0; // для формул
bool flagFuelIGN = 0; // флаг записан ли остаток топлива в момент вкл. зажигания
float FuelTrip = 0; // количество литров топлива, израсходованное за один цикл включения зажигания
//все что касается километража
float kmAge = 0; //пробег, полученный со щитка приборов
int kmAgeIGN = 0; //пробег который был в момент включения зажигания
int kmAge_last = 0; // для формул
bool flagkmAgeIGN = 0; //флаг записан ли пробег в момент вкл. зажигания
float kmTrip = 0; //пробег за один цикл включения зажигания
int kmL = 10; // интервал, через который будет происходить обновление среднего расхода на 100км
int km = 0; // переменная для расчетов
int kmeeprom = 10; // интервал, через который будет происходить подсчет среднеарифмитического расхода L100SR_TFT
int kmTFT = 0; // переменная для расчетов периодического подсчета среднеарифмитического расхода топлива L100SR_TFT
int kmREFUELING = 0; // пробег до заправки на остатке топлива
unsigned long prevWatch = 0;
unsigned long prevDvoet = 0;
unsigned long prevData = 0;
void setup() {
uint16_t ID = myGLCD.readID();
if (ID == 0xD3D3) ID = 0x1581; // write-only shield
myGLCD.begin(ID);
myGLCD.InitLCD(3);
myGLCD.clrScr();
myGLCD.setFont(&Chosence_Bold16pt7b);
//myGLCD.fillScreen(WHITE);
//myGLCD.setTextColor(WHITE, BLACK);
//myGLCD.setBackColor(BLACK);
Wire.begin();
rtc.begin();
//загрузка стартовой страницы
currentPage = '0';
drawHomeScreen();
//подсчет среднеарифметического усредненного расхода
for (int i = 0; i < 11; i++) L100_Eeprom [i]= EEPROM.read(i);
for (int i = 0; i < 11; i++) L100SR_TFT = L100SR_TFT + L100_Eeprom [i];
L100SR_TFT = (float)L100SR_TFT/110.0;
if (L100SR_TFT<0) L100SR_TFT = 0;
if (L100SR_TFT>99) L100SR_TFT = 99;
// строка ниже используется для настройки даты и времени часов
// раскоментировать, выставить времая и дату, залить в ардуино. в скетче закоментировать
// обратно и залить еще раз, иначе каждый раз будет по новой выствлятся это же время и дата
// (год, месяц, день, часы, минуты, секунды)
//rtc.adjust(DateTime(2017, 7, 21, 13, 57, 0));
#if defined debugPCM or debugGAUGE
Serial.begin(115200);
#endif
K_LINE_PCM.begin(10400);
pinMode(TX_PCM, OUTPUT);
}
void loop()
{
curmillis = millis(); // снимок текущего времени
if (curmillis - prevRequest > RequestPeriod && header == 0 )
{
if (request == INIT) fastinit(); // при необходимости делаем переподключение к PCM
else {sendMessagePCM(request); // отправляем на PCM текущий запрос
if (request == PID) RequestPeriod = 600;
if (request == DTCERASE || request == DTCREAD) RequestPeriod = 2500;}
prevRequest = curmillis;
}
receivePCM (); // приём сообщений от PCM
if (header == 0) Menu ();
if (curmillis - prevWatch > 3000) { Watch (); prevWatch = curmillis;}
if (curmillis - prevDvoet > 500) { if (!Dvoet) {myGLCD.setTextColor(WHITE); } else {myGLCD.setTextColor(BLACK); } myGLCD.print(":", 290, 5); prevDvoet = curmillis; Dvoet=!Dvoet;}
}// end loop
void fastinit() {
digitalWrite (TX_PCM, HIGH); // bus idle
delay(1500);
digitalWrite (TX_PCM, LOW); // first part fastinit (25 ms LOW)
delay(25);
digitalWrite (TX_PCM, HIGH); // second part fastinit (25 ms HIGH)
delay(25);
K_LINE_PCM.begin(10400);
sendMessagePCM(INIT); // send start communication message
}
void receivePCM () {
if (K_LINE_PCM.available() ){
// первый старт байт
if (header == 0 && Delay){TIMER_DELAY ; MessageRx_PCM[0]=K_LINE_PCM.read();
if (MessageRx_PCM[0]!=0xFF && bitRead (MessageRx_PCM[0],7)){header = 1; RESETheader_timer = 1; prevRESETheader = curmillis;
#ifdef debugPCM
Serial.print (F("Receive PCM: ")); printDebugRX(MessageRx_PCM[0]);
#endif
}
}
// второй старт байт
if (header == 1 && Delay){TIMER_DELAY ; MessageRx_PCM[1]=K_LINE_PCM.read();
#ifdef debugPCM
printDebugRX (MessageRx_PCM[1]);
#endif
if (MessageRx_PCM[1]==DIAG_address){ header = 2;}
else {
#ifdef debugPCM
Serial.println(F(" PCM Message fail address"));
#endif
header = 0; RESETheader_timer = 0;}}
// третий старт байт
if (header == 2 && Delay){
TIMER_DELAY ;
MessageRx_PCM[2]=K_LINE_PCM.read();
#ifdef debugPCM
printDebugRX (MessageRx_PCM[2]);
#endif
if (MessageRx_PCM[2]==PCM_address){ message_size = MessageRx_PCM[0]; prevRESETheader = curmillis;
if (MessageRx_PCM[0] !=0x80) {header = 4; bitWrite (message_size, 7 , 0);j=3;n=3;}
else {header = 3; j=4;n=4;}
if (message_size > bufsize) message_size = bufsize; crc = 0;}
else {header = 0;
#ifdef debugPCM
Serial.println(F("PCM Message fail address"));
#endif
RESETheader_timer = 0;}
}
// если размер сообщения указан в дополнительном байте (нулевой байт 0x80) читаем этот дополнительный байт:
if (header == 3 && Delay){
TIMER_DELAY ; prevRESETheader = curmillis;
MessageRx_PCM[3]=K_LINE_PCM.read();
#ifdef debugPCM
printDebugRX(MessageRx_PCM[3]);
#endif
message_size = MessageRx_PCM[3];
if (message_size > bufsize) message_size = bufsize;
crc = 0; header = 4;
}
// пишем тело сообщения
if (header == 4 && Delay && j< message_size+n+1) {
MessageRx_PCM[j] = K_LINE_PCM.read(); prevRESETheader = curmillis;
if (j<message_size+n) crc+= MessageRx_PCM[j]; // подсчёт КС
if (j==message_size+n) header = 5;
TIMER_DELAY ;
#ifdef debugPCM
printDebugRX(MessageRx_PCM[j]);
#endif
j++; }
}
// сообщение приняли, действуем
if (header == 5) {TIMER_DELAY ;
#ifdef debugPCM
Serial.println();
#endif
NOanswer_timer = 0; noanswers = 0; // сбрасываем таймер контроля неответов
for(byte i = 0; i<n; i++) crc+=MessageRx_PCM[i]; // прибавляем к контрольной сумме старт байты
// если контрольная сумма верна:
if ( crc == MessageRx_PCM[message_size+n])
{
#ifdef debugPCM
Serial.print (F("Received message is OK! Checksum is correct!")); Serial.print (F(" ")); Serial.println (millis()); // Если КС совпала, тут чёнибудь нужное делаем
printDebugRX_CSgood();
#endif
if (MessageRx_PCM[n]==0xC1 && MessageRx_PCM[n+1]==0x6B && MessageRx_PCM[n+2]==0x8F) {
if (currentPage!=3) {request = PID; RequestPeriod = 70;} else request = PRESENT, RequestPeriod = 4000; prevRequest = curmillis; // receive good INIT
}
else if (MessageRx_PCM[n]==0x58) Troublecodes (); // DTC
else if (MessageRx_PCM[n]==0x54 && MessageRx_PCM[n+1]==0xFF && MessageRx_PCM[n+2]==0x00){ request = PRESENT; RequestPeriod = 4000; prevRequest = curmillis;} // DTC are cleared
else if (MessageRx_PCM[n]==0x61 && MessageRx_PCM[n+1]==0x01) {dataVars() ; RequestPeriod = 70; prevRequest = curmillis; } // receive DataStream
}
// если контрольная сумма не совпала:
#ifdef debugPCM
else Serial.println("CRC fail!!!" );
#endif
message_size = 0; header=0; RESETheader_timer = 0; j=3; crc = 0;
}
// таймер ожидания байт (для успевания появления данных в буфере UART)
if (!Delay && curmillis - timerdelay > waitbyte_RX) Delay = 1;
// таймер сброса целостности сообщения (если данные оборвались посередине сообщения)
if (RESETheader_timer && curmillis - prevRESETheader > 200) {
#ifdef debugPCM
Serial.println(F("Message fail timeout"));
#endif
RESETheader_timer = 0; header = 0;}
// если нет ответа после запроса: +1 к счетчику неответов. Если накопилось 6 и более: делаем реинит.
if (request!=INIT && NOanswer_timer && curmillis - prev_NOanswer > RequestPeriod - RequestPeriod/10)
{
NOanswer_timer = 0; noanswers++;
if (noanswers>=6) { noanswers = 0; request = INIT; RequestPeriod = 3500;}
}
}// end receivePCM
void Troublecodes ()
{
if (MessageRx_PCM[n]==0x58 && MessageRx_PCM[n+1]==0x00){
myGLCD.clrScr();
drawscreen_three();
myGLCD.print("NO DTC", 165, 145);
}
// при получении сообщения о наличии ошибок DTC разберем сообщение выведем на экран ошибки
if (MessageRx_PCM[n]==0x58 && MessageRx_PCM[n+1]>0){
myGLCD.clrScr();
drawscreen_three();
for (int i=0; i<MessageRx_PCM[n+8-7]; i++ ) {
int y = i*35;
bool nolA=0; bool nolB =0;
if (!bitRead(MessageRx_PCM[n+11-7+(i*3)],6) && bitRead(MessageRx_PCM[n+11-7+(i*3)],7)){ myGLCD.setTextColor(GREEN, BLACK);
myGLCD.print(" -Passive-", 300, (75+y));} // если DTC пасивныый делаем цвет зеленый
if (bitRead(MessageRx_PCM[n+11-7+(i*3)],7) && bitRead(MessageRx_PCM[n+11-7+(i*3)],6)) { myGLCD.setTextColor(RED, BLACK);
myGLCD.print(" -Active-", 300, (75+y));} // если DTC активный, делаем цвет красный
myGLCD.print("ERROR ", 50, (75+y));
myGLCD.printNumI((i+1), 150, (75+y));
if (!bitRead(MessageRx_PCM[n+9-7+(i*3)],6) && !bitRead(MessageRx_PCM[n+9-7+(i*3)],7)) myGLCD.print(": P", 170, (75+y));
if (bitRead(MessageRx_PCM[n+9-7+(i*3)],6) && !bitRead(MessageRx_PCM[n+9-7+(i*3)],7)) myGLCD.print(": C", 170, (75+y));
if (!bitRead(MessageRx_PCM[n+9-7+(i*3)],6) && bitRead(MessageRx_PCM[n+9-7+(i*3)],7)) myGLCD.print(": B", 170, (75+y));
if (bitRead(MessageRx_PCM[n+9-7+(i*3)],6) && bitRead(MessageRx_PCM[n+9-7+(i*3)],7)) myGLCD.print(": U", 170, (75+y));
if (MessageRx_PCM[n+9-7+(i*3)]==0x00) {myGLCD.print("00", 230, (75+y)); nolA = 1;}
if (MessageRx_PCM[n+9-7+(i*3)]<=0x0F&&MessageRx_PCM[n+9-7+(i*3)]!=0) {myGLCD.print("0", 230, (75+y)); nolA = 1;}
if (nolA)myGLCD.print(String (MessageRx_PCM[n+9-7+(i*3)],HEX), 246, (75+y));
else myGLCD.print(String (MessageRx_PCM[n+9-7+(i*3)],HEX), 230, (75+y));
if (MessageRx_PCM[n+10-7+(i*3)]==0x00) {myGLCD.print("00", 262, (75+y)); nolB = 1;}
if (MessageRx_PCM[n+10-7+(i*3)]<=0x0F&&MessageRx_PCM[n+10-7+(i*3)]!=0) {myGLCD.print("0", 262, (75+y)); nolB = 1;}
if (nolB)myGLCD.print(String (MessageRx_PCM[n+10-7+(i*3)]),HEX, 278, (75+y));
else myGLCD.print(String (MessageRx_PCM[n+10-7+(i*3)],HEX), 262, (75+y));}}
request = PRESENT; RequestPeriod = 4000; prevRequest = curmillis;
}
void dataVars()
{
//Barom = MessageRx_PCM[39];
L100 = (float)LHor*100.0/(float)Speed;
LHor = (float)RPM* (float)InjQua*2.00/1000.0*60.00/1000.0/0.85;
MAF = ((MessageRx_PCM[n+22]*256)+MessageRx_PCM[n+23])/10;
BoostPres = ((MessageRx_PCM[n+24]*256)+MessageRx_PCM[n+25])/1000.0;
RPM = (MessageRx_PCM[n+35-7]*256)+MessageRx_PCM[n+36-7];
EGRmg = ((MessageRx_PCM[n+37-7]*256)+MessageRx_PCM[n+38-7])/10.0;
BoostPresCom = ((MessageRx_PCM[n+41-7]*256)+MessageRx_PCM[n+42-7])/1000.0;
Speed = ((MessageRx_PCM[n+47-7]*256)+MessageRx_PCM[n+48-7])/100;
DesaInjQua = ((MessageRx_PCM[n+53-7]*256)+MessageRx_PCM[n+54-7])/100.0;
InjQua = ((MessageRx_PCM[n+55-7]*256)+MessageRx_PCM[n+56-7])/100.0;
StaDaliv = ((MessageRx_PCM[n+57-7]*256)+MessageRx_PCM[n+58-7])/100.0;
PumpRPM = (MessageRx_PCM[n+59-7]*256)+MessageRx_PCM[n+60-7];
EGRPul = ((MessageRx_PCM[n+65-7]*256)+MessageRx_PCM[n+66-7])/100.0;
SolenPul = ((MessageRx_PCM[n+67-7]*256)+MessageRx_PCM[n+68-7])/100.0;
SolenPre = ((MessageRx_PCM[n+73-7]*256)+MessageRx_PCM[n+74-7])/100.0;
DesInj = ((MessageRx_PCM[n+75-7]*3)+(MessageRx_PCM[n+76-7])/100.0)+0.3;
ActInj = ((MessageRx_PCM[n+19-7]*3)+(MessageRx_PCM[n+20-7])/100.0)+0.3;
//TempAir = ((MessageRx_PCM[n+77-7]*26)-278)+MessageRx_PCM[n+78-7]/10.0;
//Temp = ((MessageRx_PCM[n+17-7]*26)-278)+MessageRx_PCM[n+18-7]/10.0;
//TempOil = ((MessageRx_PCM[n+21-7]*26)-278)+MessageRx_PCM[n+22-7]/10.0;
//TempFuel = ((MessageRx_PCM[n+61-7]*26)-278)+MessageRx_PCM[n+62-7]/10.0;
//ниже идут расчетные формулы более точные чем те что закоментированы выше
int A = 0;
if (MessageRx_PCM[n+77-7]<=0x0A) A = 277;
if (MessageRx_PCM[n+77-7]==0x0B || MessageRx_PCM[n+77-7]==0x0C) A = 278;
if (MessageRx_PCM[n+77-7]>=0x0D) A = 279;
double B = MessageRx_PCM[n+78-7]/10.0;
double cel , drob ;
drob = modf(B, &cel);
if (drob>0.6) cel++;
TempAir = ((MessageRx_PCM[n+77-7]*26)-A)+cel;
if (MessageRx_PCM[n+17-7]<=0x0A) A = 277;
if (MessageRx_PCM[n+17-7]==0x0B || MessageRx_PCM[n+17-7]==0x0C) A = 278;
if (MessageRx_PCM[n+17-7]>=0x0D) A = 279;
B = MessageRx_PCM[n+18-7]/10.0;
drob = modf(B, &cel);
if (drob>0.6) cel++;
Temp = ((MessageRx_PCM[n+17-7]*26)-A)+cel;
if (MessageRx_PCM[n+21-7]<=0x0A) A = 277;
if (MessageRx_PCM[n+21-7]==0x0B || MessageRx_PCM[n+21-7]==0x0C) A = 278;
if (MessageRx_PCM[n+21-7]>=0x0D) A = 279;
B = MessageRx_PCM[n+22-7]/10.0;
drob = modf(B, &cel);
if (drob>0.6) cel++;
TempOil = ((MessageRx_PCM[n+21-7]*26)-A)+cel;
if (MessageRx_PCM[n+61-7]<=0x0A) A = 277;
if (MessageRx_PCM[n+61-7]==0x0B || MessageRx_PCM[n+61-7]==0x0C) A = 278;
if (MessageRx_PCM[n+61-7]>=0x0D) A = 279;
B = MessageRx_PCM[n+62-7]/10.0;
drob = modf(B, &cel);
if (drob>0.6) cel++;
TempFuel = ((MessageRx_PCM[n+61-7]*26)-A)+cel;
//----------------------------------------------------------
//страниц HOME
//----------------------------------------------------------
//myGLCD.setBackColor(RED);
myGLCD.setTextColor(WHITE, BLACK);
//myGLCD.setBackColor(RED);
myGLCD.printNumI(Speed, 350, 7, 3);
if (currentPage == '0') {
myGLCD.printNumF(LHor, 1, 60, 40, '.',5);
myGLCD.printNumF(L100, 1, 210, 40,'.',5 );
myGLCD.printNumF(L100M, 1, 60, 75,'.',5 );
myGLCD.printNumF(L100SR_TFT, 1, 210, 75,'.',5 );
myGLCD.printNumI(kmREFUELING, 60, 110,5 );
//if (Fuel<53)
myGLCD.printNumF(Fuel, 1, 210, 110,'.',5);
//else myGLCD.print("MAX", 210, 110);
myGLCD.printNumF(kmTrip, 1, 60, 145,'.',5);
myGLCD.printNumF(FuelTrip, 1, 210, 145,'.',5);
myGLCD.printNumI(PumpRPM, 210, 180,5);
myGLCD.printNumI(RPM, 210, 215,5);
myGLCD.printNumF(Fuel2, 1, 0, 215,'.',5);
myGLCD.printNumI(Temp, 415, 40, 3);
myGLCD.printNumI(TempOil, 415, 75, 3);
myGLCD.printNumI(TempFuel, 415, 110,3);
//myGLCD.printNumI(sensors.getTempCByIndex(0), 415, 145 , 3);
myGLCD.printNumI(t, 415, 180, 3);
myGLCD.printNumI(TempAir, 415, 215, 3); }
//----------------------------------------------------------
//страниц INF1
//----------------------------------------------------------
if (currentPage == '1') {
myGLCD.printNumF(StaDaliv,1, 360, 40,'.', 3);
myGLCD.printNumF(DesInj,1, 395, 75, '.', 4);
myGLCD.printNumF(ActInj,1, 395, 110,'.', 4);
myGLCD.printNumF(DesaInjQua,1, 395, 145,'.', 4);
myGLCD.printNumF(InjQua,1, 395, 180,'.', 4);
myGLCD.printNumI(MAF, 170, 215, 4);
myGLCD.printNumF(h, 1, 418, 215); }
//----------------------------------------------------------
//страниц INF2
//----------------------------------------------------------
if (currentPage == '2') {
myGLCD.printNumF(BoostPres,1, 395, 40,'.', 4);
myGLCD.printNumF(BoostPresCom,1, 395, 75,'.', 4);
myGLCD.printNumI(EGRmg, 395, 110, 4);
myGLCD.printNumF(EGRPul,1, 410, 145,'.', 3);
myGLCD.printNumF(SolenPul, 1, 395, 180,'.', 4);
myGLCD.printNumF(SolenPre, 0, 410, 215,'.', 3); }
}
void sendMessagePCM(const byte &command)
{
#ifdef debugPCM
Serial.print (F("Send request ")); Serial.print (textRequest[command]); Serial.print (F(" to PCM ")); Serial.println (millis());
#endif
if (command != INIT) {NOanswer_timer = 1; prev_NOanswer = curmillis;} //т.к. сейчас будем делать запрос, то запускаем таймер контроля неответов
byte size = PCM_Message_TX[command][0];
const byte siZe = size+4;
byte Mes[siZe];
byte Checksum = 0;
for(byte i=0; i<siZe; i++) {
if (i==0) {Mes[i]=size; bitWrite(Mes[i], 7 , 1);}
if (i==1) Mes[i] = PCM_address;
if (i==2) Mes[i] = DIAG_address;
if (i==3) {for (byte t=1; t<size+1; t++ ) {
Mes[i]=PCM_Message_TX [command][t];
Checksum+=Mes[i] ;
K_LINE_PCM.write (Mes[i]);
if (command == INIT) delay (5); else delay (delaybyte_TX);
K_LINE_PCM.read();
i++;}}
if (i!=siZe-1) Checksum+=Mes[i];
else Mes[i] = Checksum;
K_LINE_PCM.write (Mes[i]);
if (command == INIT) delay (5); else delay (delaybyte_TX);
K_LINE_PCM.read();
}
}// end sendMessagePCM
#if defined debugPCM or debugGAUGE
void printDebugRX (const byte &inbyte) {if (inbyte<=15) Serial.print(F("0")); Serial.print (inbyte, HEX); Serial.print (F(" "));}
#endif
#ifdef debugPCM
void printDebugRX_CSgood(){
if (MessageRx_PCM[n]==0xC1 && MessageRx_PCM[n+1]==0x6B && MessageRx_PCM[n+2]==0x8F) {Serial.println (F(" Initialization OK!!!!")); }
else if (MessageRx_PCM[n]==0x58 && MessageRx_PCM[n+1]==0x00) {Serial.println (F(" NO DTC "));}
else if (MessageRx_PCM[n]==0x58 && MessageRx_PCM[n+1] >0x00) {Serial.println (F(" DTC is found!"));}
else if (MessageRx_PCM[n]==0x54 && MessageRx_PCM[n+1]==0xFF && MessageRx_PCM[n+2]==0x00){Serial.println (F(" DTC CLEARED "));}
else if (MessageRx_PCM[n]==0x61 && MessageRx_PCM[n+1]==0x01) {Serial.println (F(" Receive PCM DATAstream"));}}
#endif
void Menu () {
TouchHOME();
if (currentPage == '0') {
TouchINF1();
TouchINF2();
TouchCHECK(); }
if (currentPage == '1') {
TouchINF2();
TouchCHECK(); }
if (currentPage == '2') {
TouchINF1();
TouchCHECK(); }
if (currentPage == '3') {
TouchREAD();
TouchERASE(); }}
void drawHomeScreen() {
reNew = 1;
line() ;
Watch ();
myGLCD.print("/", 115, 7);
myGLCD.print("/", 160, 7);
myGLCD.setTextColor(RED, BLACK); // цвет линии и текста красный
myGLCD.drawLine(295,35,295,248); // линия вертикальная
myGLCD.drawLine(145,35,145,178); // линия вертикальная
myGLCD.drawLine(80,178,80,247); // линия вертикальная
myGLCD.print("L/H", 10, 40);
myGLCD.print("L/A", 148, 40);
myGLCD.print("L/V", 10, 75);
myGLCD.print("L/M", 148, 75);
myGLCD.print("D/K", 10, 110);
myGLCD.print("D/L", 148, 110);
myGLCD.print("V/K", 10, 145);
myGLCD.print("V/L", 148, 145);
myGLCD.print("PUMP RPM", 82, 180);
myGLCD.print("ENGI RPM", 82, 215);
myGLCD.print("D/La", 10, 180);
myGLCD.print("Motor C", 300, 40);
myGLCD.print("OIL C", 300, 75);
myGLCD.print("FUEL C", 300, 110);
myGLCD.print("INTER C", 300, 145);
myGLCD.print("EXTER C", 300, 180);
myGLCD.print("INTAIR C", 300, 215);
//myGLCD.setTextColor(RED);
myGLCD.drawRoundRect (15, 255, 145, 310);
myGLCD.print("INF 1", 55, 270);
myGLCD.drawRoundRect (175, 255, 305, 310);
myGLCD.print("INF 2", 215, 270);
myGLCD.drawRoundRect (335, 255, 465, 310);
myGLCD.print("CHECK", 365, 270);
myGLCD.drawRoundRect (1, 1, 77, 37);
myGLCD.print("HOME", 10, 7);
myGLCD.print("Km/h", 410, 7);
}
//-------------------------------------------------
void drawscreen_one() {
reNew = 1;
line() ;
Watch ();
myGLCD.print("/", 115, 7);
myGLCD.print("/", 160, 7);
myGLCD.setTextColor(RED, BLACK); // цвет линии и текста красный
myGLCD.print("Start of Delivery *CA:", 10, 40);
myGLCD.print("Desir inject Start *CA:", 10, 75);
myGLCD.print("Actua Inject Start *CA:", 10, 110);
myGLCD.print("Desir Inject Quan mg/s:", 10, 145);
myGLCD.print("Actu Inject Quant mg/s:", 10, 180);
myGLCD.print("MAF mg/s:", 10, 215);
myGLCD.print("Humedad %:", 255, 215);
//myGLCD.setTextColor(RED);
myGLCD.drawRoundRect (175, 255, 305, 310);
myGLCD.print("INF 2", 215, 270);
myGLCD.drawRoundRect (335, 255, 465, 310);
myGLCD.print("CHECK", 365, 270);
myGLCD.drawRoundRect (1, 1, 77, 37);
myGLCD.print("HOME", 10, 7);
myGLCD.print("Km/h", 410, 7);
}
//-------------------------------------------------
void drawscreen_two() {
reNew = 1;
line() ;
Watch ();
myGLCD.print("/", 115, 7);
myGLCD.print("/", 160, 7);
myGLCD.setTextColor(RED, BLACK); // цвет линии и текста красный
myGLCD.print("Boost Press Bar:", 10, 40);
myGLCD.print("Boost Press Com Bar:", 10, 75);
myGLCD.print("EGR command mg/s:", 10, 110);
myGLCD.print("EGR Pulse Ratio %:", 10, 145);
myGLCD.print("Solenoide Pulse %:", 10, 180);
myGLCD.print("Solenoide Boost %:", 10, 215);
//myGLCD.setColor(RED);
myGLCD.drawRoundRect (15, 255, 145, 310);
myGLCD.print("INF 1", 55, 270);
myGLCD.drawRoundRect (335, 255, 465, 310);
myGLCD.print("CHECK", 365, 270);
myGLCD.drawRoundRect (1, 1, 77, 37);
myGLCD.print("HOME", 10, 7);
myGLCD.print("Km/h", 410, 7);
}
//----------------------------------------------------------------------------
void drawscreen_three() {
reNew = 1;
Watch ();
myGLCD.print("/", 115, 7);
myGLCD.print("/", 160, 7);
myGLCD.setTextColor(RED, BLACK); // цвет линии красный
myGLCD.drawLine(1,35,479,35); // линия горизонтальная
myGLCD.drawLine(1,248,479,248); // линия горизонтальная
//myGLCD.setTextColor(RED);
myGLCD.drawRoundRect (15, 255, 145, 310);
myGLCD.print("ERASE", 55, 270);
myGLCD.drawRoundRect (335, 255, 465, 310);
myGLCD.print("READ", 365, 270);
myGLCD.drawRoundRect (1, 1, 77, 37);
myGLCD.print("HOME", 10, 5);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//кнопки тач . координаты и переходы
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TouchHOME(){
TSPoint p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > 10 && p.z < 1000)
{if (p.x > 140 && p.x < 320 && p.y > 140 && p.y < 260 && p.z > MINPRESSURE && p.z < MAXPRESSURE)
{ myGLCD.drawRoundRect (1, 1, 77, 37);
currentPage = '0';
request = PID; RequestPeriod = 10; prevRequest = curmillis; // на PCM в этом окне посылается запрос текущих параметров
myGLCD.clrScr();
drawHomeScreen();
x = 0;
y = 0;
p.z = 0;}}}
void TouchINF1(){
TSPoint p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > 10 && p.z < 1000)
{
if (p.x > 150 && p.x < 450 && p.y > 770 && p.y < 890 && p.z > MINPRESSURE && p.z < MAXPRESSURE)
{ myGLCD.drawRoundRect (15, 255, 145, 310);
currentPage = '1';
request = PID; RequestPeriod = 10; prevRequest = curmillis; // на PCM в этом окне посылается запрос текущих параметров
myGLCD.clrScr();
drawscreen_one();
x = 0;
y = 0;
p.z = 0;}}}
void TouchINF2(){
TSPoint p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > 10 && p.z < 1000)
{
if (p.x > 450 && p.x < 680 && p.y > 770 && p.y < 890 && p.z > MINPRESSURE && p.z < MAXPRESSURE)
{ myGLCD.drawRoundRect (175, 255, 305, 310);
currentPage = '2';
request = PID; RequestPeriod = 600; prevRequest = curmillis; // на PCM в этом окне посылается запрос текущих параметров
myGLCD.clrScr();
drawscreen_two();
x = 0;
y = 0;
p.z = 0;}}}
void TouchCHECK(){
TSPoint p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > 10 && p.z < 1000)
{
if (p.x > 690 && p.x < 950 && p.y > 770 && p.y < 890 && p.z > MINPRESSURE && p.z < MAXPRESSURE)
{ myGLCD.drawRoundRect (335, 255, 465, 310);
currentPage = '3';
// request = DTCREAD; RequestPeriod = 10; prevRequest = curmillis; // на PCM в этом окне посылается запрос чтения ошибок
myGLCD.clrScr();
drawscreen_three();
x = 0;
y = 0;
p.z = 0;}}}
void TouchREAD(){
TSPoint p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > 10 && p.z < 1000)
{
if (p.x > 690 && p.x < 950 && p.y > 770 && p.y < 890 && p.z > MINPRESSURE && p.z < MAXPRESSURE)
{ myGLCD.drawRoundRect (335, 255, 465, 310);
request = DTCREAD; RequestPeriod = 300; prevRequest = curmillis; // на PCM при нажатии этой кнопки посылается запрос чтения ошибок
x = 0;
y = 0;
p.z = 0;}}}
void TouchERASE(){
TSPoint p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > 10 && p.z < 1000)
{
if (p.x > 150 && p.x < 450 && p.y > 770 && p.y < 890 && p.z > MINPRESSURE && p.z < MAXPRESSURE)
{myGLCD.drawRoundRect (15, 255, 145, 310);
request = DTCERASE; RequestPeriod = 300; prevRequest = curmillis; // на PCM при нажатии этой кнопки посылается запрос удаления ошибок
x = 0;
y = 0;
p.z = 0;}}}
////////////////////////////////////////////////////////////////////////////////////////
//прорисовка линий
///////////////////////////////////////////////////////////////////////////////////////
void line() {
//myGLCD.setTextColor(RED); // цвет линии красный
myGLCD.drawLine(1,35,479,35); // линия горизонтальная
myGLCD.drawLine(1,73,479,73); // линия горизонтальная
myGLCD.drawLine(1,108,479,108); // линия горизонтальная
myGLCD.drawLine(1,143,479,143); // линия горизонтальная
myGLCD.drawLine(1,178,479,178); // линия горизонтальная
myGLCD.drawLine(1,212,479,212); // линия горизонтальная
myGLCD.drawLine(1,248,479,248); // линия горизонтальная
}
void Watch (){
DateTime now = rtc.now();
int minut = now.minute();
int hour = now.hour();
int mon = now.month();
int date = now.day();
int Year = now.year();
static int minut_last;
static int hour_last;
static int mon_last;
static int date_last;
static int Year_last;
myGLCD.setTextColor(WHITE, BLACK); //белый цвет цифры
if (date != date_last || reNew){
if (date<10 ) {
myGLCD.print("0", 85, 7);
myGLCD.printNumI(date, 100, 7); }
else if (date >=10) {
myGLCD.printNumI(date, 85, 7); }
date_last = date;
}
if (mon!=mon_last || reNew){
if ( mon<10) {
myGLCD.print("0", 130, 7);
myGLCD.printNumI(mon, 145, 7);}
else if (mon >=10) {
myGLCD.printNumI(mon, 130, 7);}
mon_last = mon;
}
if (Year!=Year_last || reNew) { myGLCD.printNumI(Year, 175, 7); Year_last = Year;}
if (hour!=hour_last || reNew){
if (hour<10) {
myGLCD.print("0",255, 7);
myGLCD.printNumI(hour, 270, 7); }
else if(hour>=10){
myGLCD.printNumI(hour, 255, 7); }
hour_last = hour;
}
if (minut!=minut_last || reNew ){
if (minut<10) {
myGLCD.print("0",300, 7);
myGLCD.printNumI(minut, 315, 7); }
else if (minut>=10){
myGLCD.printNumI(minut, 300, 7); }
minut_last = minut;
}
reNew = 0;
}
я не говорю что из за тебя, это гдето я уже накосячил, ща найду. с last.... правда, мне не хватит сала в голове, я даже не понимаю ни логику, ни как это правильно записать :(. уже испльзуется printNumI и printNumF
myGLCD.printNumF(FuelTrip, 1, 210, 145,'.',5);
myGLCD.printNumI(PumpRPM, 210, 180,5); забыл что делает 5 в конце.... отступление чтоли?
5 позиций - вот так ещё можно(или как то так) - myGLCD.printNumI(PumpRPM, 210, 180,5,"0"); Это же надо использовать там где выводится дата вместо проверки на <10 просто в конце поставить 2, "0"); и ведущий ноль сам появится.
Извините ,что в вашей теме спрашиваю.Решил себе собрать БК на свою Джили мк кросс из проекта OBduino32.Я в песочнице тему создал бортовой компьютер на ардуино.Там и скетч и схема есть.Брал из форума Джили.Человек который собирал его,тестировал ,пропал с форума.В ощем собрать собрал я,но не могу наладить вывод температуры.Может посмотрите ,подскажите.
да я ставил на 4 позиции сьезжает с места цифры , конечно можно просто координаты вывода подправить чтобы обратно на метсо посадить, это то не столь важно и не играет роли. я так думаю, и с четерьмя позициями таже фигня
увы, как налипали на кучу так и налипают, через секунду уже каша из цифр можно сказать белое пятно...
Может она не скомпилировалась ? Запусти полную пересборку.
если ты имеешь в виду птичку с лева в верху то делал... сторочка myGLCD.setBackColor(BLACK); делает затирание с другими цветами, но по чему не работает с черным.... вот это странно. ставлю RED и фон у цифр красный, цифры в png, но не моргает и затирается, а если BLACK то фон у цифр ясно что черный, но цифры не в png а белые и продолжают налипать, ПОЧЕМУ...... только с черным такое? вот в чем вопрос, по идеи черный должен работать как и другие цвета.....
Что то напутано значит в основном скетче с setColor, setTextColor. Можно попробовать оставить один setBackColor(BLACK) в setup.
Все остальные места где есть setTextColor(...) изменить на setColor(...) с одной переменной цвета.
Черный - это гашение всех rgb - может оно как то криво на аппаратном уровне работает. Сделай фон НЕ ЧЕРНЫМ для всех экранов проекта. Тогда правки во фруктовой библиотеке отменить надо скорее всего.
ща сек.
в сетапе
myGLCD.setTextColor(WHITE, BLACK);...... не моргает, не затирает
так .....myGLCD.setBackColor(BLACK);
myGLCD.setTextColor(WHITE, BLACK);......... не моргает, не затирает
наоборот ......... myGLCD.setTextColor(WHITE, BLACK);
myGLCD.setBackColor(BLACK);......... черный экран
перед выводом данных
myGLCD.setTextColor(WHITE, BLACK);...... не моргает, не затирает
так .....myGLCD.setBackColor(BLACK);
myGLCD.setTextColor(WHITE, BLACK);......... не моргает, не затирает
наоборот ......... myGLCD.setTextColor(WHITE, BLACK);
myGLCD.setBackColor(BLACK);.........заголовки белые , данных нет
меняем цвет
myGLCD.setTextColor(WHITE, RED);....... не мигает, не затирает, фон черный
так ставим ....myGLCD.setBackColor(RED);
myGLCD.setTextColor(WHITE, BLACK);.......моргает, затирает, фон красный, цифры белые
так ставим......myGLCD.setTextColor(WHITE, BLACK);
myGLCD.setBackColor(RED);........ моргает, затирает, фон красный, цифры png
так и еще встертил на просторах инета что во фрукте
Функция фонового цвета обычно использовалась с «классическим» шрифтом для перезаписи старого содержимого экрана новыми данными. Это работает только потому, что эти символы имеют одинаковый размер; это неразумно делать с пропорционально разнесенными шрифтами с символами разных размеров (которые могут перекрываться).
стандартный шрифт который вообще не указывает только размер
myGLCD.setTextSize(3);
myGLCD.setTextColor(WHITE, BLACK);
и даже с myGLCD.setColor(WHITE);
работает отлично не моргает и затирает.
как я понял с описания выше, затирание со стандартным шрифтом происходит потому что он квадратные семитричные символы имеет, а мне они не подходят, мне нужны выше и ужи, да и он как из майн крафта , уродлив. вот и вся проблема. вообщем библа фрукта это та еще дрянь
Сначала сделай так
в сетапе
myGLCD.setTextColor(WHITE, BLACK);...... не моргает, не затирает
И проверь так ли это.
Потом замени фруктовую библиотеку на исправленную из 1779 (я там ещё немного подправил) и проверь что изменилось ?!
да в сетапе так же как и перед выводом данных myGLCD.setTextColor(WHITE, BLACK); не моргает и не затирает. заменил библу , тот же результат
И цвет не поменялся ? я там типа внутри белый на красный заменил
я вообще убрал какое либо опредиление цвета из скетча, только название шрифта. не мигает, не затирает, цвет цифр и заголовков белый
Подозреваю что не ту либу тянет компилятор ! Просто удали файл либы с моими правками и проверь - ругнётся или нет ? В правках ЖЕСТКО указано 0xF800 (красный)
нет не ругается если даже изменю его имя, его нет, то компилится ,а вот если изменить имя MCUFRIEND_kbv.cpp то да ругается
Где то лежит такая же (похожая) библиотека и он тянет именно её !
стапе. есть такая же библа Adafruit-GFX1. ща уберу. убрал теперь ругается. как ее переключить теперь на эту на Adafruit-GFX? ща гляну может версия старая. 1.5.6 последняя. ща перекачаю и заменю
Через менеджер библиотек... А я какую версию правил интересно ...
короче оно использовало обе библы. но в одиночку 1.5.6 ругается. выкинул вернул старую она 1.5.3. теперь только одна , можно пробовать править вот(
к стати по этому оно просило полный адрес к файлу фонта так как он там его не находил
Вот:
А на что именно ругается свежая ?
ругается на не совместимость с мега 2560 чет такое. так вот теперь реакция пошла:). это в сетупе myGLCD.setTextColor(WHITE, BLACK); . затирание идет но остается на фоне цифр мелкий не дотертый мусор. и цифра например обороты были 800 потом 1200 то 1 остается а за остальными мелкий не дотертый мусор. Подправил везде цвета, все ок. Не знаю че ты там на колдовал но затирание идет, но остается мелкий не дотерный мусор с зади цифр, и не вытерает первую цифру при увеличении цисла в 4ох на оборотах. я думаю со скоростью таже история будет, тока при увеличении до 3ех значного, 3ех значная улетает в право на надпись км/ч и там застывает.
в сетуп теперь нет нужды кидать цвет, кинул в пару мест где нужно и все что нужно стало красны и что нужно белым.
И не могу понять как прописать линимя цвет в Adafruit_GFX.cpp четко написано
void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
как бы я не ставил всегда ошибка компиляции
myGLCD.drawLine(1,35,479,35, так RED, или 0xF800); всегда ошибка компиляции
Вроде подправил, но тестить то не на чем ...
неа ошибка
exit status 1
Ошибка компиляции для платы Arduino/Genuino Mega or Mega 2560.
А текст ошибки ? Перезалил ! Пробуй ещё ...
там текст который всю жизнь мне выдавало с самого начала проэкта. ща скомпилил и теже заметки вот они
вот с ошибкой
вот без ошибки
да где с ошибкой есть отличия
Перезалил
+ еще раз перезалил
скомпилилось но все стало рванными кубиками красными и белыми
фото бы
и к стати стало мигать. эх потерял файл где ты сделал первую подправку и оно не дотерало......
пробуй такой скетч. Убрал постоянный setcolor
мигает. Командир почти поборол, затирало не мигало, но оставался некий мелкий мусорз за цифрами. лан я домой. завтра продолжу.
я покопаю фруктовую либу и выложу к утру - где то я с математикой накосячил.
Результат: (но это именно для Gobold_Bold14pt7b шрифта первого размера)
Как вам стенд для отладки ?
для оладки шрифта? наверное трудоемко, рисовать в ней весь шрифт. а почему именно для Gobold_Bold14pt7b, я вчера подобрал более подходящий шрифт, он и прорисовывается быстрее и цифры не вооружонным глазо раборчиво видно, а то гоболде 0869 не разборчивы, я вот этот подобрал Chosence_Bold16pt7b.
если я этот поставлю то из 1831 не сработает?
Я шрифт не рисовал, я его выводил через адафруит на ...Serial :-)
Можно подкорректировать будет ... Пока проверь с прежним шрифтом.
Для "нового" шрифта в строке 1116 надо обе цифры 30 заменить на 27 - это максимальное по модулю число из последнего столбца таблицы GFXglyph шрифта.
Этот "новый" шрифт лучше выводить на один-два пикселя выше, что бы хостик буквы Q не попадал на линию разметки.
красавчик, все красиво затирает :). я и новый свой шрифт подложил, он чутка меньше, идеально работает. так а тепрь вот..... двоеточие почемуто мигает палочкой, при увеличении цифр до 4ех значно первая цифра не исчезает при падении до 3ох значной, ну и на последок данные обновляются каждые 2 секунды, в мониторе запросы так же затормозились до 1 запрос в 2 секунды (но это уже наврное по Макса чатси, тут уже нужно смотреть каким боком вывод данных и запросы связаны, почему вывод данных затормаживает запросы....) . Ты гений... за фишку с 1116 строкой спасибо, нужно запомнить..
Для ускорения надо сделать всем переменным копию с припиской last и обновлять только то что изменилось. С двоеточием - может оно так нарисовано в шрифте (потом посмотрю). С остающимся символом - надо использовать дополнительные возможности printNUM - с сделать что бы выводились нули, вместо первых цифр.
на страницах где меньше в половину инфы выводится достаточно быстро,первое, двоеточие вчера на этом шрифте были, второе, с ласт Маскс пробовал, говорит заметно мерцание, третье, printNUM уже испльзуется, чет перестала работать кнопка TouchCHECK, ну и не нашел пока что вариант дать линиям цвет и рамкам кнопок. вот сетч, пересмотри, может че увидешь
С двоеточием видимо теперь не проходит финт со стиранием - выводом двоеточия черного цвета.
Теперь не будет мерцания !!! У printnum есть ещё возможности !!! TouchCHECK я никаким боком не касался.
У нас сегодня дует (тьфу тьфу тьфу) - я сваливаю до вечера виндсерфить.
я не говорю что из за тебя, это гдето я уже накосячил, ща найду. с last.... правда, мне не хватит сала в голове, я даже не понимаю ни логику, ни как это правильно записать :(. уже испльзуется printNumI и printNumF
myGLCD.printNumF(FuelTrip, 1, 210, 145,'.',5);
myGLCD.printNumI(PumpRPM, 210, 180,5); забыл что делает 5 в конце.... отступление чтоли?
ПРИКОЛЬНО, УДАЧНО ПРОВЕСТИ ДЕНЬ ;)
5 позиций - вот так ещё можно(или как то так) - myGLCD.printNumI(PumpRPM, 210, 180,5,"0"); Это же надо использовать там где выводится дата вместо проверки на <10 просто в конце поставить 2, "0"); и ведущий ноль сам появится.
ок, ща буду пробовать
Извините ,что в вашей теме спрашиваю.Решил себе собрать БК на свою Джили мк кросс из проекта OBduino32.Я в песочнице тему создал бортовой компьютер на ардуино.Там и скетч и схема есть.Брал из форума Джили.Человек который собирал его,тестировал ,пропал с форума.В ощем собрать собрал я,но не могу наладить вывод температуры.Может посмотрите ,подскажите.
Нужно у MaksaVV спрашивать. Он у нас спец по машинам.
не могу я побороть затирание первых цифр.
myGLCD.printNumI(RPM, 210, 215,5, "0"); не работает
myGLCD.printNumI(RPM, 210, 215,5, '0'); заламывает появляется много цифр штук 8
if (RPM>=10 && RPM<100) {myGLCD.print(" ", 210, 215); }; не работает
А почему 5 позиций ? Обороты могут быть выше 9999 ?
из описания:
printNumI(2000,50,50); // результат: "2000"
printNumI(2000,50,50,0); // результат: "2000"
printNumI(2000,50,50,5); // результат: " 2000"
printNumI(2000,50,50,6,'0'); // результат: "002000"
да я ставил на 4 позиции сьезжает с места цифры , конечно можно просто координаты вывода подправить чтобы обратно на метсо посадить, это то не столь важно и не играет роли. я так думаю, и с четерьмя позициями таже фигня
Вообщем надо увидеть ... фото хотя бы ... что именно не так с '0' в крайнем параметре ...
ок. ща сделаю
не стал делать фото, долго это. вообщем при такой строке
myGLCD.printNumI(RPM, 210, 215,5, '0');
показывает 00800 оборотов. да когда поднимаю до 1200 допустим то выглядит так 01200, при падении до 800 сново так 00800.
оно то затирает нулями, но во первых два нуля почемуто, во вторых не красиво выглядит с нулями
два нуля потому что ты задал 5 позиций, а 800 это три позиции - значит впереди два нуля.
можно перерисовать какой-нибудь символ в шрифте на пустоту и выводить его вместо '0'
пробел библиотека принципиально не выводит
подходяший символ для перерисовки # - он вроде нигде не используется