summaryrefslogtreecommitdiff
path: root/wasp/boards/simulator/display.py
blob: 72423f94e1e02315ce737e9373d81ab6d1cc437e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
""" Simulated ST7789 display. """

import sys
import sdl2
import sdl2.ext

CASET = 0x2a
RASET = 0x2b
RAMWR = 0x2c

class ST7789Sim(object):
    def __init__(self, width=240, height=240):
        sdl2.ext.init()

        self.width = width
        self.height = height

        self.x = 0
        self.y = 0
        self.colclip = [0, width-1]
        self.rowclip = [0, height-1]
        self.cmd = 0

        self.window = sdl2.ext.Window("ST7789", size=(width, height))
        self.window.show()
        self.windowsurface = self.window.get_surface()
        sdl2.ext.fill(self.windowsurface, (0, 0, 0))

    def write(self, data):
        if len(data) == 1:
            # Assume if we get a byte at a time then it is command.
            # This is a simplification do we don't have to track
            # the D/C pin from within the simulator.
            self.cmd = data[0]

        elif self.cmd == CASET:
            self.colclip[0] = (data[0] << 8) + data[1]
            self.colclip[1] = (data[2] << 8) + data[3]
            self.x = self.colclip[0]

        elif self.cmd == RASET:
            self.rowclip[0] = (data[0] << 8) + data[1]
            self.rowclip[1] = (data[2] << 8) + data[3]
            self.y = self.rowclip[0]

        elif self.cmd == RAMWR:
            pixelview = sdl2.ext.PixelView(self.windowsurface)

            half = False
            for d in data:
                if not half:
                    rgb = d << 8
                    half = True
                    continue
                rgb |= d
                half = False

                pixel = ((rgb & 0xf800) >> 8,
                         (rgb & 0x07e0) >> 3,
                         (rgb & 0x001f) << 3)
            
                pixelview[self.y][self.x] = pixel

                self.x += 1
                if self.x > self.colclip[1]:
                    self.x = self.colclip[0]
                    self.y += 1
                if self.y > self.rowclip[1]:
                    self.y = self.rowclip[0]
            
            # Forcibly release the surface to ensure it is unlocked
            del pixelview
            self.window.refresh()