Exemple #1
0
class GrovePort():
    def __init__(self, port=None, type=GRXCom.TYPE.ANALOG_INPUT, mode=0):
        if port == None:
            raise TypeError("You must specify a port as an argument." \
                    " Please do so in the form B?$# where ? is A or B" \
                    " for the bank letter (which half of the PiStorms)," \
                    " $ is A or D for the port type (analog or digital)," \
                    " and # is the port number." \
                    " For example: BAA2 is Bank A, Analog port 2.")

        bank = port[:2]
        if bank == "BA":
            i2c = GRXCom.I2C.A
        elif bank == "BB":
            i2c = GRXCom.I2C.B
        else:
            raise ValueError("Invalid bank (must be A or B).")

        if port[2] == "A":
            addresses = GRXCom.ANALOG
            typeSupport = GRXCom.TYPE_SUPPORT.ANALOG
        elif port[2] == "D":
            addresses = GRXCom.DIGITAL
            typeSupport = GRXCom.TYPE_SUPPORT.DIGITAL
        else:
            raise ValueError(
                "Invalid port type (must be A for analog or D for digital).")
        if type not in typeSupport:
            raise TypeError("This port does not support that type.")

        number = int(port[3]) - 1
        # not catching an IndexError here because -1 should not be valid
        if number not in range(len(addresses)):
            raise ValueError(
                "Invalid port number (must be 1, 2, or 3 for analog; or 1 or 2 for digital)."
            )

        if mode not in range(256):
            raise ValueError(
                "Sensor port mode must be an integer between 0 and 255.")

        if port == "BBD2" and type == GRXCom.TYPE.ENCODER:
            raise ValueError(
                "BBD2 does not support encoders at this time. Please use BBD1, BAD1 or BAD2."
            )

        address = addresses[number]
        self.comm = GRXCom(i2c, address)
        self.comm.setType(type, mode)

        if type == GRXCom.TYPE.DIGITAL_INPUT:
            self.readValue = self.comm.digitalRead
        elif type == GRXCom.TYPE.ANALOG_INPUT:
            self.readValue = self.comm.analogRead
        elif type == GRXCom.TYPE.DIGITAL_OUTPUT:
            self.writeValue = self.comm.digitalWrite
        elif type == GRXCom.TYPE.ENCODER:
            self.readValue = self.comm.readEncoderValue
Exemple #2
0
class GrovePort():
    def __init__(self, port=None, type=GRXCom.TYPE.ANALOG_INPUT, mode=0):
        if port == None:
            raise TypeError("You must specify a port as an argument." \
                    " Please do so in the form B?$# where ? is A or B" \
                    " for the bank letter (which half of the PiStorms)," \
                    " $ is A or D for the port type (analog or digital)," \
                    " and # is the port number." \
                    " For example: BAA2 is Bank A, Analog port 2.")

        bank = port[:2]
        if bank == "BA":
            i2c = GRXCom.I2C.A
        elif bank == "BB":
            i2c = GRXCom.I2C.B
        else:
            raise ValueError("Invalid bank (must be A or B).")

        if port[2]=="A":
            addresses = GRXCom.ANALOG
            typeSupport = GRXCom.TYPE_SUPPORT.ANALOG
        elif port[2]=="D":
            addresses = GRXCom.DIGITAL
            typeSupport = GRXCom.TYPE_SUPPORT.DIGITAL
        else:
            raise ValueError("Invalid port type (must be A for analog or D for digital).")
        if type not in typeSupport:
            raise TypeError("This port does not support that type.")

        number = int(port[3]) - 1
        # not catching an IndexError here because -1 should not be valid
        if number not in range(len(addresses)):
            raise ValueError("Invalid port number (must be 1, 2, or 3 for analog; or 1 or 2 for digital).")

        if mode not in range(256):
            raise ValueError("Sensor port mode must be an integer between 0 and 255.")

        if port == "BBD2" and type == GRXCom.TYPE.ENCODER:
            raise ValueError("BBD2 does not support encoders at this time. Please use BBD1, BAD1 or BAD2.")

        address = addresses[number]
        self.comm = GRXCom(i2c, address)
        self.comm.setType(type, mode)

        if type == GRXCom.TYPE.DIGITAL_INPUT:
            self.readValue = self.comm.digitalRead
        elif type == GRXCom.TYPE.ANALOG_INPUT:
            self.readValue = self.comm.analogRead
        elif type == GRXCom.TYPE.DIGITAL_OUTPUT:
            self.writeValue = self.comm.digitalWrite
        elif type == GRXCom.TYPE.ENCODER:
            self.readValue = self.comm.readEncoderValue
Exemple #3
0
 def ping(self):
     GRXCom.ping()
Exemple #4
0
 def isF4Pressed(self):
     return (GRXCom.getKeyPressValue() == 40)
Exemple #5
0
 def isF3Pressed(self):
     return (GRXCom.getKeyPressValue() == 24)
Exemple #6
0
 def isF2Pressed(self):
     return (GRXCom.getKeyPressValue() == 16)
Exemple #7
0
 def getKeyPressValue(self): # F1-4
     return {0:0, 8:1, 16:2, 24:3, 40:4}[GRXCom.getKeyPressValue()]