基于树莓派RP2040的嵌入式系统实现贪吃蛇小游戏
基于树莓派RP2040的嵌入式系统在Thonny平台上实现小游戏——贪吃蛇。 本次设计的是贪吃蛇,通过蜂鸣器播放背景音乐,显示将贪吃蛇显示到240*240的屏幕上。
标签
嵌入式系统
MicroPython
2022寒假在家练
li_
更新2022-03-07
中央民族大学
613

项目总结报告

1.项目介绍

基于树莓派RP2040的嵌入式系统在Thonny平台上实现小游戏——贪吃蛇。 本次设计的是贪吃蛇,通过蜂鸣器播放背景音乐,显示将贪吃蛇显示到240*240的屏幕上。

2.硬件介绍

2.1采用树莓派Pico核心芯片RP2040:

 (1)双核Arm Cortex M0+内核,可以运行到133MHz

(2)264KB内存

(3)性能强大、高度灵活的可编程IO可用于高速数字接口

(4)片内温度传感器、并支持外部4路模拟信号输入,内部ADC采样率高达500Ksps、12位精度

(5)支持MicroPython、C、C++编程  

[注:本次程序用MicroPython编程]

2.2板上功能:

(1)240*240分辨率的彩色IPS LCD,SPI接口,控制器为ST7789

 (2)四向摇杆 + 2个轻触按键 + 一个三轴姿态传感器MMA7660用做输入控制

 (3)板上外扩2MB Flash,预刷MicroPython的UF2固件

 (4)一个红外接收管 + 一个红外发射管,一个三轴姿态传感器MMA7660,一个蜂鸣器

(5)双排16Pin连接器,有SPI、I2C以及2路模拟信号输入

(6)可以使用MicroPython、C、C++编程

(7)USB Type C连接器用于供电、程序下载

代码如下:

【1】yasnake.py(有背景音乐的)

## yasnake --- yet another snake game.
## copyright: 2021,2022 picospuch
## license: GPLv3
## board: eetree-game-kit

import random
import time
import uasyncio as asyncio
from machine import Pin, SPI, ADC

import st7789
import vga2_8x8 as fonts

from button import button
from buzzer_music import music

from board import game_kit

xAxis = ADC(Pin(29))
yAxis = ADC(Pin(28))


class color_cfg:
    black = st7789.BLACK
    magenta = st7789.MAGENTA
    blue = st7789.BLUE
    snake = st7789.MAGENTA
    food = st7789.RED
    background = st7789.color565(0, 255, 120)

class tile_cfg:
    block0 = b"\xfe" # ■
    block1 = b"\xb2" # ▓
    block = b"\xfe"

tile_cfg.block = tile_cfg.block1

class board_cfg:
    width = 30
    height = 29
    dashboard_x = 0
    dashboard_y = 29
    dashboard_width = 30
    dashboard_height = 1

class rotary_encoder:
    def __init__(self, pin_a, pin_b, callback):
        print(" init")
        self.callback = callback

        self.min_ago = 500
        self._next_call = time.ticks_ms() + self.min_ago

        self.pin_a = Pin(pin_a, Pin.IN)
        self.pin_b = Pin(pin_b, Pin.IN)

        self.pin_a.irq(trigger=Pin.IRQ_RISING|Pin.IRQ_FALLING, handler=self.debounce_handler)

    def call_callback(self, pin):
        if self.pin_b.value() != self.pin_a.value():
            self.callback("cw")
        else:
            self.callback("ccw")

    def debounce_handler(self, pin):
        #print("debounce")
        if time.ticks_ms() > self._next_call:
            self._next_call = time.ticks_ms() + self.min_ago
            self.call_callback(pin)

class hardware():
    def init():
        # screen
        spi = SPI(0, baudrate=31250000, polarity=1, phase=1,
                  sck=Pin(game_kit.lcd_sck, Pin.OUT),
                  mosi=Pin(game_kit.lcd_sda, Pin.OUT))
        tft = st7789.ST7789(
            spi,
            240,
            240,
            reset=Pin(game_kit.lcd_rst, Pin.OUT),
            dc=Pin(game_kit.lcd_dc, Pin.OUT),
            #xstart=0,
            #ystart=0,
            rotation=0)

   #     tft.offset(0, 0)
   #     tft.init()
        tft.fill(color_cfg.background)
        tft.fill_rect(0, 232, 240, 8, color_cfg.blue)

        hardware.tft = tft

pen_color = color_cfg.background

def outtextxy(x, y, c):
    global pen_color
    hardware.tft.text(fonts, c, x*8, y*8, pen_color, color_cfg.background)

def setcolor(c):
    global pen_color
    pen_color = c

class location ():
    def __init__(self, x, y):
        self.x = x
        self.y = y

class snake():
    UP = 1
    DOWN = 2
    LEFT = 3
    RIGHT = 4

    PAUSE = 1
    RUNNING = 2
    STOP = 3

    n = 6 # 6 body nodes + 1 head + 1 tail(invisible) = 8 nodes, at very first
    
    def __init__(self):
        # bgm
        self.bgm = p_music(p_music.song0, tempo=1, duty=500, pins=[
            Pin(game_kit.buzzer, Pin.OUT)])
        # controller
        #self.re = rotary_encoder(game_kit.rotary_encoder_a, game_kit.rotary_encoder_b, self.direction_callback)
        self.k1 = button(game_kit.key_a, self.k1_callback)

        self.haskey = False
        self.k = "cw"
        self.last_k = self.k

        # iot
        self.led = Pin(game_kit.led_sta, Pin.OUT)

        # interest
        self.score = 0
        self.n = snake.n
        self.state = self.RUNNING

    def direction_callback(self, d):
        print(d + "\n")
        self.haskey = True
        self.last_k = self.k
        self.k = d

    def k1_callback(self, p):
        print("k1 pressed")
        self.bgm.toggle_pause()

    async def opening(self):
        setcolor(color_cfg.snake)
        for j in range(board_cfg.height):
            for i in range(board_cfg.width):
                if j % 2 == 1:
                    x = 29 - i
                else:
                    x = i
                await asyncio.sleep_ms(5)
                outtextxy(x, j, tile_cfg.block)
        setcolor(color_cfg.background)
        for j in range(board_cfg.height):
            for i in range(board_cfg.width):
                if j % 2 == 1:
                    x = 29 - i
                else:
                    x = i
                await asyncio.sleep_ms(5)
                outtextxy(x, j, tile_cfg.block)

    def closing(self):
        self.state = snake.STOP
        setcolor(color_cfg.magenta)
        print("GAME OVER")
        outtextxy(0, 29, "GAME OVER")
    
    async def blink(self):
        self.led.value(1)
        await asyncio.sleep_ms(50)
        self.led.value(0)
        await asyncio.sleep_ms(50)
        
    async def process(self):
        bgm = asyncio.create_task(self.bgm_process())

        await self.opening()

        self.init_run()
        
        # main loop
        while True:
            self.dir_select()
            self.run()
            self.draw()
            self.judge()
            await self.blink()

    def dir_select(self):
        key = self.getch()
        xValue = xAxis.read_u16()
        yValue = yAxis.read_u16()
        director = 0
        
        if xValue <1000:
            director = snake.LEFT
        elif xValue >40000:
            director = snake.RIGHT
            
        if yValue <1000:
            director = snake.UP
        elif yValue >40000:
            director = snake.DOWN
            
        #debug
        #print("dir: "+str(director) +", d:"+ str(self.d))

        if director != 0:
            # up 1   d != down 2
            # left 3   d!= right 4
            # right 4   d!= 3
            # down 2    d!=1
            if (self.d == self.UP) and  (director != self.DOWN) :
                self.d = director
            if (self.d == self.LEFT) and  (director != self.RIGHT):
                self.d = director
            if (self.d == self.RIGHT) and  (director != self.LEFT):
                self.d = director
            if (self.d == self.DOWN) and  (director != self.UP):
                self.d = director
      




    def init_run(self):
        self.body = []

        for i in range(self.n):
            self.body.append(location((self.n - 1 - i), 0))

        self.head = location(self.n, 0)
        self.tail = location(-1, 0)

        self.food = location(-1, -1)

        setcolor(color_cfg.snake)
        outtextxy(0, 0, (self.n + 1) * tile_cfg.block)

        self.d = self.RIGHT

        self.fod()
        
    def run(self):
        if self.state != snake.RUNNING:
            return
        
        self.tail.x = self.body[self.n - 1].x
        self.tail.y = self.body[self.n - 1].y

        for i in range(self.n - 1, 0, -1):
            self.body[i].x = self.body[i - 1].x
            self.body[i].y = self.body[i - 1].y

        self.body[0].x = self.head.x
        self.body[0].y = self.head.y

        if self.d == snake.UP:
            self.head.y -= 1
        elif self.d == snake.DOWN:
            self.head.y += 1
        elif self.d == snake.LEFT:
            self.head.x -= 1
        elif self.d == snake.RIGHT:
            self.head.x += 1

        if self.head.x == -1:
            self.head.x = board_cfg.width - 1
        elif self.head.x == board_cfg.width:
            self.head.x = 0
            
        if self.head.y == -1:
            self.head.y = board_cfg.height - 1
        elif self.head.y == board_cfg.height:
            self.head.y = 0    
            
    def draw(self):
        setcolor(color_cfg.background)
        outtextxy(self.tail.x, self.tail.y, tile_cfg.block)
        setcolor(color_cfg.snake)
        outtextxy(self.head.x, self.head.y, tile_cfg.block)
        
    def judge(self):
        if self.head.x == self.food.x and self.head.y == self.food.y:
            self.body.append(location(-1, -1))
            self.n += 1
            self.fod()
            # add score
            self.score += 100
            setcolor(color_cfg.food)
            outtextxy(board_cfg.dashboard_x + 20, board_cfg.dashboard_y, "SCORE: " + str(self.score))

        for i in range(self.n):
            if self.head.x == self.body[i].x and self.head.y == self.body[i].y:
                self.closing()
        
    def fod(self):
        self.food.x = random.randrange(0, board_cfg.width)
        self.food.y = random.randrange(0, board_cfg.height)
        setcolor(color_cfg.food)
        outtextxy(self.food.x, self.food.y, tile_cfg.block)

    def getch(self):
        if self.haskey:
            self.haskey = False
            return self.k
        else:
            return None

    async def bgm_process(self):
        while True:
            await asyncio.sleep_ms(100)
            self.bgm.tick()

class p_music(music):
    song1 = '0 A5 2 26 0.6299212574958801;2 B5 2 26 0.6299212574958801;4 C6 6 26 0.6299212574958801;10 B5 2 26 0.6299212574958801;12 C6 4 26 0.6299212574958801;16 E6 4 26 0.6299212574958801;20 B5 8 26 0.6299212574958801;32 E5 4 26 0.6299212574958801;36 A5 6 26 0.6299212574958801;42 G5 2 26 0.6299212574958801;44 A5 4 26 0.6299212574958801;48 C6 4 26 0.6299212574958801;52 G5 8 26 0.6299212574958801;64 F5 2 26 0.6299212574958801;66 E5 2 26 0.6299212574958801;68 F5 6 26 0.6299212574958801;74 E5 2 26 0.6299212574958801;76 F5 4 26 0.6299212574958801;80 C6 4 26 0.6299212574958801;84 E5 8 26 0.6299212574958801;94 C6 2 26 0.6299212574958801;96 C6 2 26 0.6299212574958801;98 C6 2 26 0.6299212574958801;100 B5 6 26 0.6299212574958801;106 F#5 2 26 0.6299212574958801;108 F#5 4 26 0.6299212574958801;112 B5 4 26 0.6299212574958801;116 B5 8 26 0.6299212574958801;128 A5 2 26 0.6299212574958801;130 B5 2 26 0.6299212574958801;132 C6 6 26 0.6299212574958801;138 B5 2 26 0.6299212574958801;140 C6 4 26 0.6299212574958801;144 E6 4 26 0.6299212574958801;148 B5 8 26 0.6299212574958801;160 E5 2 26 0.6299212574958801;162 E5 2 26 0.6299212574958801;164 A5 6 26 0.6299212574958801;170 G5 2 26 0.6299212574958801;172 A5 4 26 0.6299212574958801;176 C6 4 26 0.6299212574958801;180 G5 8 26 0.6299212574958801;192 E5 4 26 0.6299212574958801;196 F5 4 26 0.6299212574958801;200 C6 2 26 0.6299212574958801;202 B5 2 26 0.6299212574958801;204 B5 4 26 0.6299212574958801;208 C6 4 26 0.6299212574958801;212 D6 4 26 0.6299212574958801;216 E6 2 26 0.6299212574958801;218 C6 2 26 0.6299212574958801;220 C6 8 26 0.6299212574958801;228 C6 2 26 0.6299212574958801;230 B5 2 26 0.6299212574958801;232 A5 4 26 0.6299212574958801;236 B5 4 26 0.6299212574958801;240 G#5 4 26 0.6299212574958801;244 A5 11 26 0.6299212574958801;256 C6 2 26 0.6299212574958801;258 D6 2 26 0.6299212574958801;260 E6 6 26 0.6299212574958801;266 D6 2 26 0.6299212574958801;268 E6 4 26 0.6299212574958801;272 G6 4 26 0.6299212574958801;276 D6 8 26 0.6299212574958801;288 G5 2 26 0.6299212574958801;290 G5 2 26 0.6299212574958801;292 C6 6 26 0.6299212574958801;298 B5 2 26 0.6299212574958801;300 C6 4 26 0.6299212574958801;304 E6 4 26 0.6299212574958801;308 E6 11 26 0.6299212574958801;324 A5 2 26 0.6299212574958801;326 B5 2 26 0.6299212574958801;328 C6 4 26 0.6299212574958801;332 B5 2 26 0.6299212574958801;334 C6 2 26 0.6299212574958801;336 D6 4 26 0.6299212574958801;340 C6 6 26 0.6299212574958801;346 G5 2 26 0.6299212574958801;348 G5 8 26 0.6299212574958801;356 F6 4 26 0.6299212574958801;360 E6 4 26 0.6299212574958801;364 D6 4 26 0.6299212574958801;368 C6 4 26 0.6299212574958801;372 E6 15 26 0.6299212574958801;388 E6 11 26 0.6299212574958801;400 E6 4 26 0.6299212574958801;404 A6 8 26 0.6299212574958801;412 G6 8 26 0.6299212574958801;420 E6 4 26 0.6299212574958801;424 D6 2 26 0.6299212574958801;426 C6 2 26 0.6299212574958801;428 C6 8 26 0.6299212574958801;436 D6 4 26 0.6299212574958801;440 C6 2 26 0.6299212574958801;442 D6 2 26 0.6299212574958801;444 D6 4 26 0.6299212574958801;448 G6 4 26 0.6299212574958801;452 E6 11 26 0.6299212574958801;464 E6 4 26 0.6299212574958801;468 A6 8 26 0.6299212574958801;476 G6 8 26 0.6299212574958801;484 E6 4 26 0.6299212574958801;488 D6 2 26 0.6299212574958801;490 C6 2 26 0.6299212574958801;492 C6 8 26 0.6299212574958801;500 D6 4 26 0.6299212574958801;504 C6 2 26 0.6299212574958801;506 D6 2 26 0.6299212574958801;508 D6 4 26 0.6299212574958801;512 B5 4 26 0.6299212574958801;516 A5 11 26 0.6299212574958801;528 A5 2 26 0.6299212574958801;530 B5 2 26 0.6299212574958801;532 C6 6 26 0.6299212574958801;538 B5 2 26 0.6299212574958801;540 C6 4 26 0.6299212574958801;544 E6 4 26 0.6299212574958801;548 B5 8 26 0.6299212574958801;560 E5 4 26 0.6299212574958801;564 A5 6 26 0.6299212574958801;570 G5 2 26 0.6299212574958801;572 A5 4 26 0.6299212574958801;576 C6 4 26 0.6299212574958801;580 G5 8 26 0.6299212574958801;592 F5 2 26 0.6299212574958801;594 E5 2 26 0.6299212574958801;596 F5 6 26 0.6299212574958801;602 E5 2 26 0.6299212574958801;604 F5 4 26 0.6299212574958801;608 C6 4 26 0.6299212574958801;612 E5 8 26 0.6299212574958801;622 C6 2 26 0.6299212574958801;624 C6 2 26 0.6299212574958801;626 C6 2 26 0.6299212574958801;628 B5 6 26 0.6299212574958801;634 F#5 2 26 0.6299212574958801;636 F#5 4 26 0.6299212574958801;640 B5 4 26 0.6299212574958801;644 B5 8 26 0.6299212574958801;656 A5 2 26 0.6299212574958801;658 B5 2 26 0.6299212574958801;660 C6 6 26 0.6299212574958801;666 B5 2 26 0.6299212574958801;668 C6 4 26 0.6299212574958801;672 E6 4 26 0.6299212574958801;676 B5 8 26 0.6299212574958801;688 E5 2 26 0.6299212574958801;690 E5 2 26 0.6299212574958801;692 A5 6 26 0.6299212574958801;698 G5 2 26 0.6299212574958801;700 A5 4 26 0.6299212574958801;704 C6 4 26 0.6299212574958801;708 G5 8 26 0.6299212574958801;720 E5 4 26 0.6299212574958801;724 F5 4 26 0.6299212574958801;728 C6 2 26 0.6299212574958801;730 B5 2 26 0.6299212574958801;732 B5 4 26 0.6299212574958801;736 C6 4 26 0.6299212574958801;740 D6 4 26 0.6299212574958801;744 E6 2 26 0.6299212574958801;746 C6 2 26 0.6299212574958801;748 C6 8 26 0.6299212574958801;756 C6 2 26 0.6299212574958801;758 B5 2 26 0.6299212574958801;760 A5 4 26 0.6299212574958801;764 B5 4 26 0.6299212574958801;768 G#5 4 26 0.6299212574958801;772 A5 11 26 0.6299212574958801'
    song0 = '0 E5 2 14;4 B4 2 14;6 C5 2 14;8 D5 2 14;10 E5 1 14;11 D5 1 14;12 C5 2 14;14 B4 2 14;16 A4 2 14;20 A4 2 14;22 C5 2 14;24 E5 2 14;28 D5 2 14;30 C5 2 14;32 B4 2 14;38 C5 2 14;40 D5 2 14;44 E5 2 14;48 C5 2 14;52 A4 2 14;56 A4 2 14;64 D5 2 14;68 F5 2 14;70 A5 2 14;74 G5 2 14;76 F5 2 14;78 E5 2 14;84 C5 2 14;86 E5 2 14;90 D5 2 14;92 C5 2 14;94 B4 2 14;98 B4 2 14;100 C5 2 14;102 D5 2 14;106 E5 2 14;110 C5 2 14;114 A4 2 14;118 A4 2 14;248 E5 8 14;256 C5 8 14;264 D5 8 14;272 B4 8 14;280 C5 8 14;288 A4 8 14;296 G#4 8 14;304 B4 8 14;313 E5 8 14;321 C5 8 14;329 D5 8 14;337 B4 4 14;341 B4 4 14;345 C5 4 14;349 E5 4 14;353 A5 8 14;361 G#5 8 14;248 C5 8 14;256 A4 8 14;264 B4 8 14;272 G#4 8 14;280 A4 8 14;288 E4 8 14;0 B4 2 14;4 G#4 2 14;6 A4 2 14;8 B4 2 14;12 A4 2 14;14 G#4 2 14;28 B4 2 14;30 A4 2 14;32 G#4 4 14;40 B4 2 14;38 A4 2 14;44 C5 2 14;52 E4 2 14;64 F4 2 14;94 G#4 2 14;100 A4 2 14;102 B4 2 14;106 C5 2 14;110 A4 2 14;114 E4 2 14;114 E4 2 14;118 E4 2 14;98 G#4 2 14;296 E4 8 14;304 G#4 8 14;313 C5 8 14;321 A4 8 14;329 B4 8 14;337 G#4 4 14;341 G#4 4 14;345 A4 4 14;349 C5 4 14;353 E5 8 14;353 E5 8 14;361 E5 8 14;48 A4 2 14;56 E4 2 14;68 D5 2 14;70 F5 2 14;74 E5 2 14;76 D5 2 14;80 E5 2 14;78 C5 2 14;80 C5 2 14;22 A4 2 14;24 C5 2 14;84 A4 2 14;86 C5 2 14;92 A4 2 14;90 B4 2 14;0 E5 2 14;4 B4 2 14;6 C5 2 14;8 D5 2 14;10 E5 1 14;11 D5 1 14;12 C5 2 14;14 B4 2 14;16 A4 2 14;20 A4 2 14;22 C5 2 14;24 E5 2 14;28 D5 2 14;30 C5 2 14;32 B4 2 14;38 C5 2 14;40 D5 2 14;44 E5 2 14;48 C5 2 14;52 A4 2 14;56 A4 2 14;64 D5 2 14;68 F5 2 14;70 A5 2 14;74 G5 2 14;76 F5 2 14;78 E5 2 14;84 C5 2 14;86 E5 2 14;90 D5 2 14;92 C5 2 14;94 B4 2 14;98 B4 2 14;100 C5 2 14;102 D5 2 14;106 E5 2 14;110 C5 2 14;114 A4 2 14;118 A4 2 14;0 B4 2 14;4 G#4 2 14;6 A4 2 14;8 B4 2 14;12 A4 2 14;14 G#4 2 14;28 B4 2 14;30 A4 2 14;32 G#4 4 14;40 B4 2 14;38 A4 2 14;44 C5 2 14;52 E4 2 14;64 F4 2 14;94 G#4 2 14;100 A4 2 14;102 B4 2 14;106 C5 2 14;110 A4 2 14;114 E4 2 14;114 E4 2 14;118 E4 2 14;98 G#4 2 14;48 A4 2 14;56 E4 2 14;68 D5 2 14;70 F5 2 14;74 E5 2 14;76 D5 2 14;80 E5 2 14;78 C5 2 14;80 C5 2 14;22 A4 2 14;24 C5 2 14;84 A4 2 14;86 C5 2 14;92 A4 2 14;90 B4 2 14;124 E5 2 14;128 B4 2 14;130 C5 2 14;132 D5 2 14;134 E5 1 14;135 D5 1 14;136 C5 2 14;138 B4 2 14;140 A4 2 14;144 A4 2 14;146 C5 2 14;148 E5 2 14;152 D5 2 14;154 C5 2 14;156 B4 2 14;162 C5 2 14;164 D5 2 14;168 E5 2 14;172 C5 2 14;176 A4 2 14;180 A4 2 14;188 D5 2 14;192 F5 2 14;194 A5 2 14;198 G5 2 14;200 F5 2 14;202 E5 2 14;208 C5 2 14;210 E5 2 14;214 D5 2 14;216 C5 2 14;218 B4 2 14;222 B4 2 14;224 C5 2 14;226 D5 2 14;230 E5 2 14;234 C5 2 14;238 A4 2 14;242 A4 2 14;124 B4 2 14;128 G#4 2 14;130 A4 2 14;132 B4 2 14;136 A4 2 14;138 G#4 2 14;152 B4 2 14;154 A4 2 14;156 G#4 4 14;164 B4 2 14;162 A4 2 14;168 C5 2 14;176 E4 2 14;188 F4 2 14;218 G#4 2 14;224 A4 2 14;226 B4 2 14;230 C5 2 14;234 A4 2 14;238 E4 2 14;238 E4 2 14;242 E4 2 14;222 G#4 2 14;172 A4 2 14;180 E4 2 14;192 D5 2 14;194 F5 2 14;198 E5 2 14;200 D5 2 14;204 E5 2 14;202 C5 2 14;204 C5 2 14;146 A4 2 14;148 C5 2 14;208 A4 2 14;210 C5 2 14;216 A4 2 14;214 B4 2 14;124 E5 2 14;128 B4 2 14;130 C5 2 14;132 D5 2 14;134 E5 1 14;135 D5 1 14;136 C5 2 14;138 B4 2 14;140 A4 2 14;144 A4 2 14;146 C5 2 14;148 E5 2 14;152 D5 2 14;154 C5 2 14;156 B4 2 14;162 C5 2 14;164 D5 2 14;168 E5 2 14;172 C5 2 14;176 A4 2 14;180 A4 2 14;188 D5 2 14;192 F5 2 14;194 A5 2 14;198 G5 2 14;200 F5 2 14;202 E5 2 14;208 C5 2 14;210 E5 2 14;214 D5 2 14;216 C5 2 14;218 B4 2 14;222 B4 2 14;224 C5 2 14;226 D5 2 14;230 E5 2 14;234 C5 2 14;238 A4 2 14;242 A4 2 14;124 B4 2 14;128 G#4 2 14;130 A4 2 14;132 B4 2 14;136 A4 2 14;138 G#4 2 14;152 B4 2 14;154 A4 2 14;156 G#4 4 14;164 B4 2 14;162 A4 2 14;168 C5 2 14;176 E4 2 14;188 F4 2 14;218 G#4 2 14;224 A4 2 14;226 B4 2 14;230 C5 2 14;234 A4 2 14;238 E4 2 14;238 E4 2 14;242 E4 2 14;222 G#4 2 14;172 A4 2 14;180 E4 2 14;192 D5 2 14;194 F5 2 14;198 E5 2 14;200 D5 2 14;204 E5 2 14;202 C5 2 14;204 C5 2 14;146 A4 2 14;148 C5 2 14;208 A4 2 14;210 C5 2 14;216 A4 2 14;214 B4 2 14;248 C5 2 13;252 C5 2 13;254 E5 2 13;250 E5 2 13;256 C5 2 13;260 C5 2 13;258 A4 2 13;262 A4 2 13;264 D5 2 13;268 D5 2 13;266 B4 2 13;270 B4 2 13;272 G#4 2 13;276 G#4 2 13;274 B4 2 13;278 B4 2 13;280 A4 2 13;284 A4 2 13;282 C5 2 13;286 C5 2 13;347 C5 2 13;351 C5 2 13;313 C5 2 13;317 C5 2 13;319 E5 2 13;315 E5 2 13;321 C5 2 13;325 C5 2 13;323 A4 2 13;327 A4 2 13;329 D5 2 13;333 D5 2 13;331 B4 2 13;335 B4 2 13;337 G#4 2 13;341 G#4 2 13;339 B4 2 13;343 B4 2 13;345 A4 2 13;349 A4 2 13;349 E5 2 13;353 E5 2 13;355 A5 2 13;359 A5 2 13;357 E5 2 13;361 E5 2 13;363 G#5 2 13;367 G#5 2 13;365 E5 2 14;292 A4 2 13;288 A4 2 13;294 E4 2 13;290 E4 2 13;300 G#4 2 13;296 G#4 2 13;302 E4 2 13;298 E4 2 13;308 G#4 2 13;304 G#4 2 13;310 B4 2 13;306 B4 2 13'
    def __init__(self, songString='0 D4 8 0', looping=True, tempo=3, duty=2512, pin=None, pins=[Pin(0)]):
        super().__init__(songString, looping, tempo, duty, pin, pins)
        self.pause = True

    def toggle_pause(self):
        self.pause = not self.pause
        if self.pause:
            print("mute\n")
            self.mute()
        else:
            print("unmute\n")
            self.unmute()
    
    def mute(self):
        for pwm in self.pwms:
            pwm.duty_u16(0)
    def unmute(self):
        for pwm in self.pwms:
            pwm.duty_u16(self.duty)

    def tick(self):
        if self.pause:
            pass
        else:
            super().tick()

