-
Notifications
You must be signed in to change notification settings - Fork 2
/
dcc.py
218 lines (167 loc) · 6.18 KB
/
dcc.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
# dcc.py
# control a DCC++ system / command station
import machine
import uasyncio as asyncio
from machine import UART, Pin
from micropython import const
import cbusobjects
import logger
FUNCTION_OFF = const(0)
FUNCTION_ON = const(1)
DIRECTION_REVERSE = const(0)
DIRECTION_FORWARD = const(1)
POWER_OFF = const(0)
POWER_ON = const(1)
COMMS_TIMEOUT = const(500)
class loco:
def __init__(self, decoder_id) -> None:
self.decoder_id = decoder_id
self.speed = 0
self.direction = DIRECTION_FORWARD
self.functions = [0] * 30
self.active = False
self.session = -1
class dccpp_connection:
def __init__(self, host=None, port: int = None):
self.logger = logger.logger()
self.host = host
self.port = port
def connect(self):
pass
def disconnect(self):
pass
def write(self, data):
pass
async def read(self) -> bytes:
pass
class dccpp_serial_connection(dccpp_connection):
def __init__(self, host=None, port: int = 0, tx: int = 0, rx: int = 0):
super().__init__(host=host, port=port)
if host is not None and isinstance(host, machine.UART):
self.uart = host
else:
self.uart = UART(port)
self.uart.init(baudrate=115200, tx=Pin(tx), rx=Pin(rx), txbuf=32, rxbuf=128, timeout=COMMS_TIMEOUT)
self.logger.log(self.uart)
def write(self, data) -> None:
self.uart.write(data)
pass
async def read(self) -> bytes:
return self.uart.read(128)
pass
class dccpp_network_connection(dccpp_connection):
def __init__(self, host: str, port: int):
super().__init__(host=host, port=port)
self.sock = None
self.writer = None
self.reader = None
def connect(self):
try:
import socket
except ImportError:
self.logger.log('import failed, device is not Pico W')
return
try:
self.sock = socket.socket()
addr_info = socket.getaddrinfo(self.host, self.port)
addr = addr_info[0][-1]
self.sock.settimeout(COMMS_TIMEOUT / 1000)
self.sock.connect(addr)
except OSError:
self.logger.log('socket connect error')
return
self.writer = asyncio.StreamWriter(self.sock)
self.reader = asyncio.StreamReader(self.sock)
def write(self, data):
self.writer.write(data)
await self.writer.drain()
async def read(self):
return self.reader.read(128)
def disconnect(self):
self.sock.shutdown()
self.sock.close()
class dccpp:
def __init__(self, connection: dccpp_connection, timeout: int = 2000) -> None:
self.logger = logger.logger()
self.connection = connection
self.request = ''
self.response = ''
self.timeout = timeout
self.attempts = 1
self.request = None
self.response = None
self.evc = asyncio.Event()
self.evt = asyncio.Event()
# self.timer = cbusobjects.timeout(self.timeout, self.evt)
self.timer = cbusobjects.timeout(self.timeout)
self.active_sessions = {} # decoder_id: loco object
self.connection.connect()
async def send_request(self) -> None:
self.logger.log(f'dccpp: send_request: request = {self.request}')
self.response = ''
self.connection.write(self.request)
data = await self.connection.read()
if data and len(data) > 0:
self.response = data.decode()
async def acquire(self, loco: loco) -> bool:
self.request = f'<t -1 {loco.decoder_id} 0 1>'
await self.send_request()
if self.response.startswith('<'):
loco.session = 99
loco.active = True
self.logger.log(f'dccpp: loco {loco.decoder_id}, session = {loco.session}, active = {loco.active}')
self.active_sessions[loco.decoder_id] = loco
else:
self.logger.log('dccpp: invalid response')
return loco.active
def dispatch(self, loco: loco) -> None:
del self.active_sessions[loco.decoder_id]
async def set_speed(self, loco: loco, speed: int) -> None:
self.request = f'<t -1 {loco.decoder_id} {speed} {loco.direction}>'
await self.send_request()
if self.response.startswith('<'):
self.logger.log(f'dccpp: {self.response}')
loco.speed = speed
else:
self.logger.log('dccpp: invalid response')
async def set_direction(self, loco: loco, direction: int) -> None:
self.request = f'<t -1 {loco.decoder_id} {loco.speed} {direction}>'
await self.send_request()
if self.response.startswith('<'):
self.logger.log(f'dccpp: {self.response}')
loco.direction = direction
else:
self.logger.log('dccpp: invalid response')
async def function(self, loco: loco, function: int, polarity: int) -> None:
self.request = f'<F {loco.decoder_id} {function} {polarity}>'
await self.send_request()
if self.response.startswith('<'):
self.logger.log(f'dccpp: {self.response}')
loco.functions[function] = polarity
else:
self.logger.log('dccpp: no response')
async def status(self) -> bool:
self.request = '<s>'
await self.send_request()
if self.response.startswith('<'):
self.logger.log(f'dccpp: {self.response}')
return True
else:
self.logger.log('dccpp: no response')
return False
async def track_power(self, state: int) -> None:
self.request = f'<{state}>'
await self.send_request()
if self.response.startswith('<'):
self.logger.log(f'dccpp: {self.response}')
else:
self.logger.log('dccpp: no response')
async def emergency_stop(self, loco: loco) -> None:
self.set_speed(loco, 1)
async def emergency_stop_all(self) -> None:
self.request = '<!>'
await self.send_request()
if self.response.startswith('<'):
self.logger.log(f'dccpp: {self.response}')
else:
self.logger.log('dccpp: no response')