Бортовой компьютер для Опель Зафира.

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Покажи как выглядит после изменения кусок +- 5 строк !

viki13viki
Offline
Зарегистрирован: 14.11.2016

bg таже ошибка

viki13viki
Offline
Зарегистрирован: 14.11.2016
   
 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_x == 1 && size_y == 1) {
        writePixel(x+xo+xx, y+yo+yy, BLACK);
    } else {
        writeFillRect(x+(xo16+xx)*size_x, y+(yo16+yy)*size_y,
          size_x, size_y, BLACK);
    }
}

                bits <<= 1;
            }
        }
        endWrite();

    } // End classic vs custom font

вот так выглядят

 

пробел не помог ошибка

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Может пробел нужен в 15 между else и {

Нигде во write и drawchar нет стирания области. Видимо где то в print оно. Где сам print не вкурю что то ...

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018
                if(bits & 0x80) {
                    if(size_x == 1 && size_y == 1) {
                        writePixel(x+xo+xx, y+yo+yy, color);
                    } else {
                        writeFillRect(x+(xo16+xx)*size_x, y+(yo16+yy)*size_y,
                          size_x, size_y, color);
                    }
                } else {
                    if(size_x == 1 && size_y == 1) {
                        writePixel(x+xo+xx, y+yo+yy, bg);
                    } else {
                        writeFillRect(x+(xo16+xx)*size_x, y+(yo16+yy)*size_y,
                          size_x, size_y, bg);
                    }
                }

Я с гитхаба тяну, там может от твоего отличаться - закинь мне на 4124275@mail.ru свой файл

viki13viki
Offline
Зарегистрирован: 14.11.2016

неа ошибка, возварщаю все на место, компилится

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

На разные библы - у меня size_x size_y у тебя просто size !!!

надо написать else {

скопировать все из if { 

заменить color на bg

написать закрывающую }

b707
Offline
Зарегистрирован: 26.05.2017

Komandir пишет:

 

Нигде во write и drawchar нет стирания области.

Конечно там нет стирания. было бы - и делать ничего не надо бы.

принцип простой = Пиксели, принадлежажие букве - рисуются цветом буквы - color. Пустые пиксели нужно рисовать цветом фона =  bg, но в библиотеке этого нет - нужно добавлять.

Мужики, я бы с удовольствем продолжил - но мне правда утром выезжать на машине за 2500 км - не до кодов :(

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

b707

не бы ло бы стирания - на экране бы было наложение, а не моргание !

Вас цитирую "сначала выполняется заливка старого текста цветом фона"

Не нахожу я этой заливки нигде ...

Ни пуха ни пера, ни гвоздя ни жезла !!!

По Вашему коду ...

в 1705 я так же сделал полную отрисовку, просто кое кто не может её адаптировать и откомпилировать со своей версией библиотеки

b707
Offline
Зарегистрирован: 26.05.2017

Командио, вот для примера глянь, как у меня на матрицах это сделано. Я вывожу пропорциональные шрифты с заполнением фона: Функция начинается со строки 556. Конечно, синтаксис вывода пикселя немного другой - но общий принцип должен быть похож.

https://github.com/board707/DMD_STM32/blob/master/DMD_STM32.cpp

Еще раз сорри -сам бы поправил, но нет времени. надеюсь хоть чуть-чуть помог.

viki13viki
Offline
Зарегистрирован: 14.11.2016

b707 спасибо за помощь, удачного отдыха

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

viki13viki вообщем надо видео того что ты называешь морганием !!! У нас тут разночтения видимо возникли ..

viki13viki
Offline
Зарегистрирован: 14.11.2016

вот загрузи с моим шрифтом он крупный хорошо видно. там есть закономерность 1 раз по часам 3 раза по данным, на второй странице где меншьше данных 1 раз часы и раз 4 данные

https://www.youtube.com/watch?v=5qcdic_b-_w

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Тут и моргание видно и что то типа наложения в нижней строке некоторых цифр.

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Тут и моргание видно и что то типа наложения в нижней строке некоторых цифр.

viki13viki
Offline
Зарегистрирован: 14.11.2016

ну если ставить биг фон стандартный то палочек внизу нету, это похоже связано с моим шрифтом. но моргания такие же самые

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Жду на 4124275@mail.ru исходники Вашей версии Adafruit_GFX.cpp... Видимо там что то кардинально не так !!!

Эта библиотека не пытается обновиться ?

viki13viki
Offline
Зарегистрирован: 14.11.2016

вот исходник 

/*
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]);
    }
}ща поищу последнюю и скачаю. вроде 2ю9ю8

ща поищу последнюю и скачаю. MCUFRIEND_kbv вроде 2.9.8 последняя ща попробую

с новой 2.9.8 не компилится ошибка

no matching function for call to 'UTFTGLUE::setFont(const GFXfont*)'

ща адафруит новую скачаю

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Всё что связано с отображением думаю надо обновить ...

viki13viki
Offline
Зарегистрирован: 14.11.2016

так а что конкретно обновить? MCUFRIEND_kbv новый не компилится, ща фрукту новую найду

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Они же связаны ... всё должно быть крайним

и UTFTGLUE ...

viki13viki
Offline
Зарегистрирован: 14.11.2016

скачал отсюда 

https://github.com/adafruit/Adafruit-GFX-Library

 

Adafruit-GFX-Library версия 1.5.6

Adafruit-GFX-Library 2.9.9 копмрилится , Ттак у меня последняя стоит она и копмилится

вот исходник свежей 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

inline GFXglyph * pgm_read_glyph_ptr(const GFXfont *gfxFont, uint8_t c)
{
#ifdef __AVR__
    return &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
#else
    // expression in __AVR__ section may generate "dereferencing type-punned pointer will break strict-aliasing rules" warning
    // In fact, on other platforms (such as STM32) there is no need to do this pointer magic as program memory may be read in a usual way
    // So expression may be simplified
    return gfxFont->glyph + c;
#endif //__AVR__
}

inline uint8_t * pgm_read_bitmap_ptr(const GFXfont *gfxFont)
{
#ifdef __AVR__
    return (uint8_t *)pgm_read_pointer(&gfxFont->bitmap);
#else
    // expression in __AVR__ section generates "dereferencing type-punned pointer will break strict-aliasing rules" warning
    // In fact, on other platforms (such as STM32) there is no need to do this pointer magic as program memory may be read in a usual way
    // So expression may be simplified
    return gfxFont->bitmap;
#endif //__AVR__
}

#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_x = textsize_y  = 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) {
    drawChar(x, y, c, color, bg, size, size);
}

// 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_x  Font magnification level in X-axis, 1 is 'original' size
    @param    size_y  Font magnification level in Y-axis, 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_x, uint8_t size_y) {

    if(!gfxFont) { // 'Classic' built-in font

        if((x >= _width)            || // Clip right
           (y >= _height)           || // Clip bottom
           ((x + 6 * size_x - 1) < 0) || // Clip left
           ((y + 8 * size_y - 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_x == 1 && size_y == 1)
                        writePixel(x+i, y+j, color);
                    else
                        writeFillRect(x+i*size_x, y+j*size_y, size_x, size_y, color);
                } else if(bg != color) {
                    if(size_x == 1 && size_y == 1)
                        writePixel(x+i, y+j, bg);
                    else
                        writeFillRect(x+i*size_x, y+j*size_y, size_x, size_y, bg);
                }
            }
        }
        if(bg != color) { // If opaque, draw vertical line for last column
            if(size_x == 1 && size_y == 1) writeFastVLine(x+5, y, 8, bg);
            else          writeFillRect(x+5*size_x, y, size_x, 8*size_y, 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  = pgm_read_glyph_ptr(gfxFont, c);
        uint8_t  *bitmap = pgm_read_bitmap_ptr(gfxFont);

        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_x > 1 || size_y > 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_x == 1 && size_y == 1) {
                        writePixel(x+xo+xx, y+yo+yy, color);
                    } else {
                        writeFillRect(x+(xo16+xx)*size_x, y+(yo16+yy)*size_y,
                          size_x, size_y, 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_y * 8;        // advance y one line
        } else if(c != '\r') {                 // Ignore carriage returns
            if(wrap && ((cursor_x + textsize_x * 6) > _width)) { // Off right?
                cursor_x  = 0;                 // Reset x to zero,
                cursor_y += textsize_y * 8;    // advance y one line
            }
            drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize_x, textsize_y);
            cursor_x += textsize_x * 6;          // Advance x one char
        }

    } else { // Custom font

        if(c == '\n') {
            cursor_x  = 0;
            cursor_y += (int16_t)textsize_y *
                        (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  = pgm_read_glyph_ptr(gfxFont, 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_x * (xo + w)) > _width)) {
                        cursor_x  = 0;
                        cursor_y += (int16_t)textsize_y *
                          (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
                    }
                    drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize_x, textsize_y);
                }
                cursor_x += (uint8_t)pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize_x;
            }
        }

    }
    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) {
    setTextSize(s, s);
}

/**************************************************************************/
/*!
    @brief   Set text 'magnification' size. Each increase in s makes 1 pixel that much bigger.
    @param  s_x  Desired text width magnification level in X-axis. 1 is default
    @param  s_y  Desired text width magnification level in Y-axis. 1 is default
*/
/**************************************************************************/
void Adafruit_GFX::setTextSize(uint8_t s_x, uint8_t s_y) {
    textsize_x = (s_x > 0) ? s_x : 1;
    textsize_y = (s_y > 0) ? s_y : 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_y * (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 = pgm_read_glyph_ptr(gfxFont, 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_x)) > _width)) {
                    *x  = 0; // Reset x to zero, advance y by one line
                    *y += textsize_y * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
                }
                int16_t tsx = (int16_t)textsize_x,
                        tsy = (int16_t)textsize_y,
                        x1 = *x + xo * tsx,
                        y1 = *y + yo * tsy,
                        x2 = x1 + gw * tsx - 1,
                        y2 = y1 + gh * tsy - 1;
                if(x1 < *minx) *minx = x1;
                if(y1 < *miny) *miny = y1;
                if(x2 > *maxx) *maxx = x2;
                if(y2 > *maxy) *maxy = y2;
                *x += xa * tsx;
            }
        }

    } else { // Default font

        if(c == '\n') {                     // Newline?
            *x  = 0;                        // Reset x to zero,
            *y += textsize_y * 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_x * 6) > _width)) { // Off right?
                *x  = 0;                    // Reset x to zero,
                *y += textsize_y * 8;       // advance y one line
            }
            int x2 = *x + textsize_x * 6 - 1, // Lower-right pixel of char
                y2 = *y + textsize_y * 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_x * 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
   @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_x The font magnification in X-axis of the label text
   @param    textsize_y The font magnification in Y-axis 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_x, uint8_t textsize_y)
{
  // Tweak arguments and pass to the newer initButtonUL() function...
  initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill,
    textcolor, label, textsize_x, textsize_y);
}

