From 9c7e29809791bbb9e72d3d244c3a8d22ae494c81 Mon Sep 17 00:00:00 2001 From: Eren Terzioglu Date: Wed, 29 Jan 2025 20:45:43 +0100 Subject: [PATCH] games/brickmatch: Add GPIO input option Add gpio input option for brickmatch example Signed-off-by: Eren Terzioglu --- games/brickmatch/Kconfig | 30 +++++ games/brickmatch/bm_input_gpio.h | 202 +++++++++++++++++++++++++++++++ games/brickmatch/bm_inputs.h | 4 + games/brickmatch/bm_main.c | 4 + 4 files changed, 240 insertions(+) create mode 100644 games/brickmatch/bm_input_gpio.h diff --git a/games/brickmatch/Kconfig b/games/brickmatch/Kconfig index 9d2d7d5c1..d88cbe120 100644 --- a/games/brickmatch/Kconfig +++ b/games/brickmatch/Kconfig @@ -53,6 +53,36 @@ config GAMES_BRICKMATCH_USE_GESTURE bool "Gesture Sensor APDS-9960 as Input" depends on SENSORS_APDS9960 +config GAMES_BRICKMATCH_USE_GPIO + bool "GPIO pins as Input" endchoice +if GAMES_BRICKMATCH_USE_GPIO + +config GAMES_BRICKMATCH_UP_KEY_PATH + string "Up key path" + default "/dev/gpio0" + ---help--- + Path of the up key to read + +config GAMES_BRICKMATCH_DOWN_KEY_PATH + string "Down key path" + default "/dev/gpio1" + ---help--- + Path of the down key to read + +config GAMES_BRICKMATCH_LEFT_KEY_PATH + string "Left key path" + default "/dev/gpio2" + ---help--- + Path of the left key to read + +config GAMES_BRICKMATCH_RIGHT_KEY_PATH + string "Right key path" + default "/dev/gpio3" + ---help--- + Path of the right key to read + +endif #GAMES_BRICKMATCH_USE_GPIO + endif diff --git a/games/brickmatch/bm_input_gpio.h b/games/brickmatch/bm_input_gpio.h new file mode 100644 index 000000000..27428d35d --- /dev/null +++ b/games/brickmatch/bm_input_gpio.h @@ -0,0 +1,202 @@ +/**************************************************************************** + * apps/games/brickmatch/bm_input_gpio.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "bm_inputs.h" + +/**************************************************************************** + * Preprocessor Definitions + ****************************************************************************/ + +struct gpio_struct_fd_s +{ + int fd_up; /* File descriptor value to read up arrow key */ + int fd_down; /* File descriptor value to read down arrow key */ + int fd_left; /* File descriptor value to read left arrow key */ + int fd_right; /* File descriptor value to read right arrow key */ +}; + +struct gpio_struct_fd_s fd_list; + +/**************************************************************************** + * Name: dev_input_init + * + * Description: + * Initialize input method. + * + * Parameters: + * dev - Input state data + * + * Returned Value: + * Zero (OK) is returned on success. A negated errno value is returned on + * failure. + * + ****************************************************************************/ + +int dev_input_init(FAR struct input_state_s *dev) +{ + /* Open the up key gpio device */ + + fd_list.fd_up = open(CONFIG_GAMES_BRICKMATCH_UP_KEY_PATH, O_RDONLY); + if (fd_list.fd_up < 0) + { + fprintf(stderr, "ERROR: Failed to open %s: %d\n", + CONFIG_GAMES_BRICKMATCH_UP_KEY_PATH, errno); + return -ENODEV; + } + + /* Open the down key gpio device */ + + fd_list.fd_down = open(CONFIG_GAMES_BRICKMATCH_DOWN_KEY_PATH, O_RDONLY); + if (fd_list.fd_down < 0) + { + fprintf(stderr, "ERROR: Failed to open %s: %d\n", + CONFIG_GAMES_BRICKMATCH_DOWN_KEY_PATH, errno); + return -ENODEV; + } + + /* Open the left key gpio device */ + + fd_list.fd_left = open(CONFIG_GAMES_BRICKMATCH_LEFT_KEY_PATH, O_RDONLY); + if (fd_list.fd_down < 0) + { + fprintf(stderr, "ERROR: Failed to open %s: %d\n", + CONFIG_GAMES_BRICKMATCH_LEFT_KEY_PATH, errno); + return -ENODEV; + } + + /* Open the right key gpio device */ + + fd_list.fd_right = open(CONFIG_GAMES_BRICKMATCH_RIGHT_KEY_PATH, O_RDONLY); + if (fd_list.fd_down < 0) + { + fprintf(stderr, "ERROR: Failed to open %s: %d\n", + CONFIG_GAMES_BRICKMATCH_RIGHT_KEY_PATH, errno); + return -ENODEV; + } + + dev->fd_gpio = (int)&fd_list; + + return OK; +} + +/**************************************************************************** + * Name: dev_read_input + * + * Description: + * Read inputs and returns result in input state data. + * + * Parameters: + * dev - Input state data + * + * Returned Value: + * Zero (OK) + * + ****************************************************************************/ + +int dev_read_input(FAR struct input_state_s *dev) +{ + struct gpio_struct_fd_s *fd = (struct gpio_struct_fd_s *)dev->fd_gpio; + int invalue = 0; + int ret; + + ret = ioctl(fd->fd_up, GPIOC_READ, (unsigned long)((uintptr_t)&invalue)); + if (ret < 0) + { + int errcode = errno; + fprintf(stderr, "ERROR: Failed to read value from %s: %d\n", + CONFIG_GAMES_BRICKMATCH_UP_KEY_PATH, errcode); + } + else + { + if (invalue != 0) + { + dev->dir = DIR_UP; + } + } + + ret = ioctl(fd->fd_down, GPIOC_READ, (unsigned long)((uintptr_t)&invalue)); + if (ret < 0) + { + int errcode = errno; + fprintf(stderr, "ERROR: Failed to read value from %s: %d\n", + CONFIG_GAMES_BRICKMATCH_DOWN_KEY_PATH, errcode); + } + else + { + if (invalue != 0) + { + dev->dir = DIR_DOWN; + } + } + + ret = ioctl(fd->fd_left, GPIOC_READ, (unsigned long)((uintptr_t)&invalue)); + if (ret < 0) + { + int errcode = errno; + fprintf(stderr, "ERROR: Failed to read value from %s: %d\n", + CONFIG_GAMES_BRICKMATCH_LEFT_KEY_PATH, errcode); + } + else + { + if (invalue != 0) + { + dev->dir = DIR_LEFT; + } + } + + ret = ioctl(fd->fd_right, GPIOC_READ, + (unsigned long)((uintptr_t)&invalue)); + if (ret < 0) + { + int errcode = errno; + fprintf(stderr, "ERROR: Failed to read value from %s: %d\n", + CONFIG_GAMES_BRICKMATCH_RIGHT_KEY_PATH, errcode); + } + else + { + if (invalue != 0) + { + dev->dir = DIR_RIGHT; + } + } + + return OK; +} + diff --git a/games/brickmatch/bm_inputs.h b/games/brickmatch/bm_inputs.h index 042667770..9497b3135 100644 --- a/games/brickmatch/bm_inputs.h +++ b/games/brickmatch/bm_inputs.h @@ -61,6 +61,10 @@ struct input_state_s #ifdef CONFIG_GAMES_BRICKMATCH_USE_GESTURE int fd_gest; #endif +#ifdef CONFIG_GAMES_BRICKMATCH_USE_GPIO + int fd_gpio; +#endif + int dir; /* Direction to move the blocks */ }; diff --git a/games/brickmatch/bm_main.c b/games/brickmatch/bm_main.c index b9155c7a7..094f411d0 100644 --- a/games/brickmatch/bm_main.c +++ b/games/brickmatch/bm_main.c @@ -50,6 +50,10 @@ #include "bm_input_gesture.h" #endif +#ifdef CONFIG_GAMES_BRICKMATCH_USE_GPIO +#include "bm_input_gpio.h" +#endif + /**************************************************************************** * Preprocessor Definitions ****************************************************************************/