def main(): # mcp3008 button reader setup # create software spi spi = bitbangio.SPI(board.D11, MISO=board.D9, MOSI=board.D10) # create the cs (chip select) cs = digitalio.DigitalInOut(board.D22) # create the mcp object mcp = MCP.MCP3008(spi, cs) # create analog input channels on pins 6 and 7 of the mcp3008 chan1 = AnalogIn(mcp, MCP.P6) chan2 = AnalogIn(mcp, MCP.P7) # setup rotary encoder in pigpio pi = pigpio.pi() # init pigpio deamon pi.set_mode(Enc_A, pigpio.INPUT) pi.set_pull_up_down(Enc_A, pigpio.PUD_UP) pi.set_mode(Enc_B, pigpio.INPUT) pi.set_pull_up_down(Enc_B, pigpio.PUD_UP) pi.callback(Enc_A, pigpio.EITHER_EDGE, rotary_interrupt) pi.callback(Enc_B, pigpio.EITHER_EDGE, rotary_interrupt) # main loop while True: # read button states if 0 <= chan1.value <= 1000: button_interrupt("Timer") if 5900 <= chan1.value <= 7000: button_interrupt("Time Adj") if 12000 <= chan1.value <= 13000: button_interrupt("Daily") if 0 <= chan2.value <= 1000: button_interrupt("Power") if 5800 <= chan2.value <= 6100: button_interrupt("Band") if 13000 <= chan2.value <= 14000: button_interrupt("Function") if 26000 <= chan2.value <= 27000: button_interrupt("Enter") if 19000 <= chan2.value <= 21000: button_interrupt("Info") if 39000 <= chan2.value <= 41000: button_interrupt("Auto Tuning") if 33000 <= chan2.value <= 34000: button_interrupt("Memory") if 44000 <= chan2.value <= 46000: button_interrupt("Dimmer")
def __init__(self, num_led=8, order='rgb', mosi=10, sclk=11, ce=None, bus_speed_hz=8000000): """Initializes the library :param num_led: Number of LEDs in the strip :param order: Order in which the colours are addressed (this differs from strip to strip) :param mosi: Master Out pin. Use 10 for SPI0, 20 for SPI1, any GPIO pin for bitbang. :param sclk: Clock, use 11 for SPI0, 21 for SPI1, any GPIO pin for bitbang. :param ce: GPIO to use for Chip select. Can be any free GPIO pin. Warning: This will slow down the bus significantly. Note: The hardware CE0 and CE1 are not used :param bus_speed_hz: Speed of the hardware SPI bus. If glitches on the bus are visible, lower the value. """ self.num_led = num_led order = order.lower() # Just in case someone use CAPS here. self.rgb = RGB_MAP.get(order, RGB_MAP['rgb']) self.global_brightness = 4 # This is a 5 bit value, i.e. from 0 to 31. Conservative 1/8th, change if desired. self.use_bitbang = False # Two raw SPI devices exist: Bitbang (software) and hardware SPI. self.use_ce = False # If true, use the BusDevice abstraction layer on top of the raw SPI device self.leds = [self.LED_START, 0, 0, 0] * self.num_led # Pixel buffer if ce is not None: # If a chip enable value is present, use the Adafruit CircuitPython BusDevice abstraction on top # of the raw SPI device (hardware or bitbang) # The next line is just here to prevent an "unused" warning from the IDE digitalio.DigitalInOut(board.D1) # Convert the chip enable pin number into an object (reflection à la Python) ce = eval("digitalio.DigitalInOut(board.D" + str(ce) + ")") self.use_ce = True # Heuristic: Test for the hardware SPI pins. If found, use hardware SPI, otherwise bitbang SPI if mosi == 10: if sclk != 11: raise ValueError("Illegal MOSI / SCLK combination") self.spi = busio.SPI(clock=board.SCLK, MOSI=board.MOSI) elif mosi == 20: if sclk != 21: raise ValueError("Illegal MOSI / SCLK combination") self.spi = busio.SPI(clock=board.SCLK_1, MOSI=board.MOSI_1) else: # Use Adafruit CircuitPython BitBangIO, because the pins do not match one of the hardware SPI devices # Reflection à la Python to get at the digital IO pins self.spi = bitbangio.SPI(clock=eval("board.D" + str(sclk)), MOSI=eval("board.D" + str(mosi))) self.use_bitbang = True # Add the BusDevice on top of the raw SPI if self.use_ce: self.spibus = SPIDevice(spi=self.spi, chip_select=ce, baudrate=bus_speed_hz) else: # If the BusDevice is not used, the bus speed is set here instead while not self.spi.try_lock(): # Busy wait to acquire the lock pass self.spi.configure(baudrate=bus_speed_hz) self.spi.unlock() # Debug if self.use_ce: print("Use software chip enable") if self.use_bitbang: print("Use bitbang SPI") else: print("Use hardware SPI")
def main(): # create menu as in example3 menu = RpiLCDMenu(7, 8, [25, 24, 23, 15]) menu.append_item( MessageItem( 'message item', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut ' 'labore et dolore magna aliqua.', menu, True)) menu.start() # setup the rotary encoder pins rot_clk = 17 rot_dt = 27 # set GPIO pins GPIO.setmode(GPIO.BCM) # set to GPIO.PUD_UP as the TEAC panel uses an encoder that is grounded GPIO.setup(rot_clk, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(rot_dt, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set default counter variable counter = 0 clkLastState = GPIO.input(rot_clk) # mcp3008 button reader setup # create software spi spi = bitbangio.SPI(board.D11, MISO=board.D9, MOSI=board.D10) # create the cs (chip select) cs = digitalio.DigitalInOut(board.D22) # create the mcp object mcp = MCP.MCP3008(spi, cs) # create analog input channels on pins 6 and 7 of the mcp3008 chan1 = AnalogIn(mcp, MCP.P6) chan2 = AnalogIn(mcp, MCP.P7) # main loop while True: # read button states if 0 <= chan1.value <= 1000: print("Timer button pressed" + str(chan1.value)) time.sleep(0.5) if 5900 <= chan1.value <= 7000: print("Time Adj button pressed" + str(chan1.value)) time.sleep(0.5) if 12000 <= chan1.value <= 13000: print("Daily button pressed" + str(chan1.value)) time.sleep(0.5) if 0 <= chan2.value <= 1000: print("Power button pressed" + str(chan2.value)) time.sleep(0.5) if 5800 <= chan2.value <= 6100: print("Band button pressed " + str(chan2.value)) time.sleep(0.5) if 13000 <= chan2.value <= 14000: print("Function button pressed" + str(chan2.value)) time.sleep(0.5) if 26000 <= chan2.value <= 27000: print("Enter button pressed" + str(chan2.value)) menu = menu.processEnter() time.sleep(0.5) if 19000 <= chan2.value <= 21000: print("Info button pressed" + str(chan2.value)) time.sleep(0.5) if 39000 <= chan2.value <= 41000: print("Auto Tuning button pressed" + str(chan2.value)) time.sleep(0.5) if 33000 <= chan2.value <= 34000: print("Memory button pressed" + str(chan2.value)) time.sleep(0.5) if 44000 <= chan2.value <= 46000: print("Dimmer button pressed" + str(chan2.value)) time.sleep(0.5) # buttons depressed # read rotary encoder states clkState = GPIO.input(rot_clk) dtState = GPIO.input(rot_dt) if clkState != clkLastState: if dtState != clkState: counter += 1 menu = menu.processUp() else: counter -= 1 menu = menu.processDown() print(counter) clkLastState = clkState
""" This example is for demonstrating how to retrieving the board ID from a BME280, which is stored in register 0xD0. It should return a result of [96] """ import board import digitalio import adafruit_bitbangio as bitbangio # Change these to the actual connections SCLK_PIN = board.D6 MOSI_PIN = board.D17 MISO_PIN = board.D18 CS_PIN = board.D5 cs = digitalio.DigitalInOut(CS_PIN) cs.switch_to_output(value=True) spi = bitbangio.SPI(SCLK_PIN, MOSI=MOSI_PIN, MISO=MISO_PIN) cs.value = 0 while not spi.try_lock(): pass spi.write([0xD0]) data = [0x00] spi.readinto(data) spi.unlock() cs.value = 1 print("Result is {}".format(data))
def main(): # setup the rotary encoder pins rot_clk = 17 rot_dt = 27 # set GPIO pins GPIO.setmode(GPIO.BCM) # set to GPIO.PUD_UP as the TEAC panel uses an encoder that is grounded GPIO.setup(rot_clk, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(rot_dt, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set default counter variable counter = 0 clkLastState = GPIO.input(rot_clk) # mcp3008 button reader setup # create software spi spi = bitbangio.SPI(board.D11, MISO=board.D9, MOSI=board.D10) # create the cs (chip select) cs = digitalio.DigitalInOut(board.D22) # create the mcp object mcp = MCP.MCP3008(spi, cs) # create analog input channels on pins 6 and 7 of the mcp3008 chan1 = AnalogIn(mcp, MCP.P6) chan2 = AnalogIn(mcp, MCP.P7) # main loop while True: # read button states if 0 <= chan1.value <= 1000: print("Timer button pressed" + str(chan1.value)) time.sleep(0.5) if 5900 <= chan1.value <= 7000: print("Time Adj button pressed" + str(chan1.value)) time.sleep(0.5) if 12000 <= chan1.value <= 13000: print("Daily button pressed" + str(chan1.value)) time.sleep(0.5) if 0 <= chan2.value <= 1000: print("Power button pressed" + str(chan2.value)) time.sleep(0.5) if 5800 <= chan2.value <= 6100: print("Band button pressed " + str(chan2.value)) time.sleep(0.5) if 13000 <= chan2.value <= 14000: print("Function button pressed" + str(chan2.value)) time.sleep(0.5) if 26000 <= chan2.value <= 27000: print("Enter button pressed" + str(chan2.value)) time.sleep(0.5) if 19000 <= chan2.value <= 21000: print("Info button pressed" + str(chan2.value)) time.sleep(0.5) if 39000 <= chan2.value <= 41000: print("Auto Tuning button pressed" + str(chan2.value)) time.sleep(0.5) if 33000 <= chan2.value <= 34000: print("Memory button pressed" + str(chan2.value)) time.sleep(0.5) if 44000 <= chan2.value <= 46000: print("Dimmer button pressed" + str(chan2.value)) time.sleep(0.5) # buttons depressed # if chan1.value > 65000: # print("Channel 1 depressed " + str(chan1.value)) # if chan2.value > 65000: # print ("Channel 2 depressed " + str(chan2.value)) # time.sleep(0.2) # read rotary encoder states clkState = GPIO.input(rot_clk) dtState = GPIO.input(rot_dt) if clkState != clkLastState: if dtState != clkState: counter += 1 else: counter -= 1 print(counter) clkLastState = clkState
def main(): # create menu as in example3 menu = RpiLCDMenu(7, 8, [25, 24, 23, 15]) function_item1 = FunctionItem("Item 1", fooFunction, [1]) function_item2 = FunctionItem("Item 2", fooFunction, [2]) menu.append_item(function_item1).append_item(function_item2) submenu = RpiLCDSubMenu(menu) submenu_item = SubmenuItem("SubMenu (3)", submenu, menu) menu.append_item(submenu_item) submenu.append_item(FunctionItem("Item 31", fooFunction, [31])).append_item( FunctionItem("Item 32", fooFunction, [32])) submenu.append_item(FunctionItem("Back", exitSubMenu, [submenu])) menu.append_item(FunctionItem("Item 4", fooFunction, [4])) menu.start() menu.debug() print("----") # press first menu item and scroll down to third one # menu.processEnter().processDown().processDown() # # enter submenu, press Item 32, press Back button # menu.processEnter().processDown().processEnter().processDown().processEnter() # # press item4 back in the menu # menu.processDown().processEnter() # setup the rotary encoder pins rot_clk = 17 rot_dt = 27 # set GPIO pins GPIO.setmode(GPIO.BCM) # set to GPIO.PUD_UP as the TEAC panel uses an encoder that is grounded GPIO.setup(rot_clk, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(rot_dt, GPIO.IN, pull_up_down=GPIO.PUD_UP) # set default counter variable counter = 0 clkLastState = GPIO.input(rot_clk) # mcp3008 button reader setup # create software spi spi = bitbangio.SPI(board.D11, MISO=board.D9, MOSI=board.D10) # create the cs (chip select) cs = digitalio.DigitalInOut(board.D22) # create the mcp object mcp = MCP.MCP3008(spi, cs) # create analog input channels on pins 6 and 7 of the mcp3008 chan1 = AnalogIn(mcp, MCP.P6) chan2 = AnalogIn(mcp, MCP.P7) # main loop while True: # read button states if 0 <= chan1.value <= 1000: print("Timer button pressed" + str(chan1.value)) time.sleep(0.5) if 5900 <= chan1.value <= 7000: print("Time Adj button pressed" + str(chan1.value)) time.sleep(0.5) if 12000 <= chan1.value <= 13000: print("Daily button pressed" + str(chan1.value)) time.sleep(0.5) if 0 <= chan2.value <= 1000: print("Power button pressed" + str(chan2.value)) time.sleep(0.5) if 5800 <= chan2.value <= 6100: print("Band button pressed " + str(chan2.value)) time.sleep(0.5) if 13000 <= chan2.value <= 14000: print("Function button pressed" + str(chan2.value)) time.sleep(0.5) if 26000 <= chan2.value <= 27000: print("Enter button pressed" + str(chan2.value)) menu = menu.processEnter() time.sleep(0.5) if 19000 <= chan2.value <= 21000: print("Info button pressed" + str(chan2.value)) time.sleep(0.5) if 39000 <= chan2.value <= 41000: print("Auto Tuning button pressed" + str(chan2.value)) time.sleep(0.5) if 33000 <= chan2.value <= 34000: print("Memory button pressed" + str(chan2.value)) time.sleep(0.5) if 44000 <= chan2.value <= 46000: print("Dimmer button pressed" + str(chan2.value)) time.sleep(0.5) # buttons depressed # read rotary encoder states clkState = GPIO.input(rot_clk) dtState = GPIO.input(rot_dt) if clkState != clkLastState: if dtState != clkState: counter += 1 menu = menu.processUp() else: counter -= 1 menu = menu.processDown() print(counter) clkLastState = clkState
def __init__(self, num_led=8, order='rgb', bus_method='spi', spi_bus=0, mosi=None, sclk=None, ce=None, bus_speed_hz=8000000, global_brightness=4): """Initializes the library :param num_led: Number of LEDs in the strip :param order: Order in which the colours are addressed (this differs from strip to strip) :param bus_method: select whether to use the (hardware) spi or to bitbang :param spi_bus: if bus_method is spi this selects the bus :param mosi: if bus_method is bitbang this sets the Master Out pin. :param sclk: if bus_method is bitbang this sets the Clock pin. :param ce: GPIO to use for Chip select. Can be any free GPIO pin. Warning: This will slow down the bus significantly. Note: The hardware CE0 and CE1 are not used :param bus_speed_hz: Speed of the hardware SPI bus. If glitches on the bus are visible, lower the value. :param global_brightness: This is a 5 bit value, i.e. from 0 to 31. """ spi_ports = {} for id_port, sclk_port, mosi_port, miso_port in spiPorts: spi_ports[id_port] = {'SCLK': sclk_port, 'MOSI': mosi_port, 'MISO': miso_port} # Just in case someone use CAPS here. order = order.lower() bus_method = bus_method.lower() self.check_input(bus_method, global_brightness, mosi, num_led, order, sclk, spi_bus, spi_ports) self.num_led = num_led self.rgb = RGB_MAP.get(order, RGB_MAP['rgb']) self.global_brightness = global_brightness self.use_bitbang = False # Two raw SPI devices exist: Bitbang (software) and hardware SPI. self.use_ce = False # If true, use the BusDevice abstraction layer on top of the raw SPI device self.leds = [self.LED_START, 0, 0, 0] * self.num_led # Pixel buffer if bus_method == 'spi': selected = spi_ports[spi_bus] self.spi = busio.SPI(clock=selected['SCLK'], MOSI=selected['MOSI']) elif bus_method == 'bitbang': self.spi = bitbangio.SPI(clock=eval("board.D" + str(sclk)), MOSI=eval("board.D" + str(mosi))) self.use_bitbang = True if ce is not None: # If a chip enable value is present, use the Adafruit CircuitPython BusDevice abstraction on top # of the raw SPI device (hardware or bitbang) # The next line is just here to prevent an "unused" warning from the IDE digitalio.DigitalInOut(board.D1) # Convert the chip enable pin number into an object (reflection à la Python) ce = eval("digitalio.DigitalInOut(board.D" + str(ce) + ")") self.use_ce = True # Add the BusDevice on top of the raw SPI if self.use_ce: self.spibus = SPIDevice(spi=self.spi, chip_select=ce, baudrate=bus_speed_hz) else: # If the BusDevice is not used, the bus speed is set here instead while not self.spi.try_lock(): # Busy wait to acquire the lock pass self.spi.configure(baudrate=bus_speed_hz) self.spi.unlock() # Debug if self.use_ce: print("Use software chip enable") if self.use_bitbang: print("Use bitbang SPI") else: print("Use hardware SPI")
def main(): # create menu as in example3 # global menu menu = RpiLCDMenu(7, 8, [25, 24, 23, 15]) function_item1 = FunctionItem("Item 1", fooFunction, [1]) function_item2 = FunctionItem("Item 2", fooFunction, [2]) menu.append_item(function_item1).append_item(function_item2) # global submenu submenu = RpiLCDSubMenu(menu) submenu_item = SubmenuItem("SubMenu (3)", submenu, menu) menu.append_item(submenu_item) submenu.append_item(FunctionItem("Item 31", fooFunction, [31])).append_item( FunctionItem("Item 32", fooFunction, [32])) submenu.append_item(FunctionItem("Back", exitSubMenu, [submenu])) menu.append_item(FunctionItem("Item 4", fooFunction, [4])) menu.start() menu.debug() print("----") # press first menu item and scroll down to third one # menu.processEnter().processDown().processDown() # # enter submenu, press Item 32, press Back button # menu.processEnter().processDown().processEnter().processDown().processEnter() # # press item4 back in the menu # menu.processDown().processEnter() # mcp3008 button reader setup # create software spi spi = bitbangio.SPI(board.D11, MISO=board.D9, MOSI=board.D10) # create the cs (chip select) cs = digitalio.DigitalInOut(board.D22) # create the mcp object mcp = MCP.MCP3008(spi, cs) # create analog input channels on pins 6 and 7 of the mcp3008 chan1 = AnalogIn(mcp, MCP.P6) chan2 = AnalogIn(mcp, MCP.P7) # setup rotary encoder in pigpio pi = pigpio.pi() # init pigpio deamon pi.set_mode(Enc_A, pigpio.INPUT) pi.set_pull_up_down(Enc_A, pigpio.PUD_UP) pi.set_mode(Enc_B, pigpio.INPUT) pi.set_pull_up_down(Enc_B, pigpio.PUD_UP) pi.callback(lambda x: rotary_interrupt( Enc_A, pigpio.EITHER_EDGE, tim=None, item=menu)) pi.callback(Enc_B, pigpio.EITHER_EDGE, rotary_interrupt) # main loop while True: # read button states if 0 <= chan1.value <= 1000: button_interrupt("Timer") if 5900 <= chan1.value <= 7000: button_interrupt("Time Adj") if 12000 <= chan1.value <= 13000: button_interrupt("Daily") if 0 <= chan2.value <= 1000: button_interrupt("Power") if 5800 <= chan2.value <= 6100: button_interrupt("Band") if 13000 <= chan2.value <= 14000: button_interrupt("Function") if 26000 <= chan2.value <= 27000: button_interrupt("Enter") if 19000 <= chan2.value <= 21000: button_interrupt("Info") if 39000 <= chan2.value <= 41000: button_interrupt("Auto Tuning") if 33000 <= chan2.value <= 34000: button_interrupt("Memory") if 44000 <= chan2.value <= 46000: button_interrupt("Dimmer")