aboutsummaryrefslogtreecommitdiffstats
path: root/remote/libsimplepwm/messages.py
blob: 87db5e9a8d9b566a0f0cdb38cf506ebfc89e0ab3 (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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#
# Simple PWM controller
# Remote control tool
#
# Copyright (c) 2018-2021 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

__all__ = [
    "SimplePWMMsg",
    "SimplePWMMsg_Nop",
    "SimplePWMMsg_Ack",
    "SimplePWMMsg_Nack",
    "SimplePWMMsg_Ping",
    "SimplePWMMsg_Pong",
    "SimplePWMMsg_GetControl",
    "SimplePWMMsg_Control",
    "SimplePWMMsg_GetSetpoints",
    "SimplePWMMsg_Setpoints",
    "SimplePWMMsg_GetPwmcorr",
    "SimplePWMMsg_Pwmcorr",
    "SimplePWMMsg_GetBatvolt",
    "SimplePWMMsg_Batvolt",
    "SimplePWMMsg_Enterboot",
]

from fractions import Fraction
from libsimplepwm.crc import *
from libsimplepwm.util import *

class SimplePWMMsg(object):
    PAYLOAD_SIZE            = 8
    SIZE                    = 1 + 1 + 1 + PAYLOAD_SIZE + 1

    MSG_MAGIC               = 0xAA
    MSG_SYNCBYTE            = b"\x00"

    MSGID_NOP               = 0
    MSGID_ACK               = 1
    MSGID_NACK              = 2
    MSGID_PING              = 3
    MSGID_PONG              = 4
    MSGID_GET_CONTROL       = 5
    MSGID_CONTROL           = 6
    MSGID_GET_SETPOINTS     = 7
    MSGID_SETPOINTS         = 8
    MSGID_GET_PWMCORR       = 9
    MSGID_PWMCORR           = 10
    MSGID_GET_BATVOLT       = 11
    MSGID_BATVOLT           = 12
    MSGID_ENTERBOOT         = 0xFF

    @staticmethod
    def _toLe16(value):
        return (value & 0xFFFF).to_bytes(2, "little")

    @staticmethod
    def _fromLe16(data, signed=False):
        return int.from_bytes(data, "little", signed=signed)

    @classmethod
    def parse(cls, data):
        assert len(data) == cls.SIZE
        magic, msgId = data[0:2]
        payload = data[3:-1]

        if magic != cls.MSG_MAGIC:
            raise SimplePWMError("Received corrupted message: Magic byte mismatch.")
        if crc8(data[:-1]) != data[-1]:
            raise SimplePWMError("Received corrupted message: CRC mismatch.")

        if msgId == cls.MSGID_NOP:
            return SimplePWMMsg_Nop._parse(payload)
        elif msgId == cls.MSGID_ACK:
            return SimplePWMMsg_Ack._parse(payload)
        elif msgId == cls.MSGID_NACK:
            return SimplePWMMsg_Nack._parse(payload)
        elif msgId == cls.MSGID_PING:
            return SimplePWMMsg_Ping._parse(payload)
        elif msgId == cls.MSGID_PONG:
            return SimplePWMMsg_Pong._parse(payload)
        elif msgId == cls.MSGID_GET_CONTROL:
            return SimplePWMMsg_GetControl._parse(payload)
        elif msgId == cls.MSGID_CONTROL:
            return SimplePWMMsg_Control._parse(payload)
        elif msgId == cls.MSGID_GET_SETPOINTS:
            return SimplePWMMsg_SetSetpoints._parse(payload)
        elif msgId == cls.MSGID_SETPOINTS:
            return SimplePWMMsg_Setpoints._parse(payload)
        elif msgId == cls.MSGID_GET_PWMCORR:
            return SimplePWMMsg_GetPwmcorr._parse(payload)
        elif msgId == cls.MSGID_PWMCORR:
            return SimplePWMMsg_Pwmcorr._parse(payload)
        elif msgId == cls.MSGID_GET_BATVOLT:
            return SimplePWMMsg_GetBatvolt._parse(payload)
        elif msgId == cls.MSGID_BATVOLT:
            return SimplePWMMsg_Batvolt._parse(payload)
        elif msgId == cls.MSGID_ENTERBOOT:
            return SimplePWMMsg_Enterboot._parse(payload)

        printError(f"Received unknown message: 0x{msgId:X}")
        return None

    @classmethod
    def _parse(cls, payload):
        return cls()

    def __init__(self, msgId):
        self.msgId = msgId

    def getData(self, payload=None):
        if payload is None:
            payload = b"\x00" * self.PAYLOAD_SIZE
        if len(payload) < self.PAYLOAD_SIZE:
            payload += b"\x00" * (self.PAYLOAD_SIZE - len(payload))
        assert len(payload) == self.PAYLOAD_SIZE
        data = bytearray( (self.MSG_MAGIC, self.msgId, 0, ) ) + payload
        data.append(crc8(data))
        assert len(data) == self.SIZE
        return data

class SimplePWMMsg_Nop(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_NOP

    def __init__(self):
        super().__init__(self.MSGID)

    def __str__(self):
        return f"NOP"

class SimplePWMMsg_Ack(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_ACK

    def __init__(self):
        super().__init__(self.MSGID)

    def __str__(self):
        return f"ACK"

class SimplePWMMsg_Nack(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_NACK

    def __init__(self):
        super().__init__(self.MSGID)

    def __str__(self):
        return f"NACK"

class SimplePWMMsg_Ping(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_PING

    def __init__(self):
        super().__init__(self.MSGID)

    def __str__(self):
        return f"PING"

class SimplePWMMsg_Pong(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_PONG

    def __init__(self):
        super().__init__(self.MSGID)

    def __str__(self):
        return f"PONG"

class SimplePWMMsg_GetControl(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_GET_CONTROL

    def __init__(self):
        super().__init__(self.MSGID)

    def __str__(self):
        return f"GET_CONTROL"

class SimplePWMMsg_Control(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_CONTROL

    MSG_CTLFLG_ANADIS    = 0x01
    MSG_CTLFLG_EEPDIS    = 0x02

    @classmethod
    def _parse(cls, payload):
        flags = payload[0]
        return cls(flags)

    def __init__(self, flags):
        super().__init__(self.MSGID)
        self.flags = flags

    def getData(self):
        payload = bytearray( (self.flags, ) )
        return super().getData(payload)

    def __str__(self):
        return f"CONTROL(flags=0x{self.flags:X})"

class SimplePWMMsg_GetSetpoints(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_GET_SETPOINTS

    MSG_GETSPFLG_HSL    = 0x01

    @classmethod
    def _parse(cls, payload):
        flags = payload[0]
        return cls(flags)

    def __init__(self, flags):
        super().__init__(self.MSGID)
        self.flags = flags

    def getData(self):
        payload = bytearray( (self.flags, ) )
        return super().getData(payload)

    def __str__(self):
        return f"GET_SETPOINTS(flags=0x{self.flags:X})"

class SimplePWMMsg_Setpoints(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_SETPOINTS

    MSG_SPFLG_HSL        = 0x01

    @classmethod
    def _parse(cls, payload):
        flags = payload[0]
        nrSp = payload[1]
        if nrSp > 3:
            printError("SimplePWMMsg_Setpoints: Received invalid nr_sp.")
            return None
        setpoints = [ cls._fromLe16(payload[2 + i*2 : 2 + i*2 + 2])
                  for i in range(nrSp) ]
        return cls(flags, setpoints)

    def __init__(self, flags, setpoints):
        super().__init__(self.MSGID)
        self.flags = flags
        self.setpoints = setpoints

    def getData(self):
        payload = bytearray( (self.flags, len(self.setpoints), ) )
        for sp in self.setpoints:
            payload += self._toLe16(sp)
        return super().getData(payload)

    def __str__(self):
        return f"SETPOINTS(flags=0x{self.flags:X}, setpoints={self.setpoints})"

class SimplePWMMsg_GetPwmcorr(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_GET_PWMCORR

    @classmethod
    def _parse(cls, payload):
        index = payload[0]
        return cls(index)

    def __init__(self, index):
        super().__init__(self.MSGID)
        self.index = index

    def getData(self):
        payload = bytearray( (self.index, ) )
        return super().getData(payload)

    def __str__(self):
        return f"GET_PWMCORR(index={self.index})"

class SimplePWMMsg_Pwmcorr(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_PWMCORR

    @classmethod
    def _parse(cls, payload):
        index = payload[0]
        numerator = cls._fromLe16(payload[1 : 1 + 2])
        denominator = cls._fromLe16(payload[3 : 3 + 2])
        if numerator == denominator:
            numerator = denominator = 1
        try:
            fraction = Fraction(numerator, denominator)
        except (ValueError, ZeroDivisionError) as e:
            printError("SimplePWMMsg_Pwmcorr: Received invalid fraction.")
            return None
        return cls(index, fraction)

    def __init__(self, index, fraction):
        super().__init__(self.MSGID)
        self.index = index
        self.fraction = fraction

    def getData(self):
        payload = bytearray( (self.index,) )
        numerator, denominator = self.fraction.numerator, self.fraction.denominator
        if numerator == denominator:
            numerator = denominator = 0
        payload += self._toLe16(numerator)
        payload += self._toLe16(denominator)
        return super().getData(payload)

    def __str__(self):
        return f"PWMCORR(index={self.index}, fraction={self.fraction})"

class SimplePWMMsg_GetBatvolt(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_GET_BATVOLT

    def __init__(self):
        super().__init__(self.MSGID)

    def __str__(self):
        return f"GET_BATVOLT"

class SimplePWMMsg_Batvolt(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_BATVOLT

    @classmethod
    def _parse(cls, payload):
        meas = cls._fromLe16(payload[0:2])
        drop = cls._fromLe16(payload[2:4])
        return cls(meas, drop)

    def __init__(self, meas, drop):
        super().__init__(self.MSGID)
        self.meas = meas
        self.drop = drop

    def getData(self):
        payload = bytearray()
        payload += self._toLe16(self.meas)
        payload += self._toLe16(self.drop)
        return super().getData(payload)

    def __str__(self):
        return f"BATVOLT(meas={self.meas}, drop={self.drop})"

class SimplePWMMsg_Enterboot(SimplePWMMsg):
    MSGID = SimplePWMMsg.MSGID_ENTERBOOT

    MSG_BOOTMAGIC = 0xB007

    @classmethod
    def _parse(cls, payload):
        if cls._fromLe16(payload[0:2]) != cls.MSG_BOOTMAGIC:
            printError("SimplePWMMsg_Enterboot: Received invalid magic.")
            return None
        return cls()

    def __init__(self):
        super().__init__(self.MSGID)

    def getData(self):
        payload = bytearray()
        payload += self._toLe16(self.MSG_BOOTMAGIC)
        return super().getData(payload)

    def __str__(self):
        return f"ENTERBOOT"

# vim: ts=4 sw=4 expandtab
bues.ch cgit interface