-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathReceive.ino
More file actions
78 lines (62 loc) · 2.28 KB
/
Copy pathReceive.ino
File metadata and controls
78 lines (62 loc) · 2.28 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
// *** Receive ***
// This 1st example will make the PC toggle the integrated led on the arduino board.
// It demonstrates how to:
// - Define commands
// - Set up a serial connection
// - Receive a command with a parameter from the PC
#include <CmdMessenger.h> // CmdMessenger
// Blinking led variables
bool ledState = 0; // Current state of Led
const int kBlinkLed = 13; // Pin of internal Led
// Attach a new CmdMessenger object to the default Serial port
CmdMessenger cmdMessenger = CmdMessenger(Serial);
// We can define up to a default of 50 cmds total, including both directions (send + receive)
// and including also the first 4 default command codes for the generic error handling.
// If you run out of message slots, then just increase the value of MAXCALLBACKS in CmdMessenger.h
// This is the list of recognized commands. These can be commands that can either be sent or received.
// In order to receive, attach a callback function to these events
//
// Note that commands work both directions:
// - All commands can be sent
// - Commands that have callbacks attached can be received
//
// This means that both sides should have an identical command list:
// both sides can either send it or receive it (or even both)
// Commands
enum
{
kSetLed, // Command to request led to be set in specific state
};
// Callbacks define on which received commands we take action
void attachCommandCallbacks()
{
cmdMessenger.attach(kSetLed, OnSetLed);
}
// Callback function that sets led on or off
void OnSetLed()
{
// Read led state argument, interpret string as boolean
ledState = cmdMessenger.readBoolArg();
// Set led
digitalWrite(kBlinkLed, ledState?HIGH:LOW);
}
// Setup function
void setup()
{
// Listen on serial connection for messages from the PC
// 115200 is the max speed on Arduino Uno, Mega, with AT8u2 USB
// Use 57600 for the Arduino Duemilanove and others with FTDI Serial
Serial.begin(115200);
// Adds newline to every command
cmdMessenger.printLfCr();
// Attach my application's user-defined callback methods
attachCommandCallbacks();
// set pin for blink LED
pinMode(kBlinkLed, OUTPUT);
}
// Loop function
void loop()
{
// Process incoming serial data, and perform callbacks
cmdMessenger.feedinSerialData();
}