java - Processing blocks drawing function while writing to the serial port -
in processing i've wrote simple program gets pixel of image , sends it's values serial port. done inside draw function , iterates through pixel array in each draw event.
so image of 200 x 200 there 40000 pixels , draw function called 40000 times. don't see result of change make during processing. after 30 seconds data serialized , changes becomes visible.
what need in order draw , see result during writing serial? asynchronous thread solution? i've tried this, , calling redraw method, nothing seems help.
for 200x200 image loop through 40000 pixels, shouldn't need call draw() function often. can have loop each each pixel running once per draw() call, in case pixels changing, otherwise, can cache pixel values once in setup()
regarding writing serial, shouldn't complicated. here's proof of concept sketch illustrating 1 way of writing thread writes 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 test pattern for(int = 0 ; < 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 serial port"); for(int = 0 ; < np; i++) serial.write((int)brightness(image.pixels[i])); } //implement runnable - can run thread, alternatively extend thread class serialthread implements runnable{ serial serial; boolean running = false;//keep tread running int data;//store byte send boolean hasdata;//keep track if recent data 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) , usb cable"); e.printstacktrace(); } } //handled public void run(){ while(running){//endless loop keep thread running if(hasdata){//if there data write if(serial != null) serial.write(data); //send via serial, if port open hasdata = false;//mark data sent } } } public void write(int data){ this.data = data; hasdata = true; } }
Comments
Post a Comment