as you may know i am displaying a sprite in ST7735 screen, the issue is that im getting an horrible flickering, the question is, how can i avoid flicker and to make the game run smootly?
sketch below:
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
// For the breakout, you can use any 2 or 3 pins
// These pins will also work for the 1.8" TFT shield
#define TFT_CS 11
#define TFT_RST 8 // you can also connect this to the Arduino reset
// in which case, set this #define pin to 0!
#define TFT_DC 9
// Option 1 (recommended): must use the hardware SPI pins
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
// an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example)
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
// Option 2: use any pins but a little slower!
#define TFT_SCLK 13 // set these to be whatever pins you like!
#define TFT_MOSI 11 // set these to be whatever pins you like!
//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
#define SKIP_TICKS 1 // 1000 / 50fps
#define MAX_FRAMESKIP 5
int xpos = 20;
// background
const unsigned int BCKGRDCOL = tft.Color565(138,235,244);
// bird
const unsigned int BIRDCOL = tft.Color565(255,254,174);
#define C0 BCKGRDCOL
#define C1 tft.Color565(195,165,75)
#define C2 BIRDCOL
#define C3 ST7735_WHITE
#define C4 ST7735_RED
#define C5 tft.Color565(251,216,114)
static unsigned int birdcol[] =
{ C0, C0, C1, C1, C1, C1, C1, C0,
C0, C1, C2, C2, C2, C1, C3, C1,
C0, C2, C2, C2, C2, C1, C3, C1,
C1, C1, C1, C2, C2, C3, C1, C1,
C1, C2, C2, C2, C2, C2, C4, C4,
C1, C2, C2, C2, C1, C5, C4, C0,
C0, C1, C2, C1, C5, C5, C5, C0,
C0, C0, C1, C5, C5, C5, C0, C0};
void setup(void)
{
// Serial.begin(9600);
// Serial.print("Hello! ST7735 TFT Test");
// Use this initializer if you're using a 1.8" TFT
tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
tft.fillScreen(ST7735_BLACK);
tft.setRotation( 3 );//1 means landspace and 0 portrait
}//setup
void loop()
{
//variables for gameloop
double delta, old_time, next_game_tick, current_time;
next_game_tick = current_time = millis();
int loops;
while( 1 )
{
loops = 0;
while( millis() > next_game_tick && loops < MAX_FRAMESKIP )
{
//input
//update
xpos+=1;
//render
tft.fillScreen(ST7735_BLACK);
drawPixel2( xpos, 20, 8, 8, birdcol);
//processing the ti[
old_time = current_time;
current_time = millis();
delta = (current_time-old_time)/1000;
next_game_tick += SKIP_TICKS;
loops++;
}//
}//
/**
* this function show the pixels that compose the sprite
*/
void drawPixel2( int x, int y, int w, int h, unsigned int *data)
{
uint8_t mapIndex =0;
for(int i=0;i < h;i++)
{
for(int j=0;j < w;j++)
{
tft.drawPixel( x + i , y + j, data[ mapIndex ] );
mapIndex++;
} //
}
}//