15. Interface and Application Programming¶
This is the assessment for this module:
Group assignment
- Compare as many tool options as possible.
Individual assignment
- Write an application that interfaces a user with an input and/or output device that you made.
Individual assignment¶
For this week I decided to make an Interface that could allow any user to blink a LED on the PCB I made during Input devices week.
I decided to use Python along with TKinter in order to create a basic Interface: two buttons that can turn the LED on or off. Before moving on with TKinter, I tried to do basic things with other libraries, such as:
The latter allowed me to create fancier interfaces:
Since I couldn’t find a proper way to make communicate my board with the PC, I kept looking on the web until I found useful material for connecting the board to my PC. This is when I decided to use TKinter.
Programming the Board¶
This is the code I flashed on the PCB through Arduino IDE:
#include <SoftwareSerial.h>
//defining which pins are going to be Receiver and Transmitter
SoftwareSerial mySerial (2, 1);
//creating a variable, a character that determines if the user selects on or off
char userInput;
void setup()
{
//setting pin3 as the output
pinMode(3, OUTPUT);
//setting the baudrate
mySerial.begin(9600);
}
void loop() {
delay (200);
//If the board receives an input from the serial
if (mySerial.available() > 0) {
//the board reads the input
userInput = mySerial.read();
//if the board receives a 1 message from the serial, the LED turns on
if(userInput == '1'){
digitalWrite(3, HIGH);
}
//if the board receives a 0 message from the serial, the LED turns off
if(userInput == '0'){
digitalWrite(3, LOW);
}
}
}
Coding with Python: Interface¶
I am still less than a beginner with Python, I intend to learn how to work with it in the following months. This is the code I used for my basic blink LED interface:
import serial #importing the pySerial
from tkinter import *
import tkinter as tk
#specifying the communication through FTDI, setting the specific port, baudrate and timeout
commPort = "COM9"
ser = serial.Serial(commPort, baudrate = 9600, timeout = 1)
#setting the functions
#the code on arduino is waiting for "on" as a serial input to turn on the LED
#b stands for a byte of data, that's what I am sending
def turnOnLED():
ser.write(b'1')
#the code on arduino is waiting for "off" as a serial input to turn off the LED
#also here b stands for a byte of data, that's what I am sending
def turnOffLED():
ser.write(b'0')
#creating the window with tkinter
root = Tk()
#giving a title to the window
root.title("Week 15 - Interface - Blink LED")
#defining and placing the buttons. First I have to create a widget, then I place it within the window.
btn_On= tk.Button(root, text="Turn on the LED", command=turnOnLED)
btn_On.grid(row=1, column=0)
btn_Off= tk.Button(root, text="Turn off the LED", command=turnOffLED)
btn_Off.grid(row=1, column=2)
#defining window's size
root.geometry("320x320")
#making sure the GUI continuously runs
root.mainloop()
This is how it works:
Group Assignment¶
Here below you can find a comparison between two python libraries: QT and Tkinter, and javascript’s node.js.
Python¶
TKInter¶
Good
-
Comes already bundled with Python on Windows.
-
The learning curve is relatively easy for simple stuff.
Bad
-
Looks kind of ugly and obsolete.
-
Can be frustrating/limiting for complex applications.
-
Documentation could be better.
QT¶
Good
-
Good cross-platform support.
-
Looks good and native on any platform.
-
There is good documentation online through communities.
-
There is also a Graphical UI creator (QT Creator).
Bad
-
Heavy libraries. while compiling QT adds about 20Mb of .dll/.pyd files to the result.
-
Often there is the need to use the C++ documentation and to “translate” it into Python.
-
The learning curve is quite steep, mastering requires years.
Javascript¶
Good
-
Speed: the execution of the program is quick as it saves the time required to connect to the server.
-
Is easy to understand and learn. The structure is simple for the users as well as the developers.
-
Since all modern browsers support JavaScript, it is seen almost everywhere.
-
Provides various interfaces to developers for creating catchy webpages.
-
JavaScript is now capable of front-end as well as back-end development.
Bad
-
Not so safe: Since the code is accessible to the user, they may use it for malicious purposes.
-
The browser interprets JavaScript differently in different browsers.
-
A single code error can stop the rendering of the entire JavaScript code on the website.
Node.js¶
Good
-
Easy Scalability.
-
The learning curve is not so steep.
-
The community is huge and active.
-
Caching: no need to re-execute the codes as caching allows applications to load the web pages faster and responds more swiftly to the user.
Bad
-
Application Programming Interface (API) is Not Stable.
-
Does not have a Strong Library Support system.