def main():
    hardware.init()
    s = snake()
    asyncio.run(s.process())

main()

【2】st7789.py

"""
st7789 tft driver in MicroPython based on devbis' st7789py_mpy module from
https://github.com/devbis/st7789py_mpy.

I added support for display rotation, scrolling and drawing text using 8 and 16
bit wide bitmap fonts with heights that are multiples of 8.  Included are 12
bitmap fonts derived from classic pc text mode fonts.
"""

import time
from micropython import const
import ustruct as struct

# commands
ST7789_NOP = const(0x00)
ST7789_SWRESET = const(0x01)
ST7789_RDDID = const(0x04)
ST7789_RDDST = const(0x09)

ST7789_SLPIN = const(0x10)
ST7789_SLPOUT = const(0x11)
ST7789_PTLON = const(0x12)
ST7789_NORON = const(0x13)

ST7789_INVOFF = const(0x20)
ST7789_INVON = const(0x21)
ST7789_DISPOFF = const(0x28)
ST7789_DISPON = const(0x29)
ST7789_CASET = const(0x2A)
ST7789_RASET = const(0x2B)
ST7789_RAMWR = const(0x2C)
ST7789_RAMRD = const(0x2E)

ST7789_PTLAR = const(0x30)
ST7789_VSCRDEF = const(0x33)
ST7789_COLMOD = const(0x3A)
ST7789_MADCTL = const(0x36)
ST7789_VSCSAD = const(0x37)

ST7789_MADCTL_MY = const(0x80)
ST7789_MADCTL_MX = const(0x40)
ST7789_MADCTL_MV = const(0x20)
ST7789_MADCTL_ML = const(0x10)
ST7789_MADCTL_BGR = const(0x08)
ST7789_MADCTL_MH = const(0x04)
ST7789_MADCTL_RGB = const(0x00)

ST7789_RDID1 = const(0xDA)
ST7789_RDID2 = const(0xDB)
ST7789_RDID3 = const(0xDC)
ST7789_RDID4 = const(0xDD)

COLOR_MODE_65K = const(0x50)
COLOR_MODE_262K = const(0x60)
COLOR_MODE_12BIT = const(0x03)
COLOR_MODE_16BIT = const(0x05)
COLOR_MODE_18BIT = const(0x06)
COLOR_MODE_16M = const(0x07)

# Color definitions
BLACK = const(0x0000)
BLUE = const(0x001F)
RED = const(0xF800)
GREEN = const(0x07E0)
CYAN = const(0x07FF)
MAGENTA = const(0xF81F)
YELLOW = const(0xFFE0)
WHITE = const(0xFFFF)

_ENCODE_PIXEL = ">H"
_ENCODE_POS = ">HH"
_DECODE_PIXEL = ">BBB"

_BUFFER_SIZE = const(256)

_BIT7 = const(0x80)
_BIT6 = const(0x40)
_BIT5 = const(0x20)
_BIT4 = const(0x10)
_BIT3 = const(0x08)
_BIT2 = const(0x04)
_BIT1 = const(0x02)
_BIT0 = const(0x01)

def color565(red, green=0, blue=0):
    """
    Convert red, green and blue values (0-255) into a 16-bit 565 encoding.
    """
    try:
        red, green, blue = red  # see if the first var is a tuple/list
    except TypeError:
        pass
    return (red & 0xf8) << 8 | (green & 0xfc) << 3 | blue >> 3

def _encode_pos(x, y):
    """Encode a postion into bytes."""
    return struct.pack(_ENCODE_POS, x, y)

def _encode_pixel(color):
    """Encode a pixel color into bytes."""
    return struct.pack(_ENCODE_PIXEL, color)

