def test_rc6_decode(self):
        self.test_name = 'RC-6 IR protocol'
        self.trial_count = 20

        carrier_freq = 36.0e3
        sample_rate = 10.0 * carrier_freq
        rise_time = sigp.min_rise_time(sample_rate) * 1.01

        for i in xrange(self.trial_count):
            self.update_progress(i + 1)

            msg_count = randint(1, 10)

            msgs = []
            for i in xrange(msg_count):
                do_mode6 = choice([True, False])
                if do_mode6:
                    msg = rc6.RC6Message(addr=randint(0,255), cmd=randint(0,255), toggle=randint(0,1), \
                        mode=6, customer=randint(0,2**15-1))
                else:
                    msg = rc6.RC6Message(addr=randint(0, 255),
                                         cmd=randint(0, 255),
                                         toggle=randint(0, 1),
                                         mode=randint(0, 5))
                msgs.append(msg)

            do_modulation = choice((True, False))

            edges = rc6.rc6_synth(msgs, message_interval=5.0e-3)
            if do_modulation:
                duty = uniform(0.1, 0.9)
                edges = ir.modulate(edges, carrier_freq, duty_cycle=duty)

            samples = sigp.synth_wave(edges, sample_rate, rise_time)
            #samples = sigp.dropout(samples, 2.6e-4, 3.8e-4)
            #samples = sigp.capacify(samples, 4.0e-7, 50.0)
            noisy = sigp.amplify(sigp.noisify(samples, snr_db=30), gain=5.0)
            waveform = list(noisy)

            #wave_samples = stream.sample_stream_to_samples(waveform)
            #plt.plot(wave_samples)
            #plt.show()

            records = list(rc6.rc6_decode(iter(waveform)))
            #print('\nDecoded {} records'.format(len(records)))

            #for m in msgs:
            #    print(m)

            #for r in records:
            #    for s in r.summary():
            #        print(s)

            self.assertEqual(len(msgs), len(records),
                             'Mismatch in decoded record count')

            for rec, msg in zip(records, msgs):
                self.assertEqual(rec.data, msg, 'Mismatched messages')
Пример #2
0
    def test_rc6_decode(self):
        self.test_name = 'RC-6 IR protocol'
        self.trial_count = 20

        carrier_freq = 36.0e3
        sample_rate = 10.0 * carrier_freq
        rise_time = sigp.min_rise_time(sample_rate) * 1.01

        for i in xrange(self.trial_count):
            self.update_progress(i+1)

            msg_count = randint(1,10)

            msgs = []
            for i in xrange(msg_count):
                do_mode6 = choice([True, False])
                if do_mode6:
                    msg = rc6.RC6Message(addr=randint(0,255), cmd=randint(0,255), toggle=randint(0,1), \
                        mode=6, customer=randint(0,2**15-1))
                else:
                    msg = rc6.RC6Message(addr=randint(0,255), cmd=randint(0,255), toggle=randint(0,1), mode=randint(0,5))
                msgs.append(msg)

            do_modulation = choice((True, False))

            edges = rc6.rc6_synth(msgs, message_interval=5.0e-3)
            if do_modulation:
                duty = uniform(0.1, 0.9)
                edges = ir.modulate(edges, carrier_freq, duty_cycle=duty)

            samples = sigp.synth_wave(edges, sample_rate, rise_time)
            #samples = sigp.dropout(samples, 2.6e-4, 3.8e-4)
            #samples = sigp.capacify(samples, 4.0e-7, 50.0)
            noisy = sigp.amplify(sigp.noisify(samples, snr_db=30), gain=5.0)
            waveform = list(noisy)

            #wave_samples = stream.sample_stream_to_samples(waveform)
            #plt.plot(wave_samples)
            #plt.show()

            records = list(rc6.rc6_decode(iter(waveform)))
            #print('\nDecoded {} records'.format(len(records)))

            #for m in msgs:
            #    print(m)

            #for r in records:
            #    for s in r.summary():
            #        print(s)

            self.assertEqual(len(msgs), len(records), 'Mismatch in decoded record count')

            for rec, msg in zip(records, msgs):
                self.assertEqual(rec.data, msg, 'Mismatched messages')
