Basic display and LEDs

Preparing For This Tutorial:

You need only the LEGO MINDSTORMS brick.

Time constraints: starting from 45 min - single lesson

Exercise

Prepare files.

Note. If you had got installed LEGO SOFTWARE EDUCATION, you could find all LEGO images in BMP format in C:Program Files (x86)LEGO SoftwareLEGO MINDSTORMS Edu EV3ResourcesBrickResourcesEducationImagesfiles

  1. Create a special directory called ‘images’ in your robot’s directory and copy all BMP files into it. Keep the structure of subdirectories. (/home/robot/images/files/eyes).

  2. Create a programme that will:

    1. Display white text ‘’Python’’ in the upper half of the black screen and dark text ‘’is fun’’ in the lower half of the white screen.

    2. Plot a rectangle point by point and it’s diagonal. (Don’t use rectangle nor line method. You could use it as a separate exercise).

    3. Display any bmp image from the folder (cover and scale it).

    4. Display on the computer screen a list of available fonts. Display on the brick screen the text ,,Python is fun” using luBS font where size is from 08 to 14.

    5. Present a possibility of LEDs. LEFT LED displays a different color than RIGHT. The programme should present at least two times all available colours: ‘ORANGE’, ‘BLACK’, ‘AMBER’, ‘RED’, ‘YELLOW’, ‘GREEN’.

    6. Display sequentially green, yellow and red colors - use tuples to encode the color.

Theory

The EV3 has a 178 x 128 pixels monochrome (black and white) LCD. The coordinates of the top-left pixel are (0, 0) and the coordinates of the bottom-right pixel are (177, 127).

image0

Controlling the two LEDs located under the buttons of the EV3 brick (Python controls the left and right LEDs separately) is very easy. The RED and GREEN colours are the colours of the LEDs. Other colours: orange, amber and yellow are made by blending green and red. In the ev3dev2 library it is possible to obtain a custom colour. Colour is represented by two valued tuple. Tuple (1,0) means RED colour and (0,1) means GREEN colour. When you put (1,1) or (0.5,0.5) you obtain an AMBER colour. Try yourself with any other values (0.3,0.5) etc.

Tip to exercise 6. When we divide a dividend \(a\) by a divisor \(b\), we get a quotient \(q\) with a remainder \(r\). \(a \div b = q\) with a remainder \(r\) where \(0 \leqslant r < b\). If we can find a remainder \(r\) , in the expressions above, then we calculate \(a\ mod\ b = r\)

Programme displays sequentially green, yellow and red colours. Use (0,1), (0.5,0.5) and (1,1) tuples to coding the colours, loop and function mod (%). Complete the table below.

i

i%3 ( i mod 3)

(i%3) / 2

1-((i%3)/2)

0

0

1

1

2

2

6

0

7

1

8

2

Short help on programming

Commands/functions needed for the exercise

image13

image14

image15

Example Display white text ‘’Python’’ in the upper half of the black screen and dark text ‘’is fun’’ in the lower half of the white screen.

  #!/usr/bin/env python3

from ev3dev2.display import Display

from time import sleep

lcd = Display()

lcd.draw.rectangle((0,64,177,128), fill='black')

lcd.draw.text((32,32),'Python')

lcd.draw.text((32,80),'is fun',fill='white')

lcd.update()

sleep(6)

Example Display any bmp image.

#!/usr/bin/env python3

from ev3dev.ev3 import \*

from time import sleep

from ev3dev2.display import Display

from PIL import Image # Python Image Library (PIL)

lcd = Display()# create an instance of the object

im = Image.open('/home/robot/images/files/Eyes/Angry.bmp') #file
handling

lcd.image.paste(im, (0,0))

#point(0,0) - start drow from the upper left corner

lcd.update()

sleep(5)
Next Section - Basic motors