processing sketch

Home Forums Procoding processing sketch

This topic contains 2 replies, has 1 voice, and was last updated by  audiocommander 2 years, 5 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1093

    MARCELO

    I need to connect to a fadecandy server. Any suggestions? Thank you!
    Here is the code:

    import java.net.*;
    import java.util.Arrays;

    OPC opc;

    void setup()
    {
    size(640, 360);

    // Connect to the local instance of fcserver
    opc = new OPC(this, “192.168.2.1”, 7890);

    // Map an 8×8 grid of LEDs to the center of the window
    opc.ledGrid8x8(0, width/2, height/2, height / 12.0, 0, false);
    }

    void draw()
    {
    background(0);

    // Draw the image, centered at the mouse location
    float dotSize = height * 0.5;
    fill(204, 102, 0);
    ellipse ( mouseX , mouseY , dotSize, dotSize);
    }

    public class OPC
    {
    Socket socket;
    OutputStream output;
    String host;
    int port;

    int[] pixelLocations;
    byte[] packetData;
    byte firmwareConfig;
    String colorCorrection;
    boolean enableShowLocations;

    OPC(PApplet parent, String host, int port)
    {
    this.host = host;
    this.port = port;
    this.enableShowLocations = true;
    parent.registerDraw(this);
    }

    // Set the location of a single LED
    void led(int index, int x, int y)
    {
    // For convenience, automatically grow the pixelLocations array. We do want this to be an array,
    // instead of a HashMap, to keep draw() as fast as it can be.
    if (pixelLocations == null) {
    pixelLocations = new int[index + 1];
    } else if (index >= pixelLocations.length) {
    pixelLocations = Arrays.copyOf(pixelLocations, index + 1);
    }

    pixelLocations[index] = x + width * y;
    }

    // Set the location of several LEDs arranged in a strip.
    // Angle is in radians, measured clockwise from +X.
    // (x,y) is the center of the strip.
    void ledStrip(int index, int count, float x, float y, float spacing, float angle, boolean reversed)
    {
    float s = sin(angle);
    float c = cos(angle);
    for (int i = 0; i < count; i++) {
    led(reversed ? (index + count – 1 – i) : (index + i),
    (int)(x + (i – (count-1)/2.0) * spacing * c + 0.5),
    (int)(y + (i – (count-1)/2.0) * spacing * s + 0.5));
    }
    }

    // Set the location of several LEDs arranged in a grid. The first strip is
    // at 'angle', measured in radians clockwise from +X.
    // (x,y) is the center of the grid.
    void ledGrid(int index, int stripLength, int numStrips, float x, float y,
    float ledSpacing, float stripSpacing, float angle, boolean zigzag)
    {
    float s = sin(angle + HALF_PI);
    float c = cos(angle + HALF_PI);
    for (int i = 0; i > 8);
    header[3] = (byte)(packetLen & 0xFF);
    header[4] = 0x00; // System ID high byte
    header[5] = 0x01; // System ID low byte
    header[6] = 0x00; // Command ID high byte
    header[7] = 0x01; // Command ID low byte

    try {
    output.write(header);
    output.write(content);
    } catch (Exception e) {
    dispose();
    }
    }

    // Automatically called at the end of each draw().
    // This handles the automatic Pixel to LED mapping.
    // If you aren’t using that mapping, this function has no effect.
    // In that case, you can call setPixelCount(), setPixel(), and writePixels()
    // separately.
    void draw()
    {
    if (pixelLocations == null) {
    // No pixels defined yet
    return;
    }

    if (output == null) {
    // Try to (re)connect
    connect();
    }
    if (output == null) {
    return;
    }

    int numPixels = pixelLocations.length;
    int ledAddress = 4;

    setPixelCount(numPixels);
    loadPixels();

    for (int i = 0; i > 16);
    packetData[ledAddress + 1] = (byte)(pixel >> 8);
    packetData[ledAddress + 2] = (byte)pixel;
    ledAddress += 3;

    if (enableShowLocations) {
    pixels[pixelLocation] = 0xFFFFFF ^ pixel;
    }
    }

    writePixels();

    if (enableShowLocations) {
    updatePixels();
    }
    }

    // Change the number of pixels in our output packet.
    // This is normally not needed; the output packet is automatically sized
    // by draw() and by setPixel().
    void setPixelCount(int numPixels)
    {
    int numBytes = 3 * numPixels;
    int packetLen = 4 + numBytes;
    if (packetData == null || packetData.length != packetLen) {
    // Set up our packet buffer
    packetData = new byte[packetLen];
    packetData[0] = 0; // Channel
    packetData[1] = 0; // Command (Set pixel colors)
    packetData[2] = (byte)(numBytes >> 8);
    packetData[3] = (byte)(numBytes & 0xFF);
    }
    }

    // Directly manipulate a pixel in the output buffer. This isn’t needed
    // for pixels that are mapped to the screen.
    void setPixel(int number, color c)
    {
    int offset = 4 + number * 3;
    if (packetData == null || packetData.length > 16);
    packetData[offset + 1] = (byte) (c >> 8);
    packetData[offset + 2] = (byte) c;
    }

    // Read a pixel from the output buffer. If the pixel was mapped to the display,
    // this returns the value we captured on the previous frame.
    color getPixel(int number)
    {
    int offset = 4 + number * 3;
    if (packetData == null || packetData.length < offset + 3) {
    return 0;
    }
    return (packetData[offset] << 16) | (packetData[offset + 1] << 8) | packetData[offset + 2];
    }

    // Transmit our current buffer of pixel values to the OPC server. This is handled
    // automatically in draw() if any pixels are mapped to the screen, but if you haven't
    // mapped any pixels to the screen you'll want to call this directly.
    void writePixels()
    {
    if (packetData == null || packetData.length == 0) {
    // No pixel buffer
    return;
    }
    if (output == null) {
    // Try to (re)connect
    connect();
    }
    if (output == null) {
    return;
    }

    try {
    output.write(packetData);
    } catch (Exception e) {
    dispose();
    }
    }

    void dispose()
    {
    // Destroy the socket. Called internally when we've disconnected.
    if (output != null) {
    println("Disconnected from OPC server");
    }
    socket = null;
    output = null;
    }

    void connect()
    {
    // Try to connect to the OPC server. This normally happens automatically in draw()
    try {
    socket = new Socket(host, port);
    socket.setTcpNoDelay(true);
    output = socket.getOutputStream();
    println("Connected to OPC server");
    } catch (ConnectException e) {
    dispose();
    } catch (IOException e) {
    dispose();
    }

    sendColorCorrectionPacket();
    sendFirmwareConfigPacket();
    }
    }

    #1096

    audiocommander
    Keymaster

    Hi Marcelo,

    I’m sorry, I don’t know facecandy, but processing.js runs in a browser and is translated to JavaScript. Therefore it does not support JAVA libraries like java.net or OPC. And there are no sockets on JavaScript either.

    You can use XHR to connect to a HTTP server and send and receive XML or (preferably) JSON. There are also WebSockets (client-to-server only, not client-to-client). But to my knowledge you might need to use JavaScript for that.

    Not sure if this helps?

    Regards,
    Michael

    #1097

    audiocommander
    Keymaster

    I’ve just seen the Facecandy readme and it says that you can connect via WebSockets. This topic might help you to get started: http://www.audiocommander.de/blog/forums/topic/udp-socket/

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘processing sketch’ is closed to new replies.