基于UTFT库的Arduino应用(移植flappy bird)

怎么肥四,网上关于UTFT这个库的资料真的是,少之又少!

偶然看到有一位外国人写的flappy bird程序,放在了B站上。看完了觉得可以玩一下,然后又衍生出来了一些其他的idea~于是乎,整!

只能这么说吧:以前学生时代,通常做开发就是,选择硬件、写驱动、写逻辑、测试。这里的选硬件,通常就选一下资料比较齐全的、相关协议比较友好,当然,优先考虑的事自己比较熟悉的硬件。在Arduion上就不能这么玩了,网上有很多针对硬件开发好了的库,根据库去匹配去挑选硬件再来做,会更省事更方便,驱动就可以省略了,最多就是重写一些函数。

由于网上做过移植的只有一位大神,然后也没看到有其他资料了,就想着试试看,坑真的不是一般的多。

首先,我选择的是UTFT这个库,看官方资料是最快捷方便的方法:

UTFT库官方网站icon-default.png?t=N7T8http://www.rinkydinkelectronics.com/library.php?id=51官网中,库已经是好几年前的驱动了,当然还是适用的。

第一件事,下载UTFT库文件,地址如下:

UTFT.zipicon-default.png?t=N7T8http://www.rinkydinkelectronics.com/download.php?f=UTFT.zip请一定一定一定一定下载原版的...我一开始嫌弃速度有点慢,用了别的地方的链接,调了我一个小时,一直都不行,最后发现就是别人动过程序引起的!

第二件事,安装完库文件后,看手册。手册地址在

C:\Users\Administrator\Documents\Arduino\libraries\UTFT\Documentation

这里面。分三部分,UTFT.pdf、UTFT_Requirements.pdf、UTFT_Supported_display_modules_&_controllers.pdf这三个文件。先看第三个文件,这里有它支持的型号。看到了这些型号主控,就可以根据自己的需求,去选购硬件了(真特么反人类,以往都是选型完了写驱动的,为了省下写驱动的功夫,反过来操作了)。

第三件事,就是选购硬件了。

我选购的是ILI9341驱动的板子。不得不说,整个选购过程遇到了挺多坑的。。。

一开始,没选对芯片驱动对应的板,这块板不支持UTFT;

然后,在网上翻资料的时候,又下了一些别人推荐的库(也是UTFT的库,有问题啊!所以推荐,就用官方的就好了,不行就自己改驱动代码),这个库有问题我又不知道,各种乱调;

最后买了个新的匹配的驱动板,原装驱动,调了一番终于搞掂~

所以说,一定要原版的库,然后,库自带的手册一定要认认真真研读,虽然是纯英文的,也要认真看!

接下来是程序解析和测试方面了。

驱动分两样,通过查看构造函数可以知道:

UTFT(byte model, int RS, int WR, int CS, int RST, int SER=0);

还有一个是

UTFT(byte model, int SDA, int SCL, int CS, int RST[, int RS=0]);

第二个构造函数,从驱动的文档可见:

SDA----MOSI,SDI

SCL----SCLK,SCK,CLK,CLOCK

CS-----SS,CE

RST----RESET

有些变量是有这些别名的。

UTFT包安装完毕后会有一些demo的,店家也有发一些demo的。

首先可以按照配置接线,然后,测试一下店家的demo,这样可以确保硬件正常。有一个点需要重点关注一下的,就是:我用的Mage2560板子,Arduino的板子输出的高电平都是5V的,而TFT屏幕如果没有转接板或者经过处理,则高电平电压是3.3V,所以是无法直接使用!所以需要购买零件搭建驱动电路,可以在某宝搜索一下“电平转换”相关字眼,就能找到对应的电平转换的工具了,很便宜,8口的也就几块钱的事儿,由于需要有屏幕的、触摸的,所以8口有点紧凑,我就买了两块,搭建了16口电平转换~

测试成功后,就可以使用UTFT和UTouch的demo,尝试点亮和使用屏幕了。测试没问题后就可以开始整活儿啦!

网上借用了

www.HowToMechatronics.com

的代码,然后修改后移植。

移植后的效果并不咋样,可能是因为我选购的屏幕的问题吧诶、、、先放出效果来看看:

移植flappybird_20240713

修改后的代码如下:

