encode dict

This commit is contained in:
S. Seegel 2020-11-19 09:12:59 +01:00
parent 4df197e4c3
commit 6687cc1f1b
2 changed files with 59 additions and 10 deletions

View file

@ -127,7 +127,7 @@ class IT32(RcProtocol): #switch1
return self._ookdata return self._ookdata
def encode(self, args): def encode(self, args):
if args.code: if hasattr(args, "code") and args.code:
if re.match("^[01]{32}$", args.code): if re.match("^[01]{32}$", args.code):
return self.__encode(args.code) return self.__encode(args.code)
@ -193,13 +193,13 @@ class ITTristate(RcProtocol): #old intertechno systems
return code return code
def encode(self, args): def encode(self, args):
if args.code: if hasattr(args, 'code') and args.code:
if re.match("^[01Ff]{12}$", args.code): if re.match("^[01Ff]{12}$", args.code):
return self.__encode(args.code) return self.__encode(args.code)
code = "" code = ""
code += self.__encode_int(ord(args.house[0]) - ord('A'), 4) code += self.__encode_int(ord(args.house[0]) - ord('A'), 4)
if args.group: if hasattr(args, 'group') and args.group:
code += self.__encode_int(args.group - 1, 2) code += self.__encode_int(args.group - 1, 2)
code += self.__encode_int(args.unit - 1, 2) code += self.__encode_int(args.unit - 1, 2)
else: else:
@ -403,6 +403,7 @@ class PDM32(RcProtocol):
'0': [2, 1], '0': [2, 1],
} }
RcProtocol.__init__(self) RcProtocol.__init__(self)
self._parser.add_argument("-c", "--code")
def decode(self, pulsetrain): def decode(self, pulsetrain):
code, tb = self._decode_symbols(pulsetrain[0:-2]) code, tb = self._decode_symbols(pulsetrain[0:-2])
@ -413,6 +414,14 @@ class PDM32(RcProtocol):
"timebase": tb, "timebase": tb,
} }
def encode(self, args):
self._reset()
self._add_symbols(args.code)
self._add_pulses([1, 11])
self._add_finish()
return self._ookdata
protocols = [ protocols = [
IT32(), IT32(),
Switch15(), Switch15(),
@ -426,9 +435,20 @@ protocols = [
def encode(protocol, args): def encode(protocol, args):
for p in protocols: for p in protocols:
if (protocol): if (protocol):
print("found ptoto")
if p._name == protocol: if p._name == protocol:
return (p.encode(p._parser.parse_args(args)), p._timebase, p._repetitions) return (p.encode(p._parser.parse_args(args)), p._timebase, p._repetitions)
class RCStruct:
def __init__(self, **entries):
self.__dict__.update(entries)
def encode_dict(dict):
s = RCStruct(**dict)
for p in protocols:
if p._name == s.protocol:
return (p.encode(s), p._timebase, p._repetitions)
def decode(pulsetrain): def decode(pulsetrain):
dec = None dec = None
res = [] res = []

View file

@ -3,10 +3,10 @@
from raspyrfm import * from raspyrfm import *
import sys import sys
import time import time
import it32 #import it32
import tristate #import tristate
import bistate24 #import bistate24
import fs20 #import fs20
from argparse import ArgumentParser from argparse import ArgumentParser
import wave, struct import wave, struct
import threading import threading
@ -61,6 +61,8 @@ trainbuf = []
class RxThread(threading.Thread): class RxThread(threading.Thread):
def __init__(self): def __init__(self):
self.__event = threading.Event()
self.__event.set()
threading.Thread.__init__(self) threading.Thread.__init__(self)
def __rxcb(self, rfm): def __rxcb(self, rfm):
@ -75,7 +77,7 @@ class RxThread(threading.Thread):
else: else:
wf = None wf = None
while True: while self.__event.isSet():
fifo = rfm.read_fifo_wait(64) fifo = rfm.read_fifo_wait(64)
ba = bytearray() ba = bytearray()
@ -109,11 +111,25 @@ class RxThread(threading.Thread):
wf.writeframes('') wf.writeframes('')
def run(self): def run(self):
self.__event.wait()
rfm.set_params(
Datarate = 20.0 #kbit/s
)
rfm.start_receive(self.__rxcb) rfm.start_receive(self.__rxcb)
def suspend(self):
self.__event.clear()
def resume(self):
self.__event.set()
rxthread = RxThread() rxthread = RxThread()
rxthread.daemon = True rxthread.daemon = True
rxthread.start() rxthread.start()
lasttime = 0
class Args:
pass
while True: while True:
time.sleep(0.1) time.sleep(0.1)
@ -125,5 +141,18 @@ while True:
dec = rcprotocols.decode(train) dec = rcprotocols.decode(train)
if (dec): if (dec):
print(dec) print(dec)
else: #else:
print(train) print(train)
if time.time() > lasttime:
lasttime = time.time() + 5
print("sdfkl")
args = Args()
#rxthread.suspend()
args.code = "01010011110011111110000111111111"
#rfm.set_params(
# Datarate = 1000.0 / 600
#)
#txdata = rcprotocols.encode("PDM32", args)
#print(txdata)
#rxthread.resume()