class ST7789():

    def __init__(self, spi, width, height, reset, dc, cs=None, backlight=None,
                 xstart=-1, ystart=-1, rotation=0):
        """
        Initialize display.
        """
        if (width, height) != (240, 240) and (width, height) != (135, 240):
            raise ValueError(
                "Unsupported display. Only 240x240 and 135x240 are supported."
            )

        self._display_width = self.width = width
        self._display_height = self.height = height
        self.spi = spi
        self.reset = reset
        self.dc = dc
        self.cs = cs
        self.backlight = backlight
        self._rotation = rotation % 4
        
        self.xstart = xstart
        self.ystart = ystart

        self.spi.write(bytes(0xff))#

        self.hard_reset()
        self.soft_reset()
        self.sleep_mode(False)

        self._set_color_mode(COLOR_MODE_65K|COLOR_MODE_16BIT)
        time.sleep_ms(50)
        self.rotation(self._rotation)
        self.inversion_mode(True)
        time.sleep_ms(10)
        self.write(ST7789_NORON)
        time.sleep_ms(10)
        if backlight is not None:
            backlight.value(1)
        self.fill(0)
        self.write(ST7789_DISPON)
        time.sleep_ms(500)

    def write(self, command=None, data=None):
        """SPI write to the device: commands and data."""
        if self.cs:
            self.cs.off()

        if command is not None:
            self.dc.off()
            self.spi.write(bytes([command]))
        if data is not None:
            self.dc.on()
            self.spi.write(data)

        if self.cs:
            self.cs.on()

    def hard_reset(self):
        """
        Hard reset display.
        """
        if self.cs:
            self.cs.off()

        if self.reset:
            self.reset.on()
        time.sleep_ms(50)
        if self.reset:
            self.reset.off()
        time.sleep_ms(50)
        if self.reset:
            self.reset.on()
        time.sleep_ms(150)

        if self.cs:
            self.cs.on()

    def soft_reset(self):
        """
        Soft reset display.
        """
        self.write(ST7789_SWRESET)
        time.sleep_ms(150)

    def sleep_mode(self, value):
        """
        Enable or disable display sleep mode.

        Args:
            value (bool): if True enable sleep mode. if False disable sleep
            mode
        """
        if value:
            self.write(ST7789_SLPIN)
        else:
            self.write(ST7789_SLPOUT)

    def inversion_mode(self, value):
        """
        Enable or disable display inversion mode.

        Args:
            value (bool): if True enable inversion mode. if False disable
            inversion mode
        """
        if value:
            self.write(ST7789_INVON)
        else:
            self.write(ST7789_INVOFF)

    def _set_color_mode(self, mode):
        """
        Set display color mode.

        Args:
            mode (int): color mode
                COLOR_MODE_65K, COLOR_MODE_262K, COLOR_MODE_12BIT,
                COLOR_MODE_16BIT, COLOR_MODE_18BIT, COLOR_MODE_16M
        """
        self.write(ST7789_COLMOD, bytes([mode & 0x77]))

    def rotation(self, rotation):
        """
        Set display rotation.

        Args:
            rotation (int): 0-Portrait, 1-Landscape, 2-Inverted Portrait,
            3-Inverted Landscape
        """
        self._rotation = rotation % 4
        if self._rotation == 0:         # Portrait
            madctl = ST7789_MADCTL_RGB
            self.width = self._display_width
            self.height = self._display_height
            if self._display_width == 135:
                self.xstart = 52
                self.ystart = 40

        elif self._rotation == 1:       # Landscape
            madctl = ST7789_MADCTL_MX | ST7789_MADCTL_MV | ST7789_MADCTL_RGB
            self.width = self._display_height
            self.height = self._display_width
            if self._display_width == 135:
                self.xstart = 40
                self.ystart = 53

        elif self._rotation == 2:       # Inverted Portrait
            madctl = ST7789_MADCTL_MX | ST7789_MADCTL_MY | ST7789_MADCTL_RGB
            self.width = self._display_width
            self.height = self._display_height
            if self._display_width == 135:
                self.xstart = 53
                self.ystart = 40
        else:                           # Inverted Landscape
            madctl = ST7789_MADCTL_MV | ST7789_MADCTL_MY | ST7789_MADCTL_RGB
            self.width = self._display_height
            self.height = self._display_width
            if self._display_width == 135:
                self.xstart = 40
                self.ystart = 52

        self.write(ST7789_MADCTL, bytes([madctl]))

    def _set_columns(self, start, end):
        """
        Send CASET (column address set) command to display.

        Args:
            start (int): column start address
            end (int): column end address
        """
        if start <= end <= self.width:
            self.write(ST7789_CASET, _encode_pos(
                start+self.xstart, end + self.xstart))

    def _set_rows(self, start, end):
        """
        Send RASET (row address set) command to display.

        Args:
            start (int): row start address
            end (int): row end address
       """
        if start <= end <= self.height:
            self.write(ST7789_RASET, _encode_pos(
                start+self.ystart, end+self.ystart))

    def set_window(self, x0, y0, x1, y1):
        """
        Set window to column and row address.

        Args:
            x0 (int): column start address
            y0 (int): row start address
            x1 (int): column end address
            y1 (int): row end address
        """
        self._set_columns(x0, x1)
        self._set_rows(y0, y1)
        self.write(ST7789_RAMWR)

    def vline(self, x, y, length, color):
        """
        Draw vertical line at the given location and color.

        Args:
            x (int): x coordinate
            Y (int): y coordinate
            length (int): length of line
            color (int): 565 encoded color
        """
        self.fill_rect(x, y, 1, length, color)

    def hline(self, x, y, length, color):
        """
        Draw horizontal line at the given location and color.

        Args:
            x (int): x coordinate
            Y (int): y coordinate
            length (int): length of line
            color (int): 565 encoded color
        """
        self.fill_rect(x, y, length, 1, color)

    def pixel(self, x, y, color):
        """
        Draw a pixel at the given location and color.

        Args:
            x (int): x coordinate
            Y (int): y coordinate
            color (int): 565 encoded color
        """
        self.set_window(x, y, x, y)
        self.write(None, _encode_pixel(color))

    def blit_buffer(self, buffer, x, y, width, height):
        """
        Copy buffer to display at the given location.

        Args:
            buffer (bytes): Data to copy to display
            x (int): Top left corner x coordinate
            Y (int): Top left corner y coordinate
            width (int): Width
            height (int): Height
        """
        self.set_window(x, y, x + width - 1, y + height - 1)
        self.write(None, buffer)

    def rect(self, x, y, w, h, color):
        """
        Draw a rectangle at the given location, size and color.

        Args:
            x (int): Top left corner x coordinate
            y (int): Top left corner y coordinate
            width (int): Width in pixels
            height (int): Height in pixels
            color (int): 565 encoded color
        """
        self.hline(x, y, w, color)
        self.vline(x, y, h, color)
        self.vline(x + w - 1, y, h, color)
        self.hline(x, y + h - 1, w, color)

    def fill_rect(self, x, y, width, height, color):
        """
        Draw a rectangle at the given location, size and filled with color.

        Args:
            x (int): Top left corner x coordinate
            y (int): Top left corner y coordinate
            width (int): Width in pixels
            height (int): Height in pixels
            color (int): 565 encoded color
        """
        self.set_window(x, y, x + width - 1, y + height - 1)
        chunks, rest = divmod(width * height, _BUFFER_SIZE)
        pixel = _encode_pixel(color)
        self.dc.on()
        if chunks:
            data = pixel * _BUFFER_SIZE
            for _ in range(chunks):
                self.write(None, data)
        if rest:
            self.write(None, pixel * rest)

    def fill(self, color):
        """
        Fill the entire FrameBuffer with the specified color.

        Args:
            color (int): 565 encoded color
        """
        self.fill_rect(0, 0, self.width, self.height, color)

    def line(self, x0, y0, x1, y1, color):
        """
        Draw a single pixel wide line starting at x0, y0 and ending at x1, y1.

        Args:
            x0 (int): Start point x coordinate
            y0 (int): Start point y coordinate
            x1 (int): End point x coordinate
            y1 (int): End point y coordinate
            color (int): 565 encoded color
        """
        steep = abs(y1 - y0) > abs(x1 - x0)
        if steep:
            x0, y0 = y0, x0
            x1, y1 = y1, x1
        if x0 > x1:
            x0, x1 = x1, x0
            y0, y1 = y1, y0
        dx = x1 - x0
        dy = abs(y1 - y0)
        err = dx // 2
        if y0 < y1:
            ystep = 1
        else:
            ystep = -1
        while x0 <= x1:
            if steep:
                self.pixel(y0, x0, color)
            else:
                self.pixel(x0, y0, color)
            err -= dy
            if err < 0:
                y0 += ystep
                err += dx
            x0 += 1

    def vscrdef(self, tfa, vsa, bfa):
        """
        Set Vertical Scrolling Definition.

        To scroll a 135x240 display these values should be 40, 240, 40.
        There are 40 lines above the display that are not shown followed by
        240 lines that are shown followed by 40 more lines that are not shown.
        You could write to these areas off display and scroll them into view by
        changing the TFA, VSA and BFA values.

        Args:
            tfa (int): Top Fixed Area
            vsa (int): Vertical Scrolling Area
            bfa (int): Bottom Fixed Area
        """
        struct.pack(">HHH", tfa, vsa, bfa)
        self.write(ST7789_VSCRDEF, struct.pack(">HHH", tfa, vsa, bfa))

    def vscsad(self, vssa):
        """
        Set Vertical Scroll Start Address of RAM.

        Defines which line in the Frame Memory will be written as the first
        line after the last line of the Top Fixed Area on the display

        Example:

            for line in range(40, 280, 1):
                tft.vscsad(line)
                utime.sleep(0.01)

        Args:
            vssa (int): Vertical Scrolling Start Address

        """
        self.write(ST7789_VSCSAD, struct.pack(">H", vssa))

    def _text8(self, font, text, x0, y0, color=WHITE, background=BLACK):
        """
        Internal method to write characters with width of 8 and
        heights of 8 or 16.

        Args:
            font (module): font module to use
            text (str): text to write
            x0 (int): column to start drawing at
            y0 (int): row to start drawing at
            color (int): 565 encoded color to use for characters
            background (int): 565 encoded color to use for background
        """
        for char in text:
            ch = ord(char)
            if (font.FIRST <= ch < font.LAST
                    and x0+font.WIDTH <= self.width
                    and y0+font.HEIGHT <= self.height):

                if font.HEIGHT == 8:
                    passes = 1
                    size = 8
                    each = 0
                else:
                    passes = 2
                    size = 16
                    each = 8

                for line in range(passes):
                    idx = (ch-font.FIRST)*size+(each*line)
                    buffer = struct.pack('>64H',
                        color if font.FONT[idx] & _BIT7 else background,
                        color if font.FONT[idx] & _BIT6 else background,
                        color if font.FONT[idx] & _BIT5 else background,
                        color if font.FONT[idx] & _BIT4 else background,
                        color if font.FONT[idx] & _BIT3 else background,
                        color if font.FONT[idx] & _BIT2 else background,
                        color if font.FONT[idx] & _BIT1 else background,
                        color if font.FONT[idx] & _BIT0 else background,
                        color if font.FONT[idx+1] & _BIT7 else background,
                        color if font.FONT[idx+1] & _BIT6 else background,
                        color if font.FONT[idx+1] & _BIT5 else background,
                        color if font.FONT[idx+1] & _BIT4 else background,
                        color if font.FONT[idx+1] & _BIT3 else background,
                        color if font.FONT[idx+1] & _BIT2 else background,
                        color if font.FONT[idx+1] & _BIT1 else background,
                        color if font.FONT[idx+1] & _BIT0 else background,
                        color if font.FONT[idx+2] & _BIT7 else background,
                        color if font.FONT[idx+2] & _BIT6 else background,
                        color if font.FONT[idx+2] & _BIT5 else background,
                        color if font.FONT[idx+2] & _BIT4 else background,
                        color if font.FONT[idx+2] & _BIT3 else background,
                        color if font.FONT[idx+2] & _BIT2 else background,
                        color if font.FONT[idx+2] & _BIT1 else background,
                        color if font.FONT[idx+2] & _BIT0 else background,
                        color if font.FONT[idx+3] & _BIT7 else background,
                        color if font.FONT[idx+3] & _BIT6 else background,
                        color if font.FONT[idx+3] & _BIT5 else background,
                        color if font.FONT[idx+3] & _BIT4 else background,
                        color if font.FONT[idx+3] & _BIT3 else background,
                        color if font.FONT[idx+3] & _BIT2 else background,
                        color if font.FONT[idx+3] & _BIT1 else background,
                        color if font.FONT[idx+3] & _BIT0 else background,
                        color if font.FONT[idx+4] & _BIT7 else background,
                        color if font.FONT[idx+4] & _BIT6 else background,
                        color if font.FONT[idx+4] & _BIT5 else background,
                        color if font.FONT[idx+4] & _BIT4 else background,
                        color if font.FONT[idx+4] & _BIT3 else background,
                        color if font.FONT[idx+4] & _BIT2 else background,
                        color if font.FONT[idx+4] & _BIT1 else background,
                        color if font.FONT[idx+4] & _BIT0 else background,
                        color if font.FONT[idx+5] & _BIT7 else background,
                        color if font.FONT[idx+5] & _BIT6 else background,
                        color if font.FONT[idx+5] & _BIT5 else background,
                        color if font.FONT[idx+5] & _BIT4 else background,
                        color if font.FONT[idx+5] & _BIT3 else background,
                        color if font.FONT[idx+5] & _BIT2 else background,
                        color if font.FONT[idx+5] & _BIT1 else background,
                        color if font.FONT[idx+5] & _BIT0 else background,
                        color if font.FONT[idx+6] & _BIT7 else background,
                        color if font.FONT[idx+6] & _BIT6 else background,
                        color if font.FONT[idx+6] & _BIT5 else background,
                        color if font.FONT[idx+6] & _BIT4 else background,
                        color if font.FONT[idx+6] & _BIT3 else background,
                        color if font.FONT[idx+6] & _BIT2 else background,
                        color if font.FONT[idx+6] & _BIT1 else background,
                        color if font.FONT[idx+6] & _BIT0 else background,
                        color if font.FONT[idx+7] & _BIT7 else background,
                        color if font.FONT[idx+7] & _BIT6 else background,
                        color if font.FONT[idx+7] & _BIT5 else background,
                        color if font.FONT[idx+7] & _BIT4 else background,
                        color if font.FONT[idx+7] & _BIT3 else background,
                        color if font.FONT[idx+7] & _BIT2 else background,
                        color if font.FONT[idx+7] & _BIT1 else background,
                        color if font.FONT[idx+7] & _BIT0 else background
                    )
                    self.blit_buffer(buffer, x0, y0+8*line, 8, 8)

                x0 += 8

    def _text16(self, font, text, x0, y0, color=WHITE, background=BLACK):
        """
        Internal method to draw characters with width of 16 and heights of 16
        or 32.

        Args:
            font (module): font module to use
            text (str): text to write
            x0 (int): column to start drawing at
            y0 (int): row to start drawing at
            color (int): 565 encoded color to use for characters
            background (int): 565 encoded color to use for background
        """
        for char in text:
            ch = ord(char)
            if (font.FIRST <= ch < font.LAST
                    and x0+font.WIDTH <= self.width
                    and y0+font.HEIGHT <= self.height):

                if font.HEIGHT == 16:
                    passes = 2
                    size = 32
                    each = 16
                else:
                    passes = 4
                    size = 64
                    each = 16

                for line in range(passes):
                    idx = (ch-font.FIRST)*size+(each*line)
                    buffer = struct.pack('>128H',
                        color if font.FONT[idx] & _BIT7 else background,
                        color if font.FONT[idx] & _BIT6 else background,
                        color if font.FONT[idx] & _BIT5 else background,
                        color if font.FONT[idx] & _BIT4 else background,
                        color if font.FONT[idx] & _BIT3 else background,
                        color if font.FONT[idx] & _BIT2 else background,
                        color if font.FONT[idx] & _BIT1 else background,
                        color if font.FONT[idx] & _BIT0 else background,
                        color if font.FONT[idx+1] & _BIT7 else background,
                        color if font.FONT[idx+1] & _BIT6 else background,
                        color if font.FONT[idx+1] & _BIT5 else background,
                        color if font.FONT[idx+1] & _BIT4 else background,
                        color if font.FONT[idx+1] & _BIT3 else background,
                        color if font.FONT[idx+1] & _BIT2 else background,
                        color if font.FONT[idx+1] & _BIT1 else background,
                        color if font.FONT[idx+1] & _BIT0 else background,
                        color if font.FONT[idx+2] & _BIT7 else background,
                        color if font.FONT[idx+2] & _BIT6 else background,
                        color if font.FONT[idx+2] & _BIT5 else background,
                        color if font.FONT[idx+2] & _BIT4 else background,
                        color if font.FONT[idx+2] & _BIT3 else background,
                        color if font.FONT[idx+2] & _BIT2 else background,
                        color if font.FONT[idx+2] & _BIT1 else background,
                        color if font.FONT[idx+2] & _BIT0 else background,
                        color if font.FONT[idx+3] & _BIT7 else background,
                        color if font.FONT[idx+3] & _BIT6 else background,
                        color if font.FONT[idx+3] & _BIT5 else background,
                        color if font.FONT[idx+3] & _BIT4 else background,
                        color if font.FONT[idx+3] & _BIT3 else background,
                        color if font.FONT[idx+3] & _BIT2 else background,
                        color if font.FONT[idx+3] & _BIT1 else background,
                        color if font.FONT[idx+3] & _BIT0 else background,
                        color if font.FONT[idx+4] & _BIT7 else background,
                        color if font.FONT[idx+4] & _BIT6 else background,
                        color if font.FONT[idx+4] & _BIT5 else background,
                        color if font.FONT[idx+4] & _BIT4 else background,
                        color if font.FONT[idx+4] & _BIT3 else background,
                        color if font.FONT[idx+4] & _BIT2 else background,
                        color if font.FONT[idx+4] & _BIT1 else background,
                        color if font.FONT[idx+4] & _BIT0 else background,
                        color if font.FONT[idx+5] & _BIT7 else background,
                        color if font.FONT[idx+5] & _BIT6 else background,
                        color if font.FONT[idx+5] & _BIT5 else background,
                        color if font.FONT[idx+5] & _BIT4 else background,
                        color if font.FONT[idx+5] & _BIT3 else background,
                        color if font.FONT[idx+5] & _BIT2 else background,
                        color if font.FONT[idx+5] & _BIT1 else background,
                        color if font.FONT[idx+5] & _BIT0 else background,
                        color if font.FONT[idx+6] & _BIT7 else background,
                        color if font.FONT[idx+6] & _BIT6 else background,
                        color if font.FONT[idx+6] & _BIT5 else background,
                        color if font.FONT[idx+6] & _BIT4 else background,
                        color if font.FONT[idx+6] & _BIT3 else background,
                        color if font.FONT[idx+6] & _BIT2 else background,
                        color if font.FONT[idx+6] & _BIT1 else background,
                        color if font.FONT[idx+6] & _BIT0 else background,
                        color if font.FONT[idx+7] & _BIT7 else background,
                        color if font.FONT[idx+7] & _BIT6 else background,
                        color if font.FONT[idx+7] & _BIT5 else background,
                        color if font.FONT[idx+7] & _BIT4 else background,
                        color if font.FONT[idx+7] & _BIT3 else background,
                        color if font.FONT[idx+7] & _BIT2 else background,
                        color if font.FONT[idx+7] & _BIT1 else background,
                        color if font.FONT[idx+7] & _BIT0 else background,
                        color if font.FONT[idx+8] & _BIT7 else background,
                        color if font.FONT[idx+8] & _BIT6 else background,
                        color if font.FONT[idx+8] & _BIT5 else background,
                        color if font.FONT[idx+8] & _BIT4 else background,
                        color if font.FONT[idx+8] & _BIT3 else background,
                        color if font.FONT[idx+8] & _BIT2 else background,
                        color if font.FONT[idx+8] & _BIT1 else background,
                        color if font.FONT[idx+8] & _BIT0 else background,
                        color if font.FONT[idx+9] & _BIT7 else background,
                        color if font.FONT[idx+9] & _BIT6 else background,
                        color if font.FONT[idx+9] & _BIT5 else background,
                        color if font.FONT[idx+9] & _BIT4 else background,
                        color if font.FONT[idx+9] & _BIT3 else background,
                        color if font.FONT[idx+9] & _BIT2 else background,
                        color if font.FONT[idx+9] & _BIT1 else background,
                        color if font.FONT[idx+9] & _BIT0 else background,
                        color if font.FONT[idx+10] & _BIT7 else background,
                        color if font.FONT[idx+10] & _BIT6 else background,
                        color if font.FONT[idx+10] & _BIT5 else background,
                        color if font.FONT[idx+10] & _BIT4 else background,
                        color if font.FONT[idx+10] & _BIT3 else background,
                        color if font.FONT[idx+10] & _BIT2 else background,
                        color if font.FONT[idx+10] & _BIT1 else background,
                        color if font.FONT[idx+10] & _BIT0 else background,
                        color if font.FONT[idx+11] & _BIT7 else background,
                        color if font.FONT[idx+11] & _BIT6 else background,
                        color if font.FONT[idx+11] & _BIT5 else background,
                        color if font.FONT[idx+11] & _BIT4 else background,
                        color if font.FONT[idx+11] & _BIT3 else background,
                        color if font.FONT[idx+11] & _BIT2 else background,
                        color if font.FONT[idx+11] & _BIT1 else background,
                        color if font.FONT[idx+11] & _BIT0 else background,
                        color if font.FONT[idx+12] & _BIT7 else background,
                        color if font.FONT[idx+12] & _BIT6 else background,
                        color if font.FONT[idx+12] & _BIT5 else background,
                        color if font.FONT[idx+12] & _BIT4 else background,
                        color if font.FONT[idx+12] & _BIT3 else background,
                        color if font.FONT[idx+12] & _BIT2 else background,
                        color if font.FONT[idx+12] & _BIT1 else background,
                        color if font.FONT[idx+12] & _BIT0 else background,
                        color if font.FONT[idx+13] & _BIT7 else background,
                        color if font.FONT[idx+13] & _BIT6 else background,
                        color if font.FONT[idx+13] & _BIT5 else background,
                        color if font.FONT[idx+13] & _BIT4 else background,
                        color if font.FONT[idx+13] & _BIT3 else background,
                        color if font.FONT[idx+13] & _BIT2 else background,
                        color if font.FONT[idx+13] & _BIT1 else background,
                        color if font.FONT[idx+13] & _BIT0 else background,
                        color if font.FONT[idx+14] & _BIT7 else background,
                        color if font.FONT[idx+14] & _BIT6 else background,
                        color if font.FONT[idx+14] & _BIT5 else background,
                        color if font.FONT[idx+14] & _BIT4 else background,
                        color if font.FONT[idx+14] & _BIT3 else background,
                        color if font.FONT[idx+14] & _BIT2 else background,
                        color if font.FONT[idx+14] & _BIT1 else background,
                        color if font.FONT[idx+14] & _BIT0 else background,
                        color if font.FONT[idx+15] & _BIT7 else background,
                        color if font.FONT[idx+15] & _BIT6 else background,
                        color if font.FONT[idx+15] & _BIT5 else background,
                        color if font.FONT[idx+15] & _BIT4 else background,
                        color if font.FONT[idx+15] & _BIT3 else background,
                        color if font.FONT[idx+15] & _BIT2 else background,
                        color if font.FONT[idx+15] & _BIT1 else background,
                        color if font.FONT[idx+15] & _BIT0 else background
                    )
                    self.blit_buffer(buffer, x0, y0+8*line, 16, 8)
            x0 += font.WIDTH

    def text(self, font, text, x0, y0, color=WHITE, background=BLACK):
        """
        Draw text on display in specified font and colors. 8 and 16 bit wide
        fonts are supported.

        Args:
            font (module): font module to use.
            text (str): text to write
            x0 (int): column to start drawing at
            y0 (int): row to start drawing at
            color (int): 565 encoded color to use for characters
            background (int): 565 encoded color to use for background
        """
        if font.WIDTH == 8:
            self._text8(font, text, x0, y0, color, background)
        else:
            self._text16(font, text, x0, y0, color, background)

