Esempio n. 1
0
    def test_misc(self):
        ba = BitSequence(12, msb=True, length=16)
        bb = BitSequence(12, msb=True, length=14)
        l = [ba, bb]
        l.sort()
        self.assertEqual(str(l), "[00110000000000, 0011000000000000]")
        self.assertEqual(str(ba.tobytes()), "[48, 0]")
        self.assertEqual(str(ba.tobytes(True)), "[0, 12]")
        self.assertEqual(str(bb.tobytes(True)), "[0, 12]")

        b = BitSequence(length=254)
        b[0:4] = '1111'
        self.assertEqual(str(b), '254: 000000 00000000 00000000 00000000 ' \
         '00000000 00000000 00000000 00000000 00000000 00000000 00000000 ' \
         '00000000 00000000 00000000 00000000 00000000 00000000 00000000 ' \
         '00000000 00000000 00000000 00000000 00000000 00000000 00000000 ' \
         '00000000 00000000 00000000 00000000 00000000 00000000 00001111')
        self.assertEqual(str(b.tobytes()),
            '[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ' \
            '0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15]')

        b = BitSequence(bytes_=[0xa0, '\x0f', 0x77], msb=False, msby=False)
        self.assertEqual(str(['%02x' % x for x in b.tobytes(False)]),
                         "['a0', '0f', '77']")
        b = BitSequence(bytes_=[0xa0, '\x0f', 0x77], msb=True, msby=True)
        self.assertEqual(str(['%02x' % x for x in b.tobytes(True)]),
                         "['a0', '0f', '77']")
        b = BitSequence(length=3)
        b[6] = '1'
        self.assertEqual(str(b), '7: 1000000')
Esempio n. 2
0
    def test_misc(self):
        ba = BitSequence(12, msb=True, length=16)
        bb = BitSequence(12, msb=True, length=14)
        l = [ba, bb]
        l.sort(key=int)
        self.assertEqual(str(l), "[00110000000000, 0011000000000000]")
        self.assertEqual(str(ba.tobytes()), "[48, 0]")
        self.assertEqual(str(ba.tobytes(True)), "[0, 12]")
        self.assertEqual(str(bb.tobytes(True)), "[0, 12]")

        b = BitSequence(length=254)
        b[0:4] = '1111'
        self.assertEqual(str(b), '254: 000000 00000000 00000000 00000000 '
         '00000000 00000000 00000000 00000000 00000000 00000000 00000000 '
         '00000000 00000000 00000000 00000000 00000000 00000000 00000000 '
         '00000000 00000000 00000000 00000000 00000000 00000000 00000000 '
         '00000000 00000000 00000000 00000000 00000000 00000000 00001111')
        self.assertEqual(str(b.tobytes()),
            '[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '
            '0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15]')

        b = BitSequence(bytes_=[0xa0, '\x0f', 0x77], msb=False, msby=False)
        self.assertEqual(str(['%02x' % x for x in b.tobytes(False)]),
                         "['a0', '0f', '77']")
        b = BitSequence(bytes_=[0xa0, '\x0f', 0x77], msb=True, msby=True)
        self.assertEqual(str(['%02x' % x for x in b.tobytes(True)]),
                         "['a0', '0f', '77']")
        b = BitSequence(length=7)
        b[6] = '1'
        self.assertEqual(str(b), '7: 1000000')
Esempio n. 3
0
 def shift_register(self, out, use_last=False):
     if len(out) == 0:
         return bytearray()
     if isinstance(out, tuple):
         out = BitSequence(bytes_=out[1])[:out[0]]
     elif isinstance(out, bytearray):
         out = BitSequence(bytes_=out)
     elif not isinstance(out, BitSequence):
         raise Exception('Expect a BitSequence')
     length = len(out)
     if use_last:
         (out, self._last) = (out[:-1], int(out[-1]))
     byte_count = len(out) // 8
     pos = 8 * byte_count
     bit_count = len(out) - pos
     if not byte_count and not bit_count:
         raise Exception("Nothing to shift")
     if byte_count:
         blen = byte_count - 1
         cmd = array('B',
                     (Ftdi.RW_BYTES_PVE_NVE_LSB, blen, (blen >> 8) & 0xff))
         cmd.extend(out[:pos].tobytes(msby=True))
         self._stack_cmd(cmd)
     if bit_count:
         cmd = array('B', (Ftdi.RW_BITS_PVE_NVE_LSB, bit_count - 1))
         cmd.append(out[pos:].tobyte())
         self._stack_cmd(cmd)
     self._sync()
     bs = BitSequence()
     byte_count = length // 8
     pos = 8 * byte_count
     bit_count = length - pos
     if byte_count:
         data = self._ftdi.read_data_bytes(byte_count, 4)
         if not data:
             raise Exception('Unable to read data from FTDI')
         byteseq = BitSequence(bytes_=data, length=8 * byte_count)
         bs.append(byteseq)
     if bit_count:
         data = self._ftdi.read_data_bytes(1, 4)
         if not data:
             raise Exception('Unable to read data from FTDI')
         byte = data[0]
         # need to shift bits as they are shifted in from the MSB in FTDI
         byte >>= 8 - bit_count
         bitseq = BitSequence(byte, length=bit_count)
         bs.append(bitseq)
     assert len(bs) == length
     return bytearray(bs.tobytes())