-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_dataref.ino
More file actions
96 lines (83 loc) · 3.52 KB
/
command_dataref.ino
File metadata and controls
96 lines (83 loc) · 3.52 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
// command_dataref.ino
// Execute command-type datarefs (e.g., toggle LED, reset, etc.)
// Copy to a new file named command_dataref.ino
//
// Protocol:
// 1. Host sends: "CMD <command_name>"
// 2. Arduino responds: "CMD_EXECUTED <command_name>"
//
// This sketch demonstrates how to handle various commands and trigger actions.
// Commands are simple text strings that can be mapped to hardware actions.
//
// Example commands:
// CMD toggleLED -> CMD_EXECUTED toggleLED
// CMD pulseLED -> CMD_EXECUTED pulseLED
// CMD reset -> CMD_EXECUTED reset
//
// Hardware: Uses built-in LED for visual feedback of command execution
const int LED_PIN = LED_BUILTIN; // LED for command feedback
unsigned long lastBlink = 0; // Timer for blink animation
bool ledState = false; // Track current LED state
void setup() {
pinMode(LED_PIN, OUTPUT); // Configure LED pin
digitalWrite(LED_PIN, LOW); // Start with LED off
Serial.begin(115200); // Serial communication at 115200 baud
}
void loop() {
// Check for incoming commands from host
if (Serial.available()) {
String line = Serial.readStringUntil('\n');
line.trim();
// Parse CMD command: CMD <command_name>
if (line.startsWith("CMD ")) {
String cmd = line.substring(4).trim(); // Extract command name
executeCommand(cmd); // Execute the command
Serial.println("CMD_EXECUTED " + cmd); // Acknowledge execution
}
}
// Optional: blink LED when it's supposed to be "on" (visual feedback)
if (ledState && (millis() - lastBlink) > 500) {
lastBlink = millis(); // Update blink timer
digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Toggle LED state
}
}
// Execute specific commands based on their name
void executeCommand(const String &cmd) {
if (cmd == "toggleLED") {
// Toggle the LED state
ledState = !ledState; // Invert current state
digitalWrite(LED_PIN, ledState ? HIGH : LOW); // Apply new state
} else if (cmd == "pulseLED") {
// Quick blink sequence (3 rapid blinks)
bool currentState = ledState; // Remember current state
ledState = false; // Turn off blinking
digitalWrite(LED_PIN, LOW); // LED off during pulse
for (int i = 0; i < 3; i++) {
digitalWrite(LED_PIN, HIGH); // LED on
delay(100); // 100ms on time
digitalWrite(LED_PIN, LOW); // LED off
delay(100); // 100ms off time
}
// Restore previous state after pulse
ledState = currentState;
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
} else if (cmd == "reset") {
// Graceful "soft reset" command
// In a real implementation, you might:
// - Reset all variables to default values
// - Reset hardware to initial state
// - Clear any pending operations
Serial.println("RESET_CMD_RECEIVED"); // Acknowledge reset
} else if (cmd == "status") {
// Report current system status
Serial.print("STATUS LED:");
Serial.print(ledState ? "ON" : "OFF");
Serial.println(" READY");
} else if (cmd == "version") {
// Report version/firmware information
Serial.println("CMD_EXECUTED version 1.0");
} else {
// Unknown command
Serial.println("UNKNOWN_CMD: " + cmd);
}
}