viernes, 16 de marzo de 2012

Simple Button Class for Processing


Abstract

Continuing with our goal of communicating with arduino like micros, this time I brought you with a simple button class to use with processing. I've made a simple example of how to use it and also I have created a more elaborated example, using the buttons to turn on/off leds of my teensy.





Simple Button Class for Processing

The idea is to use it, as is, with no knowlegde about how to code a button... that' s why this class is like a library.

DISCLAIMER **
THIS SOFTWARE IS PROVIDED TO YOU "AS IS," AND WE MAKE NO EXPRESS OR IMPLIED WARRANTIES WHATSOEVER WITH RESPECT TO ITS FUNCTIONALITY, OPERABILITY, OR USE, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR INFRINGEMENT. WE EXPRESSLY DISCLAIM ANY LIABILITY WHATSOEVER FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, INCIDENTAL OR SPECIAL DAMAGES, INCLUDING, WITHOUT LIMITATION, LOST REVENUES, LOST PROFITS, LOSSES RESULTING FROM BUSINESS INTERRUPTION OR LOSS OF DATA, REGARDLESS OF THE FORM OF ACTION OR LEGAL THEORY UNDER WHICH THE LIABILITY MAY BE ASSERTED, EVEN IF ADVISED OF THE POSSIBILITY OR LIKELIHOOD OF SUCH DAMAGES.

A simple example in the following lines:


ADbutton button1;
 boolean pressed=false;
 void setup()
 {
     size(290,120);
     smooth();
     button1 = new ADbutton(80, 40, 150, 30, 7, "Press Me");
 }
 void buttonRun()
 {
     if (pressed)
       button1.label("Press Me");
     else
       button1.label("unPress Me");  
     pressed=!pressed; 
 }
 void draw()
 {
     background(#04583F);
     if (button1.update())
        buttonRun();
 }

A very simple approach to use button to run functions each time a button is pressed. This example instantiate button1,inside setup(), based on ADbutton class indicating (x,y) position on the screen, (width,height) of the button, radius of the rounded corners of the button and the label.

Then, on function draw() we run button1.update() that has to goals: the first one is to draw the button itself and the second one is to return a boolean indicating if the button was pressed.

If the button was pressed is evaluated at the if statement. If the returned value is true it runs buttonRun() function.

In the buttonRun() function we evaluate the boolean pressed variable to change the label of the button, so it behaves like a push button.

The better way I found to use the class is to create a tab on processing, copy and paste the code and then create your program in the main tab using the class.

The video shows the quick deploy of the class...



Download: buttonClass.pde
Download: arrayOfButtons.pde

No hay comentarios:

Publicar un comentario