I've created (with a ton of help) a gaming library for esp32 that utilizes ili9341 drivers and tft's. I actually have two libraries, one for teensy and the other for esp32. I have the teensy version complete but now I'm ready for the esp32 version so I can create my rpg game unhindered by space. I'm using an esp32 by ezsbc and I have every thing working tft wise.
I'm having trouble getting the buttons up and going. I really want to use the Arduino bounce library for this but the esp32 Arduino core is missing a lot of stuff needed to use the bounce library. Stuff like wprogram and inttypes and so on.
I have done some searching and cant really find any hard core evidence of actually adding the buttons. That being said I do have a button library that should work with the esp32 but so far I cant get them to work. The button pad for sure works as all ive done is replace the mcu that was working with buttons to the esp32.
here is my button.h file
Code: Select all
#ifndef BUTTONS_H
#define BUTTONS_H
#include <Arduino.h>
#include "settings.c"
class Buttons {
public:
void begin();
void update();
boolean pressed(uint8_t button);
boolean released(uint8_t button);
boolean held(uint8_t button, uint8_t time);
boolean repeat(uint8_t button, uint8_t period);
uint8_t timeHeld(uint8_t button);
uint8_t pins[NUM_BTN];
uint8_t states[NUM_BTN];
};
#endif /* BUTTONS_H */
Code: Select all
#include "Buttons.h"
void Buttons::begin() {
pins[BTN_LEFT] = BTN_LEFT_PIN;
pins[BTN_UP] = BTN_UP_PIN;
pins[BTN_RIGHT] = BTN_RIGHT_PIN;
pins[BTN_DOWN] = BTN_DOWN_PIN;
pins[BTN_A] = BTN_A_PIN;
pins[BTN_B] = BTN_B_PIN;
pins[BTN_X] = BTN_X_PIN;
pins[BTN_Y] = BTN_Y_PIN;
pins[BTN_S] = BTN_S_PIN;
pins[BTN_T] = BTN_T_PIN;
// states[BTN_LEFT] = 0;
// states[BTN_UP] = 0;
// states[BTN_RIGHT] = 0;
// states[BTN_DOWN] = 0;
// states[BTN_A] = 0;
// states[BTN_B] = 0;
// states[BTN_X] = 0;
// states[BTN_Y] = 0;
// states[BTN_S] = 0;
// states[BTN_T] = 0;
}
/*
* reads each button states and store it
*/
void Buttons::update() {
for (uint8_t thisButton = 0; thisButton < NUM_BTN; thisButton++) {
pinMode(pins[thisButton], INPUT_PULLUP); //enable internal pull up resistors
if (digitalRead(pins[thisButton]) == LOW) { //if button pressed
states[thisButton]++; //increase button hold time
} else {
if (states[thisButton] == 0)//button idle
continue;
if (states[thisButton] == 0xFF)//if previously released
states[thisButton] = 0; //set to idle
else
states[thisButton] = 0xFF; //button just released
}
pinMode(pins[thisButton], INPUT); //disable internal pull up resistors to save power
}
}
/*
* Returns true when 'button' is pressed.
* The button has to be released for it to be triggered again.
*/
boolean Buttons::pressed(uint8_t button) {
if (states[button] == 1)
return true;
else
return false;
}
/*
* return true if 'button' is released
*/
boolean Buttons::released(uint8_t button) {
if (states[button] == 0xFF)
return true;
else
return false;
}
/**
* returns true ONCE when 'button' is held for 'time' frames
* @param button The button's ID
* @param time How much frames button must be held, between 1 and 254.
* @return true when 'button' is held for 'time' frames
*/
boolean Buttons::held(uint8_t button, uint8_t time){
if(states[button] == (time+1))
return true;
else
return false;
}
/**
* returns true every 'period' frames when 'button' is held
* @param button The button's ID
* @param period How much frames button must be held, between 1 and 254.
* @return true if the button is held for the given time
*/
boolean Buttons::repeat(uint8_t button, uint8_t period) {
if (period <= 1) {
if ((states[button] != 0xFF) && (states[button]))
return true;
} else {
if ((states[button] != 0xFF) && ((states[button] % period) == 1))
return true;
}
return false;
}
/**
*
* @param button The button's ID
* @return The number of frames during which the button has been held.
*/
uint8_t Buttons::timeHeld(uint8_t button){
if(states[button] != 0xFF)
return states[button];
else
return 0;
}
Code: Select all
#ifndef SETTINGS_C
#define SETTINGS_C
#define ENABLE_GUI 1 //enable menu, keyboard, pop-up, volume adjust functions
//SD card
#define SD_CS 10
//number of buttons
#define NUM_BTN 10
///////////////////////////////////////////////////////////////////
#define BTN_UP 1
#define BTN_RIGHT 2
#define BTN_DOWN 3
#define BTN_LEFT 0
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
#define BTN_A 4
#define BTN_B 5
#define BTN_X 6
#define BTN_Y 7
///////////////////////////////////////////////////////////////////
#define BTN_S 8
#define BTN_T 9
///////////////////////////////////////////////////////////////////
/////////////////////////////buttons pins//////////////////////////
///////////////////////////////////////////////////////////////////
#define BTN_UP_PIN 34
#define BTN_RIGHT_PIN 32
#define BTN_DOWN_PIN 35
#define BTN_LEFT_PIN 33
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
#define BTN_A_PIN 13 // A button //right
#define BTN_B_PIN 14 // B button //down
#define BTN_X_PIN 12 // X button //up
#define BTN_Y_PIN 15 // Y button //left
///////////////////////////////////////////////////////////////////
#define BTN_S_PIN 1 // start
#define BTN_T_PIN 27 // select
#endif /* SETTINGS_C */
Code: Select all
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 (tft.buttons.repeat(BTN_RIGHT,1));//{X--:}
if (tft.buttons.repeat(BTN_LEFT,1));//{X++:}
if (tft.buttons.repeat(BTN_DOWN,1));//{Y--:}
if (tft.buttons.repeat(BTN_UP,1));//{Y--:}
if (tft.buttons.repeat(BTN_UP,1)){
tft.drawBitmap1(player_x, player_y,paul_rearblack,16,16,BLACK);
tft.drawBitmap1(player_x, player_y,paul_rearblue,16,16,BLUE);
tft.drawBitmap1(player_x, player_y,paul_rearbrown,16,16,BROWN);
tft.drawBitmap1(player_x, player_y,paul_reargrey,16,16,GREY);
tft.drawBitmap1(player_x, player_y,paul_rearpink,16,16,PINK);
tft.drawBitmap1(player_x, player_y,paul_rearred,16,16,YELLOW);
player_direction = 1;
player_y = player_y - 1;}
if(player_y <= 0){