Пример #1
0
 def test_melody(self):
     from Arduino.arduino import build_cmd_str
     pin = 9
     notes = ["C4"]
     duration = 4
     C4_NOTE = 262
     self.board.Melody(pin, notes, [duration])
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str('to', (len(notes), pin, C4_NOTE, duration)))
     self.assertEquals(self.mock_serial.output[1],
         build_cmd_str('nto', (pin,)))
Пример #2
0
    def Melody(self, pin, melody, durations):
        """
        Plays a melody.
        inputs:
            pin: digital pin number for playback
            melody: list of tones
            durations: list of duration (4=quarter note, 8=eighth note, etc.)
        length of melody should be of same
        length as length of duration

        Melodies of the following lenght, can cause trouble
        when playing it multiple times.
            board.Melody(9,["C4","G3","G3","A3","G3",0,"B3","C4"],
                                                [4,8,8,4,4,4,4,4])
        Playing short melodies (1 or 2 tones) didn't cause
        trouble during testing
        """
        NOTES = dict(
            B0=31, C1=33, CS1=35, D1=37, DS1=39, E1=41, F1=44, FS1=46, G1=49,
            GS1=52, A1=55, AS1=58, B1=62, C2=65, CS2=69, D2=73, DS2=78, E2=82,
            F2=87, FS2=93, G2=98, GS2=104, A2=110, AS2=117, B2=123, C3=131,
            CS3=139, D3=147, DS3=156, E3=165, F3=175, FS3=185, G3=196, GS3=208,
            A3=220, AS3=233, B3=247, C4=262, CS4=277, D4=294, DS4=311, E4=330,
            F4=349, FS4=370, G4=392, GS4=415, A4=440,
            AS4=466, B4=494, C5=523, CS5=554, D5=587, DS5=622, E5=659, F5=698,
            FS5=740, G5=784, GS5=831, A5=880, AS5=932, B5=988, C6=1047,
            CS6=1109, D6=1175, DS6=1245, E6=1319, F6=1397, FS6=1480, G6=1568,
            GS6=1661, A6=1760, AS6=1865, B6=1976, C7=2093, CS7=2217, D7=2349,
            DS7=2489, E7=2637, F7=2794, FS7=2960, G7=3136, GS7=3322, A7=3520,
            AS7=3729, B7=3951, C8=4186, CS8=4435, D8=4699, DS8=4978)
        if (isinstance(melody, list)) and (isinstance(durations, list)):
            length = len(melody)
            cmd_args = [length, pin]
            if length == len(durations):
                cmd_args.extend([NOTES.get(melody[note])
                                for note in range(length)])
                cmd_args.extend([durations[duration]
                                for duration in range(len(durations))])
                cmd_str = build_cmd_str("to", cmd_args)
                try:
                    self.sr.write(cmd_str)
                    self.sr.flush()
                except:
                    pass
                cmd_str = build_cmd_str("nto", [pin])
                try:
                    self.sr.write(cmd_str)
                    self.sr.flush()
                except:
                    pass
            else:
                return -1
        else:
            return -1
Пример #3
0
 def test_melody(self):
     from Arduino.arduino import build_cmd_str
     pin = 9
     notes = ["C4"]
     duration = 4
     C4_NOTE = 262
     self.board.Melody(pin, notes, [duration])
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str('to', (len(notes), pin, C4_NOTE, duration)))
     self.assertEquals(self.mock_serial.output[1],
         build_cmd_str('nto', (pin,)))
 def test_digitalRead(self):
     from Arduino.arduino import build_cmd_str
     pin = 9
     self.board.pinMode(pin, INPUT)
     self.mock_serial.push_line(READ_LOW)
     self.assertEqual(self.board.digitalRead(pin), READ_LOW)
     self.assertEqual(self.mock_serial.output[1], build_cmd_str('dr', (pin,)))
