-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.cpp
149 lines (122 loc) · 3.08 KB
/
app.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include "serial_communication.h"
#include "console.h"
#include "commands.h"
#include "utils.h"
#define DEBUG
#define SHOW_NUMBER_AUTO -1 //show Arduino numbers auto
Serial *serial;
bool test_serial(Serial* serial);
bool test_console();
void print_logo();
bool parse_command();
void show_number(int number);
bool test_serial(Serial* serial)
{
std::cout << "run test_serial" << std::endl;
// for(;;)
// {
// char *received_message = serial->Read(100);
// std::cout << "receiving message from serial: " << received_message << std::endl;
// }
std::cout << "end test_serial" << std::endl;
return true;
}
bool test_console()
{
std::cout << "start test_console" << std::endl;
Console::info("testing console->info");
Console::error("testing console->error");
std::cout << "end test_console" << std::endl;
return true;
}
void print_logo()
{
const char *logo =
" ____ _ _ \n"
"| _ \ __ _ ___ __| |_ _(_)_ __ ___ \n"
"| |_) / _` / __|/ _` | | | | | '_ \ / _ \ \n"
"| _ < (_| \__ \ (_| | |_| | | | | | (_) |\n"
"|_| \_\__,_|___/\__,_|\__,_|_|_| |_|\___/ \n"
" \n";
Console::println(logo);
}
bool parse_command()
{
char *commands[2] = { new char, new char };
std::cin >> commands[0];
std::cin >> commands[1];
//std::cout << "command: " << command << std::endl;
//std::cout << "command len: " << sizeof(command) << std::endl;
if(strcmp(commands[0], CMD_SHOW_NUMBER) == 0)
{
if(strcmp(commands[1], ARG_AUTO) == 0)
{
show_number(SHOW_NUMBER_AUTO);
}
else
{
show_number(str_to_int(commands[1]));
}
}
return true;
}
void show_number(int number)
{
byte message_to_send[5] = {
0x00, 0xFF, 0xFF, 0xFF, (byte)'\n'
};
if(number == SHOW_NUMBER_AUTO)
{
message_to_send[1] = 0x01;
}
else
{
message_to_send[1] = 0x00;
}
message_to_send[3] = (byte)number;
serial->Write(message_to_send);
// Console::info("sending message to serial:");
// char convert_buffer[100];
// for(int i(0); i < 4; ++i)
// {
// sprintf(convert_buffer, "%x", message_to_send[i]);
// std::cout << convert_buffer << std::endl;
// }
// std::cout << std::endl;
}
int main()
{
#ifdef DEBUG
std::cout << "run app" << std::endl;
#endif
print_logo();
serial = new Serial();
const char *port_name = "/dev/ttyACM0";
serial->Open(port_name);
#ifdef DEBUG
if(!test_serial(serial))
{
std::cout << ConsoleColor::red << "test_serial failed" << std::endl;
return -1;
}
if(!test_console())
{
std::cout << ConsoleColor::red << "test_console failed" << std::endl;
return -1;
}
#endif
for(;;)
{
Console::print("command>");
if(!parse_command())
{
break;
}
}
serial->Close();
delete serial;
serial = NULL;
Console::debug("end app");
return 0;
}