Skip to content

Instantly share code, notes, and snippets.

@dwblair
Last active June 17, 2016 00:42
Show Gist options
  • Select an option

  • Save dwblair/ecc9e1acff1d07ff8bf8560c9a7f2198 to your computer and use it in GitHub Desktop.

Select an option

Save dwblair/ecc9e1acff1d07ff8bf8560c9a7f2198 to your computer and use it in GitHub Desktop.
// Demo Code for SerialCommand Library
// Craig Versek, Jan 2014
// based on code from Steven Cogswell, May 2011
#include <SerialCommand.h>
#include <RTCZero.h>
#define arduinoLED 13 // Arduino LED on board
SerialCommand sCmd(Serial); // The demo SerialCommand object, initialize with any Stream object
/* Create an rtc object */
RTCZero rtc;
int AlarmTime;
void setup() {
rtc.begin();
pinMode(arduinoLED, OUTPUT); // Configure the onboard LED for output
digitalWrite(arduinoLED, LOW); // default to LED off
Serial.begin(9600);
Serial.println("Ready");
// Setup callbacks for SerialCommand commands
sCmd.addCommand("ON", LED_on); // Turns LED on
sCmd.addCommand("OFF", LED_off); // Turns LED off
sCmd.addCommand("HELLO", sayHello); // Echos the string argument back
sCmd.addCommand("SLEEP", sleep); // Converts two arguments to integers and echos them back
sCmd.setDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
Serial.println("Ready");
}
void loop() {
int num_bytes = sCmd.readSerial(); // fill the buffer
if (num_bytes > 0){
sCmd.processCommand(); // process the command
}
delay(10);
}
void LED_on(SerialCommand this_sCmd) {
this_sCmd.println("LED on");
digitalWrite(arduinoLED, HIGH);
}
void LED_off(SerialCommand this_sCmd) {
this_sCmd.println("LED off");
digitalWrite(arduinoLED, LOW);
}
void sayHello(SerialCommand this_sCmd) {
char *arg;
arg = this_sCmd.next(); // Get the next argument from the SerialCommand object buffer
if (arg != NULL) { // As long as it existed, take it
this_sCmd.print("Hello ");
this_sCmd.println(arg);
}
else {
this_sCmd.println("Hello, whoever you are");
}
}
void sleep(SerialCommand this_sCmd) {
int shutdownNumber, sleepNumber;
char *arg;
this_sCmd.println("We're in processCommand");
arg = this_sCmd.next();
if (arg != NULL) {
shutdownNumber = atoi(arg); // Converts a char string to an integer
// this_sCmd.print("First argument was: ");
// this_sCmd.println(aNumber);
}
else {
// this_sCmd.println("No arguments");
}
arg = this_sCmd.next();
if (arg != NULL) {
sleepNumber = atol(arg);
// this_sCmd.print("Second argument was: ");
//this_sCmd.println(aNumber);
}
else {
// this_sCmd.println("No second argument");
}
Serial.print("Shutting down process: ");
Serial.print(shutdownNumber);
Serial.println(" seconds.");
// give aNumber seconds til sleep (wait for BBB to shut down)
for (int a=0;a<shutdownNumber;a++) {
digitalWrite(13, HIGH); // turn the LED on
delay(1000);
digitalWrite(13, LOW); // turn the LED off
delay(1000);
}
Serial.print("Going to sleep for ");
Serial.print(sleepNumber);
Serial.println(" seconds.");
Serial.println("\n Note: device may reconnect on a different USB port.");
// now sleep for sleepNumber seconds
AlarmTime = rtc.getSeconds()+sleepNumber; // Adds 10 seconds to alarm time
AlarmTime = AlarmTime % 60; // checks for roll over 60 seconds and corrects
Serial.print("Next Alarm Time:");
Serial.println(AlarmTime);
rtc.setAlarmSeconds(AlarmTime); // Wakes at next alarm time
rtc.enableAlarm(rtc.MATCH_SS); // Match seconds only
rtc.attachInterrupt(alarmMatch);
Serial.end();
USBDevice.detach(); // Safely detach the USB prior to sleeping
rtc.standbyMode(); // Sleep until next alarm match
USBDevice.attach(); // Re-attach the USB, audible sound on windows machines
// Simple indication of being awake
for (int i=0;i<5;i++) {
digitalWrite(13, HIGH); // turn the LED on
delay(100);
digitalWrite(13, LOW); // turn the LED off
delay(100);
}
delay(1000); // Delay added to make serial more reliable
Serial.begin(9600);
while (! Serial); // Wait until Serial is ready
Serial.println("Awake");
}
// This gets set as the default handler, and gets called when no other command matches.
void unrecognized(SerialCommand this_sCmd) {
SerialCommand::CommandInfo command = this_sCmd.getCurrentCommand();
this_sCmd.print("Did not recognize \"");
this_sCmd.print(command.name);
this_sCmd.println("\" as a command.");
}
void alarmMatch() // Do something when interrupt called
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment