-
Notifications
You must be signed in to change notification settings - Fork 6
/
esp.py
74 lines (65 loc) · 2.41 KB
/
esp.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
from contextlib import contextmanager
from time import sleep
from typing import Optional, Union
class Esp:
def __init__(self,
nand: bool = False,
xavier: bool = False,
orin: bool = False,
v05: bool = False,
device: Optional[str] = None,
) -> None:
print('Initializing ESP...')
self.en = 436 if xavier else 492 if orin else 216
self.g0 = 428 if xavier else 460 if orin else 50
self.gpio_en = f'gpio{self.en}' if not orin else 'PAC.06'
self.gpio_g0 = f'gpio{self.g0}' if not orin else 'PR.04'
if orin and v05:
self.en, self.g0 = self.g0, self.en
self.gpio_en, self.gpio_g0 = self.gpio_g0, self.gpio_en
if device is None:
self.device = '/dev/ttyTHS' + ('0' if xavier else '0' if orin else '1')
else:
self.device = device
self.on = 1 if nand else 0
self.off = 0 if nand else 1
def write(self, path: str, value: Union[str, int]) -> None:
try:
print(f'echo {value:3} > /sys/class/gpio/{path}')
with open(f'/sys/class/gpio/{path}', 'w') as f:
f.write(f'{value}\n')
except Exception:
print(f'could not write {value} to {path}')
@contextmanager
def pin_config(self) -> None:
print('Configuring pins...')
self.write('export', self.en)
sleep(0.5)
self.write('export', self.g0)
sleep(0.5)
self.write(f'{self.gpio_en}/direction', 'out')
sleep(0.5)
self.write(f'{self.gpio_g0}/direction', 'out')
sleep(0.5)
yield
self.write('unexport', self.en)
sleep(0.5)
self.write('unexport', self.g0)
@contextmanager
def flash_mode(self):
print('Bringing microcontroller into flash mode...')
self.write(f'{self.gpio_en}/value', self.on)
sleep(0.5)
self.write(f'{self.gpio_g0}/value', self.on)
sleep(0.5)
self.write(f'{self.gpio_en}/value', self.off)
sleep(0.5)
yield
self.activate()
def activate(self) -> None:
print('Bringing microcontroller into normal operation mode...')
self.write(f'{self.gpio_g0}/value', self.off)
sleep(0.5)
self.write(f'{self.gpio_en}/value', self.on)
sleep(0.5)
self.write(f'{self.gpio_en}/value', self.off)