-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample1.ino
126 lines (113 loc) · 2.29 KB
/
sample1.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
// Constantes
const int buttonPin = 2;
const int ledPin = 13;
const int boardId = 5;
// Variables
int buttonState = 1;
int ledState = 1;
// Variables liees a la lectures des packets
int packetState = 1;
int packetId = 0;
int packetDataType = 0;
int packetData[16];
int packetDataLen = 0;
int packetDataPtr = 0;
int pending = 0;
/* Tableaux pour les stocker les arguments des packets */
// unsigned char
unsigned char ucharv[8];
int ucharc = 0;
// float
float floatv[8];
int floatc = 0;
// Fonctions d'envoi de donnees
void sendPacket(int id);
void sendUByte(int id, int value);
void sendBoardId();
// Lorsqu'un packet est recu
void processPacket()
{
if (packetId == 254) {
sendBoardId();
} else if (packetId == 200) {
digitalWrite(ledPin, LOW);
} else if (packetId == 201) {
digitalWrite(ledPin, HIGH);
} else if (packetId == 150) {
buttonState = digitalRead(buttonPin);
sendPacket(100+buttonState);
}
}
void setup()
{
Serial.begin(115200);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
pinMode(ledPin, OUTPUT);
packetState = 1;
while (Serial.read() != -1) {}
sendBoardId();
}
void loop()
{
if (Serial.available() > 0) {
pending = Serial.read();
switch (packetState) {
case 1:
if (pending == 129) {
packetState = 2;
ucharc = 0;
}
break;
case 2:
packetId = pending;
packetState = 3;
break;
case 3:
if (pending == 128) {
processPacket();
packetState = 1;
} else {
packetDataType = pending;
packetState = 4;
packetDataLen = pending & 0b1111;
packetDataPtr = 0;
}
break;
case 4:
packetData[packetDataPtr++] = pending;
if (packetDataPtr == packetDataLen) {
if (packetDataType == 1) {
ucharv[ucharc++] = pending;
}
packetState = 3;
}
}
}
pending = digitalRead(buttonPin);
if (pending != buttonState) {
buttonState = pending;
sendPacket(100 + buttonState);
}
}
// Envoyer un packet sans argument
void sendPacket(int id)
{
Serial.write(129);
Serial.write(id);
Serial.write(128);
}
// Envoyer un packet avec un unsigned char en argument
void sendUByte(int id, int value)
{
Serial.write(129);
Serial.write(id);
Serial.write(1);
Serial.write(value);
Serial.write(128);
}
// Envoyer l'identifiant de la carte
void sendBoardId()
{
sendUByte(255, boardId);
}