Skip to content

Commit 8b121eb

Browse files
eren-terziogluacassis
authored andcommitted
games/match4: Add new match4 game
Add match4 game support Signed-off-by: Eren Terzioglu <eren.terzioglu@espressif.com>
1 parent 0c680bc commit 8b121eb

File tree

7 files changed

+1662
-0
lines changed

7 files changed

+1662
-0
lines changed

games/match4/Kconfig

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config GAMES_MATCH4
7+
bool "Match 4 Game"
8+
default n
9+
---help---
10+
Enable Match 4 game.
11+
12+
if GAMES_MATCH4
13+
14+
config GAMES_MATCH4_PROGNAME
15+
string "Program name"
16+
default "match"
17+
---help---
18+
This is the name of the program that will be used when the NSH ELF
19+
program is installed.
20+
21+
config GAMES_MATCH4_PRIORITY
22+
int "Match 4 Game task priority"
23+
default 100
24+
25+
config GAMES_MATCH4_STACKSIZE
26+
int "Match 4 Game stack size"
27+
default DEFAULT_TASK_STACKSIZE
28+
29+
config MATCH4_GAME_DEBUG
30+
bool "Print board status to the serial console for debugging"
31+
default n
32+
33+
config GAMES_MATCH4_LED_MATRIX_PATH
34+
string "LED matrix path"
35+
default "/dev/leds0"
36+
---help---
37+
Path of the led matrix
38+
39+
config GAMES_MATCH4_LED_MATRIX_ROWS
40+
int "LED Matrix row count"
41+
default 8
42+
43+
config GAMES_MATCH4_LED_MATRIX_COLS
44+
int "LED Matrix column count"
45+
default 8
46+
47+
#
48+
# Input Device Selection
49+
#
50+
51+
choice
52+
prompt "Input Device (Serial Console, GPIO, etc)"
53+
default GAMES_MATCH4_USE_CONSOLEKEY
54+
55+
config GAMES_MATCH4_USE_CONSOLEKEY
56+
bool "Serial Console as Input"
57+
58+
config GAMES_MATCH4_USE_GPIO
59+
bool "GPIO pins as Input"
60+
endchoice
61+
62+
if GAMES_MATCH4_USE_GPIO
63+
64+
config GAMES_MATCH4_DOWN_KEY_PATH
65+
string "Down key path"
66+
default "/dev/gpio1"
67+
---help---
68+
Path of the down key to read
69+
70+
config GAMES_MATCH4_LEFT_KEY_PATH
71+
string "Left key path"
72+
default "/dev/gpio2"
73+
---help---
74+
Path of the left key to read
75+
76+
config GAMES_MATCH4_RIGHT_KEY_PATH
77+
string "Right key path"
78+
default "/dev/gpio3"
79+
---help---
80+
Path of the right key to read
81+
82+
endif #GAMES_MATCH4_USE_GPIO
83+
84+
endif

games/match4/Make.defs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
############################################################################
2+
# apps/games/match4/Make.defs
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
ifneq ($(CONFIG_GAMES_MATCH4),)
24+
CONFIGURED_APPS += $(APPDIR)/games/match4
25+
endif

games/match4/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
############################################################################
2+
# apps/games/match4/Makefile
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
include $(APPDIR)/Make.defs
24+
25+
# Match4 game info
26+
27+
PROGNAME = $(CONFIG_GAMES_MATCH4_PROGNAME)
28+
PRIORITY = $(CONFIG_GAMES_MATCH4_PRIORITY)
29+
STACKSIZE = $(CONFIG_GAMES_MATCH4_STACKSIZE)
30+
MODULE = $(CONFIG_GAMES_MATCH4)
31+
32+
# Match4 game application
33+
34+
MAINSRC = match4_main.c
35+
36+
include $(APPDIR)/Application.mk

