Weird canvas in ST7735 0.96 80*160 tft (SOLVED)

I’m trying to use Adafruit GFX Canvas in St7735 0.96’’ 160x80 tft display with arduino nano. Without Canvas all work ok, but flickering on screen updates. With Canvas I see only garbage.

What I'm doing wrong? Please, help!

My code:

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include "FreeMonoBold24pt7b.h"

#define TFT_CS        10
#define TFT_RST        9
#define TFT_DC         8
#define TFT_BLK        3

#define BRIGHTNESS  16   // tft backlight brightness 0-255

Adafruit_ST7735 tft = Adafruit_ST7735 (TFT_CS, TFT_DC, TFT_RST);

GFXcanvas1 canvas (160, 80);

void setup()
{

    analogWrite (TFT_BLK, BRIGHTNESS);
    tft.initR (INITR_MINI160x80);
    tft.fillScreen (0x0000);
    tft.setRotation (1);

    canvas.setFont (&FreeMonoBold24pt7b); // Use custom font and
    canvas.setTextSize(1);

}

void loop (void)
{
    canvas.fillScreen (0x0000);     // Clear canvas (not display)
    canvas.setCursor (0, 24);       // Pos. is BASE LINE when using fonts!
    canvas.println ("10");
    tft.drawBitmap (0, 24, canvas.getBuffer(), canvas.width(), canvas.height(), 0xFFFF, 0x0000);
    delay(250);
    canvas.fillScreen (0x0000);     // Clear canvas (not display)
    canvas.setCursor (0, 24);       // Pos. is BASE LINE when using fonts!
    canvas.println ("20");
    tft.drawBitmap (0, 24, canvas.getBuffer(), canvas.width(), canvas.height(), 0xFFFF, 0x0000);
    delay(250);
    canvas.fillScreen (0x0000);     // Clear canvas (not display)
    canvas.setCursor (0, 24);       // Pos. is BASE LINE when using fonts!
    canvas.println ("30");
    tft.drawBitmap (0, 24, canvas.getBuffer(), canvas.width(), canvas.height(), 0xFFFF, 0x0000);
    delay(250);

}

What type Nano? That size canvas will need 25,600 bytes of memory.

Don't erase the screen each time you update. Either draw a rectangle over the text in the background color, or print the previous text in the background color, to erase what is on the screen before printing the new text. If you have enough memory you could make the canvas just large enough to cover the area of the text.

@vakulenko your canvas is the same size as your display so when you write the canvas to the display the origin of the canvas should be x=0 y=0.

e.g.
tft.drawBitmap (0, 0, canvas.getBuffer(), canvas.width(), canvas.height(), 0xffff, 0);

I don't have the font that you are using but it works with the built in fonts, can you try the canvas with the Adafruit built in fonts.

EDIT: I agree with @david_2018 , it would be better to create a canvas the same size , or slightly larger than the font.

curious, maybe you also have to setRotation on the canvas..
it's a 1 bit canvas, 1 bit per pixel = 1,600 bytes..
depending upon which nano you could still be running out of ram..
and yes you should draw at 0,0..
good luck.. ~q

The problem was with huge canvas (not sufficient RAM). Solved.

The way i "clear" text, graphic or bitmap is this:

  1. draw(WHITE)
  2. display()
  3. draw(BLACK).

This clears (blackens) the text/graphic inside the buffer without taking time to show the buffer on the OLED. Any WHITE pixels/text can be overlayed inside the buffer. The next call to display() will do the actual "clearing" and "drawing" of all the pixels.

    display.drawBitmap(center + rx, center + ry, bitmap, BITMAP_WIDTH, BITMAP_HEIGHT, WHITE);
    display.display();
    display.drawBitmap(center + rx, center + ry, bitmap, BITMAP_WIDTH, BITMAP_HEIGHT, BLACK);