def set_col_addr(col_start, col_end):
    GPIO.output(led, GPIO.LOW)
    D0 = 0x21
    D1 = col_start & 0x7f
    D2 = col_end & 0x7f
    myData = [D0, D1, D2]
    spi.writebytes(myData)
def oledprintf(ch):
    start_col = 0
    page2_3 = 0

    for j in range(len(ch)):
        if ch[j] != '\n' and ch[j] != '\r':
            mychar = [None] * 12
            tmp = ord(ch[j]) - ord(' ')
            tmp1 = (tmp << 3) + (tmp << 2)  #  tmp x 12
            for i in range(12):
                mychar[i] = font7x14[tmp1]
                tmp1 += 1
            end_col = start_col + 5
            set_col_addr(start_col, end_col)
            if page2_3 == 0:
                set_page_addr(0, 1)
            else:
                set_page_addr(2, 3)

            GPIO.output(led, GPIO.HIGH)
            spi.writebytes(mychar)
            start_col += 7
        else:
            page2_3 = 1
            start_col = 0
        j += 1
def set_page_addr(page_start, page_end):
    GPIO.output(led, GPIO.LOW)
    D0 = 0x22
    D1 = page_start & 0x3
    D2 = page_end & 0x3
    myData = [D0, D1, D2]
    spi.writebytes(myData)
Beispiel #4
0
    def testWaitForEventSwitchbounce(self):
        self.finished = False

        def bounce():
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            time.sleep(0.01)
            GPIO.output(LOOP_OUT, GPIO.LOW)
            time.sleep(0.01)
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            time.sleep(0.01)
            GPIO.output(LOOP_OUT, GPIO.LOW)
            time.sleep(0.2)
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            time.sleep(0.01)
            GPIO.output(LOOP_OUT, GPIO.LOW)
            time.sleep(0.01)
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            time.sleep(0.01)
            GPIO.output(LOOP_OUT, GPIO.LOW)
            self.finished = True

        GPIO.output(LOOP_OUT, GPIO.LOW)
        t1 = Timer(0.1, bounce)
        t1.start()

        starttime = time.time()
        GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=100)
        GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=100)
        finishtime = time.time()
        self.assertGreater(finishtime - starttime, 0.2)
        while not self.finished:
            time.sleep(0.1)
def ssd1306_init():
    GPIO.output(led, GPIO.LOW)
    myData = [
        0xa8, 0x3f, 0xd3, 0x0, 0x40, 0xa0, 0xc0, 0xda, 0x2, 0x81, 0x7f, 0xa4,
        0xa6, 0xd5, 0x80, 0x8d, 0x14, 0xaf
    ]
    spi.writebytes(myData)
Beispiel #6
0
def GPIO_IO_TESTING():
    print('== Testing GPIO INPUT/OUTPUT ==')
    for mode in ['phys', 'TB', 'BCM']:
        GPIO.setmode(modeMap[mode])
        LPin = [pinTable[pins[0] - 1][mode] for pins in pairPins]
        RPin = [pinTable[pins[1] - 1][mode] for pins in pairPins]
        if (-1 in LPin or -1 in RPin):
            print('Some pins use the 3.3V or GND pin.')
            exit()
        for IPin, OPin in [(LPin, RPin), (RPin, LPin)]:
            GPIO.setup(IPin, GPIO.IN)
            GPIO.setup(OPin, GPIO.OUT)
            if (False in [GPIO.gpio_function(pin) == GPIO.IN for pin in IPin]
                    or False
                    in [GPIO.gpio_function(pin) == GPIO.OUT for pin in OPin]):
                print('Check GPIO.gpio_function or GPIO.setup.')
                exit()
            for volt in [GPIO.HIGH, GPIO.LOW]:
                GPIO.output(OPin, volt)
                OResult = [GPIO.input(pin) == volt for pin in OPin]
                IResult = [
                    GPIO.input(IPin[i]) == GPIO.input(OPin[i])
                    for i in range(len(IPin))
                ]
                if (False in OResult):
                    print('Check Pin[%d].' % (OPin[OResult.index(False)]))
                    exit()
                if (False in IResult):
                    print('Check Pin[%d].' % (IPin[IResult.index(False)]))
                    exit()
        print("[PASS] GPIO.setmode(%s)" % (modeNameMap[mode]))
        GPIO.cleanup()
    print('===============================')
 def __init__(self, dev='/dev/spidev2.0', spd=1000000):
     spi.openSPI(device=dev, speed=spd)
     GPIO.setmode(GPIO.BOARD)
     GPIO.setup(self.NRSTPD, GPIO.OUT)
     GPIO.setup(18, GPIO.OUT)
     GPIO.output(self.NRSTPD, 1)
     self.MFRC522_Init()
