LMS 2012
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
d_terminal.c
Go to the documentation of this file.
1 /*
2  * LEGO® MINDSTORMS EV3
3  *
4  * Copyright (C) 2010-2013 The LEGO Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 
22 #include "lms2012.h"
23 
24 #if (HARDWARE != SIMULATION)
25 
26 #include <stdio.h>
27 #include <termios.h>
28 #include <unistd.h>
29 
30 #ifndef STDIN_FILENO
31  #define STDIN_FILENO 0
32 #endif
33 
34 #ifndef STDOUT_FILENO
35  #define STDOUT_FILENO 1
36 #endif
37 
38 struct termios TerminalAttr;
39 struct termios TerminalSavedAttr;
40 
41 RESULT TerminalResult = FAIL;
42 
43 
44 RESULT dTerminalInit(void)
45 {
46  RESULT Result = FAIL;
47 
48  if (tcgetattr(STDIN_FILENO,&TerminalAttr) >= 0)
49  {
50  TerminalSavedAttr = TerminalAttr;
51 
52  TerminalAttr.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
53  TerminalAttr.c_lflag |= ECHO;
54  TerminalAttr.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
55  TerminalAttr.c_cflag &= ~(CSIZE | PARENB);
56  TerminalAttr.c_cflag |= CS8;
57  TerminalAttr.c_oflag &= ~(OPOST);
58 
59  TerminalAttr.c_cc[VMIN] = 0;
60  TerminalAttr.c_cc[VTIME] = 0;
61 
62  if (tcsetattr(STDIN_FILENO,TCSANOW,&TerminalAttr) >= 0)
63  {
64  Result = OK;
65  }
66  }
67  TerminalResult = Result;
68 
69  return (Result);
70 }
71 
72 
73 RESULT dTerminalRead(UBYTE *pData)
74 {
75 #ifdef DEBUG_TRACE_KEY
76  static int OldTmp = 1;
77 #endif
78  RESULT Result = FAIL;
79  int Tmp;
80 
81  if (TerminalResult == OK)
82  {
83  Result = BUSY;
84 
85  Tmp = read(STDIN_FILENO,pData,1);
86  if (Tmp == 1)
87  {
88  Result = OK;
89 #ifdef DEBUG_TRACE_KEY
90  printf("[%c]",(char)*pData);
91 #endif
92 
93  }
94 #ifdef DEBUG_TRACE_KEY
95  else
96  {
97  if (Tmp != OldTmp)
98  {
99  printf("{%d}",Tmp);
100  }
101  }
102  OldTmp = Tmp;
103 #endif
104  }
105 
106  return (Result);
107 }
108 
109 
110 RESULT dTerminalWrite(UBYTE *pData,UWORD Cnt)
111 {
112  if (TerminalResult == OK)
113  {
114  if (write(STDOUT_FILENO,pData,(size_t)Cnt) != Cnt)
115  {
116  TerminalResult = FAIL;
117  }
118  }
119 
120  return (OK);
121 }
122 
123 
124 RESULT dTerminalExit(void)
125 {
126  if (TerminalResult == OK)
127  {
128  tcsetattr(STDIN_FILENO,TCSAFLUSH,&TerminalSavedAttr);
129  }
130  TerminalResult = FAIL;
131 
132  return (OK);
133 }
134 
135 #else
136 
137 RESULT dTerminalInit(void)
138 {
139  return (OK);
140 }
141 
142 
143 RESULT dTerminalRead(UBYTE *pData)
144 {
145  return (FAIL);
146 }
147 
148 
149 RESULT dTerminalWrite(UBYTE *pData,UWORD Cnt)
150 {
151  return (OK);
152 }
153 
154 
155 RESULT dTerminalExit(void)
156 {
157  return (OK);
158 }
159 
160 
161 #endif
RESULT dTerminalExit(void)
Definition: d_terminal.c:155
RESULT dTerminalWrite(UBYTE *pData, UWORD Cnt)
Definition: d_terminal.c:149
RESULT dTerminalRead(UBYTE *pData)
Definition: d_terminal.c:143
unsigned char UBYTE
Basic Type used to symbolise 8 bit unsigned values.
Definition: lmstypes.h:29
unsigned short UWORD
Basic Type used to symbolise 16 bit unsigned values.
Definition: lmstypes.h:30
RESULT dTerminalInit(void)
Definition: d_terminal.c:137