Пример #5
0
 def test_digitalRead(self):
     from Arduino.arduino import build_cmd_str
     pin = 9
     self.mock_serial.push_line(READ_LOW)
     self.assertEquals(self.board.digitalRead(pin), READ_LOW)
     self.assertEquals(self.mock_serial.output[0],
                       build_cmd_str('dr', (pin, )))
Пример #6
0
 def test_pulseIn_high(self):
     from Arduino.arduino import build_cmd_str
     expected_duration = 230
     pin = 9
     self.mock_serial.push_line(expected_duration)
     self.assertEquals(self.board.pulseIn(pin, HIGH), expected_duration)
     self.assertEquals(self.mock_serial.output[0], build_cmd_str('pi', (pin,)))
Пример #7
0
 def test_analogWrite(self):
     from Arduino.arduino import build_cmd_str
     pin = 9
     value = 255
     self.board.analogWrite(pin, value)
     self.assertEquals(self.mock_serial.output[0],
                       build_cmd_str('aw', (pin, value)))
Пример #8
0
 def test_analogWrite(self):
     from Arduino.arduino import build_cmd_str
     pin = 9
     value = 255
     self.board.analogWrite(pin, value)
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str('aw', (pin, value)))
Пример #9
0
 def test_version(self):
     from Arduino.arduino import build_cmd_str
     expected_version = "version"
     self.mock_serial.push_line(expected_version)
     self.assertEqual(self.board.version(), expected_version)
     self.assertEqual(self.mock_serial.output[0].decode('UTF-8'),
                      build_cmd_str('version'))
Пример #10
0
 def test_pulseIn_high(self):
     from Arduino.arduino import build_cmd_str
     expected_duration = 230
     pin = 9
     self.mock_serial.push_line(expected_duration)
     self.assertEquals(self.board.pulseIn(pin, HIGH), expected_duration)
     self.assertEquals(self.mock_serial.output[0], build_cmd_str('pi', (pin,)))
Пример #11
0
def get_version(sr):
    cmd_str = build_cmd_str("version")
    try:
        sr.write(cmd_str)
        sr.flush()
    except Exception:
        return None
    return sr.readline().replace("\r\n", "")
Пример #12
0
 def test_analogRead(self):
     from Arduino.arduino import build_cmd_str
     pin = 9
     expected = 1023
     self.mock_serial.push_line(expected)
     self.assertEquals(self.board.analogRead(pin), expected)
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str('ar', (pin,)))
Пример #13
0
def get_version(sr):
    cmd_str = build_cmd_str("version")
    try:
        sr.write(cmd_str)
        sr.flush()
    except Exception:
        return None
    return sr.readline().replace("\r\n", "")
Пример #14
0
 def test_pulseIn_low(self):
     from Arduino.arduino import build_cmd_str
     expected_duration = 230
     self.mock_serial.push_line(expected_duration)
     pin = 9
     self.assertEqual(self.board.pulseIn(pin, LOW), expected_duration)
     self.assertEqual(self.mock_serial.output[0].decode('UTF-8'),
                      build_cmd_str('pi', (-pin, )))
Пример #15
0
 def test_analogRead(self):
     from Arduino.arduino import build_cmd_str
     pin = 9
     expected = 1023
     self.mock_serial.push_line(expected)
     self.assertEquals(self.board.analogRead(pin), expected)
     self.assertEquals(self.mock_serial.output[0],
                       build_cmd_str('ar', (pin, )))
Пример #16
0
 def test_shiftOut(self):
     from Arduino.arduino import build_cmd_str
     dataPin = 2
     clockPin = 3
     pinOrder = MSBFIRST
     value = 0xff
     self.board.shiftOut(dataPin, clockPin, pinOrder, value)
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str('so', (dataPin, clockPin, pinOrder, value)))
Пример #17
0
 def test_shiftOut(self):
     from Arduino.arduino import build_cmd_str
     dataPin = 2
     clockPin = 3
     pinOrder = MSBFIRST
     value = 0xff
     self.board.shiftOut(dataPin, clockPin, pinOrder, value)
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str('so', (dataPin, clockPin, pinOrder, value)))
Пример #18
0
 def detach(self, pin):
     position = self.servo_pos[pin]
     cmd_str = build_cmd_str("svd", (position, ))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
     del self.servo_pos[pin]
