mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
games/match4: Add new match4 game
Add match4 game support Signed-off-by: Eren Terzioglu <eren.terzioglu@espressif.com>
This commit is contained in:
parent
0c680bcb8e
commit
8b121eb138
7 changed files with 1662 additions and 0 deletions
84
games/match4/Kconfig
Normal file
84
games/match4/Kconfig
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see the file kconfig-language.txt in the NuttX tools repository.
|
||||
#
|
||||
|
||||
config GAMES_MATCH4
|
||||
bool "Match 4 Game"
|
||||
default n
|
||||
---help---
|
||||
Enable Match 4 game.
|
||||
|
||||
if GAMES_MATCH4
|
||||
|
||||
config GAMES_MATCH4_PROGNAME
|
||||
string "Program name"
|
||||
default "match"
|
||||
---help---
|
||||
This is the name of the program that will be used when the NSH ELF
|
||||
program is installed.
|
||||
|
||||
config GAMES_MATCH4_PRIORITY
|
||||
int "Match 4 Game task priority"
|
||||
default 100
|
||||
|
||||
config GAMES_MATCH4_STACKSIZE
|
||||
int "Match 4 Game stack size"
|
||||
default DEFAULT_TASK_STACKSIZE
|
||||
|
||||
config MATCH4_GAME_DEBUG
|
||||
bool "Print board status to the serial console for debugging"
|
||||
default n
|
||||
|
||||
config GAMES_MATCH4_LED_MATRIX_PATH
|
||||
string "LED matrix path"
|
||||
default "/dev/leds0"
|
||||
---help---
|
||||
Path of the led matrix
|
||||
|
||||
config GAMES_MATCH4_LED_MATRIX_ROWS
|
||||
int "LED Matrix row count"
|
||||
default 8
|
||||
|
||||
config GAMES_MATCH4_LED_MATRIX_COLS
|
||||
int "LED Matrix column count"
|
||||
default 8
|
||||
|
||||
#
|
||||
# Input Device Selection
|
||||
#
|
||||
|
||||
choice
|
||||
prompt "Input Device (Serial Console, GPIO, etc)"
|
||||
default GAMES_MATCH4_USE_CONSOLEKEY
|
||||
|
||||
config GAMES_MATCH4_USE_CONSOLEKEY
|
||||
bool "Serial Console as Input"
|
||||
|
||||
config GAMES_MATCH4_USE_GPIO
|
||||
bool "GPIO pins as Input"
|
||||
endchoice
|
||||
|
||||
if GAMES_MATCH4_USE_GPIO
|
||||
|
||||
config GAMES_MATCH4_DOWN_KEY_PATH
|
||||
string "Down key path"
|
||||
default "/dev/gpio1"
|
||||
---help---
|
||||
Path of the down key to read
|
||||
|
||||
config GAMES_MATCH4_LEFT_KEY_PATH
|
||||
string "Left key path"
|
||||
default "/dev/gpio2"
|
||||
---help---
|
||||
Path of the left key to read
|
||||
|
||||
config GAMES_MATCH4_RIGHT_KEY_PATH
|
||||
string "Right key path"
|
||||
default "/dev/gpio3"
|
||||
---help---
|
||||
Path of the right key to read
|
||||
|
||||
endif #GAMES_MATCH4_USE_GPIO
|
||||
|
||||
endif
|
||||
25
games/match4/Make.defs
Normal file
25
games/match4/Make.defs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
############################################################################
|
||||
# apps/games/match4/Make.defs
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
ifneq ($(CONFIG_GAMES_MATCH4),)
|
||||
CONFIGURED_APPS += $(APPDIR)/games/match4
|
||||
endif
|
||||
36
games/match4/Makefile
Normal file
36
games/match4/Makefile
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
############################################################################
|
||||
# apps/games/match4/Makefile
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
include $(APPDIR)/Make.defs
|
||||
|
||||
# Match4 game info
|
||||
|
||||
PROGNAME = $(CONFIG_GAMES_MATCH4_PROGNAME)
|
||||
PRIORITY = $(CONFIG_GAMES_MATCH4_PRIORITY)
|
||||
STACKSIZE = $(CONFIG_GAMES_MATCH4_STACKSIZE)
|
||||
MODULE = $(CONFIG_GAMES_MATCH4)
|
||||
|
||||
# Match4 game application
|
||||
|
||||
MAINSRC = match4_main.c
|
||||
|
||||
include $(APPDIR)/Application.mk
|
||||
239
games/match4/match4_input_console.h
Normal file
239
games/match4/match4_input_console.h
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
/****************************************************************************
|
||||
* apps/games/match4/match4_input_console.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 <nuttx/ascii.h>
|
||||
#include <termios.h>
|
||||
|
||||
#include "match4_inputs.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Preprocessor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* Termios functions to have getch on Linux/NuttX */
|
||||
|
||||
static struct termios g_old;
|
||||
static struct termios g_new;
|
||||
|
||||
/****************************************************************************
|
||||
* Name: init_termios
|
||||
*
|
||||
* Description:
|
||||
* Initialize g_new terminal I/O settings.
|
||||
*
|
||||
* Parameters:
|
||||
* echo - Enable echo flag
|
||||
*
|
||||
* Returned Value:
|
||||
* None.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void init_termios(int echo)
|
||||
{
|
||||
tcgetattr(0, &g_old); /* grab old terminal i/o settings */
|
||||
g_new = g_old; /* use old settings as starting */
|
||||
g_new.c_lflag &= ~ICANON; /* disable buffered I/O */
|
||||
g_new.c_lflag &= ~ECHO; /* disable ECHO bit */
|
||||
g_new.c_lflag |= echo ? ECHO : 0; /* set echo mode if requested */
|
||||
tcsetattr(0, TCSANOW, &g_new); /* apply terminal I/O settings */
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: deinit_termios
|
||||
*
|
||||
* Description:
|
||||
* Restore back g_old terminal I/O settings.
|
||||
*
|
||||
* Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* None.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void deinit_termios(void)
|
||||
{
|
||||
tcsetattr(0, TCSANOW, &g_old); /* restore old terminal i/o settings */
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: reset_termios
|
||||
*
|
||||
* Description:
|
||||
* Restore g_old terminal i/o settings.
|
||||
*
|
||||
* Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* None.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void reset_termios(void)
|
||||
{
|
||||
tcsetattr(0, TCSANOW, &g_old);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: getch_
|
||||
*
|
||||
* Description:
|
||||
* Read 1 character.
|
||||
*
|
||||
* Parameters:
|
||||
* echo - Enable echo mode flag
|
||||
*
|
||||
* Returned Value:
|
||||
* Read character.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
char getch_(int echo)
|
||||
{
|
||||
char ch;
|
||||
|
||||
init_termios(echo);
|
||||
ch = getchar();
|
||||
reset_termios();
|
||||
|
||||
return ch;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: getch
|
||||
*
|
||||
* Description:
|
||||
* Read 1 character without echo.
|
||||
*
|
||||
* Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* Read character.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
char getch(void)
|
||||
{
|
||||
return getch_(0);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dev_input_init
|
||||
*
|
||||
* Description:
|
||||
* Initialize input method.
|
||||
*
|
||||
* Parameters:
|
||||
* dev - Input state data
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int dev_input_init(FAR struct input_state_s *dev)
|
||||
{
|
||||
init_termios(0);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dev_input_deinit
|
||||
*
|
||||
* Description:
|
||||
* Deinitialize input method.
|
||||
*
|
||||
* Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int dev_input_deinit(void)
|
||||
{
|
||||
deinit_termios();
|
||||
|
||||
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)
|
||||
{
|
||||
char ch;
|
||||
|
||||
/* Arrows keys return three bytes: 27 91 [65-68] */
|
||||
|
||||
if ((ch = getch()) == ASCII_ESC)
|
||||
{
|
||||
if ((ch = getch()) == ASCII_LBRACKET)
|
||||
{
|
||||
ch = getch();
|
||||
if (ch == ASCII_B)
|
||||
{
|
||||
dev->dir = DIR_DOWN;
|
||||
}
|
||||
else if (ch == ASCII_C)
|
||||
{
|
||||
dev->dir = DIR_RIGHT;
|
||||
}
|
||||
else if (ch == ASCII_D)
|
||||
{
|
||||
dev->dir = DIR_LEFT;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dev->dir = DIR_NONE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dev->dir = DIR_NONE;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
194
games/match4/match4_input_gpio.h
Normal file
194
games/match4/match4_input_gpio.h
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
/****************************************************************************
|
||||
* apps/games/match4/match4_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 "match4_inputs.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Preprocessor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
struct gpio_struct_fd_s
|
||||
{
|
||||
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 down key gpio device */
|
||||
|
||||
fd_list.fd_down = open(CONFIG_GAMES_MATCH4_DOWN_KEY_PATH, O_RDONLY);
|
||||
if (fd_list.fd_down < 0)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Failed to open %s: %d\n",
|
||||
CONFIG_GAMES_MATCH4_DOWN_KEY_PATH, errno);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* Open the left key gpio device */
|
||||
|
||||
fd_list.fd_left = open(CONFIG_GAMES_MATCH4_LEFT_KEY_PATH, O_RDONLY);
|
||||
if (fd_list.fd_down < 0)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Failed to open %s: %d\n",
|
||||
CONFIG_GAMES_MATCH4_LEFT_KEY_PATH, errno);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
/* Open the right key gpio device */
|
||||
|
||||
fd_list.fd_right = open(CONFIG_GAMES_MATCH4_RIGHT_KEY_PATH, O_RDONLY);
|
||||
if (fd_list.fd_down < 0)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Failed to open %s: %d\n",
|
||||
CONFIG_GAMES_MATCH4_RIGHT_KEY_PATH, errno);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
dev->fd_gpio = (int)&fd_list;
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dev_input_deinit
|
||||
*
|
||||
* Description:
|
||||
* Deinitialize input method.
|
||||
*
|
||||
* Parameters:
|
||||
* None
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int dev_input_deinit(void)
|
||||
{
|
||||
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_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_MATCH4_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_MATCH4_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_MATCH4_RIGHT_KEY_PATH, errcode);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (invalue != 0)
|
||||
{
|
||||
dev->dir = DIR_RIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
60
games/match4/match4_inputs.h
Normal file
60
games/match4/match4_inputs.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/****************************************************************************
|
||||
* apps/games/match4/match4_inputs.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>
|
||||
|
||||
/****************************************************************************
|
||||
* Preprocessor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef DIR_NONE
|
||||
# define DIR_NONE 0
|
||||
#endif
|
||||
|
||||
#ifndef DIR_LEFT
|
||||
# define DIR_LEFT 1
|
||||
#endif
|
||||
|
||||
#ifndef DIR_RIGHT
|
||||
# define DIR_RIGHT 2
|
||||
#endif
|
||||
|
||||
#ifndef DIR_DOWN
|
||||
# define DIR_DOWN 4
|
||||
#endif
|
||||
|
||||
struct input_state_s
|
||||
{
|
||||
#ifdef CONFIG_GAMES_MATCH4_USE_CONSOLEKEY
|
||||
int fd_con;
|
||||
#endif
|
||||
#ifdef CONFIG_GAMES_MATCH4_USE_GPIO
|
||||
int fd_gpio;
|
||||
#endif
|
||||
|
||||
int dir; /* Direction to move the blocks */
|
||||
};
|
||||
|
||||
1024
games/match4/match4_main.c
Normal file
1024
games/match4/match4_main.c
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue