You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/07/29 12:29:01 UTC

[GitHub] [incubator-nuttx-apps] acassis commented on a diff in pull request #1238: apps/games: Add shift game

acassis commented on code in PR #1238:
URL: https://github.com/apache/incubator-nuttx-apps/pull/1238#discussion_r933192461


##########
games/shift/shift_input_console.h:
##########
@@ -0,0 +1,133 @@
+/****************************************************************************
+ * apps/games/shift/shift_input_console.h
+ *
+ * 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 <termios.h>
+
+#include "shift_inputs.h"
+
+/****************************************************************************
+ * Preprocessor Definitions
+ ****************************************************************************/
+
+/* Termios functions to have getch on Linux/NuttX */
+
+static struct termios g_old;
+static struct termios g_new;
+
+/* Initialize g_new terminal I/O settings */
+
+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 ? ECHO : ~ECHO; /* set echo mode */

Review Comment:
   you are right, it only works to disable the echo, it needs to be broken in two parts:
   ```
   g_new.c_lflag &= ~ECHO; /* clear ECHO bit */
   g_new.c_lflag |= echo ? ECHO : 0;  /* enable echo if requested */
   ```



##########
games/shift/shift_input_console.h:
##########
@@ -0,0 +1,133 @@
+/****************************************************************************
+ * apps/games/shift/shift_input_console.h
+ *
+ * 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 <termios.h>
+
+#include "shift_inputs.h"
+
+/****************************************************************************
+ * Preprocessor Definitions
+ ****************************************************************************/
+
+/* Termios functions to have getch on Linux/NuttX */
+
+static struct termios g_old;
+static struct termios g_new;
+
+/* Initialize g_new terminal I/O settings */
+
+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 ? ECHO : ~ECHO; /* set echo mode */
+  tcsetattr(0, TCSANOW, &g_new);        /* apply terminal I/O settings */
+}
+
+/* Restore g_old terminal i/o settings */
+
+void reset_termios(void)
+{
+  tcsetattr(0, TCSANOW, &g_old);
+}
+
+/* Read 1 character - echo defines echo mode */
+
+char getch_(int echo)
+{
+    char ch;
+    init_termios(echo);
+    ch = getchar();
+    reset_termios();
+    return ch;

Review Comment:
   Thanks



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org