Skip to content

Commit

Permalink
Merge pull request #842 from Daft-Freak/pico-gpio-input
Browse files Browse the repository at this point in the history
Generic GPIO input driver
  • Loading branch information
Daft-Freak authored May 31, 2024
2 parents 8bec3c4 + c91e92d commit fc3f57d
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 186 deletions.
2 changes: 1 addition & 1 deletion 32blit-pico/board/display_pack/config.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(BLIT_BOARD_NAME "Display Pack")

blit_driver(display st7789)
blit_driver(input gpio_abxy)
blit_driver(input gpio)
5 changes: 5 additions & 0 deletions 32blit-pico/board/display_pack/config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#pragma once

#define BUTTON_A_PIN 12
#define BUTTON_B_PIN 13
#define BUTTON_X_PIN 14
#define BUTTON_Y_PIN 15

#define DISPLAY_HEIGHT 135
#define LCD_ROTATION 90

Expand Down
2 changes: 1 addition & 1 deletion 32blit-pico/board/display_pack_2/config.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(BLIT_BOARD_NAME "Display Pack 2.0")

blit_driver(display st7789)
blit_driver(input gpio_abxy)
blit_driver(input gpio)
5 changes: 5 additions & 0 deletions 32blit-pico/board/display_pack_2/config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#pragma once

#define BUTTON_A_PIN 12
#define BUTTON_B_PIN 13
#define BUTTON_X_PIN 14
#define BUTTON_Y_PIN 15

#define DISPLAY_WIDTH 320
#define LCD_ROTATION 90

