Unfortunately i still have to complete my input/output devices, anyway i already
writed some of the firmwares so i was able to write an application to control and
debug my output board.
I have previous expirience wi pyGTK so it was a easy choice... and python development
can be very quick :)
First step is to call required libraries
import gi
import sys
import serial, time
import os
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
Then i set up the serial connection, serial device is hardcoded but i don't think
it's a problem because it's easy to modify the script.
Anyway i thinking about implement serial device selection with a GUI.
os.system('clear')
#Set up serial connection options
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
)
print(ser.name)
ser.isOpen()
time.sleep(1)
Here i build a new GTK window object, every GUI widget and logic in my simple
application has to be here.
I can also set up default size of the window.
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="Led output debug", application=app)
self.set_default_size(300, 100)
self.set_border_width(10)
self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(self.box)
This is a switch button to enable/disable LED output.
Whenever the button is pressed "on_enable_clicked" function is called.
self.enable_switch = Gtk.Switch()
self.enable_switch.set_active(False)
self.enable_switch.connect("notify::active", self.on_enable_clicked)
self.enable_frm = Gtk.Frame()
self.enable_frm.set_label("Enable switch")
self.box.pack_start(self.enable_frm, True, True, 0)
self.enable_frm.add(self.enable_switch)
Here I set up the slider to control the Vu-meter like behaviour of the output device.
"on_scale_moved" function is called when the slider is moved.
slider_options = Gtk.Adjustment(0, 0, 100, 5, 10, 0)
self.led_scale = Gtk.Scale(
orientation=Gtk.Orientation.HORIZONTAL, adjustment=slider_options)
self.led_scale.set_digits(0)
self.led_scale.set_hexpand(True)
self.led_scale.set_valign(Gtk.Align.START)
self.led_scale.connect("value-changed", self.on_scale_moved)
self.slider_frm = Gtk.Frame()
self.slider_frm.set_label("LED output slider")
self.box.pack_end(self.slider_frm, True, True, 0)
self.slider_frm.add(self.led_scale)
These are the function called by widgets, i print a message in the terminal for debug, set up a command and send it through serial.
def on_enable_clicked(self, switch, gparam):
if switch.get_active():
state = "on"
else:
state = "off"
print("Local software: LEDs " + state + ".")
serialcmd = "enable_set:" + state + ";"
ser.write(serialcmd.encode())
time.sleep(1)
def on_scale_moved(self, event):
print("Local software: LED output slider was moved to " + str(int(self.led_scale.get_value())) + "%.")
serialcmd = "output_set:"+str(int(self.led_scale.get_value()))+";"
ser.write(serialcmd.encode())
For an easy application like this there is nothing important here, this is a standard code snippet to enable a GTK application.
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
Last step: initialize the application and handle the exis status when the window is closed.
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)