-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoGUIDemo.ino
More file actions
93 lines (83 loc) · 1.73 KB
/
ArduinoGUIDemo.ino
File metadata and controls
93 lines (83 loc) · 1.73 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
#define SERIAL_INPUT_BUFFER_LEN 150
#define CMD_START_CHAR '*'
#define CMD_TOKEN_DELIMITER ","
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(9600);
Serial.println("@STARTED");
}
void loop()
{
loopSerialIO();
}
void loopSerialIO(void)
{
static char serialBuffer[SERIAL_INPUT_BUFFER_LEN];
static int serialBufferOffset = 0;
while (Serial.available())
{
serialBuffer[serialBufferOffset] = Serial.read();
if (serialBuffer[serialBufferOffset] == '\n')
{
serialBuffer[serialBufferOffset] = '\0';
serialBufferOffset = 0;
handleCommand(serialBuffer);
}
else
{
if (serialBufferOffset < (SERIAL_INPUT_BUFFER_LEN - 1))
{
serialBufferOffset++;
}
}
}
}
void handleCommand(char *buffer)
{
if (buffer[0] != CMD_START_CHAR)
{
Serial.print("@ERROR,INVALID_COMMAND,");
Serial.println(buffer);
return;
}
char *command = strtok(buffer, CMD_TOKEN_DELIMITER);
if (strcmp(command, "*PING") == 0)
{
Serial.println("@PONG");
}
else if (strcmp(command, "*BLINK_LED") == 0)
{
char *valueStr = strtok(NULL, CMD_TOKEN_DELIMITER);
if (valueStr != NULL)
{
int numBlinks = atoi(valueStr);
Serial.println("@BLINK_LED,START");
blinkLED(numBlinks);
Serial.println("@BLINK_LED,FINISH");
}
else
{
Serial.println("@BLINK_LED,ERROR,INVALID_NUM_BLINKS");
}
}
else
{
Serial.print("@ERROR,INVALID_COMMAND,");
Serial.println(command);
}
}
void blinkLED(int numBlinks)
{
for (int i = 0; i < numBlinks; i++)
{
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
if (i < numBlinks - 1)
{
delay(250);
}
}
}