Пример #3
0
    def test_uart_decode(self):
        self.test_name = 'UART message'
        self.trial_count = 10
        for i in xrange(self.trial_count):
            self.update_progress(i + 1)

            msg = []
            for _ in xrange(20):
                msg.append(chr(random.choice(xrange(ord('0'), ord('z')))))

            msg = ''.join(msg)

            baud = random.choice((110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, \
            56000, 57600, 115200, 128000, 153600, 230400, 256000, 460800, 921600))

            sample_rate = baud * 100.0
            rise_time = sigp.min_rise_time(
                sample_rate) * 10.0  # 10x min rise time
            parity = random.choice((None, 'even', 'odd'))

            bits = random.choice((7, 8, 9))

            #print('\nTRIAL {}: msg="{}", baud={}, parity={}, bits={}'.format(i, msg, baud, parity, bits))

            edges = uart.uart_synth(bytearray(msg.encode('utf-8')),
                                    bits,
                                    baud,
                                    parity=parity,
                                    idle_start=100.0 / sample_rate)

            samples = sigp.synth_wave(edges,
                                      sample_rate,
                                      rise_time,
                                      ripple_db=60)

            noisy = sigp.amplify(sigp.noisify(samples, snr_db=20), gain=-15.0)
            waveform = list(noisy)
            frames = uart.uart_decode(iter(waveform),
                                      bits=bits,
                                      polarity=uart.UARTConfig.IdleLow,
                                      parity=parity,
                                      baud_rate=None)
            frames = list(frames)
            #print(''.join(str(d) for d in frames))
            decoded_msg = ''.join(str(d) for d in frames)

            self.assertEqual(msg, decoded_msg, \
                "Message not decoded successfully msg:'{0}', baud:{1}, parity:{2}, bits:{3}".format(msg, \
                baud, parity, bits))
Пример #4
0
    def test_sirc_decode(self):
        self.test_name = 'SIRC IR protocol'
        self.trial_count = 20

        carrier_freq = 40.0e3
        sample_rate = 10.0 * carrier_freq
        rise_time = sigp.min_rise_time(sample_rate) * 1.01

        for i in xrange(self.trial_count):
            self.update_progress(i+1)

            msg_count = randint(1,10)

            msgs = []
            for i in xrange(msg_count):
                if choice((True, False)):
                    msg = sirc.SIRCMessage(cmd=randint(0, 127), device=randint(0, 255))
                else:
                    msg = sirc.SIRCMessage(cmd=randint(0, 127), device=randint(0, 31), \
                        extended=randint(0,255))
                msgs.append(msg)

            do_modulation = choice((True, False))

            edges = sirc.sirc_synth(msgs)
            if do_modulation:
                duty = uniform(0.1, 0.9)
                edges = ir.modulate(edges, carrier_freq, duty_cycle=duty)

            samples = sigp.synth_wave(edges, sample_rate, rise_time)
            #samples = sigp.dropout(samples, 2.6e-4, 3.8e-4)
            noisy = sigp.amplify(sigp.noisify(samples, snr_db=30), gain=5.0)
            waveform = list(noisy)

            #wave_samples = stream.sample_stream_to_samples(waveform)
            #plt.plot(wave_samples)
            #plt.show()

            records = list(sirc.sirc_decode(iter(waveform)))
            #print('\nDecoded {} records'.format(len(records)))

            self.assertEqual(len(msgs), len(records), 'Mismatch in decoded record count')

            for rec, msg in zip(records, msgs):
                self.assertEqual(rec.data, msg, 'Mismatched messages')
    def test_nec_decode(self):
        self.test_name = 'NEC IR protocol'
        self.trial_count = 20

        carrier_freq = 38.0e3

        for i in xrange(self.trial_count):
            self.update_progress(i+1)

            msg_count = randint(1,10)

            msgs = []
            for i in xrange(msg_count):
                msg = nec.NECMessage(cmd=randint(0,255), addr_low=randint(0,255), \
                    addr_high=choice((None, randint(0,255))) )
                msgs.append(msg)

            sample_rate = uniform(8.0, 15.0) * carrier_freq
            rise_time = sigp.min_rise_time(sample_rate) * 1.01

            do_modulation = choice((True, False))

            edges = nec.nec_synth(msgs)

            if do_modulation:
                duty = uniform(0.2, 0.8)
                edges = ir.modulate(edges, carrier_freq, duty_cycle=duty)

            samples = sigp.synth_wave(edges, sample_rate, rise_time)
            #samples = sigp.dropout(samples, 2.6e-4, 3.8e-4)
            noisy = sigp.quantize(sigp.amplify(sigp.noisify(samples, snr_db=20), gain=5.0), 70.0)
            waveform = list(noisy)

            #wave_samples = stream.sample_stream_to_samples(waveform)
            #plt.plot(wave_samples)
            #plt.show()

            records = list(nec.nec_decode(iter(waveform)))
            #print('\nDecoded {} records'.format(len(records)))

            self.assertEqual(len(msgs), len(records), 'Mismatch in decoded record count')

            for rec, msg in zip(records, msgs):
                self.assertEqual(rec.data, msg, 'Mismatched messages')
