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 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)
def GPIO_PULL_UPDW_TESTING(): checkPins = [] print('== Testing GPIO PULL_UP_DOWN ==') testPin = gpioUsedPins print("Check that nothing connects to those pins: %s" % (','.join([str(x) for x in testPin]))) GPIO.setmode(GPIO.BOARD) GPIO.setup(testPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) for pin in testPin: if (GPIO.input(pin) != InternalPullUpDnValue[PullUpDnPins[pin] if pin in PullUpDnPins else GPIO.PUD_UP]): checkPins.append(pin) GPIO.setup(testPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) for pin in testPin: if (GPIO.input(pin) != InternalPullUpDnValue[PullUpDnPins[pin] if pin in PullUpDnPins else GPIO.PUD_DOWN]): checkPins.append(pin) print("[%s] Pull Up and Down" % ('PASS' if len(checkPins) <= 0 else 'FAILED')) if (len(checkPins) > 0): print('Please check those pins: %s' % (','.join([str(x) for x in checkPins]))) GPIO.cleanup() print('===============================')
def __init__(self): self.setPinMapping() GPIOPort.__init__(self, max(self.pins) + 1) self.post_value = True self.post_function = True self.gpio_setup = [] self.gpio_reset = [] self.gpio_map = None self.pinFunctionSet = set() self.valueFile = {pin:None for pin in self.pins} self.functionFile = {pin:None for pin in self.pins} self.callbacks = {} self.edge_poll = select.epoll() thread = Thread(target=self.pollEdges, daemon=True) thread.start() for pin in self.pins: # Export the pins here to prevent a delay when accessing the values for the # first time while waiting for the file group to be set self.__checkFilesystemExport__(pin) if gpio_library: gpio_library.setmode(gpio_library.ASUS) elif not Hardware().isRaspberryPi3(): # On the Pi 3 the memory mapped /dev/gpiomem file seems to give strange, inconsistent readings, like duplicated # 4 byte sequences and "oipg" ASCII values. This might be some issue with the way Python mmap works since it didn't # seem to happen with the wiringPi C library using uint32_t pointers. For now we just avoid using /dev/gpiomem on Pi 3. try: with open('/dev/gpiomem', 'rb') as gpiomem: self.gpio_map = mmap.mmap(gpiomem.fileno(), BLOCK_SIZE, prot=mmap.PROT_READ) except FileNotFoundError: pass except OSError as err: error(err)
def toggle(stat): global status status = stat if status == "on": GPIO.output(11, True) elif status == "off": GPIO.output(11, False)
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 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 attach_interrupts(self): if self.return_config_bool("pcb") and GPIO.input( self.check_pin ) == GPIO.LOW: # check if there is an pcb and if so attach the interrupts GPIO.add_event_detect(self.check_pin, GPIO.RISING, callback=self.pcb_interrupt ) # if not the interrupt gets attached if GPIO.input( self.power_pin ) == GPIO.HIGH: #when the system gets startet in the on position it gets shutdown print('shutting down') os.system("sudo shutdown -h now") else: self.led(1) GPIO.add_event_detect(self.reset_pin, GPIO.FALLING, callback=self.reset_interrupt) GPIO.add_event_detect(self.power_pin, GPIO.RISING, callback=self.power_interrupt) else: #no pcb attached so lets exit print('no PCB detected') GPIO.cleanup() exit()
def __init__(self): self.setPinMapping() GPIOPort.__init__(self, max(self.pins) + 1) self.post_value = True self.post_function = True self.gpio_setup = [] self.gpio_reset = [] self.gpio_map = None self.pinFunctionSet = set() self.valueFile = {pin: None for pin in self.pins} self.functionFile = {pin: None for pin in self.pins} for pin in self.pins: # Export the pins here to prevent a delay when accessing the values for the # first time while waiting for the file group to be set self.__checkFilesystemExport__(pin) if gpio_library: gpio_library.setmode(gpio_library.ASUS) else: try: with open('/dev/gpiomem', 'rb') as gpiomem: self.gpio_map = mmap.mmap(gpiomem.fileno(), BLOCK_SIZE, prot=mmap.PROT_READ) except FileNotFoundError: pass except OSError as err: error(err)
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)
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()
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)
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)
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)
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 testInvalidBouncetime(self): with self.assertRaises(ValueError): GPIO.add_event_detect(LOOP_IN, GPIO.RISING, bouncetime=-1) with self.assertRaises(ValueError): GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=-1) GPIO.add_event_detect(LOOP_IN, GPIO.RISING, bouncetime=123) with self.assertRaises(RuntimeError): GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=321) GPIO.remove_event_detect(LOOP_IN)
def power_interrupt(self, channel): time.sleep(self.debounce_time) # debounce if GPIO.input(self.power_pin) == GPIO.HIGH and GPIO.input( self.check_pin ) == GPIO.LOW: # shutdown function if the powerswitch is toggled self.led(0) # led and fan off os.system("killall emulationstation") #end emulationstation self.blink(20, 0.1) #wait for the metadata to be safed self.fan(0) os.system("sudo shutdown -h now")
def __init__(self, headset): Thread.__init__(self) self.hs = headset self.finish = False GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(self.DIAL_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(self.PULSE_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(self.HOOK_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
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()
def output(self): """Input button event""" try: if not GPIO.input(self.BTN_input) and not self.prev_input: print("click") self.prev_input = True return True # elif not GPIO.input(self.BTN_input) and self.prev_input: else: self.prev_input = False except KeyboardInterrupt: GPIO.cleanup() # Get a frame from the video source return False
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()
def __init__(self, LED_OK, LED_NG, BTN_input): """ Mamos setup""" self.LED_OK = LED_OK self.LED_NG = LED_NG self.BTN_input = BTN_input self.prev_input = False try: GPIO.setwarnings(False) GPIO.setmode(GPIO.ASUS) GPIO.setup(LED_OK, GPIO.OUT) GPIO.setup(LED_NG, GPIO.OUT) GPIO.setup(BTN_input, GPIO.IN, pull_up_down=GPIO.PUD_UP) except: pass
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)
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 __init__(self): rospy.init_node('l298_motorcontroller_node') GPIO.setwarnings(False) GPIO.setmode(GPIO.ASUS) wheel_radius = 0.1 ticks_pr_round = 2.0 gear = 50.0 wheel_circumference = 2.0 * math.pi * wheel_radius * gear m_pr_tick = wheel_circumference/ticks_pr_round left_motor = SingleMotorController( 238, 185, 224, 168, m_pr_tick ) right_motor = None #SingleMotorController( 239, 223, 187, 188, m_pr_tick ) DifferentialDriveMotorController(left_motor, right_motor, 0.6)
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)
def SwitchInterrupt(switch): #rotary will make a noise at the end #SO it have to make a timer for bouncing global buttonStatus, start_time, end_time, rotary_mode if start_time == 0: start_time = time.time() while 1: if GPIO.input(switch) == 0: end_time = time.time() break else: start_time == 0 return buttonTime = end_time - start_time if 2 > buttonTime > 0.01: #for wrong interrupt rotary_mode += 1 if (rotary_mode > 4): rotary_mode = 0 packet = bytearray([0xFF, 0X55, 0x0, 0x7, 0x1, rotary_mode]) _LCD.write(packet) elif buttonTime >= 5: AudioOutputMute() packet = bytearray([0xFF, 0X55, 0x0, 0x0, 0x1, 0x0]) #LCD OFF _LCD.write(packet) print('power off!!') os.system("shutdown now -h") else: start_time = 0 return start_time = 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)
def LeerSensor(self): try: if (GPIO.input(self.PirSensor)): return True else: return False except: return False