ProxyLCD in cli on Raspberry Pi

We have a nice desktop application with nice GUI but this is not very useful on Raspberry Pi. Today we will work on adding option to run without GUI. Only server and data transfer.

How to manage LCD’s without GUI? Manually of course in the ini file 🙂

Planing

Application should have one executable. So parameter will decide if GUI should be started or not. Good name for parameter is –cli.
To handle parameters we will use a buildin library called getopt.
There is one catch. We will still use PyQt5 library even without a display. We have to many pyqt usages to change them all. Signals, threads, server, main loop just to name a few. It works nicely so I do not see reasons for changing it. To my surprise pyqt can work without gui. Nice 🙂

PyQt5 is available for Raspberry Pi just install it via

sudo apt-get install python3-pyqt5

CLI option

There is one file that require changes, it is a main.py:


#!/usr/bin/python3
# -*- coding: utf-8 -*-


import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QCoreApplication
from service.tcp_server import StreamServer
from service.stream_content import StreamContent
from repository.config import Config
import getopt
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

CLI = False


def parse_argv():
    global CLI
    try:
        params, args = getopt.getopt(sys.argv[1:], "c", ['cli'])
    except getopt.GetoptError as err:
        print(err)
        sys.exit(2)

    for o, v in params:
        if o == '--cli':
            CLI = True

See that we are importing QCoreApplication. It allows us to work without X server.
What may surprise you is importing signals module. Problem is with passing signals from Python to C++ extension, it our case PyQt. So without this trick, when we run a script there is no way of stopping it with ctrl+c. That is why we use signals.
Next we have function that parse arguments and sets CLI variable.

if __name__ == '__main__':
    parse_argv()
    config = Config()

    if CLI:
        app = QCoreApplication(sys.argv)
    else:
        app = QApplication(sys.argv)

    stream_server = StreamServer(('0.0.0.0', config.server_port))
    stream_content = StreamContent(config)

    if not CLI:
        from views.main_view import MainView
        main_window = MainView(config)

    sys.exit(app.exec_())

Most important part is selection between QCoreApplication and QApplication. First one allows us to work without GUI but with all pyqt libraries.

We import main view only when we are in GUI mode.

Summary

With this change we may now run ProxyLCD on Raspberry Pi as hidden and fully operational Node 🙂
There is still one thing missing… we still need to get the app and run it. But do not worry… this can be fixed 😀

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s