From 7aeaed651f1c2e2ee1dd8923f2ffd22e2bd5e34c Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Fri, 5 Aug 2022 16:51:05 -0700 Subject: [PATCH] add code that does binary to three level + clock recovery... This is sample code, but works well.. it ensures that every bit time there is a level change, so any duplicates can be dropped.. this works so far, but I'm sure is not the most effecient way to do this... --- blinkled/genseq.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/blinkled/genseq.py b/blinkled/genseq.py index 0295bdc..40266c3 100644 --- a/blinkled/genseq.py +++ b/blinkled/genseq.py @@ -1,3 +1,81 @@ from encdec8b10b import EncDec8B10B +a = b'Hello, this is a test of encoding.' + +coder = EncDec8B10B() + +encoded = coder.encode(EncDec8B10B.COMMA) + coder.encode(a) +print('8b10b encoded:', repr(encoded)) + +decoder = EncDec8B10B() + +print(repr(decoder.decode(encoded))) +print('test decode:', repr(decoder.decode(''))) + +def jm3coder(bits, lastlvl=0): + '''Takes in a string of bits, and outputs a trinary level. + It is guaranteed that no two outputs repeat allowing for + easy clock recovery as long as the sampling rate is faster + than output rate. (e.g. 30Hz (33.3ms) sampling of a 29Hz + (34.5ms) signal. + + Note that this does not do sync, so something like an 8b10b + encoder should be used on top of this, and then transmit three + dummy bits at the begining as the decoder. The three will + cycle through all three states help ensuring level detection + + An optional paramter of lastlvl can be provided, which is + the previous signal level transmitted.''' + + r = [] + for i in bits: + v = int(i, 2) + lastlvl = (lastlvl + 1 + v) % 3 + r.append('%d' % lastlvl) + + return ''.join(r) + +encoded = jm3coder('000' + encoded) + +print('jm3coded:', repr(encoded)) + +# make a "stretched" bit string +stretched = ''.join(encoded[i:i + 50] + encoded[i:i + 50][-1] for i in range(0, len(encoded), 50)) +print('stretched:', repr(stretched)) + +def jm3decoder(trits, lastlvl=0): + '''Decodes a string encoded w/ jm3coder. + + lastlvl should/must be provided which is the last rx'd level + (it must be 0, 1 or 2). + ''' + + lookup = { + (0, 1): 0, + (0, 2): 1, + (1, 2): 0, + (1, 0): 1, + (2, 0): 0, + (2, 1): 1, + } + r = [] + for i in trits: + lvl = int(i, 3) + if lvl == lastlvl: + continue + + r.append('%d' % lookup[(lastlvl, lvl)]) + lastlvl = lvl + + return ''.join(r) + +decoder = EncDec8B10B() + +stretched = jm3decoder(stretched) + +print(repr(stretched)) + +print(repr(decoder.decode(stretched))) +print(repr(decoder.decode(''))) + print('done')