games/brickmatch: Add GPIO input option

Add gpio input option for brickmatch example

Signed-off-by: Eren Terzioglu <eren.terzioglu@espressif.com>
This commit is contained in:
Eren Terzioglu 2025-01-29 20:45:43 +01:00 committed by Alan C. Assis
parent 0da270d9d6
commit 9c7e298097
4 changed files with 240 additions and 0 deletions

View file

@ -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

View file

@ -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 <nuttx/config.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <nuttx/ioexpander/gpio.h>
#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;
}

View file

@ -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 */
};

View file

@ -50,6 +50,10 @@
#include "bm_input_gesture.h"
#endif
#ifdef CONFIG_GAMES_BRICKMATCH_USE_GPIO
#include "bm_input_gpio.h"
#endif
/****************************************************************************
* Preprocessor Definitions
****************************************************************************/