How can you disable the trun off screen mode in Processing 3? - processing

I just want my computer's screen not to turn off while a program is runnig. I know I can set it on my PC settings but I just want this app to do it.
Can anybody help me?
Thanks!

I found the following code here
import java.awt.Robot;
import java.awt.MouseInfo;
long robotLastMove = 0;
Robot robot=null;
setup(){
try{
robot = new Robot();
}catch(Exception e){e.printStackTrace();}
}
draw(){
long now = System.currentTimeMillis();
if(robot!=null && now-robotLastMove>1000*60*15){
//TODO: move back the mouse
int x = MouseInfo.getPointerInfo().getLocation().x;
int y = MouseInfo.getPointerInfo().getLocation().y;
//robot.
robot.mouseMove(x+2, y+2);
robot.mouseMove(x, y);
robotLastMove=now;
}
}
This moves the mouse automatically when your PC tries to sleep, but only a little. If you save the code inside the draw function from the above code into a different function, you can call it in the the draw to make the code look better. Like this:
import java.awt.Robot;
import java.awt.MouseInfo;
long robotLastMove = 0;
Robot robot=null;
setup(){
try{
robot = new Robot();
}catch(Exception e){e.printStackTrace();}
}
void stayAwake(){
long now = System.currentTimeMillis();
if(robot!=null && now-robotLastMove>1000*60*15){
//TODO: move back the mouse
int x = MouseInfo.getPointerInfo().getLocation().x;
int y = MouseInfo.getPointerInfo().getLocation().y;
//robot.
robot.mouseMove(x+2, y+2);
robot.mouseMove(x, y);
robotLastMove=now;
}
}
void draw(){
background(0);
stayAwake();
// the code you want to run without your pc falling asleep.
}
Good luck!

Related

Processing: running draw cycle into an independent thread