Пример #19
0
 def detach(self, pin):
     position = self.servo_pos[pin]
     cmd_str = build_cmd_str("svd", (position,))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
     del self.servo_pos[pin]
 def test_pulseIn_low(self):
     from Arduino.arduino import build_cmd_str
     expected_duration = 230
     self.mock_serial.push_line(expected_duration)
     pin = 9
     self.board.pinMode(pin, INPUT)
     self.assertEqual(self.board.pulseIn(pin, LOW), expected_duration)
     self.assertEqual(self.mock_serial.output[1],
         build_cmd_str('pi', (-pin,)))
Пример #21
0
 def test_attach(self):
     from Arduino.arduino import build_cmd_str
     pin = 10
     position = 0
     self.mock_serial.push_line(position)
     servo_min = 544
     servo_max = 2400
     self.board.Servos.attach(pin, min=servo_min, max=servo_max)
     self.assertEquals(self.mock_serial.output[0],
                       build_cmd_str('sva', (pin, servo_min, servo_max)))
Пример #22
0
 def test_attach(self):
     from Arduino.arduino import build_cmd_str
     pin = 10
     position = 0
     self.mock_serial.push_line(position)
     servo_min = 544
     servo_max = 2400
     self.board.Servos.attach(pin, min=servo_min, max=servo_max)
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str('sva', (pin, servo_min, servo_max)))
Пример #23
0
    def pulseIn_set(self, pin, val, numTrials=5):
        """
        Sets a digital pin value, then reads the response
        as a pulse width.
        Useful for some ultrasonic rangefinders, etc.

        inputs:
           pin: pin number for pulse measurement
           val: "HIGH" or "LOW". Pulse is measured
                when this state is detected
           numTrials: number of trials (for an average)
        returns:
           duration : an average of pulse length measurements

        This method will automatically toggle
        I/O modes on the pin and precondition the
        measurment with a clean LOW/HIGH pulse.
        Arduino.pulseIn_set(pin,"HIGH") is
        equivalent to the Arduino sketch code:

        pinMode(pin, OUTPUT);
        digitalWrite(pin, LOW);
        delayMicroseconds(2);
        digitalWrite(pin, HIGH);
        delayMicroseconds(5);
        digitalWrite(pin, LOW);
        pinMode(pin, INPUT);
        long duration = pulseIn(pin, HIGH);
        """
        if val == "LOW":
            pin_ = -pin
        else:
            pin_ = pin
        cmd_str = build_cmd_str("ps", (pin_, ))
        durations = []
        for s in range(numTrials):
            try:
                self.sr.write(cmd_str)
                self.sr.flush()
            except:
                pass
            rd = self.sr.readline().replace("\r\n", "")
            if rd.isdigit():
                if (int(rd) > 1):
                    durations.append(int(rd))
        if len(durations) > 0:
            duration = int(sum(durations)) / int(len(durations))
        else:
            duration = None

        try:
            return float(duration)
        except:
            return -1
Пример #24
0
 def test_shiftIn(self):
     from Arduino.arduino import build_cmd_str
     dataPin = 2
     clockPin = 3
     pinOrder = MSBFIRST
     expected = 0xff
     self.mock_serial.push_line(expected)
     self.assertEquals(self.board.shiftIn(dataPin, clockPin, pinOrder),
         expected)
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str('si', (dataPin, clockPin, pinOrder,)))
Пример #25
0
 def test_detach(self):
     from Arduino.arduino import build_cmd_str
     pin = 10
     position = 0
     # Attach first.
     self.mock_serial.push_line(position)
     self.board.Servos.attach(pin)
     self.mock_serial.reset_mock()
     self.board.Servos.detach(pin)
     self.assertEquals(self.mock_serial.output[0],
                       build_cmd_str('svd', (position, )))