【3】vga2_8x8.py

"""converted from vga_8x8.bin """
WIDTH = 8
HEIGHT = 8
FIRST = 0x00
LAST = 0xff
_FONT =\
b'\x00\x00\x00\x00\x00\x00\x00\x00'\
b'\x7e\x81\xa5\x81\xbd\x99\x81\x7e'\
b'\x7e\xff\xdb\xff\xc3\xe7\xff\x7e'\
b'\x6c\xfe\xfe\xfe\x7c\x38\x10\x00'\
b'\x10\x38\x7c\xfe\x7c\x38\x10\x00'\
b'\x38\x7c\x38\xfe\xfe\xd6\x10\x38'\
b'\x10\x38\x7c\xfe\xfe\x7c\x10\x38'\
b'\x00\x00\x18\x3c\x3c\x18\x00\x00'\
b'\xff\xff\xe7\xc3\xc3\xe7\xff\xff'\
b'\x00\x3c\x66\x42\x42\x66\x3c\x00'\
b'\xff\xc3\x99\xbd\xbd\x99\xc3\xff'\
b'\x0f\x07\x0f\x7d\xcc\xcc\xcc\x78'\
b'\x3c\x66\x66\x66\x3c\x18\x7e\x18'\
b'\x3f\x33\x3f\x30\x30\x70\xf0\xe0'\
b'\x7f\x63\x7f\x63\x63\x67\xe6\xc0'\
b'\x18\xdb\x3c\xe7\xe7\x3c\xdb\x18'\
b'\x80\xe0\xf8\xfe\xf8\xe0\x80\x00'\
b'\x02\x0e\x3e\xfe\x3e\x0e\x02\x00'\
b'\x18\x3c\x7e\x18\x18\x7e\x3c\x18'\
b'\x66\x66\x66\x66\x66\x00\x66\x00'\
b'\x7f\xdb\xdb\x7b\x1b\x1b\x1b\x00'\
b'\x3e\x61\x3c\x66\x66\x3c\x86\x7c'\
b'\x00\x00\x00\x00\x7e\x7e\x7e\x00'\
b'\x18\x3c\x7e\x18\x7e\x3c\x18\xff'\
b'\x18\x3c\x7e\x18\x18\x18\x18\x00'\
b'\x18\x18\x18\x18\x7e\x3c\x18\x00'\
b'\x00\x18\x0c\xfe\x0c\x18\x00\x00'\
b'\x00\x30\x60\xfe\x60\x30\x00\x00'\
b'\x00\x00\xc0\xc0\xc0\xfe\x00\x00'\
b'\x00\x24\x66\xff\x66\x24\x00\x00'\
b'\x00\x18\x3c\x7e\xff\xff\x00\x00'\
b'\x00\xff\xff\x7e\x3c\x18\x00\x00'\
b'\x00\x00\x00\x00\x00\x00\x00\x00'\
b'\x18\x3c\x3c\x18\x18\x00\x18\x00'\
b'\x66\x66\x24\x00\x00\x00\x00\x00'\
b'\x6c\x6c\xfe\x6c\xfe\x6c\x6c\x00'\
b'\x18\x3e\x60\x3c\x06\x7c\x18\x00'\
b'\x00\xc6\xcc\x18\x30\x66\xc6\x00'\
b'\x38\x6c\x38\x76\xdc\xcc\x76\x00'\
b'\x18\x18\x30\x00\x00\x00\x00\x00'\
b'\x0c\x18\x30\x30\x30\x18\x0c\x00'\
b'\x30\x18\x0c\x0c\x0c\x18\x30\x00'\
b'\x00\x66\x3c\xff\x3c\x66\x00\x00'\
b'\x00\x18\x18\x7e\x18\x18\x00\x00'\
b'\x00\x00\x00\x00\x00\x18\x18\x30'\
b'\x00\x00\x00\x7e\x00\x00\x00\x00'\
b'\x00\x00\x00\x00\x00\x18\x18\x00'\
b'\x06\x0c\x18\x30\x60\xc0\x80\x00'\
b'\x38\x6c\xc6\xd6\xc6\x6c\x38\x00'\
b'\x18\x38\x18\x18\x18\x18\x7e\x00'\
b'\x7c\xc6\x06\x1c\x30\x66\xfe\x00'\
b'\x7c\xc6\x06\x3c\x06\xc6\x7c\x00'\
b'\x1c\x3c\x6c\xcc\xfe\x0c\x1e\x00'\
b'\xfe\xc0\xc0\xfc\x06\xc6\x7c\x00'\
b'\x38\x60\xc0\xfc\xc6\xc6\x7c\x00'\
b'\xfe\xc6\x0c\x18\x30\x30\x30\x00'\
b'\x7c\xc6\xc6\x7c\xc6\xc6\x7c\x00'\
b'\x7c\xc6\xc6\x7e\x06\x0c\x78\x00'\
b'\x00\x18\x18\x00\x00\x18\x18\x00'\
b'\x00\x18\x18\x00\x00\x18\x18\x30'\
b'\x06\x0c\x18\x30\x18\x0c\x06\x00'\
b'\x00\x00\x7e\x00\x00\x7e\x00\x00'\
b'\x60\x30\x18\x0c\x18\x30\x60\x00'\
b'\x7c\xc6\x0c\x18\x18\x00\x18\x00'\
b'\x7c\xc6\xde\xde\xde\xc0\x78\x00'\
b'\x38\x6c\xc6\xfe\xc6\xc6\xc6\x00'\
b'\xfc\x66\x66\x7c\x66\x66\xfc\x00'\
b'\x3c\x66\xc0\xc0\xc0\x66\x3c\x00'\
b'\xf8\x6c\x66\x66\x66\x6c\xf8\x00'\
b'\xfe\x62\x68\x78\x68\x62\xfe\x00'\
b'\xfe\x62\x68\x78\x68\x60\xf0\x00'\
b'\x3c\x66\xc0\xc0\xce\x66\x3a\x00'\
b'\xc6\xc6\xc6\xfe\xc6\xc6\xc6\x00'\
b'\x3c\x18\x18\x18\x18\x18\x3c\x00'\
b'\x1e\x0c\x0c\x0c\xcc\xcc\x78\x00'\
b'\xe6\x66\x6c\x78\x6c\x66\xe6\x00'\
b'\xf0\x60\x60\x60\x62\x66\xfe\x00'\
b'\xc6\xee\xfe\xfe\xd6\xc6\xc6\x00'\
b'\xc6\xe6\xf6\xde\xce\xc6\xc6\x00'\
b'\x7c\xc6\xc6\xc6\xc6\xc6\x7c\x00'\
b'\xfc\x66\x66\x7c\x60\x60\xf0\x00'\
b'\x7c\xc6\xc6\xc6\xc6\xce\x7c\x0e'\
b'\xfc\x66\x66\x7c\x6c\x66\xe6\x00'\
b'\x3c\x66\x30\x18\x0c\x66\x3c\x00'\
b'\x7e\x7e\x5a\x18\x18\x18\x3c\x00'\
b'\xc6\xc6\xc6\xc6\xc6\xc6\x7c\x00'\
b'\xc6\xc6\xc6\xc6\xc6\x6c\x38\x00'\
b'\xc6\xc6\xc6\xd6\xd6\xfe\x6c\x00'\
b'\xc6\xc6\x6c\x38\x6c\xc6\xc6\x00'\
b'\x66\x66\x66\x3c\x18\x18\x3c\x00'\
b'\xfe\xc6\x8c\x18\x32\x66\xfe\x00'\
b'\x3c\x30\x30\x30\x30\x30\x3c\x00'\
b'\xc0\x60\x30\x18\x0c\x06\x02\x00'\
b'\x3c\x0c\x0c\x0c\x0c\x0c\x3c\x00'\
b'\x10\x38\x6c\xc6\x00\x00\x00\x00'\
b'\x00\x00\x00\x00\x00\x00\x00\xff'\
b'\x30\x18\x0c\x00\x00\x00\x00\x00'\
b'\x00\x00\x78\x0c\x7c\xcc\x76\x00'\
b'\xe0\x60\x7c\x66\x66\x66\xdc\x00'\
b'\x00\x00\x7c\xc6\xc0\xc6\x7c\x00'\
b'\x1c\x0c\x7c\xcc\xcc\xcc\x76\x00'\
b'\x00\x00\x7c\xc6\xfe\xc0\x7c\x00'\
b'\x3c\x66\x60\xf8\x60\x60\xf0\x00'\
b'\x00\x00\x76\xcc\xcc\x7c\x0c\xf8'\
b'\xe0\x60\x6c\x76\x66\x66\xe6\x00'\
b'\x18\x00\x38\x18\x18\x18\x3c\x00'\
b'\x06\x00\x06\x06\x06\x66\x66\x3c'\
b'\xe0\x60\x66\x6c\x78\x6c\xe6\x00'\
b'\x38\x18\x18\x18\x18\x18\x3c\x00'\
b'\x00\x00\xec\xfe\xd6\xd6\xd6\x00'\
b'\x00\x00\xdc\x66\x66\x66\x66\x00'\
b'\x00\x00\x7c\xc6\xc6\xc6\x7c\x00'\
b'\x00\x00\xdc\x66\x66\x7c\x60\xf0'\
b'\x00\x00\x76\xcc\xcc\x7c\x0c\x1e'\
b'\x00\x00\xdc\x76\x60\x60\xf0\x00'\
b'\x00\x00\x7e\xc0\x7c\x06\xfc\x00'\
b'\x30\x30\xfc\x30\x30\x36\x1c\x00'\
b'\x00\x00\xcc\xcc\xcc\xcc\x76\x00'\
b'\x00\x00\xc6\xc6\xc6\x6c\x38\x00'\
b'\x00\x00\xc6\xd6\xd6\xfe\x6c\x00'\
b'\x00\x00\xc6\x6c\x38\x6c\xc6\x00'\
b'\x00\x00\xc6\xc6\xc6\x7e\x06\xfc'\
b'\x00\x00\x7e\x4c\x18\x32\x7e\x00'\
b'\x0e\x18\x18\x70\x18\x18\x0e\x00'\
b'\x18\x18\x18\x18\x18\x18\x18\x00'\
b'\x70\x18\x18\x0e\x18\x18\x70\x00'\
b'\x76\xdc\x00\x00\x00\x00\x00\x00'\
b'\x00\x10\x38\x6c\xc6\xc6\xfe\x00'\
b'\x7c\xc6\xc0\xc0\xc6\x7c\x0c\x78'\
b'\xcc\x00\xcc\xcc\xcc\xcc\x76\x00'\
b'\x0c\x18\x7c\xc6\xfe\xc0\x7c\x00'\
b'\x7c\x82\x78\x0c\x7c\xcc\x76\x00'\
b'\xc6\x00\x78\x0c\x7c\xcc\x76\x00'\
b'\x30\x18\x78\x0c\x7c\xcc\x76\x00'\
b'\x30\x30\x78\x0c\x7c\xcc\x76\x00'\
b'\x00\x00\x7e\xc0\xc0\x7e\x0c\x38'\
b'\x7c\x82\x7c\xc6\xfe\xc0\x7c\x00'\
b'\xc6\x00\x7c\xc6\xfe\xc0\x7c\x00'\
b'\x30\x18\x7c\xc6\xfe\xc0\x7c\x00'\
b'\x66\x00\x38\x18\x18\x18\x3c\x00'\
b'\x7c\x82\x38\x18\x18\x18\x3c\x00'\
b'\x30\x18\x00\x38\x18\x18\x3c\x00'\
b'\xc6\x38\x6c\xc6\xfe\xc6\xc6\x00'\
b'\x38\x6c\x7c\xc6\xfe\xc6\xc6\x00'\
b'\x18\x30\xfe\xc0\xf8\xc0\xfe\x00'\
b'\x00\x00\x7e\x18\x7e\xd8\x7e\x00'\
b'\x3e\x6c\xcc\xfe\xcc\xcc\xce\x00'\
b'\x7c\x82\x7c\xc6\xc6\xc6\x7c\x00'\
b'\xc6\x00\x7c\xc6\xc6\xc6\x7c\x00'\
b'\x30\x18\x7c\xc6\xc6\xc6\x7c\x00'\
b'\x78\x84\x00\xcc\xcc\xcc\x76\x00'\
b'\x60\x30\xcc\xcc\xcc\xcc\x76\x00'\
b'\xc6\x00\xc6\xc6\xc6\x7e\x06\xfc'\
b'\xc6\x38\x6c\xc6\xc6\x6c\x38\x00'\
b'\xc6\x00\xc6\xc6\xc6\xc6\x7c\x00'\
b'\x18\x18\x7e\xc0\xc0\x7e\x18\x18'\
b'\x38\x6c\x64\xf0\x60\x66\xfc\x00'\
b'\x66\x66\x3c\x7e\x18\x7e\x18\x18'\
b'\xf8\xcc\xcc\xfa\xc6\xcf\xc6\xc7'\
b'\x0e\x1b\x18\x3c\x18\xd8\x70\x00'\
b'\x18\x30\x78\x0c\x7c\xcc\x76\x00'\
b'\x0c\x18\x00\x38\x18\x18\x3c\x00'\
b'\x0c\x18\x7c\xc6\xc6\xc6\x7c\x00'\
b'\x18\x30\xcc\xcc\xcc\xcc\x76\x00'\
b'\x76\xdc\x00\xdc\x66\x66\x66\x00'\
b'\x76\xdc\x00\xe6\xf6\xde\xce\x00'\
b'\x3c\x6c\x6c\x3e\x00\x7e\x00\x00'\
b'\x38\x6c\x6c\x38\x00\x7c\x00\x00'\
b'\x18\x00\x18\x18\x30\x63\x3e\x00'\
b'\x00\x00\x00\xfe\xc0\xc0\x00\x00'\
b'\x00\x00\x00\xfe\x06\x06\x00\x00'\
b'\x63\xe6\x6c\x7e\x33\x66\xcc\x0f'\
b'\x63\xe6\x6c\x7a\x36\x6a\xdf\x06'\
b'\x18\x00\x18\x18\x3c\x3c\x18\x00'\
b'\x00\x33\x66\xcc\x66\x33\x00\x00'\
b'\x00\xcc\x66\x33\x66\xcc\x00\x00'\
b'\x22\x88\x22\x88\x22\x88\x22\x88'\
b'\x55\xaa\x55\xaa\x55\xaa\x55\xaa'\
b'\x77\xdd\x77\xdd\x77\xdd\x77\xdd'\
b'\x18\x18\x18\x18\x18\x18\x18\x18'\
b'\x18\x18\x18\x18\xf8\x18\x18\x18'\
b'\x18\x18\xf8\x18\xf8\x18\x18\x18'\
b'\x36\x36\x36\x36\xf6\x36\x36\x36'\
b'\x00\x00\x00\x00\xfe\x36\x36\x36'\
b'\x00\x00\xf8\x18\xf8\x18\x18\x18'\
b'\x36\x36\xf6\x06\xf6\x36\x36\x36'\
b'\x36\x36\x36\x36\x36\x36\x36\x36'\
b'\x00\x00\xfe\x06\xf6\x36\x36\x36'\
b'\x36\x36\xf6\x06\xfe\x00\x00\x00'\
b'\x36\x36\x36\x36\xfe\x00\x00\x00'\
b'\x18\x18\xf8\x18\xf8\x00\x00\x00'\
b'\x00\x00\x00\x00\xf8\x18\x18\x18'\
b'\x18\x18\x18\x18\x1f\x00\x00\x00'\
b'\x18\x18\x18\x18\xff\x00\x00\x00'\
b'\x00\x00\x00\x00\xff\x18\x18\x18'\
b'\x18\x18\x18\x18\x1f\x18\x18\x18'\
b'\x00\x00\x00\x00\xff\x00\x00\x00'\
b'\x18\x18\x18\x18\xff\x18\x18\x18'\
b'\x18\x18\x1f\x18\x1f\x18\x18\x18'\
b'\x36\x36\x36\x36\x37\x36\x36\x36'\
b'\x36\x36\x37\x30\x3f\x00\x00\x00'\
b'\x00\x00\x3f\x30\x37\x36\x36\x36'\
b'\x36\x36\xf7\x00\xff\x00\x00\x00'\
b'\x00\x00\xff\x00\xf7\x36\x36\x36'\
b'\x36\x36\x37\x30\x37\x36\x36\x36'\
b'\x00\x00\xff\x00\xff\x00\x00\x00'\
b'\x36\x36\xf7\x00\xf7\x36\x36\x36'\
b'\x18\x18\xff\x00\xff\x00\x00\x00'\
b'\x36\x36\x36\x36\xff\x00\x00\x00'\
b'\x00\x00\xff\x00\xff\x18\x18\x18'\
b'\x00\x00\x00\x00\xff\x36\x36\x36'\
b'\x36\x36\x36\x36\x3f\x00\x00\x00'\
b'\x18\x18\x1f\x18\x1f\x00\x00\x00'\
b'\x00\x00\x1f\x18\x1f\x18\x18\x18'\
b'\x00\x00\x00\x00\x3f\x36\x36\x36'\
b'\x36\x36\x36\x36\xff\x36\x36\x36'\
b'\x18\x18\xff\x18\xff\x18\x18\x18'\
b'\x18\x18\x18\x18\xf8\x00\x00\x00'\
b'\x00\x00\x00\x00\x1f\x18\x18\x18'\
b'\xff\xff\xff\xff\xff\xff\xff\xff'\
b'\x00\x00\x00\x00\xff\xff\xff\xff'\
b'\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0'\
b'\x0f\x0f\x0f\x0f\x0f\x0f\x0f\x0f'\
b'\xff\xff\xff\xff\x00\x00\x00\x00'\
b'\x00\x00\x76\xdc\xc8\xdc\x76\x00'\
b'\x78\xcc\xcc\xd8\xcc\xc6\xcc\x00'\
b'\xfe\xc6\xc0\xc0\xc0\xc0\xc0\x00'\
b'\x00\x00\xfe\x6c\x6c\x6c\x6c\x00'\
b'\xfe\xc6\x60\x30\x60\xc6\xfe\x00'\
b'\x00\x00\x7e\xd8\xd8\xd8\x70\x00'\
b'\x00\x00\x66\x66\x66\x66\x7c\xc0'\
b'\x00\x76\xdc\x18\x18\x18\x18\x00'\
b'\x7e\x18\x3c\x66\x66\x3c\x18\x7e'\
b'\x38\x6c\xc6\xfe\xc6\x6c\x38\x00'\
b'\x38\x6c\xc6\xc6\x6c\x6c\xee\x00'\
b'\x0e\x18\x0c\x3e\x66\x66\x3c\x00'\
b'\x00\x00\x7e\xdb\xdb\x7e\x00\x00'\
b'\x06\x0c\x7e\xdb\xdb\x7e\x60\xc0'\
b'\x1e\x30\x60\x7e\x60\x30\x1e\x00'\
b'\x00\x7c\xc6\xc6\xc6\xc6\xc6\x00'\
b'\x00\xfe\x00\xfe\x00\xfe\x00\x00'\
b'\x18\x18\x7e\x18\x18\x00\x7e\x00'\
b'\x30\x18\x0c\x18\x30\x00\x7e\x00'\
b'\x0c\x18\x30\x18\x0c\x00\x7e\x00'\
b'\x0e\x1b\x1b\x18\x18\x18\x18\x18'\
b'\x18\x18\x18\x18\x18\xd8\xd8\x70'\
b'\x00\x18\x00\x7e\x00\x18\x00\x00'\
b'\x00\x76\xdc\x00\x76\xdc\x00\x00'\
b'\x38\x6c\x6c\x38\x00\x00\x00\x00'\
b'\x00\x00\x00\x18\x18\x00\x00\x00'\
b'\x00\x00\x00\x18\x00\x00\x00\x00'\
b'\x0f\x0c\x0c\x0c\xec\x6c\x3c\x1c'\
b'\x6c\x36\x36\x36\x36\x00\x00\x00'\
b'\x78\x0c\x18\x30\x7c\x00\x00\x00'\
b'\x00\x00\x3c\x3c\x3c\x3c\x00\x00'\
b'\x00\x00\x00\x00\x00\x00\x00\x00'\