Beispiel #8
0
def toggle(stat):
    global status
    status = stat
    if status == "on":
        GPIO.output(11, True)
    elif status == "off":
        GPIO.output(11, False)
def ssd1306_test():
    GPIO.output(led, GPIO.HIGH)
    spi.open(2, 0)
    spi.max_speed_hz = 5000000
    spi.mode = 0b00
    spi.bits_per_word = 8
    spi.cshigh = False
    spi.lsbfirst = False
    ssd1306_init()

    # set_page_mode()
    set_horizontal_mode()
    set_col_addr(0, 127)
    set_page_addr(0, 3)

    GPIO.output(led, GPIO.HIGH)
    for j in range(4):
        i = 0
        while i < 128:
            myData = [0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81]
            spi.writebytes(myData)
            i = i + 8

    time.sleep(2)
    clearDisplay()
    oledascii()
    time.sleep(2)
    clearDisplay()
    oledprintf("This is a test !\nIt works !\n")
    spi.close()
def oledascii():
    for k in range(3):
        start_col = 0
        for j in range(32):  # 16 characters per two pages
            mychar = [None] * 12
            tmp = j + (k << 5)
            tmp1 = (tmp << 3) + (tmp << 2)  # tmp x 12
            for i in range(12):
                mychar[i] = font7x14[tmp1]
                tmp1 += 1

            end_col = start_col + 5
            set_col_addr(start_col, end_col)
            if j < 16:
                set_page_addr(0, 1)
            else:
                set_page_addr(2, 3)

            GPIO.output(led, GPIO.HIGH)
            spi.writebytes(mychar)
            start_col += 7
            if start_col >= 112:
                start_col = 0

        time.sleep(2)
        clearDisplay()
def clearDisplay():
    set_col_addr(0, 127)
    set_page_addr(0, 3)
    GPIO.output(led, GPIO.HIGH)
    for j in range(4):
        for k in range(8):
            myData = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
            spi.writebytes(myData)
Beispiel #12
0
    def testWaitForRising(self):
        def makehigh():
            GPIO.output(LOOP_OUT, GPIO.HIGH)

        GPIO.output(LOOP_OUT, GPIO.LOW)
        t = Timer(0.1, makehigh)
        t.start()
        GPIO.wait_for_edge(LOOP_IN, GPIO.RISING)
Beispiel #13
0
    def testWaitForFalling(self):
        def makelow():
            GPIO.output(LOOP_OUT, GPIO.LOW)

        GPIO.output(LOOP_OUT, GPIO.HIGH)
        t = Timer(0.1, makelow)
        t.start()
        GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING)
Beispiel #14
0
def ProjectorOnOffSwitch(pin, state):
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(5, GPIO.OUT)
    if state == 'off':
        GPIO.output(5, GPIO.HIGH)
    if state == 'on':
        GPIO.output(5, GPIO.LOW)
    return True
def glow(col, r1, r2, r3, r4):
    GPIO.output(24 - col, 1)
    GPIO.output(pin1, 1 - r1)
    GPIO.output(pin2, 1 - r2)
    GPIO.output(pin3, 1 - r3)
    GPIO.output(pin4, 1 - r4)
    time.sleep(delay)
    off()