Пример #26
0
 def test_shiftIn(self):
     from Arduino.arduino import build_cmd_str
     dataPin = 2
     clockPin = 3
     pinOrder = MSBFIRST
     expected = 0xff
     self.mock_serial.push_line(expected)
     self.assertEquals(self.board.shiftIn(dataPin, clockPin, pinOrder),
         expected)
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str('si', (dataPin, clockPin, pinOrder,)))
Пример #27
0
    def pulseIn_set(self, pin, val, numTrials=5):
        """
        Sets a digital pin value, then reads the response
        as a pulse width.
        Useful for some ultrasonic rangefinders, etc.

        inputs:
           pin: pin number for pulse measurement
           val: "HIGH" or "LOW". Pulse is measured
                when this state is detected
           numTrials: number of trials (for an average)
        returns:
           duration : an average of pulse length measurements

        This method will automatically toggle
        I/O modes on the pin and precondition the
        measurment with a clean LOW/HIGH pulse.
        Arduino.pulseIn_set(pin,"HIGH") is
        equivalent to the Arduino sketch code:

        pinMode(pin, OUTPUT);
        digitalWrite(pin, LOW);
        delayMicroseconds(2);
        digitalWrite(pin, HIGH);
        delayMicroseconds(5);
        digitalWrite(pin, LOW);
        pinMode(pin, INPUT);
        long duration = pulseIn(pin, HIGH);
        """
        if val == "LOW":
            pin_ = -pin
        else:
            pin_ = pin
        cmd_str = build_cmd_str("ps", (pin_,))
        durations = []
        for s in range(numTrials):
            try:
                self.sr.write(cmd_str)
                self.sr.flush()
            except:
                pass
            rd = self.sr.readline().replace("\r\n", "")
            if rd.isdigit():
                if (int(rd) > 1):
                    durations.append(int(rd))
        if len(durations) > 0:
            duration = int(sum(durations)) / int(len(durations))
        else:
            duration = None

        try:
            return float(duration)
        except:
            return -1
Пример #28
0
 def test_detach(self):
     from Arduino.arduino import build_cmd_str
     pin = 10
     position = 0
     # Attach first.
     self.mock_serial.push_line(position)
     self.board.Servos.attach(pin)
     self.mock_serial.reset_mock()
     self.board.Servos.detach(pin)
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str('svd', (position,)))
Пример #29
0
 def test_writeMicroseconds(self):
     from Arduino.arduino import build_cmd_str
     pin = 10
     position = 0
     microseconds = 1500
     # Attach first.
     self.mock_serial.push_line(position)
     self.board.Servos.attach(pin)
     self.mock_serial.reset_mock()
     self.board.Servos.writeMicroseconds(pin, microseconds)
     self.assertEquals(self.mock_serial.output[0],
                       build_cmd_str("svwm", (position, microseconds)))
Пример #30
0
 def test_write(self):
     from Arduino.arduino import build_cmd_str
     pin = 10
     position = 0
     angle = 90
     # Attach first.
     self.mock_serial.push_line(position)
     self.board.Servos.attach(pin)
     self.mock_serial.reset_mock()
     self.board.Servos.write(pin, angle)
     self.assertEquals(self.mock_serial.output[0],
                       build_cmd_str("svw", (position, angle)))
Пример #31
0
 def test_write(self):
     from Arduino.arduino import build_cmd_str
     pin = 10
     position = 0
     angle = 90
     # Attach first.
     self.mock_serial.push_line(position)
     self.board.Servos.attach(pin)
     self.mock_serial.reset_mock()
     self.board.Servos.write(pin, angle)
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str("svw", (position, angle)))
Пример #32
0
 def test_writeMicroseconds(self):
     from Arduino.arduino import build_cmd_str
     pin = 10
     position = 0
     microseconds = 1500
     # Attach first.
     self.mock_serial.push_line(position)
     self.board.Servos.attach(pin)
     self.mock_serial.reset_mock()
     self.board.Servos.writeMicroseconds(pin, microseconds)
     self.assertEquals(self.mock_serial.output[0],
         build_cmd_str("svwm", (position, microseconds)))
