Ledstrip now working in java.

This commit is contained in:
jancoow
2015-05-17 12:12:14 +02:00
parent d8dbcc0719
commit a2c4f6f9eb
2 changed files with 135 additions and 0 deletions
Executable
+52
View File
@@ -0,0 +1,52 @@
import sys
import time
from neopixel import *
# LED strip configuration:
LED_COUNT = 66 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 700000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
def setLed(strip, l,r,g,b):
strip.setPixelColor(l,Color(r,g,b))
def strobetest(strip):
on = True
while True:
if on:
on = False
for i in range(strip.numPixels()):
strip.setPixelColor(i, Color(255,255,255))
else:
on = True
for i in range(strip.numPixels()):
strip.setPixelColor(i, Color(0,0,0))
strip.show()
time.sleep(100.0/1000)
if __name__ == '__main__':
ledstrip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
ledstrip.begin()
s = sys.stdin.readline().strip()
while s not in ['break', 'quit']:
message = s.split("|")
returnms = 'e'
if(message[0] == '1'):
if (len(message) == 5):
try:
led = int(message[1])
r = int(message[2])
g = int(message[3])
b = int(message[4])
setLed(ledstrip, led,r,g,b)
returnms = 'a'
except ValueError:
returnms = 'e'
elif (message[0] == '0'):
ledstrip.show()
returnms = 'a'
s = sys.stdin.readline().strip()
+83
View File
@@ -0,0 +1,83 @@
package control;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Ledstrip {
private BufferedReader inp;
private BufferedWriter out;
private Process p;
public Ledstrip(){
String cmd = "sudo python led.py";
try {
p = Runtime.getRuntime().exec(cmd);
inp = new BufferedReader( new InputStreamReader(p.getInputStream()) );
out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()) );
setLed(15, 100, 100, 100);
strobo();
}
catch (Exception err) {
err.printStackTrace();
}
}
public void close(){
try {
inp.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
p.destroy();
}
public void setLed(int led, int r, int g, int b) {
try {
out.write( "1|" + led + "|" + r + "|" + g + "|" + b + "\n" );
out.flush();
}
catch (Exception err) {
err.printStackTrace();
}
}
public void show(){
try {
out.write( "0\n" );
out.flush();
}
catch (Exception err) {
err.printStackTrace();
}
}
public void strobo(){
boolean on = true;
while(true){
if(on){
for(int i = 1; i < 66; i++){
setLed(i, 0, 0, 0);
}
}else{
for(int i = 1; i < 66; i++){
setLed(i, 255, 255, 255);
}
}
on = !on;
show();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}