X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fir_rc5%2Fpd.py;h=e18a90bfae273be8de9d5cba7308734d26397856;hp=cd94eae1159a59d816bb940622017e023e5d3d23;hb=6cbba91f23b9f9ace75b4722c9c0776b9211008d;hpb=16211e24f7a34590f02e9f8a327b1eb3647c7c62 diff --git a/decoders/ir_rc5/pd.py b/decoders/ir_rc5/pd.py index cd94eae..e18a90b 100644 --- a/decoders/ir_rc5/pd.py +++ b/decoders/ir_rc5/pd.py @@ -14,48 +14,55 @@ ## 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 St, Fifth Floor, Boston, MA 02110-1301 USA +## along with this program; if not, see . ## import sigrokdecode as srd from .lists import * +class SamplerateError(Exception): + pass + class Decoder(srd.Decoder): - api_version = 1 + api_version = 3 id = 'ir_rc5' name = 'IR RC-5' longname = 'IR RC-5' desc = 'RC-5 infrared remote control protocol.' license = 'gplv2+' inputs = ['logic'] - outputs = ['ir_rc5'] - probes = [ + outputs = [] + tags = ['IR'] + channels = ( {'id': 'ir', 'name': 'IR', 'desc': 'IR data line'}, - ] - optional_probes = [] - options = { - 'polarity': ['Polarity', 'active-low'], - 'protocol': ['Protocol type', 'standard'], - } - annotations = [ - ['bit', 'Bit'], - ['startbit1', 'Startbit 1'], - ['startbit2', 'Startbit 2'], - ['togglebit-0', 'Toggle bit 0'], - ['togglebit-1', 'Toggle bit 1'], - ['address', 'Address'], - ['command', 'Command'], - ] + ) + options = ( + {'id': 'polarity', 'desc': 'Polarity', 'default': 'active-low', + 'values': ('active-low', 'active-high')}, + {'id': 'protocol', 'desc': 'Protocol type', 'default': 'standard', + 'values': ('standard', 'extended')}, + ) + annotations = ( + ('bit', 'Bit'), + ('startbit1', 'Startbit 1'), + ('startbit2', 'Startbit 2'), + ('togglebit-0', 'Toggle bit 0'), + ('togglebit-1', 'Toggle bit 1'), + ('address', 'Address'), + ('command', 'Command'), + ) annotation_rows = ( ('bits', 'Bits', (0,)), ('fields', 'Fields', (1, 2, 3, 4, 5, 6)), ) - def __init__(self, **kwargs): + def __init__(self): + self.reset() + + def reset(self): self.samplerate = None self.samplenum = None - self.edges, self.bits, self.bits_ss_es = [], [], [] + self.edges, self.bits, self.ss_es_bits = [], [], [] self.state = 'IDLE' def start(self): @@ -69,7 +76,7 @@ class Decoder(srd.Decoder): self.halfbit = int((self.samplerate * 0.00178) / 2.0) def putb(self, bit1, bit2, data): - ss, es = self.bits_ss_es[bit1][0], self.bits_ss_es[bit2][1] + ss, es = self.ss_es_bits[bit1][0], self.ss_es_bits[bit2][1] self.put(ss, es, self.out_ann, data) def handle_bits(self): @@ -79,9 +86,9 @@ class Decoder(srd.Decoder): if i == 0: ss = max(0, self.bits[0][0] - self.halfbit) else: - ss = self.bits_ss_es[i - 1][1] + ss = self.ss_es_bits[i - 1][1] es = self.bits[i][0] + self.halfbit - self.bits_ss_es.append([ss, es]) + self.ss_es_bits.append([ss, es]) self.putb(i, i, [0, ['%d' % self.bits[i][1]]]) # Bits[0:0]: Startbit 1 s = ['Startbit1: %d' % b[0][1], 'SB1: %d' % b[0][1], 'SB1', 'S1', 'S'] @@ -128,15 +135,15 @@ class Decoder(srd.Decoder): return 'e' # Error, invalid edge distance. def reset_decoder_state(self): - self.edges, self.bits, self.bits_ss_es = [], [], [] + self.edges, self.bits, self.ss_es_bits = [], [], [] self.state = 'IDLE' - def decode(self, ss, es, data): - if self.samplerate is None: - raise Exception("Cannot decode without samplerate.") - for (self.samplenum, pins) in data: + def decode(self): + if not self.samplerate: + raise SamplerateError('Cannot decode without samplerate.') + while True: - self.ir = pins[0] + (self.ir,) = self.wait() # Wait for any edge (rising or falling). if self.old_ir == self.ir: @@ -144,8 +151,9 @@ class Decoder(srd.Decoder): # State machine. if self.state == 'IDLE': + bit = 1 self.edges.append(self.samplenum) - self.bits.append([self.samplenum, 1]) + self.bits.append([self.samplenum, bit]) self.state = 'MID1' self.old_ir = self.ir continue @@ -167,16 +175,13 @@ class Decoder(srd.Decoder): if edge == 's': self.state = 'MID0' bit = 0 if edge == 's' else None - else: - raise Exception('Invalid state: %s' % self.state) self.edges.append(self.samplenum) - if bit != None: + if bit is not None: self.bits.append([self.samplenum, bit]) - if len(self.bits) == 14 + 1: + if len(self.bits) == 14: self.handle_bits() self.reset_decoder_state() self.old_ir = self.ir -