Пример #33
0
  def size(self):
      """
      Returns size of EEPROM memory.
      """
      cmd_str = build_cmd_str("sz")
 
      try:
          self.sr.write(cmd_str)
          self.sr.flush()
          response = self.sr.readline().replace("\r\n", "")
          return int(response)
      except:
          return 0
Пример #34
0
    def shiftOut(self, dataPin, clockPin, pinOrder, value):
        """
        Shift a byte out on the datapin using Arduino's shiftOut()

        Input:
            dataPin (int): pin for data
            clockPin (int): pin for clock
            pinOrder (String): either 'MSBFIRST' or 'LSBFIRST'
            value (int): an integer from 0 and 255
        """
        cmd_str = build_cmd_str("so", (dataPin, clockPin, pinOrder, value))
        self.sr.write(cmd_str)
        self.sr.flush()
Пример #35
0
 def test_read(self):
     from Arduino.arduino import build_cmd_str
     pin = 10
     position = 0
     angle = 90
     # Attach first.
     self.mock_serial.push_line(position)
     self.board.Servos.attach(pin)
     self.mock_serial.reset_mock()
     self.mock_serial.push_line(angle)
     self.assertEqual(self.board.Servos.read(pin), angle)
     self.assertEqual(self.mock_serial.output[0].decode('UTF-8'),
                      build_cmd_str("svr", (position, )))
Пример #36
0
    def size(self):
        """
        Returns size of EEPROM memory.
        """
        cmd_str = build_cmd_str("sz")

        try:
            self.sr.write(cmd_str)
            self.sr.flush()
            response = self.sr.readline().replace("\r\n", "")
            return int(response)
        except:
            return 0
Пример #37
0
 def read(self):
     """
     returns first character read from
     existing software serial instance
     """
     if self.connected:
         cmd_str = build_cmd_str("sr")
         self.sr.write(cmd_str)
         self.sr.flush()
         response = self.sr.readline().replace("\r\n", "")
         if response:
             return response
     else:
         return False
Пример #38
0
    def shiftOut(self, dataPin, clockPin, pinOrder, value):
        """
        Shift a byte out on the datapin using Arduino's shiftOut()

        Input:
            dataPin (int): pin for data
            clockPin (int): pin for clock
            pinOrder (String): either 'MSBFIRST' or 'LSBFIRST'
            value (int): an integer from 0 and 255
        """
        cmd_str = build_cmd_str("so",
                               (dataPin, clockPin, pinOrder, value))
        self.sr.write(cmd_str)
        self.sr.flush()
Пример #39
0
 def read(self, adrress):
     """ Reads a byte from the EEPROM.
     
     :address: the location to write to, starting from 0 (int)
     """
     cmd_str = build_cmd_str("eer", (adrress, ))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
         response = self.sr.readline().replace("\r\n", "")
         if response:
             return int(response)
     except:
         return 0
Пример #40
0
 def read(self, adrress):
     """ Reads a byte from the EEPROM.
     
     :address: the location to write to, starting from 0 (int)
     """
     cmd_str = build_cmd_str("eer", (adrress,))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()            
         response = self.sr.readline().replace("\r\n", "")
         if response:
             return int(response)
     except:
         return 0
Пример #41
0
 def read(self):
     """
     returns first character read from
     existing software serial instance
     """
     if self.connected:
         cmd_str = build_cmd_str("sr")
         self.sr.write(cmd_str)
         self.sr.flush()
         response = self.sr.readline().replace("\r\n", "")
         if response:
             return response
     else:
         return False
Пример #42
0
    def attach(self, pin, min=544, max=2400):
        cmd_str = build_cmd_str("sva", (pin, min, max))

        while True:
            self.sr.write(cmd_str)
            self.sr.flush()

            rd = self.sr.readline().replace("\r\n", "")
            if rd:
                break
            else:
                log.debug("trying to attach servo to pin {0}".format(pin))
        position = int(rd)
        self.servo_pos[pin] = position
        return 1
