示例#1
0
class Blinker(object):
    '''
    Makes a LED to blink
    '''
    def __init__(self, port, delay=0.1):
        '''
        Constructor
        @param port: GPIO port number where the LED is connected to
        @param delay: Time the LED is on and off. Default 0.1s
        '''

        self._port = Gpio(port, Gpio.OUT)
        self._delay = delay
        self._isRunning = False
        self._doBlinkThread = None

    def start(self):
        '''
        Starts the LED to be blinking
        '''

        if self._doBlinkThread == None or not self._doBlinkThread.isAlive():

            self._isRunning = True
            self._doBlinkThread = Thread(target=self._doBlink)
            self._doBlinkThread.start()

    def stop(self):
        '''
        Stops the LED blinking
        '''

        self._isRunning = False
        if self._doBlinkThread != None and self._doBlinkThread.isAlive():

            self._doBlinkThread.join()

    def setDelay(self, delay):
        '''
        Set the time the LED is on and off
        @param delay: seconds
        '''

        self._delay = delay

    def _doBlink(self):
        '''
        Thread action to be the LED blinking
        '''

        status = True

        while self._isRunning:

            self._port.setValue(status)
            status = not status
            time.sleep(self._delay)

        self._port.setValue(False)

    def cleanup(self):
        '''
        Frees ressources
        '''

        self._port.cleanup()
示例#2
0
from gpio import Gpio
from time import sleep

led = Gpio(27, Gpio.OUT) #P8.17
key = Gpio(65, Gpio.IN) #P8.18

try:

    light = Gpio.LOW
    led.setValue(light)
    
    lastKeyState = Gpio.LOW
    print("Ready.\nPress toggle key to change led state or Ctrl+C to exit.")

    while True:
    
        keyState = key.getValue()
        if lastKeyState != keyState and keyState == Gpio.HIGH:
            light = not light
            led.setValue(light)
            print("Led state changed to {0}.".format("ON" if light else "OFF"))
            
        lastKeyState = keyState
            
        sleep(0.2)

except KeyboardInterrupt:

    print("\nBye!")

finally:
示例#3
0
class Ultrasonic(object):

    PULSE2CM = 17241.3793  # cm/s
    MAX_RANGE = 3500  # cm
    OUT_OF_RANGE = 0xffffffff

    def __init__(self, triggerPort, echoPort, nSamples=5):
        '''
        Constructor
        @param triggerPort: Port number of the trigger signal
        @param echoPort: Port number of the echo port
        @param nSamples: Number of samples to measure the distance
        '''

        self._nSamples = nSamples

        #Configure ports
        self._trigger = Gpio(triggerPort, Gpio.OUT)
        self._trigger.setValue(Gpio.LOW)

        self._echo = Gpio(echoPort, Gpio.IN)

        time.sleep(2)

    def read(self):
        '''
        Measures distance
        @return: Distance as centimeters
        '''

        MAX_POLL = 1000
        i = 0
        dist = 0

        while i < self._nSamples and dist != Ultrasonic.OUT_OF_RANGE:
            self._trigger.setValue(Gpio.HIGH)
            time.sleep(0.001)
            self._trigger.setValue(Gpio.LOW)

            #TODO: use system's poll mechanism or event to wait for GPIO-level change
            nPoll = 0
            while nPoll < MAX_POLL and self._echo.getValue() == Gpio.LOW:
                nPoll += 1

            if nPoll == MAX_POLL:
                raise ReadGpioException(
                    "Max poll reached: waiting for echo HIGH")

            pulseStart = time.time()

            nPoll = 0
            while nPoll < MAX_POLL and self._echo.getValue() == Gpio.HIGH:
                nPoll += 1

            if nPoll == MAX_POLL:
                raise ReadGpioException(
                    "Max poll reached: waiting for echo LOW")

            pulseEnd = time.time()

            pulseDuration = pulseEnd - pulseStart
            distSample = round(pulseDuration * Ultrasonic.PULSE2CM, 0)  #cm

            if distSample < Ultrasonic.MAX_RANGE:
                dist = (dist + distSample) / 2.0 if i != 0 else distSample
            else:
                dist = Ultrasonic.OUT_OF_RANGE

            i += 1

        return dist

    def cleanup(self):
        '''
        Frees ressources
        '''

        self._trigger.cleanup()
        self._echo.cleanup()