Best Python code snippet using uiautomator
protocol.py
Source:protocol.py  
...14GPIO_MODE_OUTPUT = "O"15GPIO_READ       = "?"16GPIO_VALUE_HIGH = "1"17GPIO_VALUE_LOW  = "0"18def _pinch(channel):19  return chr(channel+ord('a'))20    21def _valuech(value):22  if value == None or value == 0 or value == False:23    return GPIO_VALUE_LOW24  return GPIO_VALUE_HIGH25    26def _modech(mode):27  if mode == None or mode == IN:28    return GPIO_MODE_INPUT29  return GPIO_MODE_OUTPUT30# Needed for Server later  31#def _parse_pinch(ch):32#  pass #TODO inverse of _pinch33#  34def _parse_valuech(ch):35  if ch == GPIO_VALUE_LOW:36    return False37  if ch == GPIO_VALUE_HIGH:38    return True39  error("Unknown value ch:" + ch)40  return GPIO_VALUE_HIGH41    42#def _parse_modech(ch):43#  pass #TODO inverse of _modech44  45  46# CLIENT ===============================================================47# Client will be constructed like: g = GPIOClient(Serial("/dev/ttyAMC0"))48# Client will be called via an interface just like RPi.GPIO49class GPIOClient:50  """ The GPIO command set 51      Assumes the wire protocol is already in the GPIO mode.52      As we only support the GPIO module at the moment,53      that's a simple assumption to make.54  """55  IN = 056  OUT = 157  DEBUG = False58  59  def trace(self, msg):60    if self.DEBUG:61      trace(msg)62  63  def __init__(self, wire, debug=False):64    self.wire = wire65    self.DEBUG = debug66    67  def setmode(self, mode):68    #BCM or BOARD, only for compatibility with RPi.GPIO69    pass  70  def setup(self, channel, mode):71    #TODO outer wrapper needs to do validation72    #if channel < self.MIN_PIN or channel > self.MAX_PIN:73    #  raise ValueError("Invalid pin")    74    pinch = _pinch(channel)75    modech = _modech(mode)76    self._write(pinch + modech)77    #TODO read and verify echoback78  def input(self, channel):79    #self.trace("READ")80    #TODO outer wrapper needs to do validation81    #if channel < self.MIN_PIN or channel > self.MAX_PIN:82    #  raise ValueError("Invalid pin")    83    pinch = _pinch(channel)84    self._write(pinch + GPIO_READ + "\n")85    while True:86      v = self._read(3, termset="\r\n")87      if len(v) == 3:88        break89      self.trace("retrying")90      91    self.trace("input read back:" + v + " len:" + str(len(v)))92    if len(v) == 1:93      self.trace("single returned char is ord:" + str(ord(v[0])))94    valuech = v[1]95    return _parse_valuech(valuech)96  def output(self, channel, value):97    #TODO outer wrapper needs to do validation98    #if channel < self.MIN_PIN or channel > self.MAX_PIN:99    #  raise ValueError("Invalid pin")    100    ch = _pinch(channel)101    v = _valuech(value)102    if value == None or value == 0 or value == False:103      self._write(ch + GPIO_VALUE_LOW + "\n")104    else:105      self._write(ch + GPIO_VALUE_HIGH + "\n")106    #TODO read and verify echoback107  def cleanup(self):108    pass109  # redirector to wrapped comms link110  def _open(self, *args, **kwargs):111    self.trace("open")112    self.wire.open(*args, **kwargs)113  def _write(self, *args, **kwargs):114    self.trace("write:" + str(*args) + " " + str(**kwargs))115    self.wire.write(*args, **kwargs)116  def _read(self, *args, **kwargs):117    self.trace("read")118    return self.wire.read(*args, **kwargs)119    120  def _close(self):121    self.trace("close")122    self.wire.close()123# SERVER ===============================================================124#125# The server is not needed yet, this is just a placeholder for later.126# We will use this to build a server listening on a network port,127# that accepts protocol commands and reroutes them to a local GPIO128# instance (allowing remote IO over the net or any other links)129#130# This is the server.131# Commands sent in are parsed and dispatched to methods132# server will be constructed like: 133#   s = GPIOServer(Serial("/dev/ttyAMA0"), RPi.GPIO)134# or135#   s = GPIOServer(Net("localhost", 8888), sim.GPIO)136# (auto starts)137#   ...138#  s.stop()139# Do we open and close the port in here?140# How does Net wrap network.py, does it do the start() and stop()141#class GPIOServer:142#  """ A server that accepts wire protocol commands143#      and dispatches those to a real GPIO implementation.144#  """145#  def __init__(self, wire, gpio):146#    self.wire = wire147#    self.gpio = gpio148#     149#  # The wrapping server class calls this function when it receives data150#  151#  def receive(self, msg, reply):152#    msg = msg.trim()153#    #Might be multiple commands in a single message, all 2 chars long154#    #TODO change this into a resumable state machine, 155#    #otherwise they must always come in together in the same call, 156#    #which they might not do over serial157#    #Just push this into a _machine() handler and pump chars to it158#    #from here.159#    160#    while len(msg) >= 2:161#      # Consume and parse 2 chars at a time162#      msgpart = msg[:2]163#      msg = msg[2:]164#      165#  def _process(self, msg):166#    # This is always called with 2 chars167#    pinch = msg[0]168#    valuech = msg[1]169#    170#    channel = _parse_pinch(pinch)171#    if   valuech == GPIO_MODE_INPUT:172#      self._setup(channel, IN)173#      174#    elif valuech == GPIO_MODE_OUTPUT:175#      self._setup(channel, OUT)176#      177#    elif valuech == GPIO_VALUE_HIGH:178#      self._output(channel, True)179#      180#    elif valuech == GPIO_VALUE_LOW:181#      self._output(channel, False)182#      183#    elif valuech == GPIO_READ:184#      pinval = self._read(channel)...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!!
