Close

A counter app

A project log for Linux Control System

*moved to pyLCI project*An expandable control system for Linux with apps that lets you control your Linux devices using a screen and buttons

aryaArya 08/21/2015 at 04:260 Comments

Hello!

First of all, I finally have solved most of issues that had troubled co-existence of multiple apps at once. Apps may now live in peace together. One issue I know that still isn't solved is the one where setting some callbacks or outputting data on display while the app isn't activated (t.i. on the screen right now) will not do anything - except for the first time where app has been created but hasn't been activated yet, and that's not even confirmed. I just need to rethink some functions.

The next system usage example is the counter app. You know these counters with one or two buttons and a screen, where one button increases a number on the screen and the other one resets the counter? Well, why not re-implement it with an Arduino^W^W a Raspberry Pi? That way, when it's next time you'll need a counter, you can just launch an app =)

Overall, it's a simple example of how to deal with changing data.

from time import sleep

import wcs

application = wcs.wm.create_new_application("Counter app")
window = application.get_window("Counter window")

input = window.input_interface
output = window.output_interface


class Counter():
    running = False
So far, nothing unusual.
    prompt = "Counter value is:"
    counter = 0

I added a prompt variable just for usability. The counter variable is the one we will be changing all the time.

    def increment(self):
        self.counter += 1
        self.refresh()

    def decrement(self):
        self.counter -= 1
        self.refresh()

    def reset(self):
        self.counter = 0
        self.refresh()
Those are the functions we will use to change the counter. Notice the refresh() at the end? We need to output the new data on the screen each time we change the variable, otherwise nothing will be visible to the user =) The refresh() is as follows:
    def refresh(self):
        self.output.display_data(self.prompt, str(self.counter))
Again, display_data() positional arguments are printed each one on a new row of the display. As for now, there's no line-wrapping or scrolling - but I plan to change that =)
Let's finish it with a run() function, now with more callbacks:
def run(self):
        self.running = True
        input.set_callback('KEY_KPPLUS', 'increment', self)
        input.set_callback('KEY_KPMINUS', 'decrement', self)
        input.set_callback('KEY_BACKSPACE', 'reset', self)
        input.set_callback('KEY_ENTER', 'stop', self)
        input.set_callback('KEY_KPENTER', 'stop', self)
        self.refresh()
        while self.running:
            sleep(1)

Next, the usual routines:

counter = Counter(input, output)
wcs.register_object(counter)

wcs.start_daemon_thread()

wcs.wm.activate_app(application.number) #WM-side - to remove

wcs.run(counter.run, application.shutdown)


Here it is! (Sorry, no video as for now - got no camera =( )

Discussions