forked from NVSL/PiDuino_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerial.h
133 lines (112 loc) · 3.67 KB
/
Serial.h
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
/*
SPI.cpp - LinuxDuino Serial (UART) library header
Copyright (c) 2016 Jorge Garza <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SerialLinux_h
#define SerialLinux_h
#include <stdint.h>
#include "WString.h"
/////////////////////////////////////////////
// SerialLinux class (UART) //
////////////////////////////////////////////
// Printing format options
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2
// Defines for setting data, parity, and stop bits
// e.g SERIAL_ABC
// A= data (5 bits, 6 bits, 7 bits, 8 bits)
// B= parity (None, Even, Odd)
// C= stop bits (1 bit, 2 bits)
#define SERIAL_5N1 0x00
#define SERIAL_6N1 0x02
#define SERIAL_7N1 0x04
#define SERIAL_8N1 0x06 // default
#define SERIAL_5N2 0x08
#define SERIAL_6N2 0x0A
#define SERIAL_7N2 0x0C
#define SERIAL_8N2 0x0E
#define SERIAL_5E1 0x20
#define SERIAL_6E1 0x22
#define SERIAL_7E1 0x24
#define SERIAL_8E1 0x26
#define SERIAL_5E2 0x28
#define SERIAL_6E2 0x2A
#define SERIAL_7E2 0x2C
#define SERIAL_8E2 0x2E
#define SERIAL_5O1 0x30
#define SERIAL_6O1 0x32
#define SERIAL_7O1 0x34
#define SERIAL_8O1 0x36
#define SERIAL_5O2 0x38
#define SERIAL_6O2 0x3A
#define SERIAL_7O2 0x3C
#define SERIAL_8O2 0x3E
// A char not found in a valid ASCII numeric field
#define NO_IGNORE_CHAR '\x01'
// Serial Driver name (user can change it)
extern char SERIAL_DRIVER_NAME[];
class SerialLinux {
private:
int fd;
FILE * fd_file;
long timeOut;
timespec timeDiff(timespec start, timespec end);
int timedPeek();
int peekNextDigit(bool detectDecimal);
long timeDiffmillis(timespec start, timespec end);
char * int2bin(int n);
public:
SerialLinux();
void begin(int baud, unsigned char config = SERIAL_8N1);
void begin(const char *serialPort, int baud, unsigned char config = SERIAL_8N1);
void end();
int available();
int availableForWrite();
bool find(const char *target);
bool findUntil(const char *target, const char *terminator);
void flush();
long parseInt() { return parseInt(NO_IGNORE_CHAR); };
long parseInt(char ignore);
float parseFloat();
int peek();
size_t print(const String &s);
size_t print(const char str[]);
size_t print(char c);
size_t print(unsigned char b, int base);
size_t print(int n, int base);
size_t print(unsigned int n, int base);
size_t println(void);
size_t println(const String &s);
size_t println(const char c[]);
size_t println(char c);
size_t println(unsigned char b, int base);
size_t println(int num, int base);
size_t println(unsigned int num, int base);
size_t printf(const char *fmt, ... );
int read();
size_t readBytes(char buffer[], size_t length);
size_t readBytesUntil(char terminator, char buffer[], size_t length);
String readString();
String readStringUntil(char terminator);
size_t readStringCommand(char terminator, char buffer[], size_t length);
void setTimeout(long millis);
size_t write(uint8_t c);
size_t write(const char *str);
size_t write(char *buffer, size_t size);
operator bool() { return (fd == -1) ? false : true; }
};
extern SerialLinux Serial;
#endif