Пример #43
0
    def attach(self, pin, min=544, max=2400):
        cmd_str = build_cmd_str("sva", (pin, min, max))

        while True:
            self.sr.write(cmd_str)
            self.sr.flush()

            rd = self.sr.readline().replace("\r\n", "")
            if rd:
                break
            else:
                log.debug("trying to attach servo to pin {0}".format(pin))
        position = int(rd)
        self.servo_pos[pin] = position
        return 1
Пример #44
0
 def read(self, pin):
     if pin not in self.servo_pos.keys():
         self.attach(pin)
     position = self.servo_pos[pin]
     cmd_str = build_cmd_str("svr", (position, ))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
     rd = self.sr.readline().replace("\r\n", "")
     try:
         angle = int(rd)
         return angle
     except:
         return None
Пример #45
0
 def read(self, pin):
     if pin not in self.servo_pos.keys():
         self.attach(pin)
     position = self.servo_pos[pin]
     cmd_str = build_cmd_str("svr", (position,))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
     rd = self.sr.readline().replace("\r\n", "")
     try:
         angle = int(rd)
         return angle
     except:
         return None
Пример #46
0
 def write(self, address, value=0):
     """ Write a byte to the EEPROM.
         
     :address: the location to write to, starting from 0 (int)
     :value: the value to write, from 0 to 255 (byte)
     """
     
     if value > 255:
         value = 255
     elif value < 0:
         value = 0
     cmd_str = build_cmd_str("eewr", (address, value))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
Пример #47
0
 def write(self, data):
     """
     sends data to existing software serial instance
     using Arduino's 'write' function
     """
     if self.connected:
         cmd_str = build_cmd_str("sw", (data,))
         try:
             self.sr.write(cmd_str)
             self.sr.flush()
         except:
             pass
         response = self.sr.readline().replace("\r\n", "")
         if response == "ss OK":
             return True
     else:
         return False
Пример #48
0
    def write(self, address, value=0):
        """ Write a byte to the EEPROM.
            
        :address: the location to write to, starting from 0 (int)
        :value: the value to write, from 0 to 255 (byte)
        """

        if value > 255:
            value = 255
        elif value < 0:
            value = 0
        cmd_str = build_cmd_str("eewr", (address, value))
        try:
            self.sr.write(cmd_str)
            self.sr.flush()
        except:
            pass
Пример #49
0
    def shiftIn(self, dataPin, clockPin, pinOrder):
        """
        Shift a byte in from the datapin using Arduino's shiftIn().

        Input:
            dataPin (int): pin for data
            clockPin (int): pin for clock
            pinOrder (String): either 'MSBFIRST' or 'LSBFIRST'
        Output:
            (int) an integer from 0 to 255
        """
        cmd_str = build_cmd_str("si", (dataPin, clockPin, pinOrder))
        self.sr.write(cmd_str)
        self.sr.flush()
        rd = self.sr.readline().replace("\r\n", "")
        if rd.isdigit():
            return int(rd)
Пример #50
0
 def write(self, data):
     """
     sends data to existing software serial instance
     using Arduino's 'write' function
     """
     if self.connected:
         cmd_str = build_cmd_str("sw", (data, ))
         try:
             self.sr.write(cmd_str)
             self.sr.flush()
         except:
             pass
         response = self.sr.readline().replace("\r\n", "")
         if response == "ss OK":
             return True
     else:
         return False
Пример #51
0
    def shiftIn(self, dataPin, clockPin, pinOrder):
        """
        Shift a byte in from the datapin using Arduino's shiftIn().

        Input:
            dataPin (int): pin for data
            clockPin (int): pin for clock
            pinOrder (String): either 'MSBFIRST' or 'LSBFIRST'
        Output:
            (int) an integer from 0 to 255
        """
        cmd_str = build_cmd_str("si", (dataPin, clockPin, pinOrder))
        self.sr.write(cmd_str)
        self.sr.flush()
        rd = self.sr.readline().replace("\r\n", "")
        if rd.isdigit():
            return int(rd)
