Light Cube/Projektcode: Unterschied zwischen den Versionen

Aus C3MAWiki
Zur Navigation springenZur Suche springen
KKeine Bearbeitungszusammenfassung
K (Der Seiteninhalt wurde durch einen anderen Text ersetzt: „Kategorie:öffentlich {{aktives Projekt|Kontakt=Berkay|öffentlich=ja}} Wenn du Fragen zum Projektcode hast, wende dich an Ben…“)
 
Zeile 1: Zeile 1:
[[Kategorie:öffentlich]]
[[Kategorie:öffentlich]]
{{aktives Projekt|Kontakt=Berkay|öffentlich=ja}}
{{aktives Projekt|Kontakt=Berkay|öffentlich=ja}}
Dies ist der vollständige Projektcode. Die Projektseite: [[Light Cubes]]
Wenn du Fragen zum Projektcode hast, wende dich an [[Benutzer:Berkay|Berkay]]
 
 
== ESP-Platine/Bauteil ESP NodeMCU Devboard ==
 
    wifi.setmode(wifi.STATION)
    wifi.sta.config("SSID", "PW")
  ws2812.init(); 
local ledCount = 34;
local leds = string.char(10,00,0):rep(ledCount)
local hadIP = false;
local tick = false;
function readjson(data)
    ok, json = pcall(cjson.decode, data) 
    if ok then
        leds = ""
        for ledid = 1, ledCount, 1 do
            local ledData = json[ledid];
            leds = leds .. string.char(ledData.g,ledData.r,ledData.b)
        end
    else
    print("failed to decode!")
    end
end
local pseudoRemote = {}
for ledid = 1, ledCount, 1 do
    pseudoRemote[ledid]={}
    pseudoRemote[ledid].r=100
    pseudoRemote[ledid].g=0
    pseudoRemote[ledid].b=ledid
end
local remoteData = cjson.encode(pseudoRemote);
print(remoteData)
local srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload)
        print("Got something...")
        print(payload);
        readjson(payload)
        conn:send("HTTP/1.1 200 OK\n")
        conn:send("Content-Type: text/html\n\n")
    end)
end)
local led1 = 0;
local led2 = 0;
local led3 = 0;
function countBlinks(eingabe)
    while(eingabe > 0) do
        if(eingabe >= 100) then
            led1 = led1 +1
            eingabe = eingabe -100
        else
            if(eingabe >= 10) then
                led2 = led2 +1
                eingabe = eingabe -10
            else
                led3 = led3 +1
                eingabe = eingabe -1
            end
        end
    end
end
 
function update()
    ws2812.write(leds)
end
local anSekunde = false;
function blinkIP()
    anSekunde = not anSekunde
    if(anSekunde) then
        if led1 > 0 then
            ws2812.write(string.char(0,20,0))
            led1 = led1 -1
            return
        end
        if (led2 > 0) then
            ws2812.write(string.char(0,0,0) .. string.char(0,20,0))
            led2 = led2 -1
            return
        end
        if (led3 > 0) then
            ws2812.write(string.char(0,0,0):rep(2) .. string.char(0,20,0))
            led3 = led3 -1
            return
        end
        tmr.alarm(0, 100, tmr.ALARM_AUTO, update) 
    else
        ws2812.write(string.char(00,0,0):rep(ledCount))   
    end
end
function wlaninit ()
    if(not wifi.sta.getip())then 
        tick = not tick;
        if(tick)then
            ws2812.write(string.char(0,0,20):rep(ledCount))
        else
            ws2812.write(string.char(0,0,0):rep(ledCount))
        end
        return;
    else
        if not hadIP then
          hadIP = true
          local IP = wifi.sta.getip()
          local o1,o2,o3,o4 = IP:match("(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)" )
          local eingabe = tonumber(o4)
          countBlinks(eingabe);         
          tmr.alarm(0, 1000, tmr.ALARM_AUTO, blinkIP)
        end
    end
end
tmr.alarm(0, 100, tmr.ALARM_AUTO, wlaninit)
 
 
== Eclipse - Computer ==
package berkay.light.cube;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class LightCube {
public static int LED_COUNT = 34;
private static ObjectMapper mapper;
private List<Led> leds = new ArrayList<>();   
private String host;
private int port;
private Socket outsocket;
 
public LightCube(String ip, int port) throws UnknownHostException, IOException { 
this.host = ip;
this.port = port;
for (int i = 0; i < LED_COUNT; i++) {
leds.add(new Led());
}
outsocket = new Socket(this.host, this.port);
}
 
private void send() throws Exception {
String json = mapper.writeValueAsString(leds);
System.out.println(json);
 
PrintStream jsonOut = new PrintStream(outsocket.getOutputStream());
jsonOut.print(json);
jsonOut.flush();
}
 
public static void main(String[] args) throws InterruptedException, UnknownHostException, IOException {
mapper = new ObjectMapper();
 
Scanner scanner = new Scanner(System.in);
 
LightCube l1 = new LightCube("192.168.2.109", 80);   
LightCube l2 = new LightCube("192.168.2.110", 80);
System.out.println("Verbindung erfolgreich");
while (true) {
try {
if (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(" ");
if (parts.length < 4 || parts.length > 5) {
throw new IllegalStateException("requires 3 or 4 numbers");
}
int remote = Integer.parseInt(parts[0]);
int r = Integer.parseInt(parts[1]);
int g = Integer.parseInt(parts[2]);
int b = Integer.parseInt(parts[3]);
LightCube ll = null;
if(remote == 0){
ll = l1;
}
if(remote == 1){
ll = l2;
}
int index = -1;
if(parts.length == 5){
index = Integer.parseInt(parts[4]);
}
if(index == -1){
//set all
for(Led led:ll.leds){
led.setColor(r, g, b);
}
}else{
if(index < 0 ){
index = 0;
}
if(index > LED_COUNT-1){
index = LED_COUNT-1;
}
Led led = ll.leds.get(index);
led.setColor(r, g, b);
}
}
l2.send();
l1.send();
} catch (Exception e) {
e.printStackTrace();
}
 
Thread.sleep(500);
}
}
 
}

Aktuelle Version vom 1. Dezember 2016, 22:37 Uhr

Dies ist ein Projekt, an dem momentan aktiv gearbeitet wird. Wenn du Fragen hast oder mitarbeiten möchtest, wende dich an Berkay.

Wenn du Fragen zum Projektcode hast, wende dich an Berkay