def test(): """CircuitPython Text, Shape & Sprite""" if implementation.name != 'circuitpython': print() print('This demo is for CircuitPython only!') exit() try: # Configuratoin for CS and DC pins: cs_pin = DigitalInOut(board.P0_15) dc_pin = DigitalInOut(board.P0_17) rst_pin = DigitalInOut(board.P0_20) # Setup SPI bus using hardware SPI: spi = SPI(clock=board.P0_24, MOSI=board.P0_22) # Create the ILI9341 display: display = Display(spi, dc=dc_pin, cs=cs_pin, rst=rst_pin) display.clear() # Load Fixed Font fixed = XglcdFont('fonts/FixedFont5x8.c', 5, 8, letter_count=96) # Title WIDTH = 128 text = 'CircuitPython Demo' # Measure text and center length = fixed.measure_text(text) x = int((WIDTH / 2) - (length / 2)) display.draw_text(x, 6, text, fixed, color565(255, 255, 0)) # Draw title outline display.draw_rectangle(0, 0, 127, 20, color565(0, 255, 0)) # Load sprite logo = BouncingSprite('images/blinka45x48.raw', 45, 48, 239, 319, 1, display) while True: timer = monotonic() logo.update_pos() logo.draw() # Attempt to set framerate to 30 FPS timer_dif = .033333333 - (monotonic() - timer) if timer_dif > 0: sleep(timer_dif) except KeyboardInterrupt: display.cleanup()
def test(): """Test code.""" # Baud rate of 40000000 seems about the max spi = SPI(1, baudrate=40000000, sck=Pin(14), mosi=Pin(13)) display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17)) print('Loading fonts...') print('Loading arcadepix') arcadepix = XglcdFont('fonts/ArcadePix9x11.c', 9, 11) print('Loading bally') bally = XglcdFont('fonts/Bally7x9.c', 7, 9) print('Loading broadway') broadway = XglcdFont('fonts/Broadway17x15.c', 17, 15) print('Loading espresso_dolce') espresso_dolce = XglcdFont('fonts/EspressoDolce18x24.c', 18, 24) print('Loading fixed_font') fixed_font = XglcdFont('fonts/FixedFont5x8.c', 5, 8) print('Loading neato') neato = XglcdFont('fonts/Neato5x7.c', 5, 7, letter_count=223) print('Loading robotron') robotron = XglcdFont('fonts/Robotron13x21.c', 13, 21) print('Loading unispace') unispace = XglcdFont('fonts/Unispace12x24.c', 12, 24) print('Loading wendy') wendy = XglcdFont('fonts/Wendy7x8.c', 7, 8) print('Fonts loaded.') display.draw_text(0, 0, 'Arcade Pix 9x11', arcadepix, color565(255, 0, 0)) display.draw_text(0, 22, 'Bally 7x9', bally, color565(0, 255, 0)) display.draw_text(0, 43, 'Broadway 17x15', broadway, color565(0, 0, 255)) display.draw_text(0, 66, 'Espresso Dolce 18x24', espresso_dolce, color565(0, 255, 255)) display.draw_text(0, 104, 'Fixed Font 5x8', fixed_font, color565(255, 0, 255)) display.draw_text(0, 125, 'Neato 5x7', neato, color565(255, 255, 0)) display.draw_text(0, 155, 'ROBOTRON 13X21', robotron, color565(255, 255, 255)) display.draw_text(0, 190, 'Unispace 12x24', unispace, color565(255, 128, 0)) display.draw_text(0, 220, 'Wendy 7x8', wendy, color565(255, 0, 128)) sleep(9) display.clear() display.draw_text(0, 255, 'Arcade Pix 9x11', arcadepix, color565(255, 0, 0), landscape=True) display.draw_text(22, 255, 'Bally 7x9', bally, color565(0, 255, 0), landscape=True) display.draw_text(43, 255, 'Broadway 17x15', broadway, color565(0, 0, 255), landscape=True) display.draw_text(66, 255, 'Espresso Dolce 18x24', espresso_dolce, color565(0, 255, 255), landscape=True) display.draw_text(104, 255, 'Fixed Font 5x8', fixed_font, color565(255, 0, 255), landscape=True) display.draw_text(125, 255, 'Neato 5x7', neato, color565(255, 255, 0), landscape=True) display.draw_text(155, 255, 'ROBOTRON 13X21', robotron, color565(255, 255, 255), landscape=True) display.draw_text(190, 255, 'Unispace 12x24', unispace, color565(255, 128, 0), landscape=True) display.draw_text(220, 255, 'Wendy 7x8', wendy, color565(255, 0, 128), landscape=True) sleep(9) display.clear() display.draw_text(0, 0, 'Arcade Pix 9x11', arcadepix, color565(255, 0, 0), background=color565(0, 255, 255)) display.draw_text(0, 22, 'Bally 7x9', bally, color565(0, 255, 0), background=color565(0, 0, 128)) display.draw_text(0, 43, 'Broadway', broadway, color565(0, 0, 255), background=color565(255, 255, 0)) display.draw_text(0, 66, 'Espresso', espresso_dolce, color565(0, 255, 255), background=color565(255, 0, 0)) display.draw_text(0, 104, 'Fixed Font 5x8', fixed_font, color565(255, 0, 255), background=color565(0, 128, 0)) display.draw_text(0, 125, 'Neato 5x7', neato, color565(255, 255, 0), background=color565(0, 0, 255)) display.draw_text(0, 155, 'ROBOTRON 13X21', robotron, color565(255, 255, 255), background=color565(128, 128, 128)) display.draw_text(0, 190, 'Unispace', unispace, color565(255, 128, 0), background=color565(0, 128, 255)) display.draw_text(0, 220, 'Wendy 7x8', wendy, color565(255, 0, 128), background=color565(255, 255, 255)) sleep(9) display.cleanup()
class Hardware(BaseHardware): def __init__(self, config) : pin_a = Pin(BUTTON_A_PIN, mode=Pin.IN, pull=None) pin_b = Pin(BUTTON_B_PIN, mode=Pin.IN, pull=None) pin_c = Pin(BUTTON_C_PIN, mode=Pin.IN, pull=None) self.buttonA = Button(pin=pin_a) self.buttonB = Button(pin=pin_b) self.buttonC = Button(pin=pin_c) self.buttonA.press_func(self.button_A_callback, (pin_a,)) # Note how function and args are passed self.buttonB.press_func(self.button_B_callback, (pin_b,)) # Note how function and args are passed self.buttonC.press_func(self.button_C_callback, (pin_c,)) # Note how function and args are passed # display self.display = None self.screen_power = None self.header_font = None self.large_label_font = None self.small_label_font = None self.setup_screen() # screen saver # 0 - disable # x - screen saver activation in second self.screensaver = 0 self.currentcount = 0 if "screensaver" in config: self.screensaver = int(config["screensaver"]) self.currentcount = int(config["screensaver"]) self.screen_timeout_lock = Lock() # wifi super().__init__(config) def get_ble_handle(self): return self.ble_handle def set_callback(self, button, cb): if button == BUTTON_A_PIN: self.buttonA = Button(pin=Pin(BUTTON_A_PIN, mode=Pin.IN, pull=None), callback=cb, trigger=Pin.IRQ_FALLING) def button_A_callback(self, pin): print("Button (%s) changed to: %r" % (pin, pin.value())) if pin.value() == 0 : #device = self.device_req_handler["studyrmfan"] # handle the request topic = self.tranport_handler.topicprefix + 'cmnd/studyrmfan/press' self.tranport_handler.publish(topic, 'on') # rest the screen saver self.tranport_handler.loop.create_task(self.reset_screen_saver()) def button_B_callback(self, pin): print("Button (%s) changed to: %r" % (pin, pin.value())) if pin.value() == 0 : # handle the request topic = self.tranport_handler.topicprefix + 'cmnd/studyrmtemp/getstatus' self.tranport_handler.publish(topic, 'on') # rest the screen saver self.tranport_handler.loop.create_task(self.reset_screen_saver()) def button_C_callback(self, pin): print("Button (%s) changed to: %r" % (pin, pin.value())) if pin.value() == 0 : #device = self.device_req_handler["waterheater"] # handle the request topic = self.tranport_handler.topicprefix + 'cmnd/waterheater/press' self.tranport_handler.publish(topic, 'on') # rest the screen saver self.tranport_handler.loop.create_task(self.reset_screen_saver()) def clear_dashboard(self): ''' Clear the dashboard area ''' self.display.fill_rectangle(70, 80, 200, 100, color565(0,0,0)) def setup_screen(self): self.screen_power = Pin(m5stack.TFT_LED_PIN, Pin.OUT) self.screen_power.value(1) spi = SPI( 2, baudrate=40000000, miso=Pin(m5stack.TFT_MISO_PIN), mosi=Pin(m5stack.TFT_MOSI_PIN), sck=Pin(m5stack.TFT_CLK_PIN)) #display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17)) self.display = Display( spi, cs=Pin(m5stack.TFT_CS_PIN), dc=Pin(m5stack.TFT_DC_PIN), rst=Pin(m5stack.TFT_RST_PIN), width=320, height=240, rotation=0) self.display.clear() self.header_font = XglcdFont('/screen/m5stack/fonts/Unispace12x24.c', 12, 24) self.large_label_font = XglcdFont('/screen/m5stack/fonts/IBMPlexMono12x24.c', 12, 24) self.small_label_font = XglcdFont('/screen/m5stack/fonts/ArcadePix9x11.c', 9, 11) self.display.draw_text(0, 0, 'Loading...', self.large_label_font, color565(0, 0, 0), background=color565(255, 255, 255)) def show_setupcomplete(self): self.home_page() self.display.draw_text(70, 80, 'Waiting For', self.large_label_font, color565(255, 255, 255), background=color565(0, 0, 0)) self.display.draw_text(70, 100, 'Messages', self.large_label_font, color565(255, 255, 255), background=color565(0, 0, 0)) #activate the screen saver self.tranport_handler.loop.create_task(self.screen_saver_countdown()) def home_page(self): self.display.clear() self.display.draw_image('/images/blecanvas.raw',0,0,320,240) #self.display.draw_text(45, 203, 'Fan', self.large_label_font, color565(0, 0, 0), background=color565(255, 255, 255)) #self.display.draw_text(135, 203, 'Temp', self.large_label_font, color565(0, 0, 0), background=color565(255, 255, 255)) #self.display.draw_text(225, 203, 'Heater', self.large_label_font, color565(0, 0, 0), background=color565(255, 255, 255)) self.screen_power.value(1) def display_result(self, text): print("Display Result") self.clear_dashboard() self.display.draw_text(70, 80, text["line1"], self.large_label_font, color565(255, 255, 255)) self.display.draw_text(70, 100, text["line2"], self.large_label_font, color565(255, 255, 255)) self.display.draw_text(70, 120, text["line3"], self.large_label_font, color565(255, 255, 255)) def screen_off(self): self.screen_power.value(0) def screen_on(self): self.screen_power.value(1) async def reset_screen_saver(self): ''' Reset the screen Saver timeout to ''' await self.screen_timeout_lock.acquire() self.currentcount = self.screensaver self.screen_timeout_lock.release() self.screen_on() async def screen_saver_countdown(self): while True: await self.screen_timeout_lock.acquire() if self.currentcount > 0: self.currentcount -= 1 if self.currentcount == 0 : self.screen_off() self.screen_timeout_lock.release() await asyncio.sleep(1) #a=Hardware() #a.show_setupcomplete
def test(): """Test code.""" print('Loading Espresso Dolce font...') espresso_dolce = XglcdFont('fonts/EspressoDolce18x24.c', 18, 24) print('Font loaded.') # Baud rate of 40000000 seems about the max spi = SPI(1, baudrate=40000000, sck=Pin(14), mosi=Pin(13)) display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17), width=240, height=320, rotation=0) display.draw_text(0, 0, 'Espresso Dolce 18x24', espresso_dolce, color565(0, 255, 255)) display.draw_text(0, 319, 'Espresso Dolce 18x24', espresso_dolce, color565(255, 255, 0), landscape=True) sleep(5) display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17), width=320, height=240, rotation=90) display.draw_text(0, 215, 'Espresso Dolce 18x24', espresso_dolce, color565(255, 0, 255)) display.draw_text(295, 239, 'Espresso Dolce 18x24', espresso_dolce, color565(255, 255, 255), landscape=True) sleep(5) display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17), width=240, height=320, rotation=180) display.draw_text(0, 0, 'Espresso Dolce 18x24', espresso_dolce, color565(0, 0, 255)) display.draw_text(0, 319, 'Espresso Dolce 18x24', espresso_dolce, color565(255, 0, 0), landscape=True) sleep(5) display = Display(spi, dc=Pin(4), cs=Pin(16), rst=Pin(17), width=320, height=240, rotation=270) display.draw_text(0, 215, 'Espresso Dolce 18x24', espresso_dolce, color565(225, 0, 128)) display.draw_text(295, 239, 'Espresso Dolce 18x24', espresso_dolce, color565(0, 255, 0), landscape=True) sleep(5) display.cleanup()