Skip to content

How to write/draw to LCD screen? #194

Closed

Description

@ndward

Me again, the webmaster of ev3python.com where I try to make it easier for beginners to make a start with ev3dev and ev3 Python.

I haven't been able to write text or graphics to the EV3 LCD screen using EV3 Python except by running the ridiculously complex code on http://www.ev3dev.org/docs/tutorials/using-ev3-lcd/ Thirty lines of code just to draw two lines and a circle?! There has to be a better way. And how do I display test, or an image file. Please help!

Activity

  1. ddemidov commented on Sep 4, 2016

    @ddemidov
    Member

    The Screen class has draw property, which is just an instance of pillow's (python imaging library) ImageDraw.Draw class. See pillow's documentation here: https://pillow.readthedocs.io/en/3.3.x/

    All operations are applied to internally help image, which is dumped to the actual LCD when update() method is called. Here is an example that draws an alternating smiley-frawny face on EV3 screen:

    from time import sleep
    import ev3dev.auto as ev3
    
    screen = ev3.Screen()
    
    smile = True
    
    while True:
        screen.clear()
    
        # Screen.draw returns a PIL.ImageDraw handle
        screen.draw.ellipse(( 20, 20,  60, 60))
        screen.draw.ellipse((118, 20, 158, 60))
    
        if smile:
            screen.draw.arc((20, 80, 158, 100), 0, 180)
        else:
            screen.draw.arc((20, 80, 158, 100), 180, 360)
    
        smile = not smile
    
        # Update lcd display
        screen.update()
    
        sleep(1)
  2. ndward commented on Sep 4, 2016

    @ndward
    Author

    Your code gives me the following error:
    File "smiley.py", line 4, in
    screen = ev3.Screen()
    File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 2310, in init
    File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2030, in new
    return Image()._new(core.fill(mode, size, color))
    TypeError: integer argument expected, got float

    Once I've managed to write or draw to the screen, how do I get Brickman back?

  3. ddemidov commented on Sep 4, 2016

    @ddemidov
    Member

    I am still using python2 on ev3 (its the default I think). It looks like division on line 2310 should be changed to integer division (//) for this to work in python3. I am afraid I won't be able to make a pull request in a couple of weeks (on vacation, away from computer). For now, you could try to run the script with python2.

  4. ndward commented on Sep 4, 2016

    @ndward
    Author

    No worries, for I'm just leaving also for a two week vacation.

  5. ddemidov commented on Sep 4, 2016

    @ddemidov
    Member

    Once I've managed to write or draw to the screen, how do I get Brickman back?

    When you run your scripts from brickman menu, you should get your own screen. It will switch back to brickman when your script finishes. If you run your script from ssh session, you would get weird results: brickman and your script will both write to the same screen.

  6. WasabiFan commented on Sep 4, 2016

    @WasabiFan
    Member

    @ddemidov I can test that out and open a PR if it fixes the problem.

    Also, about the screen thing: I think there's a way to do it via SSH as well but can't remember the command. @dlech will have do advise on that.

  7. ndward commented on Sep 4, 2016

    @ndward
    Author

    I've never yet run a script from Brickman. I have to modify the file permissions before I can do that, don't I? Will Brickman know that it should use Python3?

  8. WasabiFan commented on Sep 4, 2016

    @WasabiFan
    Member

    @dlech will have do advise on that.

    @dlech has spoken! To run an application that writes to the display via SSH:

    1. Run sudo chvt 6. This just switches the display to show you an empty terminal instead of Brickman, so that Brickman doesn't mess with you.
    2. Run sudo conspy. This will make your SSH session show the same terminal that is on the EV3's display. Log in to the prompt it shows you.
    3. Run your program. You should see it show up on the EV3 display, as you'd expect.
      • You can work from this terminal like normal, run your program multiple times, etc.
    4. Press ESC 3 times to exit the display terminal
    5. Run sudo chvt 1 to get Brickman back.

    It seems a bit complicated, but it's reasonably easy. If you don't mind a little blinking cursor showing up on top of your program, I believe you can skip steps 2 and 4. This also means that the console isn't small, because instead of using the one on the tiny EV3 display, you are using a normal one and just displaying your code's shapes on the display.


    As for running it with Brickman, I believe you just need to do two things (once per Python program).

    1. Run chmod +x my-file.py, using the real name of your file
    2. Add the line #!/usr/bin/env python3 to the top of your file. Make sure it is the very fist line!
  9. dlech commented on Sep 4, 2016

    @dlech
    Member

    You have to press ESC 3 times to exit conspy

  10. WasabiFan commented on Sep 4, 2016

    @WasabiFan
    Member

    Fixed.

  11. WasabiFan commented on Sep 5, 2016

    @WasabiFan
    Member

    I am still using python2 on ev3 (its the default I think). It looks like division on line 2310 should be changed to integer division (//) for this to work in python3.

    This change has already been made, however @ndward is using version 0.6.0, which doesn't support Python 3 in full. See #195 (comment).

  12. ndward commented on Sep 5, 2016

    @ndward
    Author

    It's common to run, via SSH, Python scripts that can only be terminated with Ctrl+C. What is the equivalent trick to stop the same scripts if they are launched from within Brickman?

  13. ddemidov commented on Sep 5, 2016

    @ddemidov
    Member

    You can long press back button to end any program that was started with brickman. Another useful shortcut is back+enter to reset the brick.

  14. ndward commented on Sep 5, 2016

    @ndward
    Author

    I followed the instructions above to make a script executable from Brickman:
    Run chmod +x my-file.py, using the real name of your file
    Add the line #!/usr/bin/env python3 to the top of your file. Make sure it is the very fist line!

    And it worked for this script

    #!/usr/bin/env python3
    # so that script can be run from Brickman
    from ev3dev.auto import *  # needed for Sound
    Sound.beep()
    

    but not for this one (I used chmod as suggested)

    #!/usr/bin/env python3
    # so that script can be run from Brickman
    from ev3dev.auto import *
    from time import sleep
    m = LargeMotor('outB')
    
    m.run_to_rel_pos(position_sp=360, duty_cycle_sp=60, stop_command="hold")
    sleep(5)
    

    The same script works fine via SSH.

    How come the line
    #!/usr/bin/env python3
    is needed? Aren't comments ignored?
    And is it OK to add anything at the end of that line?

  15. ddemidov commented on Sep 5, 2016

    @ddemidov
    Member

    When the first line starts with a #! followed by a command, its called a shebang. Bash knows to execute the command specified and to pass the rest of the script as its argument.

    but not for this one (I used chmod as suggested)

    Make sure the file has unix line-endings. When you forget to convert from Windows format, bash thinks that an extra \r in the end of the first line is part of the command.

  16. 42 remaining items

  17. WasabiFan commented on Sep 27, 2016

    @WasabiFan
    Member

    Yeah, those errors make sense; glad to see it works overall though!

  18. ndward commented on Sep 27, 2016

    @ndward
    Author

    I thought I had the LCD nailed but I have a small problem. This script runs forever and if I start the script from Brickman I can't exit it properly with a long-press on the Back button ( the brick just starts toggling back and forth between Brickman and the graphic display). I have to a long press on Back and Enter to restart the brick.

    #!/usr/bin/env python3
    from time import sleep
    from ev3dev.ev3 import *
    
    lcd = Screen()
    
    smile = True
    
    while True:
        lcd.clear()
        lcd.draw.ellipse(( 20, 20,  60, 60))
        lcd.draw.ellipse((118, 20, 158, 60))
    
        if smile:
            lcd.draw.arc((20, 80, 158, 100), 0, 180)
        else:
            lcd.draw.arc((20, 80, 158, 100), 180, 360)
    
        smile = not smile
        lcd.update()
        sleep(1)
    
  19. ndward commented on Sep 28, 2016

    @ndward
    Author

    Do you agree that the simplest way to do a screen capture is to run this at the command line?
    fbgrab "screenshot.png"
    It seems to work for me. It saves a black and white png image called 'screenshot.png' into the current folder.

  20. WasabiFan commented on Sep 28, 2016

    @WasabiFan
    Member

    Do you agree that the simplest way to do a screen capture is to run this at the command line?

    Yes. That's actually what my Python script does; it just goes a step further in recoloring and scaling it.

    This script runs forever and if I start the script from Brickman I can't exit it properly with a long-press on the Back button ( the brick just starts toggling back and forth between Brickman and the graphic display).

    I'm not sure what's going on there, but I was able to repro it. I know that Brickman sends SIGTERM with pkill, but for a reason which I am unaware of it doesn't work for Brickman but does for me. Simplest workaround is to run pkill python3 after attempting to end it with Brickman.

  21. ddemidov commented on Sep 28, 2016

    @ddemidov
    Member

    lcd.image.paste((0,0), logo) work (no call to convert) does not work, gives error:

    My bad, should be lcd.image.paste(logo, (0,0)) (reordered arguments). Also, not sure how your above examples of paste worked like this.

  22. ndward commented on Sep 28, 2016

    @ndward
    Author

    Yes, when I changed lcd.image.paste((0,0), logo) into lcd.image.paste(logo, (0,0)) it worked, so I deduce that the simplest code to display an image would be

    #!/usr/bin/env python3
    from ev3dev.ev3 import *
    from time import sleep
    from PIL import Image, ImageDraw, ImageFont, ImageOps
    
    lcd = Screen()
    
    logo = Image.open('pics/Bomb.bmp')
    lcd.image.paste(logo, (0,0))
    lcd.update()
    sleep(6)  # when running from Brickman, need time to admire image

    In fact the linefrom PIL import Image, ImageDraw, ImageFont, ImageOps could be shortened to from PIL import ImageDraw but perhaps it is a good habit to use the fuller line?

  23. ddemidov commented on Sep 28, 2016

    @ddemidov
    Member

    In fact the linefrom PIL import Image, ImageDraw, ImageFont, ImageOps could be shortened to from PIL import ImageDraw but perhaps it is a good habit to use the fuller line?

    I guess a good habit is to just import what is necessary. Here you only use Image to load the logo, so you could omit other imports. You may also need ImageFont to load fonts. ImageOps is no longer necessary since you found a better way to load and display pictures. And you don't need to explicitly import ImageDraw because Screen class creates the draw handle for you.

  24. ndward commented on Sep 28, 2016

    @ndward
    Author

    ... and you said in an earlier message that "you can omit Image from PIL imports. That is already imported by ev3dev.ev3." so the whole PIL import line can go, leaving

    #!/usr/bin/env python3
    from ev3dev.ev3 import *
    from time import sleep
    
    lcd = Screen()
    
    logo = Image.open('pics/Bomb.bmp')
    lcd.image.paste(logo, (0,0))
    lcd.update()
    sleep(6)  # when running from Brickman, need time to admire image
    
  25. ddemidov commented on Sep 28, 2016

    @ddemidov
    Member

    That accidentally works when you do from ev3dev.ev3 import *, as this brings into global namespace everything the ev3 module defines and imports. But in general a user has no way of knowing what is being imported by the specific module, so its better to explicitly import modules one uses.

    Here Image module is used explicitly to load the bomb logo, while lcd.draw and lcd.image are instances constructed by the Screen class constructor. Strictly speaking, a user should not even know/care what classes are these instances of (except for that they have same interfaces as ImageDraw.Draw and Image).

    Baseline: I would still import Image explicitly.

  26. ndward commented on Oct 12, 2016

    @ndward
    Author

    Is it possible to use print() to print to the EV3 LCD screen when a script is run from Brickman? I was expecting this script to work in Brickman but it does not, though it works fine from SSH.

    #!/usr/bin/env python3
    
    # this prints the cosine of 45 degrees
    # works from SSH but not from Brickman
    from time import sleep
    import math
    
    print (math.cos(math.radians(45)))
    sleep(6) # give enough time for result to be read in Brickman
    
  27. ddemidov commented on Oct 15, 2016

    @ddemidov
    Member

    The following example works for me (from brickman):

    #!/usr/bin/env python3
    import time
    
    for i in range(30):
        print('#' * 30)
    
    time.sleep(5)

    Keep in mind that the default font is tiny and its possible to overlook small amount of text.

  28. ndward commented on Oct 16, 2016

    @ndward
    Author

    Yes, that works fine, thank you, and so does the script that was failing for me previously - I don't know what was wrong.

  29. ddemidov commented on Oct 16, 2016

    @ddemidov
    Member

    Ok, it looks like this may be closed now. Feel free to reopen if necessary.

  30. anzy0621 commented on Apr 1, 2019

    @anzy0621

    How do I input some coordinate values into the lego ev3 brick so that the robot and return to the x, y position that was provided as an input to it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions