<s> from builtins import object <EOL> import pluto <EOL> import re <EOL> import serial . tools . list_ports <EOL> import time <EOL> from pyfirmata import util , OUTPUT , INPUT , PWM <EOL> from pluto import LOW , HIGH , LED_BUILTIN <EOL> import exceptions <EOL> class ArduinoUtil ( object ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def digitalWrite ( board , pin_number , value ) : <EOL> if isinstance ( board , pluto . Board ) : <EOL> if board . digital [ pin_number ] . mode != OUTPUT : <EOL> board . digital [ pin_number ] . mode = OUTPUT <EOL> else : <EOL> pass <EOL> board . digital [ pin_number ] . write ( value ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def digitalRead ( board , pin_number ) : <EOL> if isinstance ( board , pluto . Board ) : <EOL> if board . digital [ pin_number ] . mode != INPUT : <EOL> board . digital [ pin_number ] . mode = INPUT <EOL> else : <EOL> pass <EOL> board . digital [ pin_number ] . read ( ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def analogWrite ( board , pin_number , value ) : <EOL> if isinstance ( board , pluto . Board ) : <EOL> if board . digital [ pin_number ] . PWM_CAPABLE : <EOL> if board . digital [ pin_number ] . mode != <NUM_LIT:3> : <EOL> board . digital [ pin_number ] . _set_mode ( PWM ) <EOL> else : <EOL> pass <EOL> board . digital [ pin_number ] . write ( value ) <EOL> else : <EOL> raise exceptions . PinError ( "<STR_LIT>" ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def analogRead ( board , pin_number ) : <EOL> if isinstance ( board , pluto . Board ) : <EOL> board . analog [ pin_number ] . read ( ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> class PortUtil ( object ) : <EOL> """<STR_LIT>""" <EOL> comports = [ p [ <NUM_LIT:0> ] for p in serial . tools . list_ports . comports ( ) ] <EOL> num_ports = len ( comports ) <EOL> auto_port = None <EOL> keywords = [ ] <EOL> patterns = [ ] <EOL> @ classmethod <EOL> def count_ports ( cls ) : <EOL> return cls . num_ports <EOL> @ classmethod <EOL> def scan ( cls , * args , ** kwargs ) : <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> cls . keywords = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> for index , val in enumerate ( args ) : <EOL> cls . keywords . append ( val ) <EOL> for keyword in cls . keywords : <EOL> p = re . compile ( '<STR_LIT>' . format ( keyword ) ) <EOL> cls . patterns . append ( p ) <EOL> for port in cls . comports : <EOL> for pattern in cls . patterns : <EOL> m = pattern . match ( port ) <EOL> if m : <EOL> cls . auto_port = m . group ( ) <EOL> else : <EOL> pass <EOL> return cls . auto_port </s>
