Skip to content

Instantly share code, notes, and snippets.

@jmagnuson
Created October 17, 2015 19:09
Show Gist options
  • Save jmagnuson/50e51e76dbd20a7b78ba to your computer and use it in GitHub Desktop.
Save jmagnuson/50e51e76dbd20a7b78ba to your computer and use it in GitHub Desktop.
#include "application.h"
/*
* Serial - link between Photon and USB
* Serial1 - link between Photon and Nano
*/
/**************************
* Config defines *
**************************/
#define M100_1_SECOND 10
#define M100_5_SECONDS 50
#define M100_30_SECONDS 300
#define M100_1_MINUTE 600
/**************************
* Global variables *
**************************/
int next_song = 0;
int millis100 = 0;
/**************************
* System functions *
**************************/
void setup() {
// Initialize variables
next_song = 0;
millis100 = 0;
//Spark.function("writeSerial", writeSerial);
// Initialize serials
Serial1.begin(115200);
Serial.begin(115200);
// Cloud Variables
//Particle.variable("next_song", &next_song, INT);
// Cloud Functions
//Particle.function("isPlaying");
//Particle.function("setNextSong");
Particle.function("sendSong");
}
void loop() {
Particle.process();
delay(100);
millis100++;
if(1 /* EVERY 100MS */) {
// Check playing status
playing = isPlaying();
}
if (millis100 % M100_30_SECONDS == 0) {
// Check server status
}
if (millis100 % M100_1_MINUTE == 0) {
// Check something else
}
/* TESTING
if(Serial1.available()) {
Serial.write(Serial1.read());
}
if (Serial.available()) {
Serial1.write(Serial.read());
}*/
//delay(50);
if (!playing) {
if (next_song) {
// Next song ready, play it
playSong(next_song);
// Clear song "queue"
next_song = 0;
} else {
// No song ready, what do?
}
} else {
// Currently playing a song
}
}
/**************************
* Local functions *
**************************/
// Send CRNL-terminated string to nano-mp3
int writeSerial(String arg) {
arg += "\r\n";
Serial1.write(arg);
return 1;
}
// Poll nano-mp3 for playing status
int isPlaying() {
Serial1.write("s");
while(!Serial1.available());
return Serial.read();
}
// Send next song index to nano-mp3
int playSong(int _next_song) {
Serial1.print(_next_song);
}
/***********************
* Cloud Functions *
***********************/
// Returns value of the next song
int getNextSong() {
return next_song;
}
// Explicitly sets next song regardless of state
int setNextSong(String _next_song) {
// TODO: Parse integer out of string
next_song = _next_song.toInt();
}
// Checks state and sets if no song next in queue
int sendSong(String _next_song) {
if (next_song) {
// Already have song in queue
return -1;
}
else {
// No song in queue
// TODO: Parse integer out of string
next_song = _next_song.toInt();
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment