Interface and application programming:

This week assignment is to write an application that interfaces with an input &/or output device that you made, as well as comparing as many tool options as possible. So I started by choosing Processing as the softawre and the programming language is python as I have never used it before and the goal as always is to try different and new things every week.

So I started by reading this very useful book that we have in our FabLab which called Make: Getting Started with Processing.py, the book is very useful if you are going to make GUI with python Mode.

Processing doesnt include support for python programming language by default, so in order to enable it you need to install an add-on called Python Mode, you can do that by clicking on the drop down menu on the right side of the toolbar and selecting add mode.

So the book start with very basic things like how to draw a circle or a rectangle, also how to change the size of the window or the colors of the background. For example if you want to draw a window that will help you to show your work you have to use the size() function, so for example to draw an ellipse you need to type this code into your editor.

size(190, 190)
ellipse(80, 80, 80, 80)

And this thing will appear after running the code.

I also took this course about python on Codecademy which is very useful.

After that I started writing the code to make my GUI:


def setup():
    global port
    global font
    size(600,400)
    font = createFont("SourceCodePro-Regular.ttf",25)
    textFont(font)


def draw():
    global val

    background(0)
    fill(0,200,0)
    strokeWeight(4)
    rect(100,100,400,200)

    fill(0,26,51)
    ellipse(200,220,100,100)
    textSize(25)
    text("Press The Button",180,140)
    textSize(40)
    text("Mistir",285,230)

    if keyPressed:
        fill(255,0,0)
        ellipse(200,220,100,100)

and after a while I ended up with this Shown below.

And if you pressed any key it changes the color to red and shows this.

"Mistir" is what I like to call our Guru/friend Mr.Francisco, so it says " Press The Button Mistir" XD.

The only thing remaining now is to make this interact with an input or output device that I made, so I took the board that I used in the previous week which was Networking and Communications and the board was used as the receiver and the code was already uplouded to the board.

But Now I have to modify the the code that I wrote in processing to make it interact with the board via serial Communication. So the first thing was to import the serial library using this command add_library('serial') and after that open the serial port in the setup() port = Serial(this, "/dev/ttyUSB0", 9600) and later using this command port.write('M') I will send 'M' whenever the key is pressed, which is eventually if received by the other board will light the LED. Final code for processing:


add_library('serial')
port = None
val = 0.0

font = None

def setup():
    global port
    global font
    size(600,400)
    font = createFont("SourceCodePro-Regular.ttf",25)
    textFont(font)
    port = Serial(this, "/dev/ttyUSB0", 9600)

def draw():
    global val

    background(0)
    fill(0,200,0)
    strokeWeight(4)
    rect(100,100,400,200)

    fill(0,26,51)
    ellipse(200,220,100,100)
    textSize(25)
    text("Press The Button",180,140)
    textSize(40)
    text("Mistir",285,230)

    if keyPressed:
        fill(255,0,0)
        ellipse(200,220,100,100)
        port.write('M')


And I used the FTDI USB to Serial Adapter to communicate between the interface and the board in order to switch on the LED in the board, I did that and everything went well.

Previous Week |-----------| Next Week

Home Page

Attachments:

Sketch properties - Processing

Interface code Python - Processing

Output board code

Makefile