Beispiel #16
0
    def pinSonar2(self, trig,echo):
        #print pin
        #print self.pinUse[pin]         
        self.pinUse[trig] = self.PSONAR
        self.pinUse[echo] = self.PSONAR        
        GPIO.setup(trig,GPIO.OUT)
        GPIO.setup(echo,GPIO.OUT)        
        ti = time.time()
        # setup a list to hold 3 values and then do 3 distance calcs and store them
        #print 'sonar started'
        distlist = [0.0,0.0,0.0,0.0,0.0]
        ts=time.time()
        for k in range(5):
            #print "sonar pulse" , k
            GPIO.output(trig, 1)    # Send Pulse high
            time.sleep(0.00001)     #  wait
            GPIO.output(trig, 0)  #  bring it back low - pulse over.
            t0=time.time() # remember current time
            GPIO.setup(echo,GPIO.IN)
            #PIN_USE[i] = PINPUT don't bother telling system
            
            t1=t0
            # This while loop waits for input pin (7) to be low but with a 0.04sec timeout 
            while ((GPIO.input(echo)==0) and ((t1-t0) < 0.02)):
                #time.sleep(0.00001)
                t1=time.time()
            t1=time.time()
            #print 'low' , (t1-t0).microseconds
            t2=t1
            #  This while loops waits for input pin to go high to indicate pulse detection
            #  with 0.04 sec timeout
            while ((GPIO.input(echo)==1) and ((t2-t1) < 0.02)):
                #time.sleep(0.00001)
                t2=time.time()
            t2=time.time()
            #print 'high' , (t2-t1).microseconds
            t3=(t2-t1)  # t2 contains time taken for pulse to return
            #print "total time " , t3
            distance=t3*343/2*100  # calc distance in cm
            distlist[k]=distance
            #print distance
            GPIO.setup(echo,GPIO.OUT)
        tf = time.time() - ts
        distance = sorted(distlist)[1] # sort the list and pick middle value as best distance
        
        #print "total time " , tf
        #for k in range(5):
            #print distlist[k]
        #print "pulse time" , distance*58
        #print "total time in microsecs" , (tf-ti).microseconds                    
        # only update Scratch values if distance is < 500cm
        if (distance > 280):
            distance = 299
        if (distance < 2):
            distance = 1

        return distance        
Beispiel #17
0
def GetCH0():
    GPIO.output(CS, GPIO.LOW)  #start
    spi.writebytes([0x06])
    temp = spi.readbytes(0x00)[0] & 0x0F  # 0000 1111
    temp = temp << 8  # yyyy 0000 0000
    temp = temp | spi.readbytes(0)[0]
    sleep(0.0001)
    GPIO.output(CS, GPIO.HIGH)  #stop
    return temp
def sensorData():
    readPin = 0
    GPIO.setup(photoPin, GPIO.OUT)
    GPIO.output(photoPin, GPIO.LOW)
    time.sleep(0.1)

    GPIO.setup(photoPin, GPIO.IN)

    while (GPIO.input(photoPin) == GPIO.LOW):
        readPin += 1
    return readPin
    def setUp(self):
        # GPIO.setmode(GPIO.BCM)

        GPIO.setup(self.in1, GPIO.OUT)
        GPIO.setup(self.in2, GPIO.OUT)
        GPIO.setup(self.en, GPIO.OUT)
        GPIO.output(self.in1, GPIO.LOW)
        GPIO.output(self.in2, GPIO.LOW)
        self.p = GPIO.PWM(self.en, 1000)

        self.p.start(self.speeds[1])
Beispiel #20
0
def glass(command):
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(3, GPIO.OUT)
    print "in glass function", command
    if command == "opaque":
        GPIO.output(3, GPIO.LOW)
        stm.sleep(1)
    if command == "transparent":
        GPIO.output(3, GPIO.HIGH)
        stm.sleep(1)
    return True
Beispiel #21
0
 def __init__(self, doorId, config):
     self.id = doorId
     self.name = config['name']
     self.relay_pin = config['relay_pin']
     self.state_pin = config['state_pin']
     self.state_pin_closed_value = config.get('state_pin_closed_value', 0)
     self.time_to_close = config.get('time_to_close', 10)
     self.time_to_open = config.get('time_to_open', 10)
     self.openhab_name = config.get('openhab_name')
     self.open_time = time.time()
     gpio.setup(self.relay_pin, gpio.OUT)
     gpio.setup(self.state_pin, gpio.IN, pull_up_down=gpio.PUD_UP)
     gpio.output(self.relay_pin, False)
    def MFRC522_Init(self):
        GPIO.output(self.NRSTPD, 1)

        self.MFRC522_Reset()

        self.Write_MFRC522(self.TModeReg, 0x8D)
        self.Write_MFRC522(self.TPrescalerReg, 0x3E)
        self.Write_MFRC522(self.TReloadRegL, 30)
        self.Write_MFRC522(self.TReloadRegH, 0)

        self.Write_MFRC522(self.TxAutoReg, 0x40)
        self.Write_MFRC522(self.ModeReg, 0x3D)
        self.AntennaOn()
Beispiel #23
0
    def testAlternateWaitForEdge(self):
        def makehigh():
            GPIO.output(LOOP_OUT, GPIO.HIGH)

        def makelow():
            GPIO.output(LOOP_OUT, GPIO.LOW)

        GPIO.output(LOOP_OUT, GPIO.LOW)
        t = Timer(2.0, makehigh)
        t2 = Timer(2.0, makelow)
        t.start()
        t2.start()
        GPIO.wait_for_edge(LOOP_IN, GPIO.RISING)
        GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING)