#include <UTFT.h>
#include <UTouch.h>

//UTFT(byte model, int SDA, int SCL, int CS, int RST[, int RS=0]);
// SDA----MOSI,SDI
// SCL----SCLK,SCK,CLK,CLOCK
// CS-----SS,CE
// RST----RESET

//UTouch(byte tclk, byte tcs, byte tdin, byte dout, byte irq);

#define LED 40
#define CS 43
#define RS 41
#define RESET 42
#define MOSI 45
#define SCK 44

//==== Creating Objects
UTFT myGLCD(DMTFT28105, MOSI, SCK, CS, RESET, RS);  //Parameters should be adjusted to your Display/Schield model
//UTouch myTouch(6, 5, 4, 3, 2);

//==== Defining Variables
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

extern unsigned int bird01[0x41A];

int x, y;

char  selectedUnit;

//Ultrasonic Sensor
const int VCC = 13;
const int trigPin = 11;
const int echoPin = 12;

long duration;
int distanceInch, distanceCm;

// RGB LEDs
const int redLed = 10;
const int greenLed = 9;
const int blueLed = 8;
int xR = 38;
int xG = 38;
int xB = 38;

// Floppy Bird
int xP = 319;
int yP = 100;
int yB = 30;
int fallRateInt = 0;
float fallRate = 0;
int score = 0;
const int button = 14;
int buttonState = 0;

