Close

Processing on the Pi - Networking

A project log for Networked Low Resolution DMD Projectors

WiFi networked electro-mechanical 7x7 pixel flip-dot displays as giant DMDs (Digital Micromirror Devices) for low resolution projection

Michael ShaubMichael Shaub 02/25/2016 at 04:480 Comments

Date: 2/20/2016

With the sequences all animated and timers allowing them to run one after another and looping back to the beginning, the last step was to add the networking. When I was testing the programs, even on a single machine, I got a bunch of errors from incomplete messages. I ended up needing to put in catch statements throughout the "Client" sketch to deal with them. I attempted to save the previous successful frame to be used in place of any that had an error.

Below is the "Client" sketch that works with the Combined 5 sketch. You'll need to adjust the IP address of your Server.

// Client - Panel 2
// 
// For LinkSprite V3 and AlfaZeta flip-dot display
// by Michael Shaub 2016

import processing.serial.*;
import processing.net.*; 

Client c; 
String input;
String goodInput;
String lastInput = "0,0,0,0,0,0,0\n";
int data[] = new int[7]; 

int panelID = 2;
int timeout = 100; //how many frames of no signal from the server until I reboot myself

//switches
boolean drawPreview = true;
boolean useSerialPort = false;

// The serial port:
Serial myPort; 

int dotSize = 50; //diameter of the dots - for preview

int row1 = 0; //hold variable for row1
int row2 = 0; //hold variable for row2
int row3 = 0; //hold variable for row3
int row4 = 0; //hold variable for row4
int row5 = 0; //hold variable for row5
int row6 = 0; //hold variable for row6
int row7 = 0; //hold variable for row7

int dropConnectionCounter = 0;

void setup() { 
  size(350, 350); //draw a review of just this one panel
  background(35);
  noStroke();
  //frameRate(5);
  ellipseMode(CORNER);
  noStroke();
  
  // Connect to the server’s IP address and port­
  c = new Client(this, "127.0.0.1", 12345); // Replace with your server’s IP and port
  
  // Open the port you are using at the rate you want:
  if(useSerialPort) myPort = new Serial(this, Serial.list()[0], 57600);
} 

void draw() { 
    
  // Receive data from server
  if (c.available() > 0) {
    dropConnectionCounter = 0;
    String tempInput = c.readString();
    try{
      input = tempInput.substring(0,tempInput.indexOf("\n"));  // Only up to the newline
    } catch (StringIndexOutOfBoundsException e) {
      println("Error at step 1!");
      input = null;
    }
    if(input == null) {
      input = lastInput.substring(0,lastInput.indexOf("\n"));  // Only up to the newline
    }else{
      lastInput = tempInput;
    }
    
    //println( input );
    data = int(split(input, ','));  // Split values into an array
    // Draw line using received coords
    bitShiftData(); //crop the data needed for this board
    drawOnScreen(); //draw on screen
    printToFlipDots(); //send data out the the flipDot board
    
  }else{
    dropConnectionCounter++;
  }
  if(dropConnectionCounter > 100){
    dropConnectionCounter = 0;
    setup();
  }
}

void drawOnScreen(){
  background(35);
  for(int j=0; j<7; j++){
    //String tempBinary = binary(int(data[j]),7);
    String tempBinary;
    try{
      tempBinary = binary(int(data[j]),7);
    } catch (ArrayIndexOutOfBoundsException e) {
      tempBinary = binary(0,7); //just write a blank row
      println("Error at step 2!");
      
    }
    for(int i=0; i<7; i++){
      int dotValue = int( tempBinary.charAt(i) )-48; //for some reason 1=49 and 0=48
      fill( dotValue*255 ); //read pixel values, set fill color to that
      ellipse(i*dotSize,j*dotSize,dotSize,dotSize);
    }
  }
}

void bitShiftData(){
  for(int i=0; i<data.length; i++){
    
    int a = data[i]; //load value of this incoming data (one row)
    int b = a >> (panelID-1)*7; //bitshift the value based on which panel this is
    //int b = a;
    data[i] = b; //update value of data with the shifted one
    //println(data[i]);
  }
}

void printToFlipDots(){
  //int test_2[]= {0x80, 0x87, 0xFF, row1, row2, row3, row4, row5, row6, row7, 0x8F};
  String arraySet = "set";
  int test_2[] = {0x80, 0x87, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0x8F};
    
  for(int i = 3; i < 10; i++){
    try{
      test_2[i] = data[0-3];
    } catch (ArrayIndexOutOfBoundsException e) {
      println("Error at step 3!");
      arraySet = null;
    }
  }
  
  if(arraySet != null){ 
    for(int i=0; i<test_2.length; i++){
      if(useSerialPort) myPort.write(test_2[i]); 
    }
  }
  
  //reset row values for next frame
  row1 = 0;
  row2 = 0;
  row3 = 0;
  row4 = 0;
  row5 = 0;
  row6 = 0;
  row7 = 0;
}

Discussions