Alex exoskeleton
ALEX SoftwareDocumentation
Keyboard.cpp
Go to the documentation of this file.
1 
2 #include "Keyboard.h"
3 
5  std::cout << "Keyboard object created, echo disabled" << std::endl;
8  /* obtain the current terminal configuration */
9  tcgetattr(STDIN_FILENO, &original);
10  /* duplicate it */
11  noecho = original;
12  /* turn off full duplex */
13  noecho.c_lflag = noecho.c_lflag ^ ECHO;
14  /* set the terminal */
15  tcsetattr(STDIN_FILENO, TCSANOW, &noecho);
16 }
18  /* restore the terminal settings */
19  std::cout << "Keyboard object deleted, echo enabled" << std::endl;
20  tcsetattr(STDIN_FILENO, TCSANOW, &original);
21 };
23  // usleep(1);
26  if (getKeyboardActive() != 0) {
27  setKeys();
28  printPressed();
29  }
30 }
33  char ch = fgetc(stdin);
34  /* Set States, limited to one key Press at a time*/
35 
36  switch (ch) {
37  // TO do add this case when timeout of read occurs
38  //case ERR:
39  // //do nothing, just capture and keep runnin
40  // printf("No input from keyboard\n");
41  case 'a':
42  case 'A':
43  currentKeyStates.a = true;
44  break;
45  case 's':
46  case 'S':
47  currentKeyStates.s = true;
48  break;
49  case 'd':
50  case 'D':
51  currentKeyStates.d = true;
52  break;
53  case 'w':
54  case 'W':
55  currentKeyStates.w = true;
56  break;
57  case 'x':
58  case 'X':
59  currentKeyStates.x = true;
60  break;
61  case 'q':
62  case 'Q':
63  currentKeyStates.q = true;
64  std::cout << std::endl
65  << "Q PRESSED, EXITING PROGRAM "
66  << std::endl;
67  break;
68  default:
69  keyboardActive = 0;
70  }
71 }
73  key_states current_state = {this->currentKeyStates.a, this->currentKeyStates.s,
75  return current_state;
76 };
78  if (getA()) {
79  std::cout
80  << "PRESSED A " << std::endl;
81  }
82  if (getS()) {
83  std::cout
84  << "PRESSED S " << std::endl;
85  }
86  if (getD()) {
87  std::cout
88  << "PRESSED D " << std::endl;
89  }
90  if (getW()) {
91  std::cout
92  << "PRESSED W " << std::endl;
93  }
94  if (getX()) {
95  std::cout
96  << "PRESSED X " << std::endl;
97  }
98 }
100  currentKeyStates.a = false;
101  currentKeyStates.s = false;
102  currentKeyStates.d = false;
103  currentKeyStates.w = false;
104  currentKeyStates.x = false;
105 }
107  return currentKeyStates.a;
108 };
109 
111  return currentKeyStates.s;
112 };
114  return currentKeyStates.d;
115 };
117  return currentKeyStates.w;
118 };
120  return currentKeyStates.x;
121 };
123  return currentKeyStates.q;
124 };
126  struct timeval tv;
127  fd_set fds;
128  tv.tv_sec = 0;
129  tv.tv_usec = 0;
130  FD_ZERO(&fds);
131  FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
132  select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
133  return FD_ISSET(STDIN_FILENO, &fds);
134 }
135 void Keyboard::nonblock(int state) {
136  struct termios ttystate;
137 
138  //get the terminal state
139  tcgetattr(STDIN_FILENO, &ttystate);
140 
141  if (state == NB_ENABLE) {
142  //turn off canonical mode
143  ttystate.c_lflag &= ~ICANON;
144  //minimum of number input read.
145  ttystate.c_cc[VMIN] = 1;
146  } else if (state == NB_DISABLE) {
147  //turn on canonical mode
148  ttystate.c_lflag |= ICANON;
149  }
150  //set the terminal attributes.
151  tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
152 }
154  return keyboardActive;
155 };
157  keyboardActive = value;
158 };
void setKeys()
reads one character from stdin and updates coresponding key state (if one occured) ...
Definition: Keyboard.cpp:31
char ch
Definition: Keyboard.h:114
key_states getStates()
getter method for key_states.
Definition: Keyboard.cpp:72
void updateInput()
defintion of <class>Input</class> pure virtual function. Updates the keyboard input devices memory st...
Definition: Keyboard.cpp:22
bool getS()
Getter method for private S key state.
Definition: Keyboard.cpp:110
~Keyboard()
Definition: Keyboard.cpp:17
bool getA()
Getter method for private A key state.
Definition: Keyboard.cpp:106
bool q
Definition: Keyboard.h:29
Keyboard()
Construct a new keyboard object.
Definition: Keyboard.cpp:4
#define NB_DISABLE
Definition: Keyboard.h:16
bool getD()
Getter method for private D key state.
Definition: Keyboard.cpp:113
bool getX()
Getter method for private X key state.
Definition: Keyboard.cpp:119
void clearCurrentStates()
clear the current key state variables
Definition: Keyboard.cpp:99
void printPressed()
Helper method, prints current keys registered as pressed.
Definition: Keyboard.cpp:77
int kbhit()
Check if keyboard has been hit - is stdin active.
Definition: Keyboard.cpp:125
void setKeyboardActive(int value)
setter method for keyboard active flag, set by kbhit function.
Definition: Keyboard.cpp:156
bool d
Definition: Keyboard.h:26
struct termios original noecho
Termios structs for turning on and off terminal echo.
Definition: Keyboard.h:113
int keyboardActive
Definition: Keyboard.h:40
void nonblock(int state)
Configure stdin to be nonblocking to rest of program.
Definition: Keyboard.cpp:135
bool w
Definition: Keyboard.h:27
bool x
Definition: Keyboard.h:28
bool getQ()
Getter method for private Q key state.
Definition: Keyboard.cpp:122
int getKeyboardActive()
getter method for keyboard active flag, set by kbhit function.
Definition: Keyboard.cpp:153
Struct listing the Keys which exist on a Keyboard.
Definition: Keyboard.h:23
bool getW()
Getter method for private W key state.
Definition: Keyboard.cpp:116
bool a
Definition: Keyboard.h:24
bool s
Definition: Keyboard.h:25
#define NB_ENABLE
Definition: Keyboard.h:17
key_states currentKeyStates
Definition: Keyboard.h:39