Skip to content

Instantly share code, notes, and snippets.

@jbobrow
Created December 4, 2018 23:13
Show Gist options
  • Save jbobrow/9869ebfdc8d81f4368f2d361408b66e9 to your computer and use it in GitHub Desktop.
Save jbobrow/9869ebfdc8d81f4368f2d361408b66e9 to your computer and use it in GitHub Desktop.
#define SYNC_PAUSE_DURATION 200
byte urge = 0;
byte brightness = 0;
Timer faceTimer[6];
Timer sendTimer;
void setup() {
}
void loop() {
// jump the urge to max on button press
if (buttonPressed()) {
urge = 200;
}
// update the status of our urge
updateSync();
// display flash then fade down
if (brightness > 0) {
brightness -= 10;
}
setColor(dim(WHITE, brightness));
}
/*
* When the synchronized pulse happens, this is what we should do
* In this case, set our brightness to 200
*/
void syncStep() {
brightness = 200;
}
/*
* Check if our neighbor is stepping based, we use the 6th bit
*/
bool isNeighborStepping(byte d) {
return (d >> 5) & 1;
}
/*
* Increment our urge based on our own clock as well as our neighbors
*/
void updateSync() {
// increment our urge
urge++;
// if our neighbor stepped, then increment our urge even more
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {
byte data = getLastValueReceivedOnFace(f);
if ( isNeighborStepping(data) ) {
if ( faceTimer[f].isExpired() ) {
// only increment once from a neighbor sending its message every 200ms
urge += (urge / 10);
faceTimer[f].set(SYNC_PAUSE_DURATION);
}
}
}
}
if (urge >= 200) {
syncStep();
urge = 0;
sendTimer.set(SYNC_PAUSE_DURATION/2);
setValueSentOnAllFaces(1 << 5);
}
else {
if (sendTimer.isExpired()) {
setValueSentOnAllFaces(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment