-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcom.ino
executable file
·150 lines (135 loc) · 3.49 KB
/
com.ino
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
#ifdef COMINO
void sendForces() {
write_order(FORCES);
write_i32(forces[0]);
write_i32(forces[1]);
}
void get_messages_from_serial()
{
if(Serial.available() > 0)
{
// The first byte received is the instruction
Order order_received = read_order();
if(order_received == HELLO)
{
// If the cards haven't say hello, check the connection
if(!is_connected)
{
is_connected = true;
write_order(HELLO);
}
else
{
// If we are already connected do not send "hello" to avoid infinite loop
write_order(ALREADY_CONNECTED);
}
}
else if(order_received == ALREADY_CONNECTED)
{
is_connected = true;
}
else
{
switch(order_received)
{
case POSITION:
{
int16_t x = read_i16();
int16_t y = read_i16();
pos[0] = x;
pos[1] = y;
pos_updated = true;
break;
}
case FORCES:
{
forces_requested = true;
break;
}
case VERSION:
{
write_order(VERSION);
write_i32(SKETCH_VERSION);
break;
}
case CONFIG:
{
float defaultSpringGain = read_i8() / 100.0;
Gains *gains = Joystick.getGains();
gains[0].defaultSpringGain = defaultSpringGain;
gains[1].defaultSpringGain = defaultSpringGain;
break;
}
// Unknown order
default:
write_order(ERROR);
write_i16(404);
return;
}
}
write_order(RECEIVED); // Confirm the reception
}
}
Order read_order()
{
return (Order) Serial.read();
}
void wait_for_bytes(int num_bytes, unsigned long timeout)
{
unsigned long startTime = millis();
//Wait for incoming bytes or exit if timeout
while ((Serial.available() < num_bytes) && (millis() - startTime < timeout)){}
}
// NOTE : Serial.readBytes is SLOW
// this one is much faster, but has no timeout
void read_signed_bytes(int8_t* buffer, size_t n)
{
size_t i = 0;
int c;
while (i < n)
{
c = Serial.read();
if (c < 0) break;
*buffer++ = (int8_t) c; // buffer[i] = (int8_t)c;
i++;
}
}
int8_t read_i8()
{
wait_for_bytes(1, 100); // Wait for 1 byte with a timeout of 100 ms
return (int8_t) Serial.read();
}
int16_t read_i16()
{
int8_t buffer[2];
wait_for_bytes(2, 100); // Wait for 2 bytes with a timeout of 100 ms
read_signed_bytes(buffer, 2);
return (((int16_t) buffer[0]) & 0xff) | (((int16_t) buffer[1]) << 8 & 0xff00);
}
int32_t read_i32()
{
int8_t buffer[4];
wait_for_bytes(4, 200); // Wait for 4 bytes with a timeout of 200 ms
read_signed_bytes(buffer, 4);
return (((int32_t) buffer[0]) & 0xff) | (((int32_t) buffer[1]) << 8 & 0xff00) | (((int32_t) buffer[2]) << 16 & 0xff0000) | (((int32_t) buffer[3]) << 24 & 0xff000000);
}
void write_order(enum Order myOrder)
{
uint8_t* Order = (uint8_t*) &myOrder;
Serial.write(Order, sizeof(uint8_t));
}
void write_i8(int8_t num)
{
Serial.write(num);
}
void write_i16(int16_t num)
{
int8_t buffer[2] = {(int8_t) (num & 0xff), (int8_t) (num >> 8)};
Serial.write((uint8_t*)&buffer, 2*sizeof(int8_t));
}
void write_i32(int32_t num)
{
int8_t buffer[4] = {(int8_t) (num & 0xff), (int8_t) (num >> 8 & 0xff), (int8_t) (num >> 16 & 0xff), (int8_t) (num >> 24 & 0xff)};
Serial.write((uint8_t*)&buffer, 4*sizeof(int8_t));
}
#endif