Пример #6
0
 def test_uart_decode(self):
     self.test_name = 'UART message'
     self.trial_count = 10
     for i in xrange(self.trial_count):
         self.update_progress(i+1)
         
         msg = []
         for _ in xrange(20):
             msg.append(chr(random.choice(xrange(ord('0'), ord('z')) )))
             
         msg = ''.join(msg)
         
         baud = random.choice((110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, \
         56000, 57600, 115200, 128000, 153600, 230400, 256000, 460800, 921600))
         
         sample_rate = baud * 100.0
         rise_time = sigp.min_rise_time(sample_rate) * 10.0 # 10x min rise time
         parity = random.choice((None, 'even', 'odd'))
         
         bits = random.choice((7, 8, 9))
         
         #print('\nTRIAL {}: msg="{}", baud={}, parity={}, bits={}'.format(i, msg, baud, parity, bits))
         
         edges = uart.uart_synth(bytearray(msg.encode('utf-8')), bits, baud, parity=parity, idle_start=100.0 / sample_rate)
         
         samples = sigp.synth_wave(edges, sample_rate, rise_time, ripple_db=60)
         
         noisy = sigp.amplify(sigp.noisify(samples, snr_db=20), gain=-15.0)
         waveform = list(noisy)
         frames = uart.uart_decode(iter(waveform), bits=bits, polarity=uart.UARTConfig.IdleLow, parity=parity, baud_rate=None)
         frames = list(frames)
         #print(''.join(str(d) for d in frames))
         decoded_msg = ''.join(str(d) for d in frames)
         
         self.assertEqual(msg, decoded_msg, \
             "Message not decoded successfully msg:'{0}', baud:{1}, parity:{2}, bits:{3}".format(msg, \
             baud, parity, bits))
