-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathConsoleShell.ino
More file actions
184 lines (160 loc) · 4.89 KB
/
Copy pathConsoleShell.ino
File metadata and controls
184 lines (160 loc) · 4.89 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// *** ConsoleShell ***
// This example shows how to use CmdMessenger as a shell, and communicate with it using the Serial Console
// This example is different from all others:
// - there is no PC counterpart
// - it will only receive commands, instead of sending commands it will use Serial.Pring
//
// Below is an example of interacting with the sample:
//
// Available commands
// 0; - This command list
// 1,<led state>; - Set led. 0 = off, 1 = on
// 2,<led brightness>; - Set led brighness. 0 - 1000
// 3; - Show led state
//
// Command> 3;
//
// Led status: on
// Led brightness: 500
//
// Command> 2,1000;
//
// Led status: on
// Led brightness: 1000
//
// Command> 1,0;
//
// Led status: off
// Led brightness: 1000
#include <CmdMessenger.h> // CmdMessenger
// PWM timing variables
unsigned long intervalOn = 0;
unsigned long prevBlinkTime = 0;
const unsigned long PWMinterval = 1000;
// Blinking led variables
bool ledState = 1; // On/Off state of Led
int ledBrightness = prevBlinkTime /2 ; // 50 % Brightness
const int kBlinkLed = 13; // Pin of internal Led
// Attach a new CmdMessenger object to the default Serial port
CmdMessenger cmdMessenger = CmdMessenger(Serial);
// This is the list of recognized commands.
// In order to receive, attach a callback function to these events
enum
{
kCommandList , // Command to request list of available commands
kSetLed , // Command to request led to be set in specific state
kSetLedBrightness , // Command to request led to be set in to specific brightness
kStatus , // Command to request led status
};
// Callbacks define on which received commands we take action
void attachCommandCallbacks()
{
// Attach callback methods
cmdMessenger.attach(OnUnknownCommand);
cmdMessenger.attach(kCommandList, OnCommandList);
cmdMessenger.attach(kSetLed, OnSetLed);
cmdMessenger.attach(kSetLedBrightness, OnSetLedBrightness);
cmdMessenger.attach(kStatus, OnStatus);
}
// Called when a received command has no attached function
void OnUnknownCommand()
{
Serial.println("This command is unknown!");
ShowCommands();
}
// Callback function that shows a list of commands
void OnCommandList()
{
ShowCommands();
}
// Callback function that sets led on or off
void OnSetLed()
{
// Read led state argument, expects 0 or 1 and interprets as false or true
ledState = cmdMessenger.readBoolArg();
ShowLedState();
}
// Callback function that sets led on or off
void OnSetLedBrightness()
{
// Read led brightness argument, expects value between 0 to 255
ledBrightness = cmdMessenger.readInt16Arg();
// Set led brightness
SetBrightness();
// Show Led state
ShowLedState();
}
// Callback function that shows led status
void OnStatus()
{
// Send back status that describes the led state
ShowLedState();
}
// Show available commands
void ShowCommands()
{
Serial.println("Available commands");
Serial.println(" 0; - This command list");
Serial.println(" 1,<led state>; - Set led. 0 = off, 1 = on");
Serial.print (" 2,<led brightness>; - Set led brighness. 0 - ");
Serial.println(PWMinterval);
Serial.println(" 3; - Show led state");
}
// Show led state
void ShowLedState()
{
Serial.print("Led status: ");
Serial.println(ledState?"on":"off");
Serial.print("Led brightness: ");
Serial.println(ledBrightness);
}
// Set led state
void SetLedState()
{
if (ledState) {
// If led is turned on, go to correct brightness using analog write
analogWrite(kBlinkLed, ledBrightness);
} else {
// If led is turned off, use digital write to disable PWM
digitalWrite(kBlinkLed, LOW);
}
}
// Set led brightness
void SetBrightness()
{
// clamp value intervalOn on 0 and PWMinterval
intervalOn = max(min((unsigned long)ledBrightness,PWMinterval),0ul);
}
// Pulse Width Modulation to vary Led intensity
// turn on until intervalOn, then turn off until PWMinterval
bool blinkLed() {
if ( micros() - prevBlinkTime > PWMinterval ) {
// Turn led on at end of interval (if led state is on)
prevBlinkTime = micros();
digitalWrite(kBlinkLed, ledState?HIGH:LOW);
} else if ( micros() - prevBlinkTime > intervalOn ) {
// Turn led off at halfway interval
digitalWrite(kBlinkLed, LOW);
}
}
// Setup function
void setup()
{
// Listen on serial connection for messages from the PC
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);
// Show command list
ShowCommands();
}
// Loop function
void loop()
{
// Process incoming serial data, and perform callbacks
cmdMessenger.feedinSerialData();
blinkLed();
}