Interface and Application Programming
The sixteenth lecture on Wednesday May 17th was about Interface and Application Programming: languages, device/data/user interfaces, graphics, multimedia, math, performance. Assignment given by Neil for this sixteenth week was:
After exploring different interface programming options, not having technical or engineering background, I decided to take it easy and build a simple Fade-IN/Fade-OUT interface in Python, and make it communicate through serial interface: pushing buttons on the User Interface LED on the board become more on less bright. I won't go through the whole PCB production process since I already had the board from Week 06, about board firmware I used the very same node-B.ino firmware from Week 15.
Here's the Python code. Setting up libraries (opted for GTK3Graphical User Interface libraries)
import gi import serial, time import os gi.require_version('Gtk', '3.0') from gi.repository import Gtk os.system('clear')
Setting up serial interface
ser = serial.Serial( port='/dev/ttyUSB0', baudrate=9600, ) print(ser.name) ser.isOpen() time.sleep(1)
Declaring the main class and setting up the basic window layout
class ButtonWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Comm Software") self.set_border_width(20) hbox = Gtk.Box(spacing=20) self.add(hbox)
Adding three buttons to the window layout
button = Gtk.Button.new_with_label("Fade IN") button.connect("clicked", self.on_fade_in_clicked) hbox.pack_start(button, True, True, 0) button = Gtk.Button.new_with_label("Fade OUT") button.connect("clicked", self.on_fade_out_clicked) hbox.pack_start(button, True, True, 0) button = Gtk.Button.new_with_mnemonic("_Close") button.connect("clicked", self.on_close_clicked) hbox.pack_start(button, True, True, 0)
Sending to serial interface command (different for each button), waiting for the board answer and printing out answer itself; last button also exits the program
def on_fade_in_clicked(self, button): print("Local software: \"Fade IN\" button was clicked") serialcmd = "dim_set:+;" ser.write(serialcmd.encode()) time.sleep(1) data_raw = ser.readline() print(data_raw.decode()) def on_fade_out_clicked(self, button): print("Local software: \"Fade OUT\" button was clicked") serialcmd = "dim_set:-;" ser.write(serialcmd.encode()) time.sleep(1) data_raw = ser.readline() print(data_raw.decode()) def on_close_clicked(self, button): print("Local software: Closing application") serialcmd = "dim_set:0;" ser.write(serialcmd.encode()) time.sleep(1) data_raw = ser.readline() print(data_raw.decode()) ser.close() Gtk.main_quit()
Adding standard GTK3 lines
win = ButtonWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()
Here's a "Hero Shot" of the application GUI
and here's a small "Hero Video"