From a2c4f6f9eb5c91ba93249bbf7552431214a394d6 Mon Sep 17 00:00:00 2001 From: jancoow Date: Sun, 17 May 2015 12:12:14 +0200 Subject: [PATCH] Ledstrip now working in java. --- led.py | 52 ++++++++++++++++++++++++ src/control/Ledstrip.java | 83 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100755 led.py create mode 100644 src/control/Ledstrip.java diff --git a/led.py b/led.py new file mode 100755 index 0000000..5e188a1 --- /dev/null +++ b/led.py @@ -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() + diff --git a/src/control/Ledstrip.java b/src/control/Ledstrip.java new file mode 100644 index 0000000..1e07e7f --- /dev/null +++ b/src/control/Ledstrip.java @@ -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(); + } + } + } +}