games/match4/match4_input_console.h

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/****************************************************************************
2+
* apps/games/match4/match4_input_console.h
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
#include <nuttx/ascii.h>
29+
#include <termios.h>
30+
31+
#include "match4_inputs.h"
32+
33+
/****************************************************************************
34+
* Preprocessor Definitions
35+
****************************************************************************/
36+
37+
/* Termios functions to have getch on Linux/NuttX */
38+
39+
static struct termios g_old;
40+
static struct termios g_new;
41+
42+
/****************************************************************************
43+
* Name: init_termios
44+
*
45+
* Description:
46+
* Initialize g_new terminal I/O settings.
47+
*
48+
* Parameters:
49+
* echo - Enable echo flag
50+
*
51+
* Returned Value:
52+
* None.
53+
*
54+
****************************************************************************/
55+
56+
void init_termios(int echo)
57+
{
58+
tcgetattr(0, &g_old); /* grab old terminal i/o settings */
59+
g_new = g_old; /* use old settings as starting */
60+
g_new.c_lflag &= ~ICANON; /* disable buffered I/O */
61+
g_new.c_lflag &= ~ECHO; /* disable ECHO bit */
62+
g_new.c_lflag |= echo ? ECHO : 0; /* set echo mode if requested */
63+
tcsetattr(0, TCSANOW, &g_new); /* apply terminal I/O settings */
64+
}
65+
66+
/****************************************************************************
67+
* Name: deinit_termios
68+
*
69+
* Description:
70+
* Restore back g_old terminal I/O settings.
71+
*
72+
* Parameters:
73+
* None
74+
*
75+
* Returned Value:
76+
* None.
77+
*
78+
****************************************************************************/
79+
80+
void deinit_termios(void)
81+
{
82+
tcsetattr(0, TCSANOW, &g_old); /* restore old terminal i/o settings */
83+
}
84+
85+
/****************************************************************************
86+
* Name: reset_termios
87+
*
88+
* Description:
89+
* Restore g_old terminal i/o settings.
90+
*
91+
* Parameters:
92+
* None
93+
*
94+
* Returned Value:
95+
* None.
96+
*
97+
****************************************************************************/
98+
99+
void reset_termios(void)
100+
{
101+
tcsetattr(0, TCSANOW, &g_old);
102+
}
103+
104+
/****************************************************************************
105+
* Name: getch_
106+
*
107+
* Description:
108+
* Read 1 character.
109+
*
110+
* Parameters:
111+
* echo - Enable echo mode flag
112+
*
113+
* Returned Value:
114+
* Read character.
115+
*
116+
****************************************************************************/
117+
118+
char getch_(int echo)
119+
{
120+
char ch;
121+
122+
init_termios(echo);
123+
ch = getchar();
124+
reset_termios();
125+
126+
return ch;
127+
}
128+
129+
/****************************************************************************
130+
* Name: getch
131+
*
132+
* Description:
133+
* Read 1 character without echo.
134+
*
135+
* Parameters:
136+
* None
137+
*
138+
* Returned Value:
139+
* Read character.
140+
*
141+
****************************************************************************/
142+
143+
char getch(void)
144+
{
145+
return getch_(0);
146+
}
147+
148+
/****************************************************************************
149+
* Name: dev_input_init
150+
*
151+
* Description:
152+
* Initialize input method.
153+
*
154+
* Parameters:
155+
* dev - Input state data
156+
*
157+
* Returned Value:
158+
* Zero (OK)
159+
*
160+
****************************************************************************/
161+
162+
int dev_input_init(FAR struct input_state_s *dev)
163+
{
164+
init_termios(0);
165+
166+
return OK;
167+
}
168+
169+
/****************************************************************************
170+
* Name: dev_input_deinit
171+
*
172+
* Description:
173+
* Deinitialize input method.
174+
*
175+
* Parameters:
176+
* None
177+
*
178+
* Returned Value:
179+
* Zero (OK)
180+
*
181+
****************************************************************************/
182+
183+
int dev_input_deinit(void)
184+
{
185+
deinit_termios();
186+
187+
return OK;
188+
}
189+
190+
/****************************************************************************
191+
* Name: dev_read_input
192+
*
193+
* Description:
194+
* Read inputs and returns result in input state data.
195+
*
196+
* Parameters:
197+
* dev - Input state data
198+
*
199+
* Returned Value:
200+
* Zero (OK)
201+
*
202+
****************************************************************************/
203+
204+
int dev_read_input(FAR struct input_state_s *dev)
205+
{
206+
char ch;
207+
208+
/* Arrows keys return three bytes: 27 91 [65-68] */
209+
210+
if ((ch = getch()) == ASCII_ESC)
211+
{
212+
if ((ch = getch()) == ASCII_LBRACKET)
213+
{
214+
ch = getch();
215+
if (ch == ASCII_B)
216+
{
217+
dev->dir = DIR_DOWN;
218+
}
219+
else if (ch == ASCII_C)
220+
{
221+
dev->dir = DIR_RIGHT;
222+
}
223+
else if (ch == ASCII_D)
224+
{
225+
dev->dir = DIR_LEFT;
226+
}
227+
}
228+
else
229+
{
230+
dev->dir = DIR_NONE;
231+
}
232+
}
233+
else
234+
{
235+
dev->dir = DIR_NONE;
236+
}
237+
238+
return OK;
239+
}

0 commit comments

Comments
 (0)