Пример #52
0
 def pinMode(self, pin, val):
     """
     Sets I/O mode of pin
     inputs:
        pin: pin number to toggle
        val: "INPUT" or "OUTPUT"
     """
     if val == "INPUT":
         pin_ = -pin
     else:
         pin_ = pin
     cmd_str = build_cmd_str("pm", (pin_, ))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
Пример #53
0
 def pinMode(self, pin, val):
     """
     Sets I/O mode of pin
     inputs:
        pin: pin number to toggle
        val: "INPUT" or "OUTPUT"
     """
     if val == "INPUT":
         pin_ = -pin
     else:
         pin_ = pin
     cmd_str = build_cmd_str("pm", (pin_,))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
Пример #54
0
 def begin(self, p1, p2, baud):
     """
     Create software serial instance on
     specified tx,rx pins, at specified baud
     """
     cmd_str = build_cmd_str("ss", (p1, p2, baud))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
     response = self.sr.readline().replace("\r\n", "")
     if response == "ss OK":
         self.connected = True
         return True
     else:
         self.connected = False
         return False
Пример #55
0
    def capacitivePin(self, pin):
        '''
        Input:
            pin (int): pin to use as capacitive sensor

        Use it in a loop!
        DO NOT CONNECT ANY ACTIVE DRIVER TO THE USED PIN !

        the pin is toggled to output mode to discharge the port,
        and if connected to a voltage source,
        will short circuit the pin, potentially damaging
        the Arduino/Shrimp and any hardware attached to the pin.
        '''
        cmd_str = build_cmd_str("cap", (pin, ))
        self.sr.write(cmd_str)
        rd = self.sr.readline().replace("\r\n", "")
        if rd.isdigit():
            return int(rd)
Пример #56
0
 def begin(self, p1, p2, baud):
     """
     Create software serial instance on
     specified tx,rx pins, at specified baud
     """
     cmd_str = build_cmd_str("ss", (p1, p2, baud))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
     response = self.sr.readline().replace("\r\n", "")
     if response == "ss OK":
         self.connected = True
         return True
     else:
         self.connected = False
         return False
Пример #57
0
    def capacitivePin(self, pin):
        '''
        Input:
            pin (int): pin to use as capacitive sensor

        Use it in a loop!
        DO NOT CONNECT ANY ACTIVE DRIVER TO THE USED PIN !

        the pin is toggled to output mode to discharge the port,
        and if connected to a voltage source,
        will short circuit the pin, potentially damaging
        the Arduino/Shrimp and any hardware attached to the pin.
        '''
        cmd_str = build_cmd_str("cap", (pin,))
        self.sr.write(cmd_str)
        rd = self.sr.readline().replace("\r\n", "")
        if rd.isdigit():
            return int(rd)
Пример #58
0
 def digitalWrite(self, pin, val):
     """
     Sends digitalWrite command
     to digital pin on Arduino
     -------------
     inputs:
        pin : digital pin number
        val : either "HIGH" or "LOW"
     """
     if val == "LOW":
         pin_ = -pin
     else:
         pin_ = pin
     cmd_str = build_cmd_str("dw", (pin_,))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
Пример #59
0
 def analogWrite(self, pin, val):
     """
     Sends analogWrite pwm command
     to pin on Arduino
     -------------
     inputs:
        pin : pin number
        val : integer 0 (off) to 255 (always on)
     """
     if val > 255:
         val = 255
     elif val < 0:
         val = 0
     cmd_str = build_cmd_str("aw", (pin, val))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass
Пример #60
0
 def analogWrite(self, pin, val):
     """
     Sends analogWrite pwm command
     to pin on Arduino
     -------------
     inputs:
        pin : pin number
        val : integer 0 (off) to 255 (always on)
     """
     if val > 255:
         val = 255
     elif val < 0:
         val = 0
     cmd_str = build_cmd_str("aw", (pin, val))
     try:
         self.sr.write(cmd_str)
         self.sr.flush()
     except:
         pass