FONT = memoryview(_FONT)

【4】button.py

### button.py --- button based on Pin and IRQ, with debounce.
## author: picospuch

import time
from machine import Pin

class button:
    def __init__(self, pin, callback=None, trigger=Pin.IRQ_RISING, min_ago=200):
        #print("button init")
        self.callback = callback
            
        self.min_ago = min_ago
        self._next_call = time.ticks_add(time.ticks_ms(), self.min_ago)

        self.pin = Pin(pin, Pin.IN, Pin.PULL_UP)

        self.pin.irq(trigger=trigger, handler=self.debounce_handler)

        self._is_pressed = False

    def call_callback(self, pin):
        #print("call_callback")
        self._is_pressed = True
        if self.callback is not None:
            self.callback(pin)

    def debounce_handler(self, pin):
        #print("debounce")
        if time.ticks_diff(time.ticks_ms(), self._next_call) > 0:
            self._next_call = time.ticks_add(time.ticks_ms(), self.min_ago)
            self.call_callback(pin)

    def value(self):
        p = self._is_pressed
        self._is_pressed = False
        return p

【5】board.py

### board.py --- eetree micropython training board configuration.
## author: picospuch

class game_kit:
    # joystick FJO8K-N VR1
    joy_x = 28
    joy_y = 29

    # buzzer Buzzer LS1
    buzzer = 23

    # accelerometer MMA7660FC U2
    accelerometer_scl = 11
    accelerometer_sda = 10
    accelerometer_int = 9

    # keys (SW3 SW4 SW5 SW6)
    key_b = 5
    key_a = 6
    key_start = 7
    key_select = 8

    # infra-red-rtx (IRM-H638T VSMB10940) U4
    ir_rx = 25
    ir_tx = 24

    # liquid-crystal-display ST7789_1.54_240x240 DS1
    lcd_sck = 2
    lcd_sda = 3
    lcd_rst = 0
    lcd_dc = 1

    # status-led STA D1
    led_sta = 4
    