void setup() {
  // Initial setup
  myGLCD.InitLCD();
  myGLCD.clrScr();
  // myTouch.InitTouch();
  // myTouch.setPrecision(PREC_MEDIUM);
  // Defining Pin Modes
  pinMode(VCC, OUTPUT);      // VCC
  pinMode(trigPin, OUTPUT);  // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);   // Sets the echoPin as an Input
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  digitalWrite(VCC, HIGH);  // +5V - Pin 13 as VCC
  myGLCD.setColor(114, 198, 206);
  myGLCD.fillRect(0, 0, 319, 239);
  drawBird(30);
  drawGround();
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {

  //delay(1);
  xP = xP - 3;
  drawPilars(xP, yP);

  yB += fallRateInt;
  fallRate = fallRate + 0.4;
  fallRateInt = int(fallRate);
  if (yB >= 220) {
    yB = 220;
  }
  if(yB>=180 || yB<=0){
    restartGame();
  }
  if((xP<=85) && (xP>=30) && (yB<=yP-2)){
    restartGame();
  }
  if((xP<=85) && (xP>=30) && (yB>=yP+60)){
    restartGame();
  }
  drawBird(yB);

  if (xP <= -51) {
    xP = 319;
    yP = rand() % 100 + 20;
    score++;
  }

  buttonState = digitalRead(button);
  if (buttonState == HIGH) {
    fallRate = -5;
  }
}


// ====== Custom Funtions ======
// drawHomeScreen - Custom Function
void drawHomeScreen() {
  // Title
  myGLCD.setBackColor(0, 0, 0);                          // Sets the background color of the area where the text will be printed to black
  myGLCD.setColor(255, 255, 255);                        // Sets color to white
  myGLCD.setFont(BigFont);                               // Sets font to big
  myGLCD.print("Arduino TFT Tutorial", CENTER, 10);      // Prints the string on the screen
  myGLCD.setColor(255, 0, 0);                            // Sets color to red
  myGLCD.drawLine(0, 32, 319, 32);                       // Draws the red line
  myGLCD.setColor(255, 255, 255);                        // Sets color to white
  myGLCD.setFont(SmallFont);                             // Sets the font to small
  myGLCD.print("by HowToMechatronics.com", CENTER, 41);  // Prints the string
  myGLCD.setFont(BigFont);
  myGLCD.print("Select Example", CENTER, 64);

  // Button - Distance Sensor
  myGLCD.setColor(16, 167, 103);                 // Sets green color
  myGLCD.fillRoundRect(35, 90, 285, 130);        // Draws filled rounded rectangle
  myGLCD.setColor(255, 255, 255);                // Sets color to white
  myGLCD.drawRoundRect(35, 90, 285, 130);        // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
  myGLCD.setFont(BigFont);                       // Sets the font to big
  myGLCD.setBackColor(16, 167, 103);             // Sets the background color of the area where the text will be printed to green, same as the button
  myGLCD.print("DISTANCE SENSOR", CENTER, 102);  // Prints the string

  // Button - RGB LED Control
  myGLCD.setColor(16, 167, 103);
  myGLCD.fillRoundRect(35, 140, 285, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(35, 140, 285, 180);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(16, 167, 103);
  myGLCD.print("RGB LED CONTROL", CENTER, 152);

  // Button - Birduino
  myGLCD.setColor(16, 167, 103);
  myGLCD.fillRoundRect(35, 190, 285, 230);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(35, 190, 285, 230);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(16, 167, 103);
  myGLCD.print("BIRDUINO GAME", CENTER, 202);
}

// Highlights the button when pressed
void drawFrame(int x1, int y1, int x2, int y2) {
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect(x1, y1, x2, y2);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(x1, y1, x2, y2);
}
//====================================================
void drawDistanceSensor() {
  myGLCD.setColor(100, 155, 203);
  myGLCD.fillRoundRect(10, 10, 60, 36);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(10, 10, 60, 36);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(100, 155, 203);
  myGLCD.print("<-", 18, 15);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("Back to Main Menu", 70, 18);
  myGLCD.setFont(BigFont);
  myGLCD.print("Ultrasonic Sensor", CENTER, 50);
  myGLCD.print("HC-SR04", CENTER, 76);
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawLine(0, 100, 319, 100);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SmallFont);
  myGLCD.print("Select Unit", 10, 114);
  myGLCD.setFont(BigFont);
  myGLCD.print("Distance:", 130, 120);
  myGLCD.setColor(223, 77, 55);
  myGLCD.fillRoundRect(10, 135, 90, 163);
  myGLCD.setColor(225, 255, 255);
  myGLCD.drawRoundRect(10, 135, 90, 163);
  myGLCD.setBackColor(223, 77, 55);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("cm", 33, 140);
  myGLCD.setColor(223, 77, 55);
  myGLCD.fillRoundRect(10, 173, 90, 201);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(10, 173, 90, 201);
  myGLCD.setBackColor(223, 77, 55);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("inch", 17, 180);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("Source code at: HowToMechatronics.com", CENTER, 220);
}

//====================================================
void drawLedControl() {
  myGLCD.setColor(100, 155, 203);
  myGLCD.fillRoundRect(10, 10, 60, 36);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect(10, 10, 60, 36);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(100, 155, 203);
  myGLCD.print("<-", 18, 15);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("Back to Main Menu", 70, 18);
  myGLCD.setFont(BigFont);
  myGLCD.print("RGB LED Control", CENTER, 50);
  myGLCD.print("LED Color:", 10, 95);
  myGLCD.print("R", 10, 135);
  myGLCD.print("G", 10, 175);
  myGLCD.print("B", 10, 215);
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawLine(0, 75, 319, 75);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRect(30, 138, 310, 148);  // R - Slider
  myGLCD.drawRect(30, 178, 310, 188);
  myGLCD.drawRect(30, 218, 310, 228);
}
//====================================================
//============= setLedColor() - Custom Funtion
void setLedColor() {
  // Maps the values of the X - Axis from 38 to 0 and 310 to 255, because we need values from 0 to 255 for turning on the led
  int xRC = map(xR, 38, 310, 0, 255);
  int xGC = map(xG, 38, 310, 0, 255);
  int xBC = map(xB, 38, 310, 0, 255);

  // Sends PWM signal to the pins of the led
  analogWrite(redLed, xRC);
  analogWrite(greenLed, xGC);
  analogWrite(blueLed, xBC);

  // Draws a rectangle with the latest color combination
  myGLCD.setColor(xRC, xGC, xBC);
  myGLCD.fillRoundRect(175, 87, 310, 119);

  // Draws the positioners
  myGLCD.setColor(255, 255, 255);
  myGLCD.fillRect(xR, 139, (xR + 4), 147);  // Positioner
  myGLCD.setColor(xRC, 0, 0);
  myGLCD.fillRect(31, 139, (xR - 1), 147);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect((xR + 5), 139, 309, 147);

  myGLCD.setColor(255, 255, 255);
  myGLCD.fillRect(xG, 179, (xG + 4), 187);
  myGLCD.setColor(0, xGC, 0);
  myGLCD.fillRect(31, 179, (xG - 1), 187);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect((xG + 5), 179, 309, 187);


  myGLCD.setColor(255, 255, 255);
  myGLCD.fillRect(xB, 219, (xB + 4), 227);
  myGLCD.setColor(0, 0, xBC);
  myGLCD.fillRect(31, 219, (xB - 1), 227);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect((xB + 5), 219, 309, 227);
}
//====================================================
void drawGround() {
  myGLCD.setColor(221, 216, 148);
  myGLCD.fillRect(0, 215, 319, 239);
  myGLCD.setColor(47, 175, 68);
  myGLCD.fillRect(0, 205, 319, 214);
  myGLCD.setColor(0, 0, 0);
  myGLCD.setBackColor(221, 216, 148);
  myGLCD.setFont(BigFont);
  myGLCD.print("Score:", 5, 220);
  myGLCD.setFont(SmallFont);
  myGLCD.print("Via @Tizi", 140, 220);
}
void drawPilars(int x, int y) {

  if (x >= 270) {
    myGLCD.setColor(0, 200, 20);
    myGLCD.fillRect(318, 0, x, y - 1);
    myGLCD.setColor(0, 0, 0);
    myGLCD.drawRect(319, 0, x - 1, y);

    myGLCD.setColor(0, 200, 20);
    myGLCD.fillRect(318, y + 81, x, 203);
    myGLCD.setColor(0, 0, 0);
    myGLCD.drawRect(319, y + 80, x - 1, 204);
  } else if (x <= 268) {
    myGLCD.setColor(114, 198, 206);
    myGLCD.fillRect(x + 51, 0, x + 53, y);
    myGLCD.setColor(0, 200, 20);
    myGLCD.fillRect(x + 49, 1, x + 1, y - 1);
    myGLCD.setColor(0, 0, 0);
    myGLCD.drawRect(x + 50, 0, x, y);
    myGLCD.setColor(114, 198, 206);
    myGLCD.fillRect(x - 1, 0, x - 3, y);

    myGLCD.setColor(114, 198, 206);
    myGLCD.fillRect(x + 51, y + 80, x + 53, 204);
    myGLCD.setColor(0, 200, 20);
    myGLCD.fillRect(x + 49, y + 81, x + 1, 203);
    myGLCD.setColor(0, 0, 0);
    myGLCD.drawRect(x + 50, y + 80, x, 204);
    myGLCD.setColor(114, 198, 206);
    myGLCD.fillRect(x - 1, y + 80, x - 3, 204);
  }
  myGLCD.setColor(0, 0, 0);
  myGLCD.setBackColor(221, 216, 148);
  myGLCD.setFont(BigFont);
  myGLCD.printNumI(score, 100, 220);
}
//====================================================
void drawBird(int y) {
  if (y <= 219) {
    myGLCD.drawBitmap(50, y, 35, 30, bird01);
    myGLCD.setColor(114, 198, 206);
    myGLCD.fillRoundRect(50, y, 85, y - 6);
    myGLCD.fillRoundRect(50, y + 30, 85, y + 36);
  } else if (y >= 200) {
    myGLCD.drawBitmap(50, 200, 35, 30, bird01);
    myGLCD.setColor(114, 198, 206);
    myGLCD.fillRoundRect(50, 200, 85, 200 - 6);
    myGLCD.fillRoundRect(50, 200 + 30, 85, 200 + 36);
  }
}
void gameOver() {
  myGLCD.clrScr();
  myGLCD.setColor(255, 255, 255);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(BigFont);
  myGLCD.print("GAME OVER", CENTER, 40);
  myGLCD.print("Score:", 100, 80);
  myGLCD.printNumI(score, 200, 80);
  myGLCD.print("Restarting...", CENTER, 120);
  myGLCD.setFont(SevenSegNumFont);
  myGLCD.printNumI(2, CENTER, 150);
  delay(1000);
  myGLCD.printNumI(1, CENTER, 150);
  delay(1000);
  myGLCD.setColor(114, 198, 206);
  myGLCD.fillRect(0, 0, 319, 239);
  drawBird(30);
  drawGround();
  delay(1000);
}
//====================================================
void restartGame() {
  delay(1000);
  gameOver();
  xP = 319;
  yB = 30;
  fallRate = 0;
  score = 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值