Освой MicroPython играючи

Сайт Александра Климова

Шкодим

/* Моя кошка замечательно разбирается в программировании. Стоит мне объяснить проблему ей - и все становится ясно. */
John Robbins, Debugging Applications, Microsoft Press, 2000

M5Stack: Класс LCD для работы с экраном (MicroPython)


from m5stack import lcd

lcd.font(lcd.FONT_UNICODE)
lcd.clear()
lcd.setCursor(0, 0)
lcd.print('Привет, Мурзик!')

Fonts

9 bit-mapped fornts and one vector 7-segment font are included. Unlimited number of fonts from file can also be used.

The following font constants are defined and can be used as font arguments:

FONT_Default, FONT_DefaultSmall, FONT_DejaVu18, FONT_Dejavu24, FONT_Ubuntu, FONT_Comic, FONT_Minya, FONT_Tooney, FONT_Small, FONT_7seg, FONT_UNICODE


Methods

lcd.pixel(x, y [,color])

Draw the pixel at position (x,y).
If color is not given, current foreground color is used.

lcd.readPixel(x, y)

Get the pixel color value at position (x,y).

lcd.line(x, y, x1, y1 [,color])

Draw the line from point (x,y) to point (x1,y1)
If color is not given, current foreground color is used.

lcd.lineByAngle(x, y, start, length, angle [,color])

Draw the line from point (x,y) with length lenght starting st distance start from center.
If color is not given, current foreground color is used.
The angle is given in degrees (0~359).

lcd.triangle(x, y, x1, y1, x2, y2 [,color, fillcolor])

Draw the triangel between points (x,y), (x1,y1) and (x2,y2).
If color is not given, current foreground color is used.
If fillcolor is given, filled triangle will be drawn.

lcd.circle(x, y, r [,color, fillcolor])

Draw the circle with center at (x,y) and radius r.
If color is not given, current foreground color is used.
If fillcolor is given, filled circle will be drawn.

lcd.ellipse(x, y, rx, ry [opt, color, fillcolor])

Draw the circle with center at (x,y) and radius r.
If color is not given, current foreground color is used.
*opt argument defines the ellipse segment to be drawn, default id 15, all ellipse segments.

Multiple segments can drawn, combine (logical or) the values.

  • 1 - upper left segment
  • 2 - upper right segment
  • 4 - lower left segment
  • 8 - lower right segment

If fillcolor is given, filled elipse will be drawn.

lcd.arc(x, y, r, thick, start, end [color, fillcolor])

Draw the arc with center at (x,y) and radius r, starting at angle start and ending at angle end
The thicknes of the arc outline is set by the thick argument
If fillcolor is given, filled arc will be drawn.

lcd.poly(x, y, r, sides, thick, [color, fillcolor, rotate])

Draw the polygon with center at (x,y) and radius r, with number of sides sides
The thicknes of the polygon outline is set by the thick argument
If fillcolor is given, filled polygon will be drawn.
If rotate is given, the polygon is rotated by the given angle (0~359)

lcd.rect(x, y, width, height, [color, fillcolor])

Draw the rectangle from the upper left point at (x,y) and width width and height height
If fillcolor is given, filled rectangle will be drawn.

lcd.roundrect(x, y, width, height, r [color, fillcolor])

Draw the rectangle with rounded corners from the upper left point at (x,y) and width width and height height
Corner radius is given by r argument.
If fillcolor is given, filled rectangle will be drawn.

lcd.clear([color])

Clear the screen with default background color or specific color if given.

lcd.clearWin([color])

Clear the current display window with default background color or specific color if given.

lcd.orient(orient)

Set the display orientation.
Use one of predifined constants:
lcd.PORTRAIT, lcd.LANDSCAPE, lcd.PORTRAIT_FLIP, lcd.LANDSCAPE_FLIP

lcd.font(font [,rotate, transparent, fixedwidth, dist, width, outline, color])

Set the active font and its characteristics.

Argument Description
font required, use font name constant or font file name
rotate optional, set font rotation angle (0~360)
transparent only draw font's foreground pixels
fixedwidth draw proportional font with fixed character width, max character width from the font is used
dist only for 7-seg font, the distance between bars
width only for 7-seg font, the width of the bar
outline only for 7-seg font, draw the outline
color font color, if not given the current foreground color is used

