~dcow90/myro-c++/extern-c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "Myro.h"
#include <sys/time.h>
#include <time.h>
#include <cmath>
#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include <cstdlib>
#include <string>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Return_Button.H>
#include <vector>
#include <stdarg.h>
#include <boost/algorithm/string/trim.hpp>

static time_t start_time;
static bool running = false;

Scribbler robot;

void wait(double time) {
	int utime = (int)(time * pow(10,6));
	if(time != 0)
		usleep(utime);
}

bool timeRemaining(double desired_time) {	
	if(!running) {
		running = true;
        start_time = time(NULL);
		return true;
	}

    if ( difftime(time(NULL), start_time) >= desired_time ){
      running = false;
      return false;
    }
    return true;
}

double currentTime(){
    time_t theTime = time(NULL);
    return (double)theTime;
}

double randomNumber(){
    return (double)rand()/RAND_MAX;
}

void connect(std::string port){
	int status = robot.connect(port); 
	if(status < 0) { 
		exit(-1);
	}
    std::cout << "Connected to Robot" << std::endl;
}

void disconnect(){
    robot.stop();
	robot.disconnect();
}

static std::vector<std::string> tokenize_string(std::string str, 
                                 char seperator, bool trim_whitespace){
    std::string temp;
    std::vector<std::string> tokens; 
    for (int i = 0; i < (int)str.length(); i++){
        if ( str[i] == seperator && !temp.empty() ){
            if(trim_whitespace) boost::algorithm::trim(temp);
            tokens.push_back(temp);
            temp.clear();
        } else if ( str[i] != seperator ){
            temp += str[i];
        }
    }
    if ( !temp.empty() ){
        if(trim_whitespace) boost::algorithm::trim(temp);
        tokens.push_back(temp);
    }
    return tokens;
}

std::string askQuestion(std::string question, std::string answerString, 
                        char seperator, bool trim_whitespace){
    return askQuestionVector(question, 
                tokenize_string(answerString,seperator,trim_whitespace));
}

/*
std::string askQuestion(const char* question, ...){
    std::vector<const char*> answers;
    va_list list;
    va_start(list, question);
    const char* answer = va_arg(list,const char*);
    while ( answer != NULL ){
        answers.push_back(answer);
        answer = va_arg(list,const char*);
    }
    va_end(list);
    return askQuestionVector(question,answers);
}
*/

std::string askQuestionVector(std::string question, 
                              std::vector<std::string> answers){
    using namespace std;
    while(true){
        string answer;
        cout << "Myro Question: " << question << endl;
        cout << "Valid Answers: " << endl;
        for (unsigned int i = 0; i < answers.size(); i++){
            cout << "   " << answers[i] << endl;
        }
        getline(cin,answer);
        //cin.ignore();
        for (unsigned int i = 0; i < answers.size(); i++){
            if (answer == answers[i])
                return answer;
        }
        cout << "Invalid Selection" << endl;
    }
}