< back to home

Interface and Application Programming

Goals

Board code

For this week assignment I decided to use my trusted hello board. My plan was to build a simple interface for toggling the led and keeping track of how button presses.
First of all I wrote the code for my board, for my idea was to make several interfaces, using different programming languages, sharing the same functionalities.
Here is my board code:

            
#include < SoftwareSerial.h >

#define TX 1
#define RX 0
#define LED 8
#define BTN 7

int leddino = 1;
int btnState = 0;
int btnStateOld = 0;
int presses = 0;

SoftwareSerial mySerial(RX, TX);
void setup() {
  mySerial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(BTN, INPUT_PULLUP);
  digitalWrite(LED, HIGH);
}

void loop() {
  while (mySerial.available() > 0) {
    leddino = mySerial.parseInt();
    if (leddino == 1) {
      digitalWrite(LED, HIGH);
    }
    else {
      digitalWrite(LED, LOW);
    }
  }
  delay(10);

  btnState = digitalRead(BTN);

  if (btnState != btnStateOld) {
    if (btnState == 0) {
      presses++;
      mySerial.println(presses);
    } 
    delay(50);
  }
  btnStateOld = btnState;
}
            
          

Nothing to see here, just your run-of-the-mill edge detection. The only thing worth nothing is in line 17, where I set my pin as output, enabling the internal pullup in order to prevent my pin to fluctuate randomly

PyQT5

I decided to use the QT framework and its python bindings. First of all I designed the interface using QT composer, a WYSIWYG editor

I then set up the proper virtual environment for my code (virtual environments in python3) and installed the PySerial and PyQT5 modules from the Python Package Index using pip ("source venv/bin/activate && pip install PyQT5 && pip install pyserial"). Here is the output of a "pip freeze":

            
(venv) sgrc@t420:~/fabacademy2017/doc/week16-interfaces/code/qt$ pip freeze
pkg-resources==0.0.0
PyQt5==5.8.2
pyserial==3.3
sip==4.19.2
            
          

And here is the code for the interface

            
#!/usr/bin/env python3

import serial
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QWidget, QDialog

serial_port = '/dev/ttyUSB0'
baudrate = 9600
ser = serial.Serial(serial_port, baudrate, write_timeout=1.0, timeout=1)

class hello_win(QDialog):
    def __init__(self, parent=None):
        super(hello_win, self).__init__(parent)
        self.initUI()

    def initUI(self):
        uic.loadUi('ui.ui', self)
        self.setWindowTitle('Hello board control panel')
        self.pushButton.clicked.connect(self.led_on)
        self.pushButton_2.clicked.connect(self.led_off)
        self.pushButton_3.clicked.connect(self.check_button)
        self.show()

    def led_on(self):
        ser.write(b'1')
        return ser.flush()

    def led_off(self):
        ser.write(b'0')
        return ser.flush()

    def check_button(self, check=0):
        try:
            check = int(ser.readline().decode("utf-8"))
        except ValueError:
            pass
        return self.lcdNumber.display(check)

################
##  Start it  ##
################
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = hello_win()
    sys.exit(app.exec_())
            
          

Quite self explantory. I could have used a QTimer class (with 0 delay) or a separate thread in order to check the button presses in (semi) real time, but I decided to keep it as simple as possible for the time being.
Here is a video of the finished product:

Conclusions

I haven't been able to test all the things I had in mind this week, and the lack of a js attempt is a disappointment; I hope I'll make amend as soon as possible.
This being said, I really liked using the python bindings for the QT: it's a powerful framework that gets things done quickly with a very good portability am

Files and Links