lcd.font(lcd.FONT_UNICODE)

lcd.attrib7seg(dist, width, outline, color)

Set characteristics of the 7-segment font

Argument Description
dist the distance between bars
width the width of the bar
outline outline color
color fill color

lcd.fontSize()

Return width and height of the active font

lcd.print(text[,x, y, color, rotate, transparent, fixedwidth, wrap])

Display the string text at possition (x,y).
If color is not given, current foreground color is used.

  • x: horizontal position of the upper left point in pixels, special values can be given:
    • CENTER, centers the text
    • RIGHT, right justifies the text
    • LASTX, continues from last X position; offset can be used: LASTX+n
  • y: vertical position of the upper left point in pixels, special values can be given:
    • CENTER, centers the text
    • BOTTOM, bottom justifies the text
    • LASTY, continues from last Y position; offset can be used: LASTY+n
  • text: string to be displayed. Two special characters are allowed in strings:
    • ‘\r’ CR (0x0D), clears the display to EOL
    • ‘\n’ LF (ox0A), continues to the new line, x=0

lcd.text(x, y, text [, color])

Display the string text at possition (x,y).
If color is not given, current foreground color is used.

  • x: horizontal position of the upper left point in pixels, special values can be given:
    • CENTER, centers the text
    • RIGHT, right justifies the text
    • LASTX, continues from last X position; offset can be used: LASTX+n
  • y: vertical position of the upper left point in pixels, special values can be given:
    • CENTER, centers the text
    • BOTTOM, bottom justifies the text
    • LASTY, continues from last Y position; offset can be used: LASTY+n
  • text: string to be displayed. Two special characters are allowed in strings:
    • ‘\r’ CR (0x0D), clears the display to EOL
    • ‘\n’ LF (ox0A), continues to the new line, x=0

lcd.textWidth(text)

Return the width of the string text using the active font fontSize

lcd.textClear(x, y, text [, color])

Clear the the screen area used by string text at possition (x,y) using the bacckground color color.
If color is not given, current background color is used.

lcd.image(x, y, file [,scale, type])

Display the image from the file file on position (x,y)

  • JPG and BMP can be displayed.
  • Constants lcd.CENTER, lcd.BOTTOM, lcd.RIGHT can be used for x&y
  • x and y values can be negative

scale (jpg): image scale factor: 0 to 3; if scale>0, image is scaled by factor 1/(2^scale) (1/2, 1/4 or 1/8)
scale (bmp): image scale factor: 0 to 7; if scale>0, image is scaled by factor 1/(scale+1)
type: optional, set the image type, constants lcd.JPG or lcd.BMP can be used. If not set, file extension and/or file content will be used to determine the image type.


lcd.image(100, 100, file="res/default.jpg", scale=0, type=lcd.JPG)

lcd.setwin(x, y, x1, y1)

Set active display window to screen rectangle (x,y) - (x1,y1)

lcd.resetwin()

Reset active display window to full screen size.

lcd.savewin()

Save active display window dimensions.

lcd.restorewin()

Restore active display window dimensions previously saved wint savewin().

lcd.screensize()

Return the display size, (width, height)

lcd.winsize()

Return the active display window size, (width, height)

lcd.hsb2rgb(hue, saturation, brightness)

Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.
Returns 24-bit integer value suitable to be used as color argiment

Arguments

  • hue: float: any number, the floor of this number is subtracted from it to create a fraction between 0 and 1. This fractional number is then multiplied by 360 to produce the hue angle in the HSB color model.
  • saturation: float; 0 ~ 1.0
  • brightness: float; 0 ~ 1.0

lcd.compileFont(file_name [,debug])

Compile the source font file (must have .c extension) to the binary font file (same name, .fon extension) which can be used as external font.
If debug=True the information about compiled font will be printed.

You can create the c source file from any tft font using the included ttf2c_vc2003.exe program. See README for instructions.

You can create c source file or .fon file from tft font using online converter.

Реклама