Пример #7
0
    def test_kline_decode(self):
        self.test_name = 'K-line message'
        self.trial_count = 10
        for i in xrange(self.trial_count):
            self.update_progress(i + 1)

            msg_count = random.randint(1, 6)
            messages = []

            for i in xrange(msg_count):
                data_bytes = random.randint(0, 6)

                protocol = random.choice((kline.KLineProtocol.ISO9141,
                                          kline.KLineProtocol.ISO14230))
                if protocol == kline.KLineProtocol.ISO9141:
                    # ISO9141 request
                    msg = [0x68, 0x6A, 0xF1]
                else:
                    header_size = random.choice((3, 4))
                    if header_size == 3:
                        msg = [0x80 + data_bytes + 1, 0xD1, 0xF1]
                    else:
                        msg = [0x80, data_bytes + 1, 0xD1, 0xF1]

                sid = random.randint(0, 0x3F)
                msg.append(sid)

                msg.extend(
                    [random.randint(0, 0xFF) for b in xrange(data_bytes)])
                cs = sum(msg) % 256
                msg.append(cs)
                messages.append(msg)

                data_bytes = random.randint(0, 6)
                if protocol == kline.KLineProtocol.ISO9141:
                    # ISO9141 response
                    msg = [0x48, 0x6B, 0xD1]
                else:
                    if header_size == 3:
                        msg = [0x80 + data_bytes + 1, 0xF1, 0xD1]
                    else:
                        msg = [0x80, data_bytes + 1, 0xF1, 0xD1]

                msg.append(sid + 0x40)
                msg.extend(
                    [random.randint(0, 0xFF) for b in xrange(data_bytes)])
                cs = sum(msg) % 256
                msg.append(cs)
                messages.append(msg)

            baud = 10400

            sample_rate = baud * 100.0
            rise_time = sigp.min_rise_time(
                sample_rate) * 10.0  # 10x min rise time

            edges = kline.iso_k_line_synth(messages,
                                           idle_start=8.0 / baud,
                                           idle_end=8.0 / baud)

            samples = sigp.synth_wave(edges,
                                      sample_rate,
                                      rise_time,
                                      ripple_db=60)

            noisy = sigp.amplify(sigp.noisify(samples, snr_db=20), gain=12.0)
            waveform = list(noisy)

            records_it = kline.iso_k_line_decode(iter(waveform))
            records = list(records_it)

            raw_decode = []
            for r in records:
                msg = r.msg.header.bytes() + r.msg.data + [r.msg.checksum]
                raw_decode.append([b.data for b in msg])

            msg_match = True
            for msg, omsg in zip(raw_decode, messages):
                if msg != omsg:
                    msg_match = False
                    break

            self.assertTrue(msg_match, "Messages not decoded successfully")
Пример #8
0
    def test_kline_decode(self):
        self.test_name = 'K-line message'
        self.trial_count = 10
        for i in xrange(self.trial_count):
            self.update_progress(i+1)
            
            msg_count = random.randint(1, 6)
            messages = []

            for i in xrange(msg_count):
                data_bytes = random.randint(0,6)

                protocol = random.choice((kline.KLineProtocol.ISO9141, kline.KLineProtocol.ISO14230))
                if protocol == kline.KLineProtocol.ISO9141:
                    # ISO9141 request
                    msg = [0x68, 0x6A, 0xF1]
                else:
                    header_size = random.choice((3, 4))
                    if header_size == 3:
                        msg = [0x80 + data_bytes+1, 0xD1, 0xF1]
                    else:
                        msg = [0x80, data_bytes+1, 0xD1, 0xF1]

                sid = random.randint(0, 0x3F)
                msg.append(sid)
                
                msg.extend([random.randint(0,0xFF) for b in xrange(data_bytes)])
                cs = sum(msg) % 256
                msg.append(cs)
                messages.append(msg)

                data_bytes = random.randint(0,6)
                if protocol == kline.KLineProtocol.ISO9141:
                    # ISO9141 response
                    msg = [0x48, 0x6B, 0xD1]
                else:
                    if header_size == 3:
                        msg = [0x80 + data_bytes+1, 0xF1, 0xD1]
                    else:
                        msg = [0x80, data_bytes+1, 0xF1, 0xD1]

                msg.append(sid + 0x40)
                msg.extend([random.randint(0,0xFF) for b in xrange(data_bytes)])
                cs = sum(msg) % 256
                msg.append(cs)
                messages.append(msg)


            baud = 10400
            
            sample_rate = baud * 100.0
            rise_time = sigp.min_rise_time(sample_rate) * 10.0 # 10x min rise time
            
            edges = kline.iso_k_line_synth(messages, idle_start=8.0 / baud, idle_end=8.0 / baud)
            
            samples = sigp.synth_wave(edges, sample_rate, rise_time, ripple_db=60)
            
            noisy = sigp.amplify(sigp.noisify(samples, snr_db=20), gain=12.0)
            waveform = list(noisy)

            records_it = kline.iso_k_line_decode(iter(waveform))
            records = list(records_it)

            raw_decode = []
            for r in records:
                msg = r.msg.header.bytes() + r.msg.data + [r.msg.checksum]
                raw_decode.append([b.data for b in msg])

            msg_match = True
            for msg, omsg in zip(raw_decode, messages):
                if msg != omsg:
                    msg_match = False
                    break

            
            self.assertTrue(msg_match, "Messages not decoded successfully")