Expand Down
2 changes: 1 addition & 1 deletion 32blit-pico/board/explorer_base/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ set(BLIT_BOARD_DEFINITIONS

blit_driver(audio beep)
blit_driver(display st7789)
blit_driver(input gpio_abxy)
blit_driver(input gpio)
6 changes: 6 additions & 0 deletions 32blit-pico/board/explorer_base/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#define BUTTON_A_PIN 12
#define BUTTON_B_PIN 13
#define BUTTON_X_PIN 14
#define BUTTON_Y_PIN 15
2 changes: 1 addition & 1 deletion 32blit-pico/board/pimoroni_picosystem/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ set(BLIT_BOARD_DEFINITIONS

blit_driver(audio beep)
blit_driver(display st7789)
blit_driver(input picosystem)
blit_driver(input gpio)
9 changes: 9 additions & 0 deletions 32blit-pico/board/pimoroni_picosystem/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

#define AUDIO_BEEP_PIN PICOSYSTEM_AUDIO_PIN

#define BUTTON_LEFT_PIN PICOSYSTEM_SW_LEFT_PIN
#define BUTTON_RIGHT_PIN PICOSYSTEM_SW_RIGHT_PIN
#define BUTTON_UP_PIN PICOSYSTEM_SW_UP_PIN
#define BUTTON_DOWN_PIN PICOSYSTEM_SW_DOWN_PIN
#define BUTTON_A_PIN PICOSYSTEM_SW_A_PIN
#define BUTTON_B_PIN PICOSYSTEM_SW_B_PIN
#define BUTTON_X_PIN PICOSYSTEM_SW_X_PIN
#define BUTTON_Y_PIN PICOSYSTEM_SW_Y_PIN

#define LED_R_PIN PICOSYSTEM_LED_R_PIN
#define LED_G_PIN PICOSYSTEM_LED_G_PIN
#define LED_B_PIN PICOSYSTEM_LED_B_PIN
Expand Down
2 changes: 1 addition & 1 deletion 32blit-pico/board/tufty2040/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ set(BLIT_BOARD_DEFINITIONS
)

blit_driver(display st7789)
blit_driver(input tufty)
blit_driver(input gpio)
13 changes: 12 additions & 1 deletion 32blit-pico/board/tufty2040/config.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
#pragma once

#define DISPLAY_WIDTH 320
#define BUTTON_UP_PIN 22
#define BUTTON_DOWN_PIN 6
#define BUTTON_A_PIN 7
#define BUTTON_B_PIN 8
#define BUTTON_X_PIN 9 // C
#define BUTTON_Y_PIN 23 // USER_SW

#define BUTTON_UP_ACTIVE_HIGH true
#define BUTTON_DOWN_ACTIVE_HIGH true
#define BUTTON_A_ACTIVE_HIGH true
#define BUTTON_B_ACTIVE_HIGH true
#define BUTTON_X_ACTIVE_HIGH true

#define DISPLAY_WIDTH 320

#define ST7789_8BIT
#define LCD_ROTATION 270
Expand Down
121 changes: 121 additions & 0 deletions 32blit-pico/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,127 @@
#define ALLOW_HIRES 1
#endif

#ifndef BUTTON_LEFT_PIN
#define BUTTON_LEFT_PIN -1
#define BUTTON_LEFT_BI_DECL
#else
#define BUTTON_LEFT_BI_DECL BUTTON_BI_DECL(BUTTON_LEFT_PIN, Left)
#endif

#ifndef BUTTON_LEFT_ACTIVE_HIGH
#define BUTTON_LEFT_ACTIVE_HIGH false
#endif

#ifndef BUTTON_RIGHT_PIN
#define BUTTON_RIGHT_PIN -1
#define BUTTON_RIGHT_BI_DECL
#else
#define BUTTON_RIGHT_BI_DECL BUTTON_BI_DECL(BUTTON_RIGHT_PIN, Right)
#endif

#ifndef BUTTON_RIGHT_ACTIVE_HIGH
#define BUTTON_RIGHT_ACTIVE_HIGH false
#endif

#ifndef BUTTON_UP_PIN
#define BUTTON_UP_PIN -1
#define BUTTON_UP_BI_DECL
#else
#define BUTTON_UP_BI_DECL BUTTON_BI_DECL(BUTTON_UP_PIN, Up)
#endif

#ifndef BUTTON_UP_ACTIVE_HIGH
#define BUTTON_UP_ACTIVE_HIGH false
#endif

#ifndef BUTTON_DOWN_PIN
#define BUTTON_DOWN_PIN -1
#define BUTTON_DOWN_BI_DECL
#else
#define BUTTON_DOWN_BI_DECL BUTTON_BI_DECL(BUTTON_DOWN_PIN, Down)
#endif

#ifndef BUTTON_DOWN_ACTIVE_HIGH
#define BUTTON_DOWN_ACTIVE_HIGH false
#endif

#ifndef BUTTON_A_PIN
#define BUTTON_A_PIN -1
#define BUTTON_A_BI_DECL
#else
#define BUTTON_A_BI_DECL BUTTON_BI_DECL(BUTTON_A_PIN, A)
#endif

#ifndef BUTTON_A_ACTIVE_HIGH
#define BUTTON_A_ACTIVE_HIGH false
#endif

#ifndef BUTTON_B_PIN
#define BUTTON_B_PIN -1
#define BUTTON_B_BI_DECL
#else
#define BUTTON_B_BI_DECL BUTTON_BI_DECL(BUTTON_B_PIN, B)
#endif

#ifndef BUTTON_B_ACTIVE_HIGH
#define BUTTON_B_ACTIVE_HIGH false
#endif

#ifndef BUTTON_X_PIN
#define BUTTON_X_PIN -1
#define BUTTON_X_BI_DECL
#else
#define BUTTON_X_BI_DECL BUTTON_BI_DECL(BUTTON_X_PIN, X)
#endif

#ifndef BUTTON_X_ACTIVE_HIGH
#define BUTTON_X_ACTIVE_HIGH false
#endif

#ifndef BUTTON_Y_PIN
#define BUTTON_Y_PIN -1
#define BUTTON_Y_BI_DECL
#else
#define BUTTON_Y_BI_DECL BUTTON_BI_DECL(BUTTON_Y_PIN, Y)
#endif

#ifndef BUTTON_Y_ACTIVE_HIGH
#define BUTTON_Y_ACTIVE_HIGH false
#endif

#ifndef BUTTON_MENU_PIN
#define BUTTON_MENU_PIN -1
#define BUTTON_MENU_BI_DECL
#else
#define BUTTON_MENU_BI_DECL BUTTON_BI_DECL(BUTTON_MENU_PIN, Menu)
#endif

#ifndef BUTTON_MENU_ACTIVE_HIGH
#define BUTTON_MENU_ACTIVE_HIGH false
#endif

#ifndef BUTTON_HOME_PIN
#define BUTTON_HOME_PIN -1
#define BUTTON_HOME_BI_DECL
#else
#define BUTTON_HOME_BI_DECL BUTTON_BI_DECL(BUTTON_HOME_PIN, Home)
#endif

#ifndef BUTTON_HOME_ACTIVE_HIGH
#define BUTTON_HOME_ACTIVE_HIGH false
#endif

#ifndef BUTTON_JOYSTICK_PIN
#define BUTTON_JOYSTICK_PIN -1
#define BUTTON_JOYSTICK_BI_DECL
#else
#define BUTTON_JOYSTICK_BI_DECL BUTTON_BI_DECL(BUTTON_JOYSTICK_PIN, Joystick)
#endif

#ifndef BUTTON_JOYSTICK_ACTIVE_HIGH
#define BUTTON_JOYSTICK_ACTIVE_HIGH false
#endif

#ifndef DEFAULT_SCREEN_FORMAT
#define DEFAULT_SCREEN_FORMAT PixelFormat::RGB565
#endif
Expand Down
92 changes: 92 additions & 0 deletions 32blit-pico/input_gpio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// generic GPIO buttons
#include "input.hpp"
#include "hardware/gpio.h"

#include "pico/binary_info.h"

#include "config.h"

#include "engine/api_private.hpp"
#include "engine/input.hpp"

// order matches blit::Button
static constexpr int button_io[] {
BUTTON_LEFT_PIN,
BUTTON_RIGHT_PIN,
BUTTON_UP_PIN,
BUTTON_DOWN_PIN,

BUTTON_A_PIN,
BUTTON_B_PIN,
BUTTON_X_PIN,
BUTTON_Y_PIN,

BUTTON_HOME_PIN,
BUTTON_MENU_PIN,
BUTTON_JOYSTICK_PIN,
};

static constexpr bool button_active[] {
BUTTON_LEFT_ACTIVE_HIGH,
BUTTON_RIGHT_ACTIVE_HIGH,
BUTTON_UP_ACTIVE_HIGH,
BUTTON_DOWN_ACTIVE_HIGH,

BUTTON_A_ACTIVE_HIGH,
BUTTON_B_ACTIVE_HIGH,
BUTTON_X_ACTIVE_HIGH,
BUTTON_Y_ACTIVE_HIGH,

BUTTON_HOME_ACTIVE_HIGH,
BUTTON_MENU_ACTIVE_HIGH,
BUTTON_JOYSTICK_ACTIVE_HIGH,
};

static void init_button(int pin, bool active_high) {
gpio_set_function(pin, GPIO_FUNC_SIO);
gpio_set_dir(pin, GPIO_IN);

if(active_high)
gpio_pull_down(pin);
else
gpio_pull_up(pin);
}

void init_input() {
for(size_t i = 0; i < std::size(button_io); i++)
init_button(button_io[i], button_active[i]);

// declare pins
#define BUTTON_BI_DECL(pin, btn) bi_decl(bi_1pin_with_name(pin, #btn" Button"));
BUTTON_LEFT_BI_DECL
BUTTON_RIGHT_BI_DECL
BUTTON_UP_BI_DECL
BUTTON_DOWN_BI_DECL
BUTTON_A_BI_DECL
BUTTON_B_BI_DECL
BUTTON_X_BI_DECL
BUTTON_Y_BI_DECL
BUTTON_HOME_BI_DECL
BUTTON_MENU_BI_DECL
BUTTON_JOYSTICK_BI_DECL
#undef BUTTON_BI_DECL
}

void update_input() {
auto io = gpio_get_all();

uint32_t new_buttons = 0;

for(size_t i = 0; i < std::size(button_io); i++) {
// pin not defined, skip
if(button_io[i] == -1)
continue;

bool pin_state = !!(io & (1 << button_io[i]));

if(pin_state == button_active[i])
new_buttons |= 1 << i;
}

blit::api.buttons = new_buttons;
}
50 changes: 0 additions & 50 deletions 32blit-pico/input_gpio_abxy.cpp

This file was deleted.

Loading

0 comments on commit fc3f57d

Please sign in to comment.