简体   繁体   中英

Processing blocks drawing function while writing to the serial port

In Processing I've wrote a simple program which gets all pixel of a image and sends it's values to the serial port. This is done inside the draw function and it iterates through the pixel array in each draw event.

So for a image of 200 x 200 there are 40000 pixels and the draw function is called 40000 times. But I don't see the result of any change I make during this processing. After 30 seconds the data is serialized and only then all changes becomes visible.

What do I need in order to draw and see immediately the result during the writing to serial? Could a asynchronous thread be a solution? I've also tried this, and calling the redraw method, but nothing seems to help.

For a 200x200 image you will loop through 40000 pixels, but you shouldn't need to call the draw() function this often. You can have a loop for each each pixel running once per draw() call, in case your pixels are actually changing, otherwise, you can cache the pixel values once in setup()

Regarding writing to serial, it shouldn't be too complicated. Here's a proof of concept sketch illustrating one way of writing a Thread that writes to serial in parallel:

import processing.serial.*;

int w = 200;
int h = 200;
int np = w*h;//total number of pixels

PImage image;

SerialThread serial;

void setup(){
  image = createImage(w,h,ALPHA);
  //draw a test pattern
  for(int i = 0 ; i < np; i++) image.pixels[i] = color(tan(i) * 127);
  //setup serial 
  serial = new SerialThread(this,Serial.list()[0],115200);
}
void draw(){
  image(image,0,0);
}
void mousePressed(){
  println("writing pixel data to serial port");
  for(int i = 0 ; i < np; i++) serial.write((int)brightness(image.pixels[i]));
}
//implement runnable - can run as thread, alternatively extend Thread
class SerialThread implements Runnable{

  Serial serial;

  boolean running = false;//keep the tread running

  int data;//store a byte to send
  boolean hasData;//keep track if the most recent data was written

  SerialThread(PApplet parent,String port,int baudrate){
    try{
      //setup serial
      this.serial = new Serial(parent,port,baudrate);
      //setup thread
      running = true;
      new Thread(this).start();
    }catch(Exception e){
      System.err.println("error opening serial port! please check settings (port/baudrate) and the usb cable");
      e.printStackTrace();
    } 
  }
  //handled
  public void run(){
    while(running){//endless loop to keep the thread running
      if(hasData){//if there is data to write
        if(serial != null) serial.write(data); //send it via serial, if the port is open
        hasData = false;//mark that the data was sent
      }
    }
  }

  public void write(int data){
    this.data = data;
    hasData = true;
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM