[GUIDE] Using Python to interact with ODROID-SHOW

Post Reply
Matoking
Posts: 14
Joined: Mon Dec 22, 2014 3:01 am
languages_spoken: english
ODROIDs: ODROID-U3 + ODROID-SHOW, ODROID-XU4
Has thanked: 0
Been thanked: 0
Contact:

[GUIDE] Using Python to interact with ODROID-SHOW

Unread post by Matoking » Mon Dec 22, 2014 7:24 pm

I created a Python script for displaying various kinds of information on an ODROID-SHOW using tabs.

Github repository: https://github.com/Matoking/SHOWtime
Thread: http://forum.odroid.com/viewtopic.php?f=88&t=7676

I also included a class named ScreenContext in context.py that allows (almost) anything to be done on the ODROID-SHOW using Python without having to worry about throttling input or entering escape commands manually. Printing text, changing background/foreground colors, performing linebreaks and most of the functionality can be done easily and neatly using method chaining.

GETTING STARTED:
Assuming you've created a .py file in the same file as context.py and have performed the steps described in INSTALL, you can start with the following template

Code: Select all

from context import Screen, ScreenContext
import atexit

ctx = ScreenContext("/dev/ttyUSB0")

# Make sure the cleanup routine is called to clear the screen
# when we close the script
atexit.register(ctx.cleanup)

# Wait 6 seconds for the screen to boot up before we start uploading anything
ctx.sleep(6).reset_lcd().set_rotation(0)
This template creates a new screen context we can use for interacting with the ODROID-SHOW. Note that we sleep for 6 seconds to make sure ODROID-SHOW is done displaying the bootup screen, after which we can be sure that all commands are received and handled correctly.

Now, we can start with a simple Hello World program. Place the following at the end of the script.

Code: Select all

# Main loop
while True:
    ctx.fg_color(Screen.RED).write("Hello").linebreak()
    ctx.fg_color(Screen.BLUE).write("world!").home()
This creates a simple loop that displays the text "Hello world!" on the ODROID-SHOW, the word "Hello" in red, on the first line, and the word "world!" in blue, on the second line.
The last home() method call makes sure the cursor is placed back at the start, otherwise the words "Hello" and "world!" would be drawn until they were offscreen.

Now you can run the script using the Python interpreter. Assuming you named the file example.py, you can just run the following
python example.py

Another thing to note is that you don't need to call sleep() to throttle the script's execution to keep the ODROID-SHOW in sync; the ScreenContext already takes care of that. However, if you do need it for any reason, you can call ctx.sleep(seconds) to halt the script's execution for any amount of seconds you want.

In case you only want to use ScreenContext but not the SHOWtime script itself, you can simply copy context.py, port_open and utils.py and place them in the same directory as your script.

TIPS:
All of the methods in ScreenContext have been commented, so you shouldn't have trouble checking it yourself for what you need. There are, however, some methods which may need some additional demonstration in order to use them as they were intended.

Prevent ghosting using ctx.write_line()
Let's try out the following script.

Code: Select all

eggs = 555
spam = 1234
while True:
    ctx.write("Eggs %d" % eggs).linebreak()
    ctx.write("Spam %d" % spam).home()
    eggs = 99
    spam = 321
Looking at the code, you would expect the screen to display the following text:

Code: Select all

Eggs 99
Spam 321
However, since we have to explicitly write over text that has already been displayed to clear it, following is displayed instead:

Code: Select all

Eggs 995
Spam 3214
Fortunately, ScreenContext has a convenient method that prints the given text to the screen and fills the rest of the line with whitespace, effectively preventing these ghosting issues. You can fix the example by doing this:

Code: Select all

eggs = 555
spam = 1234
while True:
    ctx.write_line("Eggs %d" % eggs)
    ctx.write_line("Spam %d" % spam).home()
    eggs = 99
    spam = 321
Note that this also removes the need to use linebreak() to change the line.

hokoon
Posts: 262
Joined: Tue Jun 11, 2013 2:55 pm
languages_spoken: ไทย
ODROIDs: U2, XU, U3, C-1
Has thanked: 0
Been thanked: 0
Contact:

Re: [GUIDE] Using Python to interact with ODROID-SHOW

Unread post by hokoon » Tue Dec 23, 2014 8:11 am

AWESOME

User avatar
odroid
Site Admin
Posts: 31115
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Has thanked: 31 times
Been thanked: 156 times
Contact:

Re: [GUIDE] Using Python to interact with ODROID-SHOW

Unread post by odroid » Tue Dec 23, 2014 10:02 am

Great item.
I've added this guide on our WiKi. :)
http://odroid.com/dokuwiki/doku.php?id= ... #tutorials

Confessors
Posts: 1
Joined: Wed May 04, 2016 6:01 am
languages_spoken: english
ODROIDs: odroid show 2 display,
Has thanked: 0
Been thanked: 0
Contact:

Re: [GUIDE] Using Python to interact with ODROID-SHOW

Unread post by Confessors » Wed May 04, 2016 6:05 am

Hi,
Please help how to get "odroid show 2" user button status in python? and how to settings leds on or off?
Thnx!

Post Reply

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest