Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion games/brickmatch/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#

config GAMES_BRICKMATCH
bool "Brickmatch Game"
tristate "Brickmatch Game"
default n
---help---
Enable Brickmatch games. Brickmatch is like a mix
Expand Down Expand Up @@ -88,6 +88,10 @@ config GAMES_BRICKMATCH_USE_GESTURE

config GAMES_BRICKMATCH_USE_GPIO
bool "GPIO pins as Input"

config GAMES_BRICKMATCH_USE_TOUCHSCREEN
bool "Touchscreen swipe as input"
depends on INPUT_TOUCHSCREEN
endchoice

if GAMES_BRICKMATCH_USE_GPIO
Expand Down Expand Up @@ -118,4 +122,34 @@ config GAMES_BRICKMATCH_RIGHT_KEY_PATH

endif #GAMES_BRICKMATCH_USE_GPIO

if GAMES_BRICKMATCH_USE_TOUCHSCREEN

config GAMES_BRICKMATCH_TOUCH_DEVPATH
string "Touchscreen device path"
default "/dev/input0"
---help---
Path of the touchscreen device to read swipe gestures from.

config GAMES_BRICKMATCH_TOUCH_THRESHOLD
int "Minimum swipe distance"
range 1 32767
default 24
---help---
Minimum movement in calibrated touchscreen coordinate units before
a drag is reported as a direction.

endif #GAMES_BRICKMATCH_USE_TOUCHSCREEN

if !GAMES_BRICKMATCH_USE_LED_MATRIX

config GAMES_BRICKMATCH_YOFFSET
int "Top framebuffer rows to reserve"
range 0 500000
default 0
---help---
Leave this many rows at the top of the framebuffer unchanged. This
allows a launcher or supervisor to reserve a status or control bar.

endif # !GAMES_BRICKMATCH_USE_LED_MATRIX

endif
173 changes: 173 additions & 0 deletions games/brickmatch/bm_input_touch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/****************************************************************************
* apps/games/brickmatch/bm_input_touch.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.
*
****************************************************************************/

#ifndef __APPS_GAMES_BRICKMATCH_BM_INPUT_TOUCH_H
#define __APPS_GAMES_BRICKMATCH_BM_INPUT_TOUCH_H

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <nuttx/input/touchscreen.h>

#include "bm_inputs.h"

/****************************************************************************
* Preprocessor Definitions
****************************************************************************/

/* Touch origin for the current swipe. */
Comment thread
xiaoxiang781216 marked this conversation as resolved.

static bool g_bm_touch_active;
static int32_t g_bm_touch_origin_x;
static int32_t g_bm_touch_origin_y;

/****************************************************************************
* 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)
{
dev->fd_touch = open(CONFIG_GAMES_BRICKMATCH_TOUCH_DEVPATH,
O_RDONLY | O_NONBLOCK);
if (dev->fd_touch < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: Failed to open %s: %d\n",
CONFIG_GAMES_BRICKMATCH_TOUCH_DEVPATH, errcode);
return -errcode;
}

g_bm_touch_active = false;

return OK;
}

/****************************************************************************
* Name: dev_read_input
*
* Description:
* Convert a touchscreen swipe to a game direction.
*
* Parameters:
* dev - Input state data
*
* Returned Value:
* Zero (OK)
*
****************************************************************************/

int dev_read_input(FAR struct input_state_s *dev)
{
struct touch_sample_s sample;
int32_t dx;
int32_t dy;
ssize_t nbytes;

dev->dir = DIR_NONE;

nbytes = read(dev->fd_touch, &sample, sizeof(sample));
if (nbytes < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
usleep(10000);
return OK;
}

return -errno;
}

if (nbytes != sizeof(sample) || sample.npoints < 1)
{
return OK;
}

if (sample.point[0].flags & TOUCH_UP)
{
g_bm_touch_active = false;
return OK;
}

if ((sample.point[0].flags & TOUCH_POS_VALID) == 0)
{
return OK;
}

if (sample.point[0].flags & TOUCH_DOWN)
{
g_bm_touch_active = true;
g_bm_touch_origin_x = sample.point[0].x;
g_bm_touch_origin_y = sample.point[0].y;
return OK;
}

if (!g_bm_touch_active)
{
return OK;
}

dx = sample.point[0].x - g_bm_touch_origin_x;
dy = sample.point[0].y - g_bm_touch_origin_y;

if (abs(dx) < CONFIG_GAMES_BRICKMATCH_TOUCH_THRESHOLD &&
abs(dy) < CONFIG_GAMES_BRICKMATCH_TOUCH_THRESHOLD)
{
return OK;
}

if (abs(dx) > abs(dy))
{
dev->dir = dx > 0 ? DIR_RIGHT : DIR_LEFT;
}
else
{
dev->dir = dy > 0 ? DIR_DOWN : DIR_UP;
}

g_bm_touch_origin_x = sample.point[0].x;
g_bm_touch_origin_y = sample.point[0].y;

return OK;
}

#endif /* __APPS_GAMES_BRICKMATCH_BM_INPUT_TOUCH_H */
3 changes: 3 additions & 0 deletions games/brickmatch/bm_inputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ struct input_state_s
#ifdef CONFIG_GAMES_BRICKMATCH_USE_GPIO
int fd_gpio;
#endif
#ifdef CONFIG_GAMES_BRICKMATCH_USE_TOUCHSCREEN
int fd_touch;
#endif

int dir; /* Direction to move the blocks */
};
Expand Down
Loading
Loading