mirror of
https://github.com/myhdl/myhdl.git
synced 2024-12-14 07:44:38 +08:00
37 lines
729 B
Python
37 lines
729 B
Python
|
import operator
|
||
|
|
||
|
sec = 1e9
|
||
|
|
||
|
ODD, EVEN, MARK, SPACE = range(4)
|
||
|
|
||
|
class Error(Exception):
|
||
|
pass
|
||
|
|
||
|
class ParityError(Error):
|
||
|
pass
|
||
|
|
||
|
class StopBitError(Error):
|
||
|
pass
|
||
|
|
||
|
class Config(object):
|
||
|
|
||
|
__slots__ = ("baud_rate", "n_bits", "n_stops", "parity")
|
||
|
|
||
|
def __init__(self, baud_rate=9600, n_bits=8, n_stops=1, parity=None):
|
||
|
self.baud_rate = baud_rate
|
||
|
self.n_bits = n_bits
|
||
|
self.n_stops = n_stops
|
||
|
self.parity = parity
|
||
|
|
||
|
|
||
|
def parity(data, cfg):
|
||
|
if cfg == ODD:
|
||
|
return not reduce(operator.xor, [b for b in data[8:]])
|
||
|
elif cfg== EVEN:
|
||
|
return reduce(operator.xor, [b for b in data[8:]])
|
||
|
elif cfg == MARK:
|
||
|
return 1
|
||
|
elif cfg == SPACE:
|
||
|
return 0
|
||
|
|