My Elma Project
An event loop manager for embedded systems
 All Classes Functions
user_interface.cc
1 #include <unistd.h>
2 #include "stopwatch.h"
3 
4 using namespace stopwatch;
5 
6 UserInterface::UserInterface(StopWatch& sw) : Process("user input"), _stopwatch(sw) {
7  initscr(); // Start ncurses
8  timeout(1); // Timeout for waiting for user input
9  noecho(); // Do not echo user input to the screen
10  curs_set(0); // Do not show the cursor
11 };
12 
13 void UserInterface::show_time(int x, int y, high_resolution_clock::duration d) {
14 
15  // Print the time at the desired position.
16  // mvprintw just calls sprintf
17  mvprintw(x,y,"%d:%02d:%02d",
18  std::chrono::duration_cast<std::chrono::minutes>(d).count(),
19  std::chrono::duration_cast<std::chrono::seconds>(d).count()%60,
20  (std::chrono::duration_cast<std::chrono::milliseconds>(d).count()%1000)/10
21  );
22 }
23 
25 
26  // USER INPUT
27  // get a character from the user, if one is ready.
28  // If no character is ready, getch() returns ERR (which is
29  // ignored below since there is no condition in the switch statement)
30  int c = getch();
31 
32  switch ( c ) {
33  case 's':
34  emit(Event("start/stop"));
35  break;
36  case 'r':
37  emit(Event("reset"));
38  clear(); // Clear the screen of old stuff
39  break;
40  case 'l':
41  emit(Event("lap"));
42  break;
43  case 'q':
44  std::cout << "halting\n";
45  halt();
46  break;
47  }
48 
49  // OUTPUT
50  show_time(1,1,_stopwatch.value());
51  mvprintw(3,1,"start/stop(s), lap(l), reset(r), quit(q)");
52  for ( int i=0; i<_stopwatch.laps().size(); i++ ) {
53  mvprintw(5+i, 1, "Lap %d", _stopwatch.laps().size()-i);
54  show_time(5+i, 10, _stopwatch.laps()[i]);
55  }
56 
57  // NOTE: Since the stopwatch is running every 10 ms, we should sleep
58  // the ui to give processing time back to the OS. It is debatable
59  // whether this is the right place to put this. It could also become
60  // an Elma feature, or it could go in the StopWatch class, etc.
61  // The number 9999 should also be a parameter and not a constant.
62  usleep(9999);
63 
64 }
const vector< high_resolution_clock::duration > & laps()
Get a list of lap times.
Definition: stopwatch.h:38
Definition: off.h:6
void show_time(int x, int y, high_resolution_clock::duration d)
Display the time at the given x,y position on the screen.
UserInterface(StopWatch &sw)
void update()
Update the user interface by (a) reading keyboard input and (b) writing to the screen.
high_resolution_clock::duration value()
Get the time stored by the stopwatch.
Definition: stopwatch.cc:24
A stop watch class, that inherits from StateMachine.
Definition: stopwatch.h:16