forked from Hack-a-Day/Vectorscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreennorm.py
130 lines (104 loc) · 3.43 KB
/
screennorm.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import gc9a01
from machine import Pin, SPI
import vga1_16x32 as font
import gc
import romans as deffont
class ScreenNorm:
"""
A light wrapper around the gc9a01 screen.
Attributes:
_spi (SPI): The SPI interface.
tft (gc9a01.GC9A01): The TFT display.
"""
def _spi_init(self):
self._spi = SPI(0, baudrate=40000000, sck=Pin(2, Pin.OUT), mosi=Pin(3, Pin.OUT), miso=Pin(20,Pin.IN))
def __init__(self):
"""
The constructor for ScreenNorm class.
"""
self._spi_init()
self.wake()
def get_font(self):
"""
Get the normal font
Returns:
font object
"""
return font
def get_vfont(self):
"""
Get the default vector font (romans)
"""
return deffont
def wake(self):
"""
Method to wake up the display.
"""
self._spi_init()
self.tft = gc9a01.GC9A01(self._spi, 240, 240,
reset=Pin(4, Pin.OUT),
cs=Pin(26, Pin.OUT),
dc=Pin(5, Pin.OUT),
backlight=Pin(27, Pin.OUT),
rotation=0)
self.tft.init()
def idle(self):
"""
Method to get the display out of the way for another screen helper.
"""
self._spi.deinit()
self.spi=None
self.tft=None
gc.collect()
def jpg(self,filename):
"""
Method to show a jpg on the display.
Args:
filename (str): The name of the jpg file.
"""
if self.tft!=None:
gc.collect()
self.tft.jpg(filename,0,0,1)
def text(self,x,y,txt,fg_color=gc9a01.color565(45, 217, 80),bg_color=gc9a01.color565(0,25,10)):
"""
Method to draw text on the display.
Args:
x (int): The x-coordinate of the top left corner of the text.
y (int): The y-coordinate of the top left corner of the text.
txt (str): The text to draw.
fg_color (int): The foreground color. Default is gc9a01.color565(243,191,16).
bg_color (int): The background color. Default is gc9a01.color565(26,26,26).
"""
if self.tft!=None:
self.tft.text(font,txt,x,y,fg_color,bg_color)
def text_font(self,font,x,y,txt,fg_color=gc9a01.color565(45, 217, 80),scale=1.0):
"""
Method to draw text with a font.
Args:
font (font or None): The font to use (or use default font)
x,y (int): X and Y coordinate
txt (str): String
fg_color (int) Foreground color (note: no background color -- always transparent)
"""
if self.tft!=None:
if font==None:
font=deffont
self.tft.draw(font,txt,x,y,fg_color,scale)
def clear(self,color=0):
"""
Method to clear the display with a color.
Args:
color (int): The background color. Default is 0.
"""
if self.tft!=None:
self.tft.fill_rect(0,0,240,240,color)
def pixel(self,x,y,color):
"""
Method to set a pixel on the display.
Args:
x (int): The x-coordinate of the pixel.
y (int): The y-coordinate of the pixel.
color (int): The color of the pixel.
"""
if self.tft!=None:
self.tft.pixel(x,y,color)