-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.py
282 lines (211 loc) · 9.75 KB
/
environment.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from dataclasses import dataclass, field
import yaml
from PIL import ImageFont
from yaml import Dumper, FullLoader, Loader, MappingNode, Node, UnsafeLoader
@dataclass
class SPIConfig:
bus: int
device: int
@dataclass
class I2CConfig:
port: int
address: int
@dataclass
class SerialConfig:
port: str
baudrate: int
@dataclass
class ColorConfig:
background: tuple[int, int, int]
accent: tuple[int, int, int]
accent_dark: tuple[int, int, int]
@dataclass
class AppConfig:
app_side_offset: int = 20
app_top_offset: int = 30
app_bottom_offset: int = 25
font_name: str = 'FreeSansBold.ttf'
font_header_size: int = 16
font_standard_size: int = 14
color_mode: int = 0
width: int = 480
height: int = 320
modes: list[ColorConfig] = None
# cached properties
__font_header: ImageFont.FreeTypeFont | None = None
__font_standard: ImageFont.FreeTypeFont | None = None
def __post_init__(self):
if self.modes is None:
self.modes = [
ColorConfig(
background=(0, 0, 0),
accent=(27, 251, 30),
accent_dark=(9, 64, 9)
),
ColorConfig(
background=(0, 0, 0),
accent=(255, 245, 101),
accent_dark=(59, 45, 25)
)
]
@property
def resolution(self) -> tuple[int, int]:
return self.width, self.height
@property
def app_size(self) -> tuple[int, int]:
return self.width - 2 * self.app_side_offset, self.height - self.app_top_offset - self.app_bottom_offset
@property
def font_header(self) -> ImageFont.FreeTypeFont:
if self.__font_header is None:
self.__font_header = ImageFont.truetype(self.font_name, self.font_header_size)
return self.__font_header
@property
def font_standard(self) -> ImageFont.FreeTypeFont:
if self.__font_standard is None:
self.__font_standard = ImageFont.truetype(self.font_name, self.font_standard_size)
return self.__font_standard
@property
def background(self) -> tuple[int, int, int]:
return self.modes[self.color_mode].background
@property
def accent(self) -> tuple[int, int, int]:
return self.modes[self.color_mode].accent
@property
def accent_dark(self) -> tuple[int, int, int]:
return self.modes[self.color_mode].accent_dark
@dataclass
class KeypadConfig:
# Buttons
up_pin: int = 5
down_pin: int = 6
left_pin: int = 12
right_pin: int = 13
a_pin: int = 16
b_pin: int = 26
@dataclass
class RotaryConfig:
# Rotary encoder
rotary_device: str = '/dev/input/event0'
clk_pin: int = 22
dt_pin: int = 23
sw_pin: int = 27
@dataclass
class DisplayConfig:
# Display module
rst_pin: int = 25
dc_pin: int = 24
display_device: SPIConfig = field(default_factory=lambda: SPIConfig(0, 0))
flip_display: bool = False
# Touch module
cs_pin: int = 7
irq_pin: int = 17
touch_device: SPIConfig = field(default_factory=lambda: SPIConfig(0, 1))
@dataclass
class Environment:
dev_mode: bool = False
env_sensor_config: I2CConfig = field(default_factory=lambda: I2CConfig(1, 0x76))
adc_config: I2CConfig = field(default_factory=lambda: I2CConfig(1, 0x48))
gps_module_config: SerialConfig = field(default_factory=lambda: SerialConfig('/dev/serial0', 9600))
app_config: AppConfig = field(default_factory=lambda: AppConfig())
keypad_config: KeypadConfig = field(default_factory=lambda: KeypadConfig())
rotary_config: RotaryConfig = field(default_factory=lambda: RotaryConfig())
display_config: DisplayConfig = field(default_factory=lambda: DisplayConfig())
# cached property
__is_raspberry_pi: bool | None = None
@property
def is_raspberry_pi(self):
if self.__is_raspberry_pi is None:
try:
with open('/sys/firmware/devicetree/base/model', 'r') as model_info:
self.__is_raspberry_pi = 'Raspberry Pi' in model_info.read()
except FileNotFoundError:
self.__is_raspberry_pi = False
return self.__is_raspberry_pi
def spi_config_constructor(loader: Loader | FullLoader | UnsafeLoader, node: Node) -> SPIConfig:
if isinstance(node, MappingNode):
values = loader.construct_mapping(node)
return SPIConfig(**values)
raise TypeError("node is not of type MappingNode")
def spi_config_representor(dumper: Dumper, data: SPIConfig) -> MappingNode:
return dumper.represent_mapping('!SPIConfig', {k: v for k,v in vars(data).items() if k[0] != '_'})
def i2c_config_constructor(loader: Loader | FullLoader | UnsafeLoader, node: Node) -> I2CConfig:
if isinstance(node, MappingNode):
values = loader.construct_mapping(node)
return I2CConfig(**values)
raise TypeError("node is not of type MappingNode")
def i2c_config_representor(dumper: Dumper, data: I2CConfig) -> MappingNode:
return dumper.represent_mapping('!I2CConfig', {k: v for k,v in vars(data).items() if k[0] != '_'})
def serial_config_constructor(loader: Loader | FullLoader | UnsafeLoader, node: Node) -> SerialConfig:
if isinstance(node, MappingNode):
values = loader.construct_mapping(node)
return SerialConfig(**values)
raise TypeError("node if not of type MappingNode")
def serial_config_representor(dumper: Dumper, data: SerialConfig) -> MappingNode:
return dumper.represent_mapping('!SerialConfig', {k: v for k,v in vars(data).items() if k[0] != '_'})
def color_config_constructor(loader: Loader | FullLoader | UnsafeLoader, node: Node) -> ColorConfig:
if isinstance(node, MappingNode):
values = loader.construct_mapping(node)
return ColorConfig(**values)
raise TypeError("node is not of type MappingNode")
def color_config_representor(dumper: Dumper, data: ColorConfig) -> MappingNode:
return dumper.represent_mapping('!ColorConfig', {k: v for k,v in vars(data).items() if k[0] != '_'})
def app_config_constructor(loader: Loader | FullLoader | UnsafeLoader, node: Node) -> AppConfig:
if isinstance(node, MappingNode):
values = loader.construct_mapping(node)
return AppConfig(**values)
raise TypeError("node is not of type MappingNode")
def app_config_representor(dumper: Dumper, data: AppConfig) -> MappingNode:
return dumper.represent_mapping('!AppConfig', {k: v for k,v in vars(data).items() if k[0] != '_'})
def keypad_config_constructor(loader: Loader | FullLoader | UnsafeLoader, node: Node) -> KeypadConfig:
if isinstance(node, MappingNode):
values = loader.construct_mapping(node)
return KeypadConfig(**values)
raise TypeError("node is not of type MappingNode")
def keypad_config_representor(dumper: Dumper, data: KeypadConfig) -> MappingNode:
return dumper.represent_mapping('!KeypadConfig', {k: v for k,v in vars(data).items() if k[0] != '_'})
def rotary_config_constructor(loader: Loader | FullLoader | UnsafeLoader, node: Node) -> RotaryConfig:
if isinstance(node, MappingNode):
values = loader.construct_mapping(node)
return RotaryConfig(**values)
raise TypeError("node is not of type MappingNode")
def rotary_config_representor(dumper: Dumper, data: RotaryConfig) -> MappingNode:
return dumper.represent_mapping('!RotaryConfig', {k: v for k,v in vars(data).items() if k[0] != '_'})
def display_config_constructor(loader: Loader | FullLoader | UnsafeLoader, node: Node) -> DisplayConfig:
if isinstance(node, MappingNode):
values = loader.construct_mapping(node)
return DisplayConfig(**values)
raise TypeError("node is not of type MappingNode")
def display_config_representor(dumper: Dumper, data: DisplayConfig) -> MappingNode:
return dumper.represent_mapping('!DisplayConfig', {k: v for k,v in vars(data).items() if k[0] != '_'})
def environment_constructor(loader: Loader | FullLoader | UnsafeLoader, node: Node) -> Environment:
if isinstance(node, MappingNode):
values = loader.construct_mapping(node)
return Environment(**values)
raise TypeError("node is not of type MappingNode")
def environment_representor(dumper: Dumper, data: Environment) -> MappingNode:
return dumper.represent_mapping('!Environment', {k: v for k,v in vars(data).items() if k[0] != '_'})
def configure():
yaml.add_constructor('!SPIConfig', spi_config_constructor)
yaml.add_constructor('!I2CConfig', i2c_config_constructor)
yaml.add_constructor('!SerialConfig', serial_config_constructor)
yaml.add_constructor('!ColorConfig', color_config_constructor)
yaml.add_constructor('!AppConfig', app_config_constructor)
yaml.add_constructor('!KeypadConfig', keypad_config_constructor)
yaml.add_constructor('!RotaryConfig', rotary_config_constructor)
yaml.add_constructor('!DisplayConfig', display_config_constructor)
yaml.add_constructor('!Environment', environment_constructor)
yaml.add_representer(SPIConfig, spi_config_representor)
yaml.add_representer(I2CConfig, i2c_config_representor)
yaml.add_representer(SerialConfig, serial_config_representor)
yaml.add_representer(ColorConfig, color_config_representor)
yaml.add_representer(AppConfig, app_config_representor)
yaml.add_representer(KeypadConfig, keypad_config_representor)
yaml.add_representer(RotaryConfig, rotary_config_representor)
yaml.add_representer(DisplayConfig, display_config_representor)
yaml.add_representer(Environment, environment_representor)
def load(file_name: str = 'config.yaml') -> Environment:
with open(file_name, 'r') as file:
return yaml.load(file, FullLoader)
def save(environment: Environment, file_name: str = 'config.yaml'):
with open(file_name, 'w') as file:
yaml.dump(environment, file, sort_keys=False)