M5STACK ATOM-S3 Examples

These examples run on the M5Stack ATOM-S3. See https://shop.m5stack.com/products/atoms3-dev-kit-w-0-85-inch-screen for more information.

atom_s3/feathers.py

  1"""
  2feathers.py
  3
  4    Smoothly scroll mirrored rainbow colored random curves across the display.
  5
  6"""
  7
  8import random
  9import math
 10import utime
 11from machine import Pin, SPI
 12import st7789py as st7789
 13
 14
 15def between(left, right, along):
 16    """returns a point along the curve from left to right"""
 17    dist = (1 - math.cos(along * math.pi)) / 2
 18    return left * (1 - dist) + right * dist
 19
 20
 21def color_wheel(position):
 22    """returns a 565 color from the given position of the color wheel"""
 23    position = (255 - position) % 255
 24
 25    if position < 85:
 26        return st7789.color565(255 - position * 3, 0, position * 3)
 27
 28    if position < 170:
 29        position -= 85
 30        return st7789.color565(0, position * 3, 255 - position * 3)
 31
 32    position -= 170
 33    return st7789.color565(position * 3, 255 - position * 3, 0)
 34
 35
 36def main():
 37    '''
 38    The big show!
 39    '''
 40    #enable display and clear screen
 41    spi = SPI(2, baudrate=40000000, sck=Pin(17), mosi=Pin(21), miso=None)
 42    tft = st7789.ST7789(
 43        spi,
 44        128,
 45        128,
 46        reset=Pin(34, Pin.OUT),
 47        cs=Pin(15, Pin.OUT),
 48        dc=Pin(33, Pin.OUT),
 49        backlight=Pin(16, Pin.OUT),
 50        rotation=1,
 51        color_order=st7789.BGR)
 52
 53
 54    height = tft.height         # height of display in pixels
 55    width = tft.width           # width if display in pixels
 56
 57    tfa = 1                     # top free area when scrolling
 58    bfa = 3         	        # bottom free area when scrolling
 59
 60    scroll = 0                  # scroll position
 61    wheel = 0                   # color wheel position
 62
 63    tft.vscrdef(tfa, width, bfa)    # set scroll area
 64    tft.vscsad(scroll + tfa)        # set scroll position
 65    tft.fill(st7789.BLACK)          # clear screen
 66
 67    half = (height >> 1) - 1    # half the height of the dislay
 68    interval = 0                # steps between new points
 69    increment = 0               # increment per step
 70    counter = 1                 # step counter, overflow to start
 71    current_y = 0               # current_y value (right point)
 72    last_y = 0                  # last_y value (left point)
 73
 74    # segment offsets
 75    x_offsets = [x * (width // 8) -1 for x in range(2,9)]
 76
 77    while True:
 78        # when the counter exceeds the interval, save current_y to last_y,
 79        # choose a new random value for current_y between 0 and 1/2 the
 80        # height of the display, choose a new random interval then reset
 81        # the counter to 0
 82
 83        if counter > interval:
 84            last_y = current_y
 85            current_y = random.randint(0, half)
 86            counter = 0
 87            interval = random.randint(10, 100)
 88            increment = 1/interval      # increment per step
 89
 90        # clear the first column of the display and scroll it
 91        tft.vline(scroll, 0, height, st7789.BLACK)
 92        tft.vscsad(scroll + tfa)
 93
 94        # get the next point between last_y and current_y
 95        tween = int(between(last_y, current_y, counter * increment))
 96
 97        # draw mirrored pixels across the display at the offsets using the color_wheel effect
 98        for i, x_offset in enumerate(x_offsets):
 99            tft.pixel((scroll + x_offset) % width, half + tween, color_wheel(wheel+(i<<2)))
100            tft.pixel((scroll + x_offset) % width, half - tween, color_wheel(wheel+(i<<2)))
101
102        # increment scroll, counter, and wheel
103        scroll = (scroll + 1) % width
104        wheel = (wheel + 1) % 256
105        counter += 1
106
107
108main()

atom_s3/rotations.py

 1"""
 2feathers.py
 3
 4    Smoothly scroll mirrored rainbow colored random curves across the display.
 5
 6"""
 7
 8import random
 9import math
10import time
11from machine import Pin, SPI
12import st7789py as st7789
13import vga1_8x8 as font
14
15
16def center_on(display, font, text, y, fg, bg):
17    '''
18    Center the text on the display
19    '''
20    x = (display.width - len(text) * font.WIDTH) // 2
21    display.text(font, text, x, y, fg, bg)
22
23def main():
24    '''
25    The big show!
26    '''
27    #enable display and clear screen
28
29    spi = SPI(2, baudrate=40000000, sck=Pin(17), mosi=Pin(21), miso=None)
30    tft = st7789.ST7789(
31        spi,
32        128,
33        128,
34        reset=Pin(34, Pin.OUT),
35        cs=Pin(15, Pin.OUT),
36        dc=Pin(33, Pin.OUT),
37        backlight=Pin(16, Pin.OUT),
38        rotation=1,
39        color_order=st7789.BGR)
40
41    height = tft.height         # height of display in pixels
42    width = tft.width           # width if display in pixels
43
44    while True:
45        for rotation in range(4):
46            tft.rotation(rotation)
47            tft.fill(st7789.BLACK)
48            tft.rect(0, 0, width, height, st7789.RED)
49            center_on(tft, font, "Rotation", height // 2 - font.HEIGHT, st7789.WHITE, st7789.BLACK)
50            center_on(tft, font, str(rotation), height // 2 + font.HEIGHT, st7789.WHITE, st7789.BLACK)
51            time.sleep(1)
52
53main()

atom_s3/scroll.py

 1"""
 2scroll.py
 3
 4    Smoothly scrolls all font characters up the screen on the LILYGO® TTGO
 5    T-Display. Only works with fonts with heights that are even multiples of
 6    the screen height, (i.e. 8 or 16 pixels high)
 7
 8"""
 9import time
10import random
11from machine import Pin, SPI
12import st7789py as st7789
13
14# choose a font
15
16# from romfonts import vga1_8x8 as font
17# from romfonts import vga2_8x8 as font
18# from romfonts import vga1_8x16 as font
19# from romfonts import vga2_8x16 as font
20# from romfonts import vga1_16x16 as font
21# from romfonts import vga1_bold_16x16 as font
22# from romfonts import vga2_16x16 as font
23from romfonts import vga2_bold_16x16 as font
24
25
26def main():
27    spi = SPI(2, baudrate=40000000, sck=Pin(17), mosi=Pin(21), miso=None)
28    tft = st7789.ST7789(
29        spi,
30        128,
31        128,
32        reset=Pin(34, Pin.OUT),
33        cs=Pin(15, Pin.OUT),
34        dc=Pin(33, Pin.OUT),
35        backlight=Pin(16, Pin.OUT),
36        rotation=0,
37        color_order=st7789.BGR)
38
39    last_line = tft.height - font.HEIGHT
40    tfa = 1                     # top free area when scrolling
41    bfa = 3         	        # bottom free area when scrolling
42
43    tft.vscrdef(tfa, tft.height, bfa)
44
45    tft.fill(st7789.BLUE)
46    scroll = 0
47    character = 0
48    while True:
49        tft.fill_rect(0, scroll, tft.width, 1, st7789.BLUE)
50
51        if scroll % font.HEIGHT == 0:
52            tft.text(
53                font,
54                f' x{character:02x}= {chr(character)} ',
55                0,
56                (scroll + last_line) % tft.height,
57                st7789.WHITE,
58                st7789.BLUE)
59
60            character = (character + 1) % (font.LAST+1)
61
62        tft.vscsad(scroll + tfa)
63        scroll += 1
64        time.sleep_ms(10)
65
66        if scroll == tft.height:
67            scroll = 0
68
69
70main()