SerialLight.h 2.92 KB
Newer Older
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
//    Copyright (C) 2018, ETH Zurich, D-ITET
//          Martin Reinhard         remartin       @ee.ethz.ch
//
//    This file is part of D-FaLL-System.
//    
//    D-FaLL-System is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//    
//    D-FaLL-System is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//    
//    You should have received a copy of the GNU General Public License
//    along with D-FaLL-System.  If not, see <http://www.gnu.org/licenses/>.
//    
//
//    ----------------------------------------------------------------------------------
//    DDDD        FFFFF        L     L           SSSS  Y   Y   SSSS  TTTTT  EEEEE  M   M
//    D   D       F      aaa   L     L          S       Y Y   S        T    E      MM MM
//    D   D  ---  FFFF  a   a  L     L     ---   SSS     Y     SSS     T    EEE    M M M
//    D   D       F     a  aa  L     L              S    Y        S    T    E      M   M
//    DDDD        F      aa a  LLLL  LLLL       SSSS     Y    SSSS     T    EEEEE  M   M
//
//
//    DESCRIPTION:
//	  Handles the connection to the Arduino via a serial port. 
//    Based on previously existing SerialUnix file. 
//
//    ----------------------------------------------------------------------------------


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once


#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h> //Timer
#include <fcntl.h> //Device settings
#include <termios.h> //For serial comm
#include <sys/ioctl.h> //For ioctl
#include <string.h> // needed for memset

#include <string>
#include <vector>
#include <ctime>
50
#include <fstream>
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74


//SERIAL COMMUNICATION PARAMETERS
#define MAX_SERIAL_BUFFER_SIZE 180
#define SERIAL_LIGHT_BAUD_RATE B9600

#define SERIAL_BYTE_SIZE 8
#define SERIAL_STOP_BITS 0
#define SERIAL_PARITY 0
#define SERIAL_COMMAND_END '\n'
#define SERIAL_READY_FLAG "-7\n"
#define SERIAL_FLAG_TIMEOUT 1000

#define K_WARNING  "\x1B[33m"
#define K_ERROR  "\x1B[31m"
#define K_SUCCESS  "\x1B[32m"
#define K_NORMAL  "\x1B[0m"
#define KBLU  "\x1B[34m"
#define KMAG  "\x1B[35m"
#define KCYN  "\x1B[36m"
#define KWHT  "\x1B[37m"



75
76
class SerialLight{
	
77
78
public:
	SerialLight();
79
80
81

	// Opens the connection. Needs to detect the 
	// correct port first. 
82
	void init();
83

84
	void closeConnection();
85
	bool isConnected(); 
86

87
	// send something through the port. 
88
89
	bool writeSerialLight(const char* buffer);

90
	// redundant
91
92
	std::string readSerialLight();
private:
93
94
95

	int startConnection(const char* port);

96
97
98
99
100
101
102
103
104
105
106
	int tty_fd;
	struct termios tio;

	std::vector<std::string> _commands;
	std::string _cur_command;
	std::clock_t timer;

	std::string _port;
	bool _ready;
	bool _isConnected;
};