Created
June 2, 2018 18:39
-
-
Save LatvianModder/1c552797b405ae880d9ed861f60ff2e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.core.PApplet; | |
import processing.event.MouseEvent; | |
import processing.serial.Serial; | |
/** | |
* @author LatvianModder | |
*/ | |
public class LedStrip extends PApplet | |
{ | |
public static void main(String[] args) | |
{ | |
PApplet.main("LedStrip"); | |
} | |
public Thread serialWriter; | |
public byte[] ledData = null; | |
@Override | |
public void settings() | |
{ | |
size(600, 150); | |
} | |
@Override | |
public void setup() | |
{ | |
serialWriter = new Thread(() -> { | |
try | |
{ | |
Serial serial = null; | |
for (String s : Serial.list()) | |
{ | |
if (!s.equals("COM1")) | |
{ | |
serial = new Serial(LedStrip.this, s, 1000000); | |
ledData = new byte[1 + 3 * 300]; | |
setBrightness(30); | |
println("Opened Serial at port " + s); | |
break; | |
} | |
} | |
if (serial == null) | |
{ | |
println("Serial port not found!"); | |
exit(); | |
return; | |
} | |
while (serial.readBytes() == null) | |
{ | |
Thread.sleep(1L); | |
} | |
while (serialWriter != null) | |
{ | |
if (ledData != null) | |
{ | |
serial.write(ledData); | |
while (serial.readBytes() == null) | |
{ | |
Thread.sleep(1L); | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
ex.printStackTrace(); | |
exit(); | |
} | |
}); | |
serialWriter.start(); | |
} | |
public int getBrightness() | |
{ | |
return ledData[0] & 0xFF; | |
} | |
public void setBrightness(int brightness) | |
{ | |
ledData[0] = (byte) min(max(brightness, 0), 120); | |
} | |
public void setLed(int index, int red, int green, int blue) | |
{ | |
if (index < 0 || index > (ledData.length - 1) / 3) | |
{ | |
return; | |
} | |
ledData[index * 3 + 1] = (byte) red; | |
ledData[index * 3 + 2] = (byte) green; | |
ledData[index * 3 + 3] = (byte) blue; | |
} | |
public void setLed(int index, int color) | |
{ | |
setLed(index, color >> 16, color >> 8, color); | |
} | |
@Override | |
public void dispose() | |
{ | |
super.dispose(); | |
serialWriter = null; | |
} | |
@Override | |
public void draw() | |
{ | |
background(0); | |
noStroke(); | |
if (ledData == null) | |
{ | |
return; | |
} | |
int leds = (ledData.length - 1) / 3; | |
float seconds = millis() * 0.001F; | |
int mode = (int) (seconds / 5F) % 2; | |
if (mode == 0) | |
{ | |
for (int i = 0; i < leds; i++) | |
{ | |
setLed(i, java.awt.Color.HSBtoRGB(i / 20F + seconds, 1F /*map(sin(seconds * 3F), -1F, 1F, 0.1F, 1F)*/, 1F)); | |
} | |
} | |
else if (mode == 1) | |
{ | |
for (int i = 0; i < leds; i++) | |
{ | |
setLed(i, (int) map(sin(seconds * 3F + i * 0.3F), -1F, 1F, 0, 90), 0, 0); | |
} | |
} | |
for (int i = 0; i < leds; i++) | |
{ | |
fill(ledData[i * 3 + 1] & 0xFF, ledData[i * 3 + 2] & 0xFF, ledData[i * 3 + 3] & 0xFF); | |
rect(i * 2, 0, 2, height); | |
} | |
} | |
@Override | |
public void mouseWheel(MouseEvent event) | |
{ | |
setBrightness(getBrightness() - event.getCount() * 10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment