//microdev.gr //(C) Panos Papazoglou //Jan 2023 //*********************************************************************************** //Import Library for serial communication //*********************************************************************************** import processing.serial.*; Serial myPort; //*********************************************************************************** //Declare component for image manipulation //*********************************************************************************** PImage img; //*********************************************************************************** //Initial button state //*********************************************************************************** int button=0; //*********************************************************************************** //Initial color values //*********************************************************************************** int red=255, green=255, blue=255; int bred=0, bgreen=0, bblue=0; //*********************************************************************************** //Initial value for distance measurement //*********************************************************************************** int dist=100; //*********************************************************************************** //Initial values for setting reference coordinates //*********************************************************************************** int xinit=200; int y=200; int d=150; //*********************************************************************************** //Variables for potentiometer values //*********************************************************************************** int a=0,b=0,c=0,s1=0,s2=0,s3=0; void setup() { //*********************************************************************************** //Select full screen or windowed application //*********************************************************************************** //size(1000, 900); fullScreen(); //*********************************************************************************** //Load image //*********************************************************************************** img=loadImage("ti.jpg"); //*********************************************************************************** //Set application title (window title) //*********************************************************************************** surface.setTitle("Color Mixer - microdev.gr"); //*********************************************************************************** //Select correct port number (e.g. COM8, COM12). Use device manager or Arduino IDE //for finding the assigned port by OS //*********************************************************************************** myPort = new Serial(this, "COM8", 9600); } void draw() { //*********************************************************************************** //Read a line from serial port //*********************************************************************************** String inString=myPort.readStringUntil('\n'); //*********************************************************************************** //If data exist... //*********************************************************************************** if (inString!=null) { //*********************************************************************************** //Split received string using the character "." //*********************************************************************************** String[] num=splitTokens(inString,"."); //*********************************************************************************** //Reject any unwanted characters at the beginning of the received string //*********************************************************************************** char ic=' ', ic2=' '; ic=num[0].charAt(0); if (num[0].length()>1) ic2=num[0].charAt(1); if ((ic<'0') || (ic>'9') || (ic2<'0') || (ic2>'9')) a=0; else //RED F //*********************************************************************************** //Parse to INT all the receiced tokens //*********************************************************************************** a=Integer.parseInt(num[0]); //Μετατροπή και των υπόλοιπων χαρακτήρων που λαμβάνονται σε ακέραιες τιμές //GREEN F b=Integer.parseInt(num[1]); //BLUE F c=Integer.parseInt(num[2]); //RED B s1=Integer.parseInt(num[3]); //GREEN B s2=Integer.parseInt(num[4]); //BLUE B s3=Integer.parseInt(num[5]); button=Integer.parseInt(num[6]); dist=Integer.parseInt(num[7].trim()); //*********************************************************************************** //If the switch on the board is activated, set all colors (f,b) based on potentiometers //*********************************************************************************** if (button==1) { red=a; green=b; blue=c; bred=s1; bgreen=s2; bblue=s3; } else { bred=0; bgreen=0; bblue=0; } //*********************************************************************************** //Display messages on the console for debugging //*********************************************************************************** print("p1=",a,", "); print("p2=",b,", "); print("p3=",c,", "); println("button=",button); print("s1=",s1,", "); print("s2=",s2,", "); print("s3=",s3,", "); println("dist=",dist); println("----------------------------"); } //*********************************************************************************** //Set screen background color - the screen is cleared //*********************************************************************************** background(bred,bgreen,bblue); //*********************************************************************************** //Set additional variables for the coordinates that will be used later //*********************************************************************************** int x=xinit; int dx=45, dy=30; int ts=70; int tx=xinit-50; //*********************************************************************************** //Display main word based on the distance measurement (distance sensor) //*********************************************************************************** if ((dist>=3) && (dist<6)) { fill(red,green,blue); textSize(ts); text("Welcome",tx,dy+y+2*d); println("Welcome"); } if ((dist>=6) && (dist<12)) { fill(red,green,blue);textSize(ts); text("Digital Arts",tx,dy+y+2*d); println("Digital Arts"); } if ((dist>=12) && (dist<18)) { fill(red,green,blue);textSize(ts); text("Cinema",tx,dy+y+2*d); println("Cinema"); } if (dist>=18) { fill(red,green,blue);textSize(ts); text("Microdev.gr",tx,dy+y+2*d); println("Microdev.gr"); } //*********************************************************************************** //Display image //*********************************************************************************** image(img, tx, y-50+3*d); //*********************************************************************************** //Display histogram for RGB colors //*********************************************************************************** stroke(255); line(3*xinit,150+dy+y+2*d, 3*xinit+200,150+dy+y+2*d); stroke(255,0,0); line(3*xinit+20,150+dy+y+2*d,3*xinit+20,(150+dy+y+2*d)-red); stroke(0,255,0); line(3*xinit+40,150+dy+y+2*d,3*xinit+40,(150+dy+y+2*d)-green); stroke(0,0,255); line(3*xinit+60,150+dy+y+2*d,3*xinit+60,(150+dy+y+2*d)-blue); //*********************************************************************************** //Display histogram for RGB background colors //*********************************************************************************** stroke(255,0,0); line(3*xinit+120,150+dy+y+2*d,3*xinit+120,(150+dy+y+2*d)-bred); stroke(0,255,0); line(3*xinit+140,150+dy+y+2*d,3*xinit+140,(150+dy+y+2*d)-bgreen); stroke(0,0,255); line(3*xinit+160,150+dy+y+2*d,3*xinit+160,(150+dy+y+2*d)-bblue); fill(255); text("F",3*xinit+20,220+dy+y+2*d); text("B",3*xinit+120,220+dy+y+2*d); noFill(); noStroke(); String msg=""; //*********************************************************************************** //If the switch is activated, display an indication at the bottom of the screen //and two yellow rectangles on the displayed image //*********************************************************************************** if (button==1) { textSize(20); fill(red,green,blue); text("Control=POT",3*xinit+20,height-10); noFill(); stroke(255,255,0);strokeWeight(16); rect(xinit-50,y+3*d,250,80); rect(xinit-50+100,108+y+3*d,150,80); noStroke();strokeWeight(1); } else { textSize(20); fill(red,green,blue); text("Control=MOUSE",3*xinit+20,height-10); } fill(red,green,blue);textSize(30); text("COLOR MIXER V1.0 - Panos Papazoglou",10,50); //*********************************************************************************** //Draw RED circle //*********************************************************************************** fill(red,0,0); noStroke(); circle(x,y,d); //*********************************************************************************** //Draw buttons for RED //*********************************************************************************** draw_buttons(x,y,d); //*********************************************************************************** //Display color values for RED //*********************************************************************************** msg="red="+str(red); text(msg,x-dx,y-d+35); msg="Bred="+str(bred); dy=dy+30; text(msg,x-dx,y+d+dy); //*********************************************************************************** //Draw GREEN circle //*********************************************************************************** x=2*xinit; fill(0,green,0); noStroke(); circle(x,y,d); //*********************************************************************************** //Draw buttons for GREEN //*********************************************************************************** draw_buttons(x,y,d); //*********************************************************************************** //Display color values for GREEN //*********************************************************************************** msg="green="+str(green); text(msg,x-dx,y-d+35); msg="Bgreen="+str(bgreen); text(msg,x-dx,y+d+dy); //*********************************************************************************** //Draw BLUE circle //*********************************************************************************** x=3*xinit; fill(0,0,blue); noStroke(); circle(x,y,d); //*********************************************************************************** //Draw buttons for BLUE //*********************************************************************************** draw_buttons(x,y,d); //*********************************************************************************** //Display color values for BLUE //*********************************************************************************** msg="blue="+str(blue); text(msg,x-dx,y-d+35); msg="Bblue="+str(bblue); text(msg,x-dx,y+d+dy); //*********************************************************************************** //Draw mixed color circle //*********************************************************************************** x=4*xinit; fill(red, green, blue); noStroke(); circle(x,200,d); } //*********************************************************************************** //Draw buttons (call function more polite) //*********************************************************************************** void draw_buttons(int x, int y, int d) { draw_p(x+10,(y+d/2)+10,40,30,'+'); draw_p(x-50,(y+d/2)+10,40,30,'-'); draw_p(x+10,(y+d/2)+50,40,30,'S'); draw_p(x-50,(y+d/2)+50,40,30,'R'); } //*********************************************************************************** //Draw buttons //*********************************************************************************** void draw_p(int xstart, int ystart, int wd, int ht, char s) { fill(0); stroke(255); rect(xstart, ystart, wd, ht); fill(255); textSize(30); text(s, xstart+10, ystart+30); } //*********************************************************************************** //Check which button is clicked //*********************************************************************************** void mouseClicked() { //+ RED if ((mouseX>=xinit+10) && (mouseX<=xinit+40)) if ((mouseY>=(y+d/2)+10) && ((mouseY<=(y+d/2)+30))) { red=red+5; if (red>255) red=255; } //- RED if ((mouseX>=xinit-50) && (mouseX<=xinit-50+40)) if ((mouseY>=(y+d/2)+10) && ((mouseY<=(y+d/2)+30))) { red=red-5; if (red<0) red=0; } //SET RED if ((mouseX>=xinit+10) && (mouseX<=xinit+40)) if ((mouseY>=(y+d/2)+50) && ((mouseY<=(y+d/2)+70))) red=255; //RESET RED if ((mouseX>=xinit-50) && (mouseX<=xinit-50+40)) if ((mouseY>=(y+d/2)+50) && ((mouseY<=(y+d/2)+70))) red=0; //+ GREEN if ((mouseX>=(xinit*2)+10) && (mouseX<=(xinit*2)+40)) if ((mouseY>=(y+d/2)+10) && ((mouseY<=(y+d/2)+30))) { green=green+5; if (green>255) green=255; } //- GREEN if ((mouseX>=(xinit*2)-50) && (mouseX<=(xinit*2)-50+40)) if ((mouseY>=(y+d/2)+10) && ((mouseY<=(y+d/2)+30))) { green=green-5; if (green<0) green=0; } //SET GREEN if ((mouseX>=(xinit*2)+10) && (mouseX<=(xinit*2)+40)) if ((mouseY>=(y+d/2)+50) && ((mouseY<=(y+d/2)+70))) green=255; //RESET GREEN if ((mouseX>=(xinit*2)-50) && (mouseX<=(xinit*2)-50+40)) if ((mouseY>=(y+d/2)+50) && ((mouseY<=(y+d/2)+70))) green=0; //+ BLUE if ((mouseX>=(xinit*3)+10) && (mouseX<=(xinit*3)+40)) if ((mouseY>=(y+d/2)+10) && ((mouseY<=(y+d/2)+30))) { blue=blue+5; if (blue>255) blue=255; } //- BLUE if ((mouseX>=(xinit*3)-50) && (mouseX<=(xinit*3)-50+40)) if ((mouseY>=(y+d/2)+10) && ((mouseY<=(y+d/2)+30))) { blue=blue-5; if (blue<0) blue=0; } //SET BLUE if ((mouseX>=(xinit*3)+10) && (mouseX<=(xinit*3)+40)) if ((mouseY>=(y+d/2)+50) && ((mouseY<=(y+d/2)+70))) blue=255; //RESET BLUE if ((mouseX>=(xinit*3)-50) && (mouseX<=(xinit*3)-50+40)) if ((mouseY>=(y+d/2)+50) && ((mouseY<=(y+d/2)+70))) blue=0; } //*********************************************************************************** //If the ESC key is pressed, the the application is terminated //*********************************************************************************** void keyPressed() { if (key==27) exit(); }