Skip to content

games/snake: Fix terminal issue after returning back from snake game #3126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
34 changes: 28 additions & 6 deletions games/snake/snake_input_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
****************************************************************************/

#include <nuttx/config.h>
#include <nuttx/ascii.h>
#include <termios.h>

#include "snake_inputs.h"
Expand Down Expand Up @@ -147,6 +148,27 @@ int dev_input_init(FAR struct input_state_s *dev)
return OK;
}

/****************************************************************************
* Name: dev_input_deinit
*
* Description:
* Deinitialize input method.
*
* Parameters:
* None
*
* Returned Value:
* Zero (OK)
*
****************************************************************************/

int dev_input_deinit(void)
{
reset_termios();

return OK;
}

/****************************************************************************
* Name: dev_read_input
*
Expand All @@ -167,24 +189,24 @@ int dev_read_input(FAR struct input_state_s *dev)

/* Arrows keys return three bytes: 27 91 [65-68] */

if ((ch = getch()) == 27)
if ((ch = getch()) == ASCII_ESC)
{
if ((ch = getch()) == 91)
if ((ch = getch()) == ASCII_LBRACKET)
{
ch = getch();
if (ch == 65)
if (ch == ASCII_A)
{
dev->dir = DIR_UP;
}
else if (ch == 66)
else if (ch == ASCII_B)
{
dev->dir = DIR_DOWN;
}
else if (ch == 67)
else if (ch == ASCII_C)
{
dev->dir = DIR_RIGHT;
}
else if (ch == 68)
else if (ch == ASCII_D)
{
dev->dir = DIR_LEFT;
}
Expand Down
19 changes: 19 additions & 0 deletions games/snake/snake_input_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ int dev_input_init(FAR struct input_state_s *dev)
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
*
Expand Down
1 change: 1 addition & 0 deletions games/snake/snake_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,5 +737,6 @@ int main(int argc, FAR char *argv[])
goto restart;
}

dev_input_deinit();
return 0;
}
Loading