-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart.cpp
More file actions
163 lines (147 loc) · 3.36 KB
/
uart.cpp
File metadata and controls
163 lines (147 loc) · 3.36 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include "horizonTracker.h"
#define START_BYTE 0x12
const int DOUBLE_SIZE = sizeof(double);
int uart0filestream = -1;
void UARTInit();
int writeStartByte();
int writeAngleData(double&);
int readAngleData(double&);
int readStartByte();
void reverse_array(unsigned char*, int);
void shiftLeft(unsigned char*, int, int);
unsigned char rx_buffer[256] = { 0 };
int bytesInBuffer = 0;
void UARTInit()
{
uart0filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
if(uart0filestream == -1)
{
printf("Cant open UART\n");
}
struct termios options;
tcgetattr(uart0filestream, &options);
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(uart0filestream, TCIFLUSH);
tcsetattr(uart0filestream, TCSANOW, &options);
}
int writeStartByte()
{
if(mode == SERF)
{
printf("I'm not writing the start byte. I'm the SERF!");
}
char const data[] = { START_BYTE };
if(uart0filestream)
{
int count = write(uart0filestream, &data, 1);
if(count < 0)
{
printf("UART TX error\n");
return -1;
}
}
}
int writeAngleData(double & angle) {
if(mode == MASTER)
{
printf("I'm not writing angle data. I'm the MASTER!\n");
return -1;
}
unsigned char data[DOUBLE_SIZE+1] = { 0 };
memcpy((&data+1), &angle, DOUBLE_SIZE);
//unsigned char * const doubleData = reinterpret_cast<unsigned char * const>(&angle);
if(uart0filestream)
{
int count = write(uart0filestream, &data, DOUBLE_SIZE+1);
if(count < 0)
{
printf("UART TX error\n");
return -1;
}
}
}
int readAngleData(double & angle)
{
if(mode == SERF)
{
printf("I'm not reading angle data. I'm the SERF!\n");
return -1;
}
if(uart0filestream != -1)
{
while(bytesInBuffer<200)
{
if(read(uart0filestream, (void*)(rx_buffer+bytesInBuffer), 1) <= 0)
{
return -1;
}
else
{
bytesInBuffer++;
if(rx_buffer[0]==0 && rx_buffer[8]== 0xc0)
{
memcpy(&angle, (&rx_buffer+1), DOUBLE_SIZE);
printf("%f: %x %x %x %x %x %x %x %x\n", angle, rx_buffer[1], rx_buffer[2], rx_buffer[3], rx_buffer[4], rx_buffer[5], rx_buffer[6], rx_buffer[7], rx_buffer[8]);
return 0;
}
}
}
if(bytesInBuffer >= 200) {
printf("WE FILLED THE BUFFER!\n");
}
}
}
int readStartByte()
{
if(mode == MASTER)
{
printf("I'm not reading start byte data. I'm the MASTER!\n");
return -1;
}
if(uart0filestream != -1)
{
unsigned char rx_buffer[256];
int rx_length = read(uart0filestream, (void*)rx_buffer, 255);
if(rx_length <= 0)
{
// no data
return 0;
}
else
{
if(rx_buffer[0] == START_BYTE)
{
return 1;
}
else
{
return 0;
}
}
}
}
void shiftLeft(unsigned char * bytes, int length, int shift)
{
for(int j = 0; j < length-shift; j++) {
bytes[j]=bytes[j+shift];
}
for(int j = length-shift; j < length; j++) {
bytes[j]=bytes[0];
}
}
void reverse_array( unsigned char array[], int arraylength )
{
for (int i = 0; i < (arraylength / 2); i++)
{
unsigned char temporary = array[i];
array[i] = array[(arraylength - 1) - i];
array[(arraylength - 1) - i] = temporary;
}
}