Beispiel #24
0
    def toggle_relay(self):
        state = self.get_state()
        if (state == 'open'):
            self.last_action = 'close'
            self.last_action_time = time.time()
        elif state == 'closed':
            self.last_action = 'open'
            self.last_action_time = time.time()
        else:
            self.last_action = None
            self.last_action_time = None

        gpio.output(self.relay_pin, True)
        time.sleep(0.2)
        gpio.output(self.relay_pin, False)
Beispiel #25
0
    def testExceptionInCallback(self):
        self.run_cb = False

        def cb(channel):
            with self.assertRaises(ZeroDivisionError):
                self.run_cb = True
                a = 1 / 0

        GPIO.output(LOOP_OUT, GPIO.LOW)
        GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb)
        time.sleep(0.01)
        GPIO.output(LOOP_OUT, GPIO.HIGH)
        time.sleep(0.01)
        self.assertEqual(self.run_cb, True)
        GPIO.remove_event_detect(LOOP_IN)
Beispiel #26
0
    def testWaitForEdgeInLoop(self):
        def makelow():
            GPIO.output(LOOP_OUT, GPIO.LOW)

        count = 0
        timestart = time.time()
        GPIO.output(LOOP_OUT, GPIO.HIGH)
        while True:
            t = Timer(0.1, makelow)
            t.start()
            GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING)
            GPIO.output(LOOP_OUT, GPIO.HIGH)
            count += 1
            if time.time() - timestart > 5 or count > 150:
                break
def button_test():
    print "Push button 10 times.\r\n"
    old_state = 0
    current_state = 0
    i = 0
    while i < 10:
        current_state = GPIO.input(switch)
        if old_state == 0 and current_state == 1:
            GPIO.output(led, GPIO.HIGH)
            old_state = current_state
        elif old_state == 1 and current_state == 0:
            GPIO.output(led, GPIO.LOW)
            old_state = current_state
            i += 1
        time.sleep(0.05)
Beispiel #28
0
 def write(self):
     self.runtime += 1
     o = self.orientation % 4
     for i in range(0, self.height):
         for j in range(0, self.width):
             if (o == 0):
                 self.pushBit(self.screenData[i * 8 + j])
             elif (o == 1):
                 self.pushBit(self.screenData[j * 8 + (7 - i)])
             elif (o == 2):
                 self.pushBit(self.screenData[(7 - i) * 8 + (7 - j)])
             else:
                 self.pushBit(self.screenData[(7 - j) * 8 + i])
         self.pushRow(i)
         GPIO.output(self.STORE, 1)
         GPIO.output(self.STORE, 0)
Beispiel #29
0
    def testWaitForEdgeWithCallback(self):
        def cb():
            raise Exception("Callback should not be called")

        def makehigh():
            GPIO.output(LOOP_OUT, GPIO.HIGH)

        GPIO.output(LOOP_OUT, GPIO.LOW)
        t = Timer(0.1, makehigh)

        GPIO.add_event_detect(LOOP_IN, GPIO.RISING)
        t.start()
        GPIO.wait_for_edge(LOOP_IN, GPIO.RISING)

        GPIO.output(LOOP_OUT, GPIO.LOW)
        GPIO.add_event_callback(LOOP_IN, callback=cb)
        with self.assertRaises(RuntimeError):  # conflicting edge exception
            GPIO.wait_for_edge(LOOP_IN, GPIO.RISING)

        GPIO.remove_event_detect(LOOP_IN)
Beispiel #30
0
    def __init__(self, gpio_trigger, gpio_echo, range_min=10, range_max=400):

        GPIO.setmode(GPIO.BCM)

        self._gpio_trigger = gpio_trigger
        self._gpio_echo = gpio_echo
        self._range_min = range_min
        self._range_max = range_max
        self._is_reading = False

        self._speed_sound = 17150.0  #- divided by 2 in cm/s

        self._last_time_reading = 0
        self._timeout = range_max / self._speed_sound * 2

        GPIO.setup(gpio_trigger, GPIO.OUT)
        GPIO.setup(gpio_echo, GPIO.IN)

        #- Waiting for sensor to settle
        GPIO.output(gpio_trigger, GPIO.LOW)
        time.sleep(1)