/**************************************************************************/
/*!
   @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)
{
  initButtonUL(gfx, x1, y1, w, h, outline, fill, textcolor, label, textsize, 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_x The font magnification in X-axis of the label text
   @param    textsize_y The font magnification in Y-axis 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_x, uint8_t textsize_y)
{
  _x1           = x1;
  _y1           = y1;
  _w            = w;
  _h            = h;
  _outlinecolor = outline;
  _fillcolor    = fill;
  _textcolor    = textcolor;
  _textsize_x   = textsize_x;
  _textsize_y   = textsize_y;
  _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_x),
    _y1 + (_h/2) - (4 * _textsize_y));
  _gfx->setTextColor(text);
  _gfx->setTextSize(_textsize_x, _textsize_y);
  _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]);
    }
}

 

MaksVV
Offline
Зарегистрирован: 06.08.2015

Кстати ты страницы полистай на бортовике. Все ли работает? Ошибки, стирание ошибок. Логика немного поменялась. Теперь можно не нажимать кнопку считать ошибки. Он должен это сам делать раз в три секунды. Т.е. если ошибка добавится , то она сразу покажется на экране. После запроса на стирание ошибок, он сам опять переходит на чтение ошибок.

viki13viki
Offline
Зарегистрирован: 14.11.2016

докладываю, все страницы работают. переходишь на ошибки да сразу показывает ошибку и каждые 3 секудны обновляется экран. нажал стереть, экран завис с зажатой кнопкой borrar , нажал читать зависла и она в нажатом состоянии, перешол на хоме вернулся на ошибки пишет нет ошибок и обновляется каждые 3 секунды. мне кажется раньше было удобнее, нажал прочел, нажал стер, нажал прочел нет ошибок. мигание 3 секунды раздражет

MaksVV
Offline
Зарегистрирован: 06.08.2015

Понял. А ошибка то стерлась? То есть в РСМ все правильно идет?

viki13viki
Offline
Зарегистрирован: 14.11.2016

да стерлась. все я домой. мозги кипят от шрифтоф. завтра утром продолжу

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

С крайними библиотеками что-нибудь изменилось в отображении ???

viki13viki
Offline
Зарегистрирован: 14.11.2016

Типа со сторонними? Нет. Я там выше видео кинул. Глянь 1713

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Это же видео до обновления бибилиотек ?

viki13viki
Offline
Зарегистрирован: 14.11.2016

Да. Но вроде после тоже самое. Завтра проверю

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Странно что на видео не "тикает" двоеточие !!!

Так как моргают только цифры - можно и самому нарисовать их и выводить без моргания.

viki13viki
Offline
Зарегистрирован: 14.11.2016

да я хотел об двоеточии сказать, но подумал что ща сказут , да двоетоечее фигня ща другие проблемы. вы все так стараетесь, а ща тут с двоеточием...... 

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Что бы моргало двоеточие надо скорее всего не пробел выводить для его стирания, а то же самое двоеточие, по тем же координатам, но цветом фона

viki13viki
Offline
Зарегистрирован: 14.11.2016

я думаю двое точие это последнее дело. я ща проехал и вроде штук 6 функций не работают, заврат скжу точно. типа актуальное топливо, остаток километров в баке и т.д. но это скажу точно завтра. ну и шрифт, хотя может двоетоточие и говорит о чем то не хорошем

MaksVV
Offline
Зарегистрирован: 06.08.2015

дак ведь нету ещё общения с панелью ))  оттудаж топливо то берётся

вот скетч , попытался сделать старую логику по чтению ошибок

 #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/Gobold_Bold14pt7b.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;
 

#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 prev_betweenTimer = 0;      // Таймер задержки между полученным сообщением и отправкой нового запроса
 bool betweenTimer = 0;               // Таймер задержки между полученным сообщением и отправкой нового запроса
 uint16_t betweenTimer_interval = 70; // Величина задежки, которую создает этот таймер, мс            

 
 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(&Gobold_Bold14pt7b);  

   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 && !betweenTimer) 
    {
  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.print(":", 290, 5);} else {myGLCD.print(" ", 285, 0);} 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;              // сбрасываем таймер контроля неответов 
 betweenTimer = 1; prev_betweenTimer = curmillis;// включаем таймер задержки отправки следующего запроса  
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 = 10;} 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 = 10; 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;}   

// таймер , не дающий отправить новый запрос на PCM пока не прошла задержка после принятого сообщения. 
if (betweenTimer && curmillis - prev_betweenTimer > betweenTimer_interval) {betweenTimer = 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);
    #ifdef debugPCM
    Serial.println ("     NO DTC  "); 
    #endif
                         }
  //при получении этого сообщения выдавать на третий экран "DTC BORRADO"
  if (MessageRx_PCM[n]==0x54 && MessageRx_PCM[n+1]==0xFF && MessageRx_PCM[n+2]==0x00){
    myGLCD.clrScr();             
    drawscreen_three();      
    myGLCD.print("DTC BORRADO", 165, 145);
   #ifdef debugPCM
    Serial.println ("     DTC BORRADO  "); 
   #endif
                          }
  // при получении сообщения о наличии ошибок DTC разберем сообщение выведем на экран ошибки
  if (MessageRx_PCM[n]==0x58 && MessageRx_PCM[n+1]>0){
     #ifdef debugPCM
     Serial.println ("DTC is found!");
     #endif
     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.setColor (0,255,0);
     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.setColor (255,0,0);  
     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));}} 



}



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;   


myGLCD.setColor(255, 255, 255);      //цвет текста
     myGLCD.printNumI(Speed, 350, 7, 3);
  //----------------------------------------------------------
  //страниц HOME
  //----------------------------------------------------------
  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() {
    line() ;
    Watch ();
    myGLCD.setColor(255, 0, 0); // цвет линии и текста красный
    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.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() {
    line() ;
    Watch ();
    myGLCD.setColor(255, 0, 0); // цвет линии и текста красный
    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.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() {
    line() ;
    Watch ();
    myGLCD.setColor(255, 0, 0); // цвет линии и текста красный
    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.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() {
    Watch ();
    myGLCD.setColor(255, 0, 0); // цвет линии красный
    myGLCD.drawLine(1,35,479,35); // линия горизонтальная
    myGLCD.drawLine(1,248,479,248); // линия горизонтальная
    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);
    myGLCD.print("Km/h", 410, 3);
   }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//кнопки тач . координаты и переходы
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 = 10; 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 = 10;  prevRequest = curmillis; // на PCM при нажатии этой кнопки посылается запрос удаления ошибок 
        x = 0;
        y = 0;
        p.z = 0;}}}
 ////////////////////////////////////////////////////////////////////////////////////////
 //прорисовка линий
 ///////////////////////////////////////////////////////////////////////////////////////
 void line() {
    myGLCD.setColor(255, 0, 0); // цвет линии красный
    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 m = now.minute(); 
   int hour = now.hour();
   int mon = now.month();
   int date = now.day();
 
   myGLCD.setColor(255, 255, 255); //белый цвет цифры
 
   if (date<10) { 
   myGLCD.print("0", 85, 7); 
   myGLCD.printNumI(date, 100, 7); } 
   else if (date >=10) {
   myGLCD.printNumI(date, 85, 7); }
   myGLCD.print("/", 115, 7);
   if ( mon<10) {
   myGLCD.print("0", 130, 7); 
   myGLCD.printNumI(mon, 145, 7);} 
   else if (mon >=10) {
   myGLCD.printNumI(mon, 130, 7);}  
   myGLCD.print("/", 160, 7);
   myGLCD.printNumI(now.year(), 175, 7);
   if (hour<10) {
   myGLCD.print("0",255, 7); 
   myGLCD.printNumI(hour, 270, 7); } 
   else if(hour>=10){ 
   myGLCD.printNumI(hour, 255, 7); } 
   if (m<10) {
   myGLCD.print("0",300, 7); 
   myGLCD.printNumI(m, 315, 7); } 
   else if (m>=10){ 
   myGLCD.printNumI(m, 300, 7); }
    }          

 

MaksVV
Offline
Зарегистрирован: 06.08.2015

двоеточие не мигает,  потому что его забанили (не дают мигать пока принимается сообщение от PCM) в 1735 исправил это 

viki13viki
Offline
Зарегистрирован: 14.11.2016

1735 отчет

при стартовой загрузке на хоме не отображаются разделительные линии и кнопки. нажав на невидимую кнопку перехожу на другую страницу и все норм прорисовано, возвращаюсь на хоме и все норм прорисовано. на странице ошибок дальше мигает каждые 3 секунды, это бы убрать, да сразу показало ошибку. И еще одну лажу онаружил вчера , когда сокорость превышает 100 км в час, на надпись Km/h которая правее скорости, накладывается допустим 104 и весит до изменение допустим я перешел до 106, теперь там 106 а рядом левее скорость допустим еду 80. помогает только нажав кнопку хоме пере запуск экрана хоме. ты написал исправил двоеточие, не моргает. примерно так......

Командир да, с новыми библами тоже моргает. ставил свой шрифт, так же стандартный биг и смол тоже моргают. только вообще без заявок о шрифте не моргает.

MaksVV
Offline
Зарегистрирован: 06.08.2015
Сними лог при считывании и стирании ошибок. То что в начале не рисовалась страница исправил. 
Двоеточие не мигает- это ты пробуешь при НЕ инициализированном шрифте (т.е. когда буквы все НЕ мигают)?
у тебя точно работает затирание фона при обновлении данных? может добавить setBackcolor (0,0,0)? Скорее всего двоеточие не может затереться пробелом , т.к. затирание фона не работает, но тогда и все данные при обновлении должны затирать друг друга потихоньку, образуя белые квадратики.
 
если что попробуй затереть двоеточие вместо печати пробела, рисованием черного прямоугольника.
 #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/Gobold_Bold14pt7b.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;
 

#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 prev_betweenTimer = 0;      // Таймер задержки между полученным сообщением и отправкой нового запроса
 bool betweenTimer = 0;               // Таймер задержки между полученным сообщением и отправкой нового запроса
 uint16_t betweenTimer_interval = 70; // Величина задежки, которую создает этот таймер, мс            

 
 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(&Gobold_Bold14pt7b);  

   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 && !betweenTimer) 
    {
  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.print(":", 290, 5);} else {myGLCD.print(" ", 285, 0);} 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;              // сбрасываем таймер контроля неответов 
 betweenTimer = 1; prev_betweenTimer = curmillis;// включаем таймер задержки отправки следующего запроса  
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 = 10;} 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 = 10; 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;}   

// таймер , не дающий отправить новый запрос на PCM пока не прошла задержка после принятого сообщения. 
if (betweenTimer && curmillis - prev_betweenTimer > betweenTimer_interval) {betweenTimer = 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);
    #ifdef debugPCM
    Serial.println ("     NO DTC  "); 
    #endif
                         }
  //при получении этого сообщения выдавать на третий экран "DTC BORRADO"
  if (MessageRx_PCM[n]==0x54 && MessageRx_PCM[n+1]==0xFF && MessageRx_PCM[n+2]==0x00){
    myGLCD.clrScr();             
    drawscreen_three();      
    myGLCD.print("DTC BORRADO", 165, 145);
   #ifdef debugPCM
    Serial.println ("     DTC BORRADO  "); 
   #endif
                          }
  // при получении сообщения о наличии ошибок DTC разберем сообщение выведем на экран ошибки
  if (MessageRx_PCM[n]==0x58 && MessageRx_PCM[n+1]>0){
     #ifdef debugPCM
     Serial.println ("DTC is found!");
     #endif
     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.setColor (0,255,0);
     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.setColor (255,0,0);  
     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;   


myGLCD.setColor(255, 255, 255);      //цвет текста
     myGLCD.printNumI(Speed, 350, 7, 3);
  //----------------------------------------------------------
  //страниц HOME
  //----------------------------------------------------------
  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() {
    line() ;
    Watch ();
    myGLCD.print("/", 115, 7);
    myGLCD.print("/", 160, 7);
    myGLCD.setColor(255, 0, 0); // цвет линии и текста красный
    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.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() {
    line() ;
    Watch ();
    myGLCD.print("/", 115, 7);
    myGLCD.print("/", 160, 7);
    myGLCD.setColor(255, 0, 0); // цвет линии и текста красный
    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.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() {
    line() ;
    Watch ();
    myGLCD.print("/", 115, 7);
    myGLCD.print("/", 160, 7);
    myGLCD.setColor(255, 0, 0); // цвет линии и текста красный
    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.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() {
    Watch ();
    myGLCD.print("/", 115, 7);
    myGLCD.print("/", 160, 7);
    myGLCD.setColor(255, 0, 0); // цвет линии красный
    myGLCD.drawLine(1,35,479,35); // линия горизонтальная
    myGLCD.drawLine(1,248,479,248); // линия горизонтальная
    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);
    myGLCD.print("Km/h", 410, 3);
   }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//кнопки тач . координаты и переходы
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 = 10; 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 = 10;  prevRequest = curmillis; // на PCM при нажатии этой кнопки посылается запрос удаления ошибок 
        x = 0;
        y = 0;
        p.z = 0;}}}
 ////////////////////////////////////////////////////////////////////////////////////////
 //прорисовка линий
 ///////////////////////////////////////////////////////////////////////////////////////
 void line() {
    myGLCD.setColor(255, 0, 0); // цвет линии красный
    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.setColor(255, 255, 255); //белый цвет цифры

 if (date != date_last){
   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){ 
   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) {  myGLCD.printNumI(Year, 175, 7); Year_last = Year;}
   
if (hour!=hour_last){
   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){   
   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;
}
    }          

 

 

viki13viki
Offline
Зарегистрирован: 14.11.2016

шрифт я то кокраз и поставил свой точки так и не мегают. сек ща все скажу

viki13viki
Offline
Зарегистрирован: 14.11.2016

отчет запрос и стирание ошибок

Send request INIT to PCM  5051
Receive PCM: 83 F1 11 C1 6B 8F 40 
Received message is OK! Checksum is correct!  5132
   Initialization OK!!!!
Send request PID_2101 to PCM  5207
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6E 00 00 0C 33 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 01 20 00 00 01 B3 0C 4F 03 9E 00 02 C5 
Received message is OK! Checksum is correct!  5419
     Receive PCM DATAstream
Send request PID_2101 to PCM  5769
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6F 00 00 0C 31 00 00 02 66 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D EA 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 A9 08 01 20 00 00 01 B3 0C 4F 03 9E 00 02 08 
Received message is OK! Checksum is correct!  5974
     Receive PCM DATAstream
Send request PID_2101 to PCM  6325
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6F 00 00 0C 33 00 00 02 66 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 AD 08 01 20 00 00 01 B4 0C 4E 03 9E 00 02 CD 
Received message is OK! Checksum is correct!  6542
     Receive PCM DATAstream
Send request PID_2101 to PCM  6892
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6F 00 00 0C 33 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 26 00 00 0C 6B A0 00 01 90 00 00 A9 08 01 20 00 00 01 B3 0C 4F 03 9E 00 02 C5 
Received message is OK! Checksum is correct!  7093
     Receive PCM DATAstream
Send request PID_2101 to PCM  8231
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6F 00 00 0C 33 00 00 02 66 00 00 00 00 04 33 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 A9 08 01 20 00 00 01 B4 0C 4F 03 9E 00 02 C6 
Received message is OK! Checksum is correct!  8442
     Receive PCM DATAstream
Send request PID_2101 to PCM  8516
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6E 00 00 0C 33 00 00 02 66 00 00 00 00 04 33 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 01 20 00 00 01 B3 0C 4F 03 9E 00 02 C9 
Received message is OK! Checksum is correct!  8718
     Receive PCM DATAstream
Send request PID_2101 to PCM  8792
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6F 00 00 0C 33 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 26 00 00 0C 6D A0 00 01 90 00 00 A9 08 01 20 00 00 01 B3 0C 4F 03 9E 00 02 C7 
Received message is OK! Checksum is correct!  8994
     Receive PCM DATAstream
Send request PID_2101 to PCM  9068
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6F 00 00 0C 33 00 00 02 65 00 00 00 00 04 33 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 A9 08 01 20 00 00 01 B3 0C 4F 03 9E 00 02 C4 
Received message is OK! Checksum is correct!  9277
     Receive PCM DATAstream
Send request PID_2101 to PCM  9352
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6F 00 00 0C 33 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D AC 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 A9 08 01 20 00 00 01 B3 0C 51 03 9E 00 02 CB 
Received message is OK! Checksum is correct!  9566
     Receive PCM DATAstream
Send request PID_2101 to PCM  9639
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6E 00 00 0C 33 00 00 02 65 00 00 00 00 04 33 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D EC 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 01 20 00 00 01 B3 0C 4F 03 9E 00 02 08 
Received message is OK! Checksum is correct!  9850
     Receive PCM DATAstream
Send request PID_2101 to PCM  9924
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6F 00 00 0C 33 00 00 02 65 00 00 00 00 04 33 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 A9 08 01 20 00 00 01 B3 0C 4F 03 9E 00 02 C5 
Received message is OK! Checksum is correct!  10136
     Receive PCM DATAstream
Send request PID_2101 to PCM  10210
Receive PCM: 80 F1 11 4C 61 01 01 16 35 AA 00 00 00 00 0C 6E 00 00 0C 33 00 00 02 66 00 00 00 00 04 35 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 01 20 00 00 01 B3 0C 51 03 9E 00 02 CD 
Received message is OK! Checksum is correct!  10417
     Receive PCM DATAstream
Send request DTC_READ to PCM  10627
Receive PCM: 85 F1 11 58 01 16 35 AA D5 
Received message is OK! Checksum is correct!  10699
     DTC is found!
DTC is found!
Send request PRESENT to PCM  14701
Receive PCM: 81 F1 11 7E 01 
Received message is OK! Checksum is correct!  14756
Send request PRESENT to PCM  18702
Receive PCM: 81 F1 11 7E 01 
Received message is OK! Checksum is correct!  18762
Send request PRESENT to PCM  22705
Receive PCM: 81 F1 11 7E 01 
Received message is OK! Checksum is correct!  22754
Send request DTC_ERASE to PCM  24883
Receive PCM: 83 F1 11 54 FF 00 D8 
Received message is OK! Checksum is correct!  24943
     DTC CLEARED  
Send request PRESENT to PCM  28945
Receive PCM: 81 F1 11 7E 01 
Received message is OK! Checksum is correct!  28998
Send request PRESENT to PCM  32948
Receive PCM: 81 F1 11 7E 01 
Received message is OK! Checksum is correct!  33007
Send request PRESENT to PCM  36951
Receive PCM: 81 F1 11 7E 01 
Received message is OK! Checksum is correct!  37011
Send request DTC_ERASE to PCM  39712
Send request DTC_ERASE to PCM  39734
Send request DTC_ERASE to PCM  39755
Send request DTC_ERASE to PCM  39778
Send request DTC_ERASE to PCM  39799
Send request DTC_ERASE to PCM  39821
Send request DTC_ERASE to PCM  39843
Send request DTC_ERASE to PCM  39865
Send request DTC_ERASE to PCM  39887
Send request DTC_ERASE to PCM  39910
Send request DTC_ERASE to PCM  39931
Send request DTC_ERASE to PCM  39953
Send request DTC_ERASE to PCM  39975
Send request DTC_ERASE to PCM  39997
Send request DTC_ERASE to PCM  40018
Send request DTC_ERASE to PCM  40041
Send request DTC_ERASE to PCM  40062
Send request DTC_ERASE to PCM  40084
Send request DTC_ERASE to PCM  40107
Send request DTC_ERASE to PCM  40128
Send request DTC_ERASE to PCM  40150
Send request DTC_ERASE to PCM  40173
Send request DTC_ERASE to PCM  42675
Receive PCM: 83 F1 11 54 FF 00 D8 
Received message is OK! Checksum is correct!  42734
     DTC CLEARED  
Send request PRESENT to PCM  46737
Receive PCM: 81 F1 11 7E 01 
Received message is OK! Checksum is correct!  46798
Send request PID_2101 to PCM  48918
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 33 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D6 
Received message is OK! Checksum is correct!  49136
     Receive PCM DATAstream
Send request PID_2101 to PCM  49486
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D6 
Received message is OK! Checksum is correct!  49688
     Receive PCM DATAstream
Send request PID_2101 to PCM  50038
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D2 
Received message is OK! Checksum is correct!  50243
     Receive PCM DATAstream
Send request PID_2101 to PCM  50593
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6F 00 00 0C 31 00 00 02 66 00 00 00 00 04 36 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D4 
Received message is OK! Checksum is correct!  50794
     Receive PCM DATAstream
Send request PID_2101 to PCM  51144
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 DB 
Received message is OK! Checksum is correct!  51346
     Receive PCM DATAstream
Send request PID_2101 to PCM  52486
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 33 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D3 
Received message is OK! Checksum is correct!  52691
     Receive PCM DATAstream
Send request PID_2101 to PCM  52765
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 30 00 00 02 66 00 00 00 00 04 36 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D6 
Received message is OK! Checksum is correct!  52968
     Receive PCM DATAstream
Send request PID_2101 to PCM  53042
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D2 
Received message is OK! Checksum is correct!  53246
     Receive PCM DATAstream
Send request PID_2101 to PCM  53320
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B3 0C 55 03 9E 00 02 D2 
Received message is OK! Checksum is correct!  53549
     Receive PCM DATAstream
Send request PID_2101 to PCM  53622
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D AA 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B3 0C 55 03 9E 00 02 DA 
Received message is OK! Checksum is correct!  53833
     Receive PCM DATAstream
Send request PID_2101 to PCM  53907
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D EC 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B3 0C 55 03 9E 00 02 16 
Received message is OK! Checksum is correct!  54122
     Receive PCM DATAstream
Send request PID_2101 to PCM  54196
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B3 0C 55 03 9E 00 02 D6 
Received message is OK! Checksum is correct!  54406
     Receive PCM DATAstream
Send request PID_2101 to PCM  54479
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 54 03 9E 00 02 D6 
Received message is OK! Checksum is correct!  54682
     Receive PCM DATAstream
Send request PID_2101 to PCM  54756
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6F 00 00 0C 30 00 00 02 65 00 00 00 00 04 36 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D4 
Received message is OK! Checksum is correct!  54963
     Receive PCM DATAstream
Send request PID_2101 to PCM  55036
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 30 00 00 02 66 00 00 00 00 04 30 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 54 03 9E 00 02 D2 
Received message is OK! Checksum is correct!  55249
     Receive PCM DATAstream
Send request PID_2101 to PCM  55323
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D2 
Received message is OK! Checksum is correct!  55536
     Receive PCM DATAstream
Send request PID_2101 to PCM  55610
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 33 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B3 0C 54 03 9E 00 02 CE 
Received message is OK! Checksum is correct!  55824
     Receive PCM DATAstream
Send request PID_2101 to PCM  55898
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 33 00 00 02 65 00 00 00 00 04 36 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 DB 
Received message is OK! Checksum is correct!  56107
     Receive PCM DATAstream
Send request PID_2101 to PCM  56180
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 35 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D4 
Received message is OK! Checksum is correct!  56395
     Receive PCM DATAstream
Send request PID_2101 to PCM  56469
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 30 00 00 02 65 00 00 00 00 04 35 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D1 
Received message is OK! Checksum is correct!  56672
     Receive PCM DATAstream
Send request PID_2101 to PCM  56745
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 DA 
Received message is OK! Checksum is correct!  56948
     Receive PCM DATAstream
Send request PID_2101 to PCM  57022
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B3 0C 55 03 9E 00 02 D0 
Received message is OK! Checksum is correct!  57224
     Receive PCM DATAstream
Send request PID_2101 to PCM  57297
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6F 00 00 0C 33 00 00 02 65 00 00 00 00 04 36 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D EA 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9F 00 02 19 
Received message is OK! Checksum is correct!  57506
     Receive PCM DATAstream
Send request DTC_READ to PCM  57753
Receive PCM: 82 F1 11 58 00 DC 
Received message is OK! Checksum is correct!  57820
     NO DTC  
     NO DTC  
Send request PRESENT to PCM  61821
Receive PCM: 81 F1 11 7E 01 
Received message is OK! Checksum is correct!  61869
Send request PID_2101 to PCM  66592
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D AA 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 57 03 9E 00 02 D6 
Received message is OK! Checksum is correct!  66800
     Receive PCM DATAstream
Send request PID_2101 to PCM  67150
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 33 00 00 02 66 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B3 0C 57 03 9F 00 02 D8 
Received message is OK! Checksum is correct!  67356
     Receive PCM DATAstream
Send request PID_2101 to PCM  67707
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 33 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D AA 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 57 03 9F 00 02 D7 
Received message is OK! Checksum is correct!  67921
     Receive PCM DATAstream
Send request PID_2101 to PCM  68272
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 30 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 57 03 9F 00 02 D3 
Received message is OK! Checksum is correct!  68478
     Receive PCM DATAstream
Send request PID_2101 to PCM  68829
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 58 03 9E 00 02 D7 
Received message is OK! Checksum is correct!  69035
     Receive PCM DATAstream
Send request PID_2101 to PCM  69385
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 34 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 57 03 9E 00 02 DB 
Received message is OK! Checksum is correct!  69604
     Receive PCM DATAstream
Send request PID_2101 to PCM  70744
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 57 03 9E 00 02 D8 
Received message is OK! Checksum is correct!  70948
     Receive PCM DATAstream
Send request PID_2101 to PCM  71022
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 38 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D AA 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9E 00 02 D8 
Received message is OK! Checksum is correct!  71225
     Receive PCM DATAstream
Send request PID_2101 to PCM  71299
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 35 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 57 03 9F 00 02 D8 
Received message is OK! Checksum is correct!  71501
     Receive PCM DATAstream
Send request PID_2101 to PCM  71575
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D AA 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 57 03 9E 00 02 DD 
Received message is OK! Checksum is correct!  71788
     Receive PCM DATAstream
Send request PID_2101 to PCM  71862
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D AA 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 55 03 9F 00 02 D9 
Received message is OK! Checksum is correct!  72072
     Receive PCM DATAstream
Send request PID_2101 to PCM  72145
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D EA 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 57 03 9E 00 02 19 
Received message is OK! Checksum is correct!  72354
     Receive PCM DATAstream
Send request PID_2101 to PCM  72428
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 36 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 57 03 9F 00 02 DA 
Received message is OK! Checksum is correct!  72643
     Receive PCM DATAstream
Send request PID_2101 to PCM  72717
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6F 00 00 0C 31 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D EA 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 57 03 9E 00 02 17 
Received message is OK! Checksum is correct!  72920
     Receive PCM DATAstream
Send request PID_2101 to PCM  72993
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 58 03 9E 00 02 DA 
Received message is OK! Checksum is correct!  73200
     Receive PCM DATAstream
Send request PID_2101 to PCM  73274
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6D 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D AA 00 00 00 00 09 29 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 57 03 9F 00 02 DE 
Received message is OK! Checksum is correct!  73492
     Receive PCM DATAstream
Send request PID_2101 to PCM  73566
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 57 03 9E 00 02 D5 
Received message is OK! Checksum is correct!  73779
     Receive PCM DATAstream
Send request PID_2101 to PCM  73852
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 30 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D EA 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 58 03 9E 00 02 16 
Received message is OK! Checksum is correct!  74063
     Receive PCM DATAstream
Send request PID_2101 to PCM  74137
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D EC 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 57 03 9E 00 02 1F 
Received message is OK! Checksum is correct!  74348
     Receive PCM DATAstream
Send request PID_2101 to PCM  74422
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6F 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 58 03 9E 00 02 DA 
Received message is OK! Checksum is correct!  74636
     Receive PCM DATAstream
Send request PID_2101 to PCM  74710
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 58 03 9E 00 02 D8 
Received message is OK! Checksum is correct!  74920
     Receive PCM DATAstream
Send request PID_2101 to PCM  74994
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 57 03 9E 00 02 DC 
Received message is OK! Checksum is correct!  75211
     Receive PCM DATAstream
Send request PID_2101 to PCM  75285
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 34 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D AA 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 58 03 9E 00 02 D8 
Received message is OK! Checksum is correct!  75502
     Receive PCM DATAstream
Send request DTC_READ to PCM  75757
Receive PCM: 82 F1 11 58 00 DC 
Received message is OK! Checksum is correct!  75827
     NO DTC  
     NO DTC  
Send request PID_2101 to PCM  80104
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6F 00 00 0C 31 00 00 02 66 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A7 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B3 0C 58 03 9E 00 02 D7 
Received message is OK! Checksum is correct!  80321
     Receive PCM DATAstream
Send request PID_2101 to PCM  80671
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6F 00 00 0C 31 00 00 02 65 00 00 00 00 04 35 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 27 00 00 0C 6D A0 00 01 90 00 00 AD 08 00 20 00 00 01 B4 0C 58 03 9E 00 02 DC 
Received message is OK! Checksum is correct!  80872
     Receive PCM DATAstream
Send request PID_2101 to PCM  81222
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 37 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 58 03 9E 00 02 D9 
Received message is OK! Checksum is correct!  81428
     Receive PCM DATAstream
Send request PID_2101 to PCM  81778
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 66 00 00 00 00 04 34 00 00 00 00 32 C8 03 9F 04 29 03 20 E5 62 00 00 00 00 0D EC 00 00 00 00 09 28 00 00 0C 6D A0 00 01 90 00 00 A9 08 00 20 00 00 01 B4 0C 57 03 9E 00 02 1A 
Received message is OK! Checksum is correct!  81979
     Receive PCM DATAstream
Send request PID_2101 to PCM  82329
Receive PCM: 80 F1 11 4C 61 01 00 00 00 00 00 00 00 00 0C 6E 00 00 0C 31 00 00 02 65 00 00 00 00 04 3A 00 00 00 00 32 C8 03 9E 04 29 03 20 E5 62 00 00 00 00 0D A9 00 00 00 

сработало и чтение и стирание, чутка тормознуто, но может мне показалось

при любом шрифте или без него точки не мегают

при переходе на другую страницу на всех экранах пропадают часы и дата, остаются только разделительные линии, но когда меняются минуты, минуты появляются. какгбудто прорисовывает только изменяющийся элемент

 

setBackcolor (0,0,0) ошибка 'class UTFTGLUE' has no member named 'setBackcolor'

затирание явно не происходит с этой библой. это самое оно

координаты пробела на точках были не правильные, поправил но не помогло не моргают, поставил запятую вметсо пробела начало мигать двоеточие . запятая и так далее. пробел не затирает. когда без данных просто включил по юсб точки мегают нор но есть паузы, когда включаю зажигание и идут данные точки мигают медленнее, после полного пробега мигания по данным

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Это потоу что пробел просто не рисуется так как в нем нет видимых точек. Надо рисовать двоеточие сменив цвет на цвет фона.

viki13viki
Offline
Зарегистрирован: 14.11.2016

с точками разобрались, все мигает

{myGLCD.print(":", 290, 5);} else {myGLCD.setColor(0, 0, 0); myGLCD.print(":", 290, 5);}

к стати часы и дата пересали моргать, только данные, я тока ща заметил, значит шрифт не причем тут

вот со всеми подправками

#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 "C:\Users\ViktorY\Desktop\arduino-1.8.9\libraries\Adafruit-GFX\Fonts/Gobold_Bold14pt7b.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;
 

#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 prev_betweenTimer = 0;      // Таймер задержки между полученным сообщением и отправкой нового запроса
 bool betweenTimer = 0;               // Таймер задержки между полученным сообщением и отправкой нового запроса
 uint16_t betweenTimer_interval = 70; // Величина задежки, которую создает этот таймер, мс            

 
 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(&Gobold_Bold14pt7b);  
   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 && !betweenTimer) 
    {
  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.print(":", 290, 5);} else {myGLCD.setColor(0, 0, 0); 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;              // сбрасываем таймер контроля неответов 
 betweenTimer = 1; prev_betweenTimer = curmillis;// включаем таймер задержки отправки следующего запроса  
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 = 10;} 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 = 10; 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;}   

// таймер , не дающий отправить новый запрос на PCM пока не прошла задержка после принятого сообщения. 
if (betweenTimer && curmillis - prev_betweenTimer > betweenTimer_interval) {betweenTimer = 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);
    #ifdef debugPCM
    Serial.println ("     NO DTC  "); 
    #endif
                         }
  //при получении этого сообщения выдавать на третий экран "DTC BORRADO"
  if (MessageRx_PCM[n]==0x54 && MessageRx_PCM[n+1]==0xFF && MessageRx_PCM[n+2]==0x00){
    myGLCD.clrScr();             
    drawscreen_three();      
    myGLCD.print("DTC BORRADO", 165, 145);
   #ifdef debugPCM
    Serial.println ("     DTC BORRADO  "); 
   #endif
                          }
  // при получении сообщения о наличии ошибок DTC разберем сообщение выведем на экран ошибки
  if (MessageRx_PCM[n]==0x58 && MessageRx_PCM[n+1]>0){
     #ifdef debugPCM
     Serial.println ("DTC is found!");
     #endif
     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.setColor (0,255,0);
     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.setColor (255,0,0);  
     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;   


myGLCD.setColor(255, 255, 255);      //цвет текста
     myGLCD.printNumI(Speed, 350, 7, 3);
  //----------------------------------------------------------
  //страниц HOME
  //----------------------------------------------------------
  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() {
    line() ;
    Watch ();
    myGLCD.print("/", 115, 7);
    myGLCD.print("/", 160, 7);
    myGLCD.setColor(255, 0, 0); // цвет линии и текста красный
    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.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() {
    line() ;
    Watch ();
    myGLCD.print("/", 115, 7);
    myGLCD.print("/", 160, 7);
    myGLCD.setColor(255, 0, 0); // цвет линии и текста красный
    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.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() {
    line() ;
    Watch ();
    myGLCD.print("/", 115, 7);
    myGLCD.print("/", 160, 7);
    myGLCD.setColor(255, 0, 0); // цвет линии и текста красный
    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.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() {
    Watch ();
    myGLCD.print("/", 115, 7);
    myGLCD.print("/", 160, 7);
    myGLCD.setColor(255, 0, 0); // цвет линии красный
    myGLCD.drawLine(1,35,479,35); // линия горизонтальная
    myGLCD.drawLine(1,248,479,248); // линия горизонтальная
    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);
    myGLCD.print("Km/h", 410, 3);
   }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//кнопки тач . координаты и переходы
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 = 10; 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 = 10;  prevRequest = curmillis; // на PCM при нажатии этой кнопки посылается запрос удаления ошибок 
        x = 0;
        y = 0;
        p.z = 0;}}}
 ////////////////////////////////////////////////////////////////////////////////////////
 //прорисовка линий
 ///////////////////////////////////////////////////////////////////////////////////////
 void line() {
    myGLCD.setColor(255, 0, 0); // цвет линии красный
    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.setColor(255, 255, 255); //белый цвет цифры

 if (date != date_last){
   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){ 
   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) {  myGLCD.printNumI(Year, 175, 7); Year_last = Year;}
   
if (hour!=hour_last){
   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){   
   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;
}
    }          

следующая проблем 

при переходе на другую страницу на всех экранах пропадают часы и дата, остаются только разделительные линии, но когда меняются минуты, минуты появляются. какгбудто прорисовывает только изменяющийся элемент

MaksVV
Offline
Зарегистрирован: 06.08.2015
 #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/Gobold_Bold14pt7b.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;

#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(&Gobold_Bold14pt7b);  

   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.setColor(255, 255, 255); } else {myGLCD.setColor(0,0,0); }  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.setColor (0,255,0);
     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.setColor (255,0,0);  
     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;   


myGLCD.setColor(255, 255, 255);      //цвет текста
     myGLCD.printNumI(Speed, 350, 7, 3);
  //----------------------------------------------------------
  //страниц HOME
  //----------------------------------------------------------
  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.setColor(255, 0, 0); // цвет линии и текста красный
    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.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.setColor(255, 0, 0); // цвет линии и текста красный
    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.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.setColor(255, 0, 0); // цвет линии и текста красный
    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.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.setColor(255, 0, 0); // цвет линии красный
    myGLCD.drawLine(1,35,479,35); // линия горизонтальная
    myGLCD.drawLine(1,248,479,248); // линия горизонтальная
    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);
    myGLCD.print("Km/h", 410, 3);
   }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//кнопки тач . координаты и переходы
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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.setColor(255, 0, 0); // цвет линии красный
    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.setColor(255, 255, 255); //белый цвет цифры

 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; 
    }          

 

MaksVV
Offline
Зарегистрирован: 06.08.2015

viki13viki пишет:
к стати часы и дата пересали моргать, только данные, я тока ща заметил, значит шрифт не причем тут

потому что я пофиксил. Сейчас на экран выводятся часы, только при изменении их цифр. Остальное тоже попроавил, надеюсь 1743

viki13viki
Offline
Зарегистрирован: 14.11.2016

да Макс двоеточие и прорисовка даты и часов норм. 

myGLCD.setBackColor(0,0,0);

кидал в луп в сетуп и в void drawscreen_one()  не помогает

MaksVV
Offline
Зарегистрирован: 06.08.2015

viki13viki пишет:
setBackcolor (0,0,0) ошибка 'class UTFTGLUE' has no member named 'setBackcolor'

затирание явно не происходит с этой библой. это самое оно

потому что надо myGLCD.setBackColor(0,0,0);

добавь в сетап это

viki13viki
Offline
Зарегистрирован: 14.11.2016

Макс, пока занять чтобы закончить общение с панелью? Вижу не появляешься. Когда сможешь добей плиз общение с панелью. Спасибо. Только без спешки, когда сможешь, ты мне не обязан.

MaksVV
Offline
Зарегистрирован: 06.08.2015

Ок. Уехал в отпуск. В середине августа приеду, доделаем.

viki13viki
Offline
Зарегистрирован: 14.11.2016

Некоторые выводы. поставил в сетуп закрашивание фона текста myGLCD.setTextColor(WHITE, BLACK);

теперь картина яснее. вид такой. 1 секунда данные, 1 секунда закрашеные места, такой цыкл отображения. в то время в мониторе происходит всего один запрос в секунду, получается что каждый второй запрос на экран не выводится а выводятся закрашеные места. Закоментил вывод данных на экран, в мониторе запроси посыпались быстро. Вывод такой, что, вывод на экран данных затормаживает запросы к эбу, по всей вероятности по этому и видно мигание, данные не выводятся достаточно быстро чтобы не было моргания. Пока все, копаю........

Komandir
Komandir аватар
Offline
Зарегистрирован: 18.08.2018

Напиши отдельный скетч - просто вывод текта на экран в одно и тоже место в цикле нестандарнтным шрифтом. Будет могать ?