QUESTION
I've noticed that draw() cycle is interrupted by events elaboration.
In the following example the circle animation will stop at mouse click until the elaborating_function() ends.
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
}
void draw(){
background(#818B95);
//simple animation
fill(0,116,217);
circle(circle_x, 200, 50);
circle_x += animation_speed;
if(circle_x>800){ circle_x = 0; }
}
void mouseClicked() {
elaborating_function();
}
void elaborating_function(){
println("elaboration start");
delay(1000);
println("elaboration end");
}
Of course, a simple solution to run the elaboration without stopping the animation could be to thread("elaborating_function");
But my question is: if it is possible to run the draw cycle into an independent thread instead?
SOLUTION
I've found a possible solution inverting my problem and creating an "independent cycle" parallel to the draw one. Within this cycle is possible to run any function and it will not interfere with the draw execution. Every event triggered by the user needs only to set a specific variable in order to activate (once or more time) the function within the cycle.
int circle_x = 0;
int animation_speed = 5;
boolean call_elaborating_function = false;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
IndependentCycle independentCycle = new IndependentCycle();
independentCycle.setFrequency(1);
new Thread(independentCycle).start();
}
void draw(){
background(#818B95);
//simple animation
fill(0,116,217);
circle(circle_x, 200, 50);
circle_x += animation_speed;
if(circle_x>800){ circle_x = 0; }
}
public class IndependentCycle implements Runnable{
private int frequency; //execution per seconds
public IndependentCycle(){
frequency = 0;
}
public void setFrequency(int frequency){
this.frequency = 1000/frequency;
println(this.frequency);
}
public void run(){
while(true){
print(".");
delay(this.frequency);
//DO STUFF HERE
//WITH IF EVENT == ture IN ORDER TO RUN JUST ONCE !!
if(call_elaborating_function){
call_elaborating_function = false;
elaborating_function();
}
}
}
}
void mouseClicked() {
call_elaborating_function = true;
}
void elaborating_function(){
println("elaboration start");
delay(1000);
println("elaboration end");
}
As far as I know Processing has it's own AnimationThread.
Your proposed solution to thread elaborating_function() is great.
You could have a basic class that implements Runnable if you need a bit more control. With this thread running in parallel, Processing's main animation thread should run along side it just fine without pausing rendering.
This options sounds much simpler than trying to mess with Processing's AnimationThread and potentially have to deal with unexpected behaviour.
What is the actual goal you're trying achieve ?

How to fill a rectangle with some color along with printing a string in Processing console?

I'm developing a GUI for Arduino mega 2560 using Processing (control p5 library).
My board senses analog pin A0 and continuously displays its value as string in console. If a specific digital pin goes high then It sends the error string to processing console and waits for reset to be pressed.
Ex: A1-B1 error press reset
If A1-B1 is error then I want my GUI to fill the rectangle with red along with displaying string
" A1-B1 error press reset"
How to I do this?
Here's my processing code
import java.util.*;
import at.mukprojects.console.*;
Console console;
import processing.serial.*;
Serial port;
import controlP5.*;
ControlP5 cp5;
int myColorBackground = color(0, 0, 0);
float k,l;
String val;
int i;
char a;
void setup() {
size(800,600);
frame.setResizable(true);
smooth();
noStroke();
printArray(Serial.list());
port = new Serial(this,Serial.list()[0],9600);
port.bufferUntil(10);
cp5 = new ControlP5(this); //init gui lib
console = new Console(this); //init console
console.start();
}
void draw() {
background(myColorBackground);
fill(250, 131, 3); //text color
console.draw();
k= (width*0.75);
l=(0.25*height)-50;
fill(0);
stroke(250, 131, 1);
rect(k+20, l+20, 12,12);
fill(250, 131, 3);
textFont(font, 16);
text("A1-B1", k+100, l+20);
}
void serialEvent(Serial myPort) {
while(port.available()>0){
val = port.readStringUntil(10);
}
if (val!=null)
{
println(val);
}
}
The best advice we can give you is to break your problem down into smaller steps and take those pieces on one at a time.
For example, can you create a simple sketch that displays a message after the mouse has been clicked? Forget about the Arduino for a minute and just get this working by itself. It might look something like this:
boolean mouseWasPressed = false;
void draw(){
if(mouseWasPressed){
background(255, 0 , 0);
}
}
void mousePressed(){
mouseWasPressed = true;
}
Separately from that, get a sketch working that just shows the Arduino message in the console. It sounds like you might already have a lot of that done, but try to isolate it in a small example program.
When you have both of those working separately, then you can start thinking about combining them into one program. And if you get stuck, you can post a MCVE showing exactly which step you're stuck on. Good luck.

Make image disappear after X seconds (processing)

I'm currently working on a project in which I want an image to pop up after 3 seconds. Once that image has popped up the user has to click on the image to make a "done" image pop up that will disappear automatically after 3 seconds.
I've got most of it working except for the disappearing part. Does anyone know how I can time the image to disappear after 3 seconds?
PImage medic;
PImage medicD;
float time;
float startTime;
final int waitpopup = 3000;
final int DISPLAY_DURATION = 3000;
boolean showimage = true;
boolean showclock = true;
boolean showimagedone = true;
boolean hasClicked;
Clock clock;
void setup (){
size (1080, 1920);
medic = loadImage("medic.png");
medicD = loadImage("medicD.png");
clock = new Clock(width /2, height /2);
time = millis();
}
void draw() {
background (0);
imageMode(CENTER);
if (showclock) clock.display();
if (showimage && millis() - time > waitpopup) {
image(medic, width/2, height/2, 540, 540);
} if (hasClicked == true) {
showimage = false;
image(medicD, width/2, height/2, 540, 540);
} if (millis() > startTime + DISPLAY_DURATION) {
showimagedone = false;
}
}
void mousePressed() {
hasClicked = true;
startTime = time;
}
You can use the millis() function or the frameCount variable to check how much time has gone by, then do something after X seconds or after X frames.
You're already doing some of the work with the showimagedone variable, but you need to use that variable to conditionally draw your image.
I recommend starting with a simpler example and getting that working. Here's one example:
int clickedFrame;
boolean on = false;
int duration = 60;
void draw(){
if(on){
background(255);
if(frameCount > clickedFrame + duration){
on = false;
}
}
else{
background(0);
}
}
void mousePressed(){
clickedFrame = frameCount;
on = true;
}
This code show a white background for one second whenever the user clicks the mouse. You need to do something similar with your images.
Related posts:
How to make a delay in processing project?
How can I draw only every x frames?
Removing element from ArrayList every 500 frames
Timing based events in Processing
How to add +1 to variable every 10 seconds in Processing?
How to create something happen when time = x
making a “poke back” program in processing
Processing: How do i create an object every “x” time
Timer using frameRate and frame counter reliable?
Adding delay in Processing
Please also consult the Processing reference for more information.
If you still can't get it working, please post a MCVE (not your full project!) in a new question and we'll go from there. Good luck.

Kinect infra image not showing - why?

I have installed openni2.2, nite2.2 and kinect SDK 1.6 along with Simpleopenni library for processing. Everything working fine except infrared image - it is simply not there. That is really strange since at the same time I can clearly see the depth image (and depthimage logically need the infra camera and projector working to run). So I assume there is a problem with drivers or software? I would like to use kinect as infrared camera. Please help, below I attach my test code:
/* --------------------------------------------------------------------------
* SimpleOpenNI IR Test
* --------------------------------------------------------------------------
* Processing Wrapper for the OpenNI/Kinect library
* http://code.google.com/p/simple-openni
* --------------------------------------------------------------------------
* prog: Max Rheiner / Interaction Design / zhdk / http://iad.zhdk.ch/
* date: 02/16/2011 (m/d/y)
* ----------------------------------------------------------------------------
*/
import SimpleOpenNI.*;
SimpleOpenNI context;
void setup()
{
context = new SimpleOpenNI(this);
// enable depthMap generation
if(context.enableDepth() == false)
{
println("Can't open the depthMap, maybe the camera is not connected!");
exit();
return;
}
// enable ir generation
if(context.enableIR() == false)
{
println("Can't open the depthMap, maybe the camera is not connected!");
exit();
return;
}
background(200,0,0);
size(context.depthWidth() + context.irWidth() + 10, context.depthHeight());
}
void draw()
{
// update the cam
context.update();
// draw depthImageMap
image(context.depthImage(),0,0);
// draw irImageMap
image(context.irImage(),context.depthWidth() + 10,0);
}
This does the job:
context.enableIR(1,1,1);
I have the exact same issue.
It's not a solution but the closest I can get to getting an infra-red image from the kinect is by getting the point cloud from the depth Image
That soltuion is here
import SimpleOpenNI.*;
import processing.opengl.*;
SimpleOpenNI kinect;
void setup()
{
size( 1024, 768, OPENGL);
kinect = new SimpleOpenNI( this );
kinect.enableDepth();
}
void draw()
{
background( 0);
kinect.update();
image(kinect.depthImage(),0,0,160,120);//check depth image
translate( width/2, height/2, -1000);
rotateX( radians(180));
stroke(255);
PVector[] depthPoints = kinect.depthMapRealWorld();
//the program get stucked in the for loop it loops 307200 times and I don't have any points output
for( int i = 0; i < depthPoints.length ; i+=4)//draw point for every 4th pixel
{
PVector currentPoint = depthPoints[i];
if(i == 0) println(currentPoint);
point(currentPoint.x, currentPoint.y, currentPoint.z );
}
}
Are you able to capture the infrared stream, but you just can't see it?
Then the issue might be RANGE (which it should be in [0, 255]).
I had this issue in Python and C++; I solved it by dividing the array by the range (max-min) and then multiply all entries by 255.
user3550091 is right!
For reference here is my complete working code (Processing+OpenNI):
import SimpleOpenNI.*;
SimpleOpenNI context;
void setup(){
size(640 * 2 + 10, 480);
context = new SimpleOpenNI(this);
if(context.isInit() == false){
println("fail");
exit();
return;
}
context.enableDepth();
// enable ir generation
//context.enableIR(); old line
context.enableIR(1,1,1); //new line
background(200,0,0);
}
void draw(){
context.update();
image(context.depthImage(),context.depthWidth() + 10,0);
image(context.irImage(),0,0);
}

Create more than one window of a single sketch in Processing

How to create more than one window of a single sketch in Processing?
Actually I want to detect and track a particular color (through webcam) in one window and display the detected co-ordinates as a point in another window.Till now I'm able to display the points in the same window where detecting it.But I want to split it into two different windows.
You need to create a new frame and a new PApplet... here's a sample sketch:
import javax.swing.*;
SecondApplet s;
void setup() {
size(640, 480);
PFrame f = new PFrame(width, height);
frame.setTitle("first window");
f.setTitle("second window");
fill(0);
}
void draw() {
background(255);
ellipse(mouseX, mouseY, 10, 10);
s.setGhostCursor(mouseX, mouseY);
}
public class PFrame extends JFrame {
public PFrame(int width, int height) {
setBounds(100, 100, width, height);
s = new SecondApplet();
add(s);
s.init();
show();
}
}
public class SecondApplet extends PApplet {
int ghostX, ghostY;
public void setup() {
background(0);
noStroke();
}
public void draw() {
background(50);
fill(255);
ellipse(mouseX, mouseY, 10, 10);
fill(0);
ellipse(ghostX, ghostY, 10, 10);
}
public void setGhostCursor(int ghostX, int ghostY) {
this.ghostX = ghostX;
this.ghostY = ghostY;
}
}
One option might be to create a sketch twice the size of your original window and just offset the detected coordinates by half the sketch's size.
Here's a very rough code snippet (assumming blob will be a detected color blob):
int camWidth = 320;
int camHeight = 240;
Capture cam;
void setup(){
size(camWidth * 2,camHeight);
//init cam/opencv/etc.
}
void draw(){
//update cam and get data
image(cam,0,0);
//draw
rect(camWidth+blob.x,blob.y,blob.width,blob.height);
}
To be honest, it might be easier to overlay the tracked information. For example, if you're doing color tracking, just display the outlines of the bounding box of the tracked area.
If you really really want to display another window, you can use a JPanel.
Have a look at this answer for a running code example.
I would recommend using G4P, a GUI library for Processing that has some functionality built in for handling multiple windows. I have used this before with a webcam and it worked well. It comes with a GWindow object that will spawn a window easily. There is a short tutorial on the website that explains the basics.
I've included some old code that I have that will show you the basic idea. What is happening in the code is that I make two GWindows and send them each a PImage to display: one gets a webcam image and the other an effected image. The way you do this is to augment the GWinData object to also include the data you would like to pass to the windows. Instead of making one specific object for each window I just made one object with the two PImages in it. Each GWindow gets its own draw loop (at the bottom of the example) where it loads the PImage from the overridden GWinData object and displays it. In the main draw loop I read the webcam and then process it to create the two images and then store them into the GWinData object.
Hopefully that gives you enough to get started.
import guicomponents.*;
import processing.video.*;
private GWindow window;
private GWindow window2;
Capture video;
PImage sorted;
PImage imgdif; // image with pixel thresholding
MyWinData data;
void setup(){
size(640, 480,P2D); // Change size to 320 x 240 if too slow at 640 x 480
// Uses the default video input, see the reference if this causes an error
video = new Capture(this, 640, 480, 24);
numPixels = video.width * video.height;
data = new MyWinData();
window = new GWindow(this, "TEST", 0,0, 640,480, true, P2D);
window.isAlwaysOnTop();
window.addData(data);
window.addDrawHandler(this, "Window1draw");
window2 = new GWindow(this, "TEST", 640,0 , 640,480, true, P2D);
window2.isAlwaysOnTop();
window2.addData(data);
window2.addDrawHandler(this, "Window2draw");
loadColors("64rev.csv");
colorlength = mycolors.length;
distances = new float[colorlength];
noCursor();
}
void draw()
{
if (video.available())
{
background(0);
video.read();
image(video,0,0);
loadPixels();
imgdif = get(); // clones the last image drawn to the screen v1.1
sorted = get();
/// Removed a lot of code here that did the processing
// hand data to our data class to pass to other windows
data.sortedimage = sorted;
data.difimage = imgdif;
}
}
class MyWinData extends GWinData {
public PImage sortedimage;
public PImage difimage;
MyWinData(){
sortedimage = createImage(640,480,RGB);
difimage = createImage(640,480,RGB);
}
}
public void Window1draw(GWinApplet a, GWinData d){
MyWinData data = (MyWinData) d;
a.image(data.sortedimage, 0,0);
}
public void Window2draw(GWinApplet a, GWinData d){
MyWinData data = (MyWinData) d;
a.image(data.difimage,0,0);
}

Resources