Skip to content

Instantly share code, notes, and snippets.

@jacobjoaquin
Created June 10, 2018 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobjoaquin/e197ed5984aad07dde004de269da3959 to your computer and use it in GitHub Desktop.
Save jacobjoaquin/e197ed5984aad07dde004de269da3959 to your computer and use it in GitHub Desktop.
int numLEDs = 16;
float LEDRingRadius = 80;
ArrayList<LED> ledList;
int position = 0;
ArrayList<LED> ledEmulatorList;
class LED {
float x = 0;
float y = 0;
color c = 0;
LED(float x, float y) {
this.x = x;
this.y = y;
}
void draw() {
pushStyle();
noStroke();
fill(c);
ellipse(x, y, 8, 8);
noFill();
stroke(128);
ellipse(x, y, 10, 10);
popStyle();
}
}
ArrayList<LED> createLEDRing() {
ArrayList<LED> ledList = new ArrayList<LED>();
for (int i = 0; i < numLEDs; i++) {
float n = (float) i / (float) numLEDs;
float angle = n * TWO_PI;
PVector p = PVector.fromAngle(angle).mult(LEDRingRadius);
ledList.add(new LED(p.x, p.y));
}
return ledList;
}
ArrayList<Integer> serializeLEDs() {
ArrayList<Integer> list = new ArrayList<Integer>(numLEDs);
for (LED led : ledList) {
list.add(led.c);
}
return list;
}
void settings() {
size(500, 500);
}
void setup() {
frameRate(5);
ledList = createLEDRing();
ledEmulatorList = createLEDRing();
textAlign(CENTER, CENTER);
textSize(20);
}
void draw() {
background(0);
// Update LED data
for (int i = 0; i < ledList.size(); i++) {
LED led = ledList.get(i);
if (position == i) {
led.c = color(255, 0, 0);
} else {
led.c = color(0);
}
}
position = (position + 1) % numLEDs;
// Display LEDs
pushMatrix();
translate(125, height / 2.0);
for (LED led : ledList) {
led.draw();
}
fill(255);
text("Original", 0, 100);
popMatrix();
// Serialize LED data
ArrayList<Integer> data = serializeLEDs();
// Print Data
println(data);
// This is where you send the serialized data out
// ...
// And this is where we pretend we receive the serialize data elsewhere
for (int i = 0; i < ledEmulatorList.size(); i++) {
LED led = ledEmulatorList.get(i);
led.c = data.get(i);
}
// Display LED Emulator
pushMatrix();
translate(375, height / 2.0);
for (LED led : ledEmulatorList) {
led.draw();
}
fill(255);
text("Emulator", 0, 100);
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment