using digital pins as momentary switches for gaming

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

using digital pins as momentary switches for gaming

Postby Duhjoker » Thu Sep 14, 2017 8:26 pm

hi guys

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 */

here is my cpp

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;
    
}
here is the settings file

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 */

here is an example

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){
https://github.com/Duhjoker
Last edited by Duhjoker on Fri Sep 15, 2017 6:41 am, edited 1 time in total.
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: using digital pins as momentary switches

Postby Duhjoker » Fri Sep 15, 2017 3:04 am

Any thing? Clues, hints, suggestions, links, explanations, etc. Maybe I have the ground pin for the pad wrong?

Also I moved my whole game sketch into the esp32 and when I compile it says its using 2 points more storage space than what the teensy 3.6 uses. Plus it costs 110,000 some odd more bytes than the teensy. Whats going here? The ezsbc has the 4mb wroom chip? and why does my sketch cost more?
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

permal
Posts: 384
Joined: Sun May 14, 2017 5:36 pm

Re: using digital pins as momentary switches for gaming

Postby permal » Fri Sep 15, 2017 1:55 pm

Are you asking how to read GPIOs? You can have a look at my Input-class which is part of my C++ library for the ESP32. It doesn't have any built in debouncing if tht is actually what you are asking for.

https://github.com/PerMalmberg/Smooth/b ... /Input.cpp

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: using digital pins as momentary switches for gaming

Postby Duhjoker » Fri Sep 15, 2017 9:03 pm

Ok I'm trying to use the digital IO's on the board as button inputs. I was told I could name any pin on the board as what I wanted it to be. So my program above should read the settings file for the PIN numbers then configure them to work as inputs with it set as input pull up.

Don't really know much about gpio I've been using the numbers silk screened to the board.

I looked up wprogram.h and read the lil arduino history and found out I can use arduino.h instead which takes the place of wprogram.h. To test it I downloaded bounce2 and compiled it with bounce2 included in my game sketch. It compiles.
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

Who is online

Users browsing this forum: Bing [Bot] and 171 guests