Best Python code snippet using autotest_python
ftdi_decoder.py
Source:ftdi_decoder.py  
1from enum import Enum2from collections import namedtuple3class FtdiCommandType(Enum):4    # Command type is unknown5    UNKNOWN = 06    # Clocking data on TDI7    CLOCK_TDI = 0x108    # Capturing data on TDO9    CLOCK_TDO = 0x2010    # Clocking data on TMS11    CLOCK_TMS = 0x4012    # Configure low octet of GPIO's13    SET_GPIO_LOW_BYTE = 0x8014    # Get line state of low octet of GPIO's15    GET_GPIO_LOW_BYTE = 0x8116    # Configure high octet of GPIO's17    SET_GPIO_HIGH_BYTE = 0x8218    # Get line state of high octet of GPIO's19    GET_GPIO_HIGH_BYTE = 0x8320    DISABLE_LOOPBACK = 0x8521    # Set FTDI clock rate by setting the clock divider22    SET_DIVISOR = 0x8623    FLUSH = 0x8724    DISABLE_DIV_BY_5 = 0x8a25    DISABLE_RCLK = 0x9726    # Run clock without changing TDI/TDO/TMS27    CLOCK_NO_DATA = 0x8f28class FtdiFlags(Enum):29    # Data changes on negative edge30    NEG_EDGE_OUT = 0x131    # Operation length is in bits32    BITWISE = 0x233    # Data is captured on negative edge34    NEG_EDGE_IN = 0x435    # Data is sent LSB first36    LSB_FIRST = 0x837    # Is TDI held high or low when clocking bits?38    TDI_HIGH = 0x8039FtdiCommand = namedtuple('FtdiCommand', 'type flags command_frame reply_frame opcode length data reply')40class DecodeError(RuntimeError):41    """ Raised when a decoding error is found. """42    def __init__(self, msg, commands, last_byte=None):43        RuntimeError.__init__(self, msg)44        self.commands = commands45        self.last_byte = last_byte46    def get_last_byte(self):47        return self.last_byte48def get_write_flags(byte):49    """ Get command flags for write from command byte. Returns list of FtdiFlags. """50    flags = []51    for flag in [FtdiFlags.NEG_EDGE_OUT, FtdiFlags.BITWISE, FtdiFlags.NEG_EDGE_IN, FtdiFlags.LSB_FIRST]:52        if byte & flag.value != 0:53            flags.append(flag)54    return flags55def read_data(byte, ftdi_bytes, ftdi_replies):56    """ Get written data, and returned reply (if any). """57    if byte & FtdiFlags.BITWISE.value != 0:58        # Bitwise write59        number_of_bits = ftdi_bytes.popleft() + 160        if number_of_bits > 7:61            raise DecodeError('Bitwise clocking should only clock 7 or less bits, found {}'.format(number_of_bits), commands=None, last_byte=byte)62        data = [ftdi_bytes.popleft()]63        reply = None64        if byte & FtdiCommandType.CLOCK_TDO.value != 0:65            reply = [ftdi_replies.popleft()]66        return number_of_bits, data, reply67    else:68        # Byte write69        number_of_bytes = ftdi_bytes.popleft()70        number_of_bytes |= ftdi_bytes.popleft() << 871        number_of_bytes += 172        data = [ftdi_bytes.popleft() for _ in range(number_of_bytes)]73        reply = None74        if byte & FtdiCommandType.CLOCK_TDO.value != 0:75            reply = [ftdi_replies.popleft() for _ in range(number_of_bytes)]76        return number_of_bytes, data, reply77def decode_commands(ftdi_bytes, ftdi_replies):78    """ Attempt to decode commands from ftdi_bytes, paired with replies. """79    ftdi_commands = []80    def add_command(command_type, command_opcode, flags=None, length=None, data=None, reply=None):81        if length is not None:82            #assert len(data) == length83            #if reply is not None:84            #    assert len(reply) == length85            pass86        if reply is not None:87            reply_frame = ftdi_replies.current_frame()88        else:89            reply_frame = None90        ftdi_commands.append(FtdiCommand(91            type=command_type,92            opcode=command_opcode,93            command_frame=ftdi_bytes.current_frame(),94            reply_frame=reply_frame,95            flags=flags,96            length=length,97            data=data,98            reply=reply))99    while len(ftdi_bytes):100        byte = ftdi_bytes.popleft()101        if byte == 0xaa:102            reply = [ftdi_replies.popleft() for _ in range(2)]103            add_command(FtdiCommandType.UNKNOWN, byte, reply=reply)104        elif byte == 0xab:105            reply = [ftdi_replies.popleft() for _ in range(2)]106            add_command(FtdiCommandType.UNKNOWN, byte, reply=reply)107        elif byte == FtdiCommandType.DISABLE_RCLK.value:108            add_command(FtdiCommandType.DISABLE_RCLK, byte)109        elif byte & FtdiCommandType.CLOCK_TMS.value != 0:110            if byte & FtdiCommandType.CLOCK_TDI.value != 0:111                raise DecodeError('When clocking TMS, cannot clock TDI?', ftdi_commands, last_byte=byte)112            flags = get_write_flags(byte)113            length, data, reply = read_data(byte, ftdi_bytes, ftdi_replies)114            add_command(115                    command_type=FtdiCommandType.CLOCK_TMS,116                    command_opcode=byte,117                    flags=flags,118                    length=length,119                    data=data,120                    reply=reply,121                    )122        elif byte & FtdiCommandType.CLOCK_TDI.value != 0:123            if byte & FtdiCommandType.CLOCK_TMS.value != 0:124                raise DecodeError('When clocking TDI, cannot clock TMS?', ftdi_commands, last_byte=byte)125            flags = get_write_flags(byte)126            length, data, reply = read_data(byte, ftdi_bytes, ftdi_replies)127            add_command(128                    command_type=FtdiCommandType.CLOCK_TDI,129                    command_opcode=byte,130                    flags=flags,131                    length=length,132                    data=data,133                    reply=reply,134                    )135        elif byte & FtdiCommandType.CLOCK_TDO.value != 0:136            # These shouldn't been detected in the previous elif blocks137            assert byte & FtdiCommandType.CLOCK_TMS.value == 0, hex(byte)138            assert byte & FtdiCommandType.CLOCK_TDI.value == 0, hex(byte)139            flags = get_write_flags(byte)140            length = 0141            if byte & FtdiFlags.BITWISE.value != 0:142                length = ftdi_bytes.popleft() + 1143                if length > 7:144                    raise DecodeError('Bitwise clocking should only clock 7 or less bits, found {}'.format(length), ftdi_commands, last_byte=byte)145                reply = [ftdi_replies.popleft()]146            else:147                length = ftdi_bytes.popleft()148                length |= ftdi_bytes.popleft() << 8149                length += 1150                reply = [ftdi_replies.popleft() for _ in range(length)]151            add_command(152                    command_type=FtdiCommandType.CLOCK_TDO,153                    command_opcode=byte,154                    flags=flags,155                    length=length,156                    reply=reply,157                    )158        elif byte == FtdiCommandType.CLOCK_NO_DATA.value:159            flags = []160            length = ftdi_bytes.popleft()161            length |= ftdi_bytes.popleft() << 8162            length += 1163            add_command(164                    command_type=FtdiCommandType.CLOCK_NO_DATA,165                    command_opcode=byte,166                    flags=flags,167                    length=length,168                    )169        elif byte == FtdiCommandType.SET_GPIO_LOW_BYTE.value:170            data = [ftdi_bytes.popleft() for _ in range(2)]171            add_command(FtdiCommandType.SET_GPIO_LOW_BYTE, byte, data=data)172        elif byte == FtdiCommandType.GET_GPIO_LOW_BYTE.value:173            reply = [ftdi_replies.popleft()]174            add_command(FtdiCommandType.GET_GPIO_LOW_BYTE, byte, reply=reply)175        elif byte == FtdiCommandType.SET_GPIO_HIGH_BYTE.value:176            data = [ftdi_bytes.popleft() for _ in range(2)]177            add_command(FtdiCommandType.SET_GPIO_HIGH_BYTE, byte, data=data)178        elif byte == FtdiCommandType.GET_GPIO_HIGH_BYTE.value:179            reply = [ftdi_replies.popleft()]180            add_command(FtdiCommandType.GET_GPIO_HIGH_BYTE, byte, reply=reply)181        elif byte == FtdiCommandType.DISABLE_LOOPBACK.value:182            add_command(FtdiCommandType.DISABLE_LOOPBACK, byte)183        elif byte == FtdiCommandType.SET_DIVISOR.value:184            data = ftdi_bytes.popleft()185            data |= ftdi_bytes.popleft() << 8186            add_command(FtdiCommandType.SET_DIVISOR, byte, data=[data])187        elif byte == FtdiCommandType.FLUSH.value:188            if not ftdi_replies.at_boundry():189                raise DecodeError('Should have a RX boundry in reply data?',190                        ftdi_commands, last_byte=byte)191            add_command(FtdiCommandType.FLUSH, byte)192        elif byte == FtdiCommandType.DISABLE_DIV_BY_5.value:193            add_command(FtdiCommandType.DISABLE_DIV_BY_5, byte)194        else:195            raise DecodeError('Unknown byte {}'.format(hex(byte)), ftdi_commands, last_byte=byte)196    if len(ftdi_replies) != 0:197        raise DecodeError('Leftover RX data, leftover = {}.'.format(len(ftdi_replies)), ftdi_commands)...simonsays2.py
Source:simonsays2.py  
...69        if not correct:70            break71    72    # reset the loop back73    usbbox.disable_loopback()74    75    time.sleep(0.5)76    77    # show result to user on box78    if correct:79        # as we reset the clock after the lights went out80        # the "rtc" (real-time clock) value of the last pressed key81        # should tell us how long the user took to press all of the82        # buttons83        last_press=keyevents[-1].rtc84        print "correct (%dms)" % last_press85        flash(usbbox)86    else:87        print "user said:", " ".join([BUTTON_COLORS[event.key_code] for event in keyevents])88        print "wrong"89        flash(usbbox,rate=0.25,count=3)90    91    return correct92usbbox=USBBox()93print "Simon Says"94print "consists of several rounds of sequences being shown:"95print "1) LEDs will flash on box"96print "2) a sequence of LEDs will be shown"97print "3) after the sequence finishes the LEDs will flash again"98print "4) enter in the sequence previously shown (press the buttons that match the LEDs)"99print "5) if you get it right the LEDs will flash once and it'll repeat the cycle (with one more item to remember)"100print "6) if you get it wrong the LEDs will flash three times and you'll be asked if you want to play a new game"101# use logset so that 1 turns LED on and 0 turn LED off102usbbox.leds.logic=0xFF103# and turn off the LEDs104usbbox.leds.state=0x00105# ensure loop back isn't set106usbbox.disable_loopback()107    108while True:109    command=raw_input("play game y/n [y]: ").strip()110    if command == '':111        command = 'y'112    if command.lower() != 'y':113        break114    115    for i in range(1,50):116        time.sleep(1)117        print "%d to remember" % i118        if not simon_says(usbbox,i):...loopback.py
Source:loopback.py  
...43    # Put CTP7 into loopback testing mode44    writeReg(getNode('GEM_AMC.GEM_SYSTEM.TESTS.GBT_LOOPBACK_EN'), 1)45    sleep(0.1)46    return47def disable_loopback():48    writeReg(getNode('GEM_AMC.GEM_SYSTEM.TESTS.GBT_LOOPBACK_EN'), 0)49    sleep(0.1)50    return51def select_OH(oh):52    # select the OH to be used53    writeReg(getNode('GEM_AMC.GEM_TESTS.OH_LOOPBACK.CTRL.OH_SELECT'), oh)54    sleep(0.1)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
