#include <SPI.h>
#include <Grafx.h>
#include <SD.h>
#include <Bounce.h>
#define __CS1 10
#define __DC 9
#define SD_CS 4
#define BUFFPIXEL 20
uint8_t errorCode = 0;
Grafx tft = Grafx(__CS1, __DC);
int player_x = 160;
int player_y = 110;
int player_direction = 2;
int x=-0,y=0;
const byte square[] PROGMEM = {32,32,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111};
//////dpad + select buttons
const int buttonUp = 33; //up button
Bounce ButtonUp = Bounce(buttonUp, 10); // 10 ms debounce
const int buttonDown = 38; //down_button
Bounce ButtonDown = Bounce(buttonDown, 10); // 10 ms debounce
const int buttonLeft = 35; //left button
Bounce ButtonLeft = Bounce(buttonLeft, 10); // 10 ms debounce
const int buttonRight = 17; //right button
Bounce ButtonRight = Bounce(buttonRight, 10); // 10 ms debounce
const int buttonS = 21; //select button
Bounce ButtonS = Bounce(buttonS, 10); // 10 ms debounce
//////action + start buttons
const int buttonX = 32; // X button up
Bounce ButtonX = Bounce(buttonX, 10); // 10 ms debounce
const int buttonY = 26; // Y button left
Bounce ButtonY = Bounce(buttonY, 10); // 10 ms debounce
const int buttonA = 30; // A button right
Bounce ButtonA = Bounce(buttonA, 10); // 10 ms debounce
const int buttonB = 28; // B buttun down
Bounce ButtonB = Bounce(buttonB, 10); // 10 ms debounce
const int buttonT = 4; // Start button
Bounce ButtonT = Bounce(buttonT, 10); // 10 ms debounce
void setup() {
Serial.begin(38400);
long unsigned debug_start = millis();
while (!Serial && ((millis() - debug_start) <= 5000));
Serial.println("serial ok, testing lib...");
tft.begin();
tft.setFrameRate(62);
tft.persistence = false;
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonLeft, INPUT_PULLUP);
pinMode(buttonRight, INPUT_PULLUP);
pinMode(buttonS, INPUT_PULLUP);
pinMode(buttonX, INPUT_PULLUP);
pinMode(buttonY, INPUT_PULLUP);
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
pinMode(buttonT, INPUT_PULLUP);
//Serial.print("Initializing SD card...");
// if (!SD.begin(SD_CS)) {
// Serial.println("failed!");
// }
// Serial.println("OK!")
//the following it's mainly for Teensy
//it will help you to understand if you have choosed the
//wrong combination of pins!
errorCode = tft.getErrorCode();
if (errorCode != 0) {
Serial.print("Init error! ");
if (bitRead(errorCode, 0)) Serial.print("MOSI or SCLK pin mismach!\n");
if (bitRead(errorCode, 1)) Serial.print("CS or DC pin mismach!\n");
}
else {
Serial.println("Inited");
}
}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
void loop(void) {
//updates the GameRIot (the display, the sound, the buttons, everyyhing)
//returns true when it's time to render a new frame (20 times/second)
if(tft.updateAll()){
if (ButtonUp.update()) {
if (ButtonUp.fallingEdge()){
tft.drawBitmap1(player_x, player_y, square,32,32,RED);
player_direction = 1;
}
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
if (ButtonDown.update()) {
if (ButtonDown.fallingEdge()){
tft.drawBitmap1(player_x, player_y, square,32,32,GREEN);
player_direction = 2;
}
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
if (ButtonLeft.update()) {
if (ButtonLeft.fallingEdge()){
tft.drawBitmap1(player_x, player_y, square,32,32,BLUE);
player_direction = 3;
}
}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
if (ButtonRight.update()) {
if (ButtonRight.fallingEdge()){
tft.drawBitmap1(player_x, player_y, square,32,32,YELLOW);
player_direction = 4;
}
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
if (ButtonS.update()) {
if (ButtonS.fallingEdge()){
tft.drawBitmap1(player_x, player_y, square,32,32,PURPLE);
player_direction = 5;
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
if (ButtonX.update()) {
if (ButtonX.fallingEdge()){
tft.drawBitmap1(player_x, player_y, square,32,32,PINK);
player_direction = 6;
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
if (ButtonY.update()) {
if (ButtonY.fallingEdge()){
tft.drawBitmap1(player_x, player_y, square,32,32,LIGHTBROWN);
player_direction = 7;
}
}
/////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
if (ButtonA.update()) {
if (ButtonA.fallingEdge()){
tft.drawBitmap1(player_x, player_y, square,32,32,LIGHTGREY);
player_direction = 8;
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
if (ButtonB.update()) {
if (ButtonB.fallingEdge()){
tft.drawBitmap1(player_x, player_y, square,32,32,CYAN);
player_direction = 9;
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
if (ButtonT.update()) {
if (ButtonT.fallingEdge()){
tft.drawBitmap1(player_x, player_y, square,32,32,MAGENTA);
player_direction = 10;
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
///////////////// PLAYER DIRECTION ////////////////////////
if (player_direction == 1){
tft.drawBitmap1(player_x, player_y, square,32,32,RED);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
else if (player_direction == 2){
tft.drawBitmap1(player_x, player_y, square,32,32,GREEN);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
else if (player_direction == 3){
tft.drawBitmap1(player_x, player_y, square,32,32,BLUE);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
else if (player_direction == 4){
tft.drawBitmap1(player_x, player_y, square,32,32,YELLOW);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
else if (player_direction == 5){
tft.drawBitmap1(player_x, player_y, square,32,32,PURPLE);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
else if (player_direction == 6){
tft.drawBitmap1(player_x, player_y, square,32,32,PINK);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
else if (player_direction == 7){
tft.drawBitmap1(player_x, player_y, square,32,32,LIGHTBROWN);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
else if (player_direction == 8){
tft.drawBitmap1(player_x, player_y, square,32,32,LIGHTGREY);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
else if (player_direction == 9){
tft.drawBitmap1(player_x, player_y, square,32,32,CYAN);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
else if (player_direction == 10){
tft.drawBitmap1(player_x, player_y, square,32,32,MAGENTA);
}
}
}