class pin_cfg:
    yellow_led = 20
    blue_led = 21
    green_led = 22
    red_led = 26
    
    buzzer = 19
    mic = 27
    
    i2c0_scl = 17
    i2c0_sda = 16
    
    i2c1_scl = 15
    i2c1_sda = 14

    spi1_mosi = 11
    spi1_sck = 10
    spi1_dc = 9
    spi1_rstn = 8
    spi1_cs = 29

    adc0 = 26
    adc1 = 27

    k1 = 12
    k2 = 13

    pot = 28

(【1】在原代码基础上做了轻微调整

【2】—【5】均在库里gitee.com/eetree-git/RP2040_Game_Kit/tree/main/lecture)

3.实现的功能及展示

FsVskuaun8bArTLz9SokXW0hIo9_

按键A后可长鸣。

4.问题

在运行代码的过程中发现st7789.py下的类ST7789中未定义offset以及init,导致无法运行。

FrHNP5EWqfMtwZogaj9UGYmhg5A3

所以我将相应的两行注释后,继续运行,仍有报错,错因尚未弄清楚。

  在按下按键A后,蜂鸣器会有一阵长鸣,再按一下,声音消失。

FsHTS20DMndTihDsZzZjdX5oJCDw

  1. 未来的计划

 本次只实现了背景图的展示以及按键后长鸣的效果,未达到理想效果,未来希望通过代码的调整修改能够完整实现贪吃蛇小游戏的功能同时能够完整呈现出背景音乐。

 基于树莓派RP2040的嵌入式系统进行一些项目的实现以及micropython的学习应该是一个长远的过程,硬禾学堂提供了相当丰富的开源学习资料以及大量免费视频。同时在这几次的视频学习中,我认为对我来说最大的收获是学会依靠一切可以得到的或者开源的信息不断学习化为己有,以及学会在官方网站或者库中进行查寻和学习,只要学会了学习的方法以及来源,坚持下去,总会有所成果!

附件下载
yasnake.py
剩余附件在百度网盘,链接如下: 链接:https://pan.baidu.com/s/1Ci8qrkk_kntwHZ9ACvSwMg 提取码:lixf
团队介绍
李晓芳 中央民族大学
评论
0 / 100
查看更多
目录
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2023 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号