Fab Academy 2018 - Thierry Dassé

Week 13 : Interface and Application Programming

Input device aplication

For the interface assignment, I have chosen my input device because it can be very interesting to have a graphical visualisation of a sensor.
I used Processing because you can do fast simple applications with it. You now can program with different languages like Java (default) and Python.
I like Python very much but to improve myself in Java, I used this language.
The application has four buttons:

Input device final program is on week11 assignement.

import processing.serial.*; // library to use serial devices
 
Serial port;
String command = "";
int value = 0;

class Button { // rectangle frames you can click on
  int x,y,w,h; // x,y are bottom left corner coordinates
               // w,h are rectangle width and height
  String title,command; // title is button name
                        // command is string to launch to input device board when clicking the button

  Button(int x,int y,int w,int h,String title,String command) { //class constructor
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.title = new String(title);
    this.command = new String(command);
  }
  
  int isclicked(int mousex,int mousey) { // if button is pressed then send command to input device command
    if ((x<= mousex) && (mousex<=x+w) && (y<= mousey) && (mousey<=y+h)) {
        port.write(command); // send command to the device
        this.drawit(190); // change color button
        delay(100); // wait 0.1 second
        return 1;
    } else {
      return 0;
    }
  }
  
  void drawit() { // draw button with default value
    this.drawit(220);
  }
  
  void drawit(int c) { // draw the button
    stroke(180);
    fill(c);
    rect(x,y,w,h,5);
    fill(100);
    text(title,x+w/2,y+h/2);
  }
}
  
//buttons setup
Button bread = new Button(300,50,150,50,"Read","R");
Button blisten = new Button(300,150,150,50,"Listen","L");
Button bstop = new Button(300,250,150,50,"Stop","S");
Button bcalibrate = new Button(300,350,150,50,"Calibrate","C");
 
void setup() { // setup command
  size(500,500); //size the window
  background(255);
  port = new Serial(this,"/dev/ttyUSB0",9600); //open serial communication between computer and board
  strokeWeight(1);
  textAlign(CENTER,CENTER);
}

void draw() {
  background(255); //clear the screen
   //draw last FSR sensor level
  stroke(180);
  fill(220);
  rect(50,height-50-(value >> 2),100,value >> 2,5);
  if (value>80) {
    fill(100);
    text(str(value),100,height-50-(value >> 3));
  }
   // draw buttons
  bread.drawit();
  blisten.drawit();
  bstop.drawit();
  bcalibrate.drawit();
  
  if (mousePressed) { //if mouse pressed then transmit to buttons to execute commands
    bread.isclicked(mouseX,mouseY);
    blisten.isclicked(mouseX,mouseY);
    bstop.isclicked(mouseX,mouseY);
    bcalibrate.isclicked(mouseX,mouseY);
  }
  
  while (port.available() > 0) { //wait for new FSR sensor level from serial port
    int c = port.read();
    command = command + (char)c;
    if (c == '\n') {
      value = Integer.valueOf(command.trim());
      command = "";
    }
  }
}