class LcdManager: def __init__(self, config): config = config['lcd_manager'] self.dimmer_pin = int(config['dimmer_pin']) GPIO.setup(self.dimmer_pin, GPIO.OUT) self.turn_on_backlight() self.lcd_width = int(config['width']) self.lcd = CharLCD( pin_rs=int(config['rs']), pin_e=int(config['e']), pins_data=[int(y) for y in config['data_pins'].split(",")], auto_linebreaks=True, numbering_mode=GPIO.BCM, cols=self.lcd_width) for foo, bar in zip(range(len(register_char)), register_char): self.lcd.create_char(foo, bar) self.lines = [ TextLine('', self.lcd_width), TextLine('', self.lcd_width) ] def turn_on_backlight(self): GPIO.output(self.dimmer_pin, GPIO.HIGH) def turn_off_backlight(self): GPIO.output(self.dimmer_pin, GPIO.LOW) def close(self): self.turn_off_backlight() self.lcd.close(clear=True) def set_lines(self, line, line2): self.lines[0].set_text(line) self.lines[1].set_text(line2) def update(self): i = 0 for line in self.lines: self.lcd.cursor_pos = (i, 0) self.lcd.write_string(str(line).ljust(self.lcd_width)) i += 1
class Display: def __init__(self): self.lcd = CharLCD(pin_rs=15, pin_rw=18, pin_e=16, pins_data=[21, 22, 23, 24], numbering_mode=GPIO.BOARD) self._create_chars() def update_display(self, count, interval, counter=0, camera_connected=False): self.lcd.cursor_pos = (0, 0) self.lcd.write_string("Interval:") self.lcd.cursor_pos = (0, 9) self.lcd.write_string(str(interval) + " ") if not counter == 0: self.lcd.cursor_pos = (0, 13) self.lcd.write_string(str(counter)[:3] + " ") self.lcd.cursor_pos = (1, 0) self.lcd.write_string("Shots:" + str(count) + " ") if camera_connected: # displays camera icon if camera connected self.lcd.cursor_pos = (1, 13) self.lcd.write_string("\x00\x01\x02") def _create_chars(self): # camera icon bitmap0 = (0b00000, 0b00000, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011, 0b00011) bitmap1 = (0b01110, 0b11111, 0b11011, 0b10001, 0b00000, 0b10001, 0b11011, 0b11111) bitmap2 = (0b00000, 0b00000, 0b11000, 0b01000, 0b11000, 0b11000, 0b11000, 0b11000) self.lcd.create_char(0, bitmap0) self.lcd.create_char(1, bitmap1) self.lcd.create_char(2, bitmap2)
import RPi.GPIO as GPIO from RPLCD.gpio import CharLCD GPIO.setwarnings(False) lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23], numbering_mode=GPIO.BOARD) heart = [ 0b00000, 0b01010, 0b11111, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000 ] lcd.cursor_pos = (0, 6) lcd.write_string(u'I ') lcd.create_char(0, heart) lcd.write_string(unichr(0)) lcd.cursor_pos = (1, 2) lcd.write_string(u'Raspberry Pi')
pin_e=35, pins_data=[33, 31, 29, 23], numbering_mode=GPIO.BOARD) special_chars = defaultdict(lambda x: None) special_chars["å"] = chr(0) special_chars["ä"] = chr(1) special_chars["ö"] = chr(2) special_chars_code = [ (0b00100, 0b01010, 0b00100, 0b01010, 0b10001, 0b11111, 0b10001, 0b00000), (0b00000, 0b01010, 0b00000, 0b01010, 0b10001, 0b11111, 0b10001, 0b00000), (0b00000, 0b01010, 0b00000, 0b01110, 0b10001, 0b10001, 0b01110, 0b00000), ] for i, spc in enumerate(special_chars_code): lcd.create_char(i, spc) lcd.write_string("Testing") lcd.clear() trips = [("Vårdcentralen (Värmdö)", "Slussen"), ("Farstaviken", "Slussen")] while True: rows = [] for t in trips: try: departures = get_departures(t[0], t[1]) origin_name = departures[0]["LegList"]["Leg"][0]["Origin"][ "name"] + ":" departure_times = ", ".join( map(lambda d: d["LegList"]["Leg"][0]["Origin"]["time"][:-3],
class RaspberryPiIODiagnoser: def __init__(self): logger.debug(FUNCTION_CALL_MSG) GPIO.setwarnings(False) self.front_button_gpio = 4 self.back_button_gpio = 3 self.back_led_gpio = 14 # power LED self.bit_mode = 4 self.pin_modes = { 4: [33, 31, 29, 23], 8: [40, 38, 36, 32, 33, 31, 29, 23] } self.lcd = CharLCD(numbering_mode=GPIO.BOARD, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=self.pin_modes[self.bit_mode]) self.front_button = Button( pin=self.front_button_gpio, bounce_time=0.01, hold_time=self.mintime, # hold_repeat=True ) self.front_button.when_pressed = self.front_button__when_pressed self.front_button.when_held = self.front_button__when_held self.front_button.when_released = self.front_button__when_released self.back_button = Button( pin=self.back_button_gpio, bounce_time=0.01, hold_time=self.mintime, # hold_repeat=True ) self.back_button.when_pressed = self.back_button__when_pressed self.back_button.when_held = self.back_button__when_held self.back_button.when_released = self.back_button__when_released self.back_led = LED(self.back_led_gpio) self.lcd.clear() self.lcd.home() smiley = ( 0b00000, 0b01010, 0b01010, 0b00000, 0b10001, 0b10001, 0b01110, 0b00000, ) self.lcd.create_char(0, smiley) s = chr(0) line1 = f"{s}{s}{s}{s}{s}{s}{s}PI{s}{s}{s}{s}{s}{s}{s}" line2 = f"{s}{s}{s}{s}DIAGNOSH{s}{s}{s}{s}" self.lcd.cursor_pos = (0, 0) self.lcd.write_string(line1) self.lcd.cursor_pos = (1, 0) self.lcd.write_string(line2) def front_button__when_pressed(self): logger.debug(FUNCTION_CALL_MSG) def front_button__when_held(self): logger.debug(FUNCTION_CALL_MSG) def front_button__when_released(self): logger.debug(FUNCTION_CALL_MSG) def back_button__when_pressed(self): logger.debug(FUNCTION_CALL_MSG) def back_button__when_held(self): logger.debug(FUNCTION_CALL_MSG) def back_button__when_released(self): logger.debug(FUNCTION_CALL_MSG)
class RaspberryPi(IODevice): def __init__(self, shutdown_system=False, output_rows=2, output_columns=16): logger.debug(f"{FUNCTION_CALL_MSG}, {__class__}") super().__init__(shutdown_system=shutdown_system, output_rows=output_rows, output_columns=output_columns) GPIO.setwarnings(False) self.front_button_gpio = 4 self.back_button_gpio = 3 self.back_led_gpio = 14 # power LED self.bit_mode = 4 self.pin_modes = { 4: [33, 31, 29, 23], 8: [40, 38, 36, 32, 33, 31, 29, 23] } self.lcd = CharLCD( numbering_mode=GPIO.BOARD, cols=self.output_columns, rows=self.output_rows, pin_rs=37, pin_e=35, pins_data=self.pin_modes[self.bit_mode], ) self.print_cleanup_method = self.print_cleanup_method_override self.print_method = self.print_method_override smiley = ( 0b00000, 0b01010, 0b01010, 0b00000, 0b10001, 0b10001, 0b01110, 0b00000, ) self.lcd.create_char(0, smiley) self.smiley = chr(0) self.front_button = ButtonWrapper(pin=self.front_button_gpio, bounce_time=0.01, hold_time=1, hold_repeat=True) self.front_button.when_pressed = self.front_button__when_pressed self.front_button.when_held = self.front_button__when_held self.front_button.when_released = self.front_button__when_released self.back_button = ButtonWrapper(pin=self.back_button_gpio, bounce_time=0.01, hold_time=1, hold_repeat=True) self.back_button.when_pressed = self.back_button__when_pressed self.back_button.when_held = self.back_button__when_held self.back_button.when_released = self.back_button__when_released self.back_led = LED(self.back_led_gpio) self.startup_method = self.set_startup_method def print_cleanup_method_override(self): logger.debug(FUNCTION_CALL_MSG) self.lock.acquire() self.lcd.home() self.lcd.clear() self.lock.release() def print_method_override(self, framebuffer): for row in framebuffer: try: self.lcd.write_string(row) self.lcd.crlf() except Exception as e: print("Something went wrong:") print(e) return def shutdown(self): logger.debug(FUNCTION_CALL_MSG) if self.shutdown_system: logger.debug("Shutting down system") self.back_led.on() os.system("sudo poweroff") else: raise NotImplementedError # logger.debug("Shutting down Pi process") # This throws some gpiozero related error on exit, but oh well # os.kill(self.pid, signal.SIGUSR1) # TODO: Keeping this around for the blinking logic that I need to reimplement # def shutdown_old(self, hold_time=6): # logger.debug(FUNCTION_CALL_MSG) # # find how long the button has been held # p = self.back_button.pressed_time # logger.debug(f"Held for {p} seconds") # # blink rate will increase the longer we hold # # the button down. E.g., at 2 seconds, use 1/4 second rate. # self.back_led.blink(on_time=0.5 / p, off_time=0.5 / p) # if p > hold_time: # # Depending on system, either shutdown # # whole system, or just the object process # if self.shutdown_system: # logger.debug("Shutting down system") # self.back_led.on() # os.system("sudo poweroff") # else: # raise NotImplementedError # # logger.debug("Shutting down Pi process") # # # This throws some gpiozero related error on exit, but oh well # # os.kill(self.pid, signal.SIGUSR1) def default_splash_screen(self): lines = [ f"{self.smiley*3}RASPBERRY{self.smiley*4}", f"{self.smiley*7}PI{self.smiley*7}" ] self.stream_lines(lines) def default_startup_text(self): startup_steps_lines = [['Loading', '.'], ['Loading', '..'], ['Loading', '...']] self.stream_multiples_of_lines(startup_steps_lines) # TODO: Rename this def set_startup_method(self): self.default_startup_text() self.default_splash_screen() def front_button__when_pressed(self): logger.debug(FUNCTION_CALL_MSG) def front_button__when_held(self): logger.debug(FUNCTION_CALL_MSG) def front_button__when_released(self): logger.debug(FUNCTION_CALL_MSG) def back_button__when_pressed(self): logger.debug(FUNCTION_CALL_MSG) def back_button__when_held(self): logger.debug(FUNCTION_CALL_MSG) def back_button__when_released(self): logger.debug(FUNCTION_CALL_MSG) self.back_led.off() def run(self): self.print_cleanup_method() self.startup_method() super().run()
rows=2, pin_rs=4, pin_e=17, pins_data=[19, 5, 6, 13, 18, 22, 23, 24], numbering_mode=GPIO.BCM, auto_linebreaks=True, pin_backlight=None, backlight_enabled=True, compat_mode=True) lcd.clear() warnings.simplefilter("ignore") line = (0b00000, 0b00000, 0b00000, 0b11111, 0b00000, 0b00000, 0b00000, 0b00000) stickman = (0b00100, 0b01010, 0b00100, 0b01110, 0b10101, 0b00100, 0b01010, 0b10001) lcd.create_char(1, stickman) lcd.create_char(2, line) def init(): lcd.cursor_mode = "hide" up.when_pressed = up1 up.when_held = up1 down.when_pressed = down1 down.when_held = down1 left.when_pressed = left1 left.when_held = left1 right.when_pressed = right1 right.when_held = right1
lcd.clear() lcd.cursor_mode = CursorMode.hide lcd.write_string('Paris: 21{deg}C\n\rZ{uuml}rich: 18{deg}C'.format( deg=unichr(176), uuml=unichr(129))) print( 'Text should now show "Paris: 21°C, Zürich: 18°C" without any encoding issues.', end='') input() # Test custom chars lcd.clear() happy = (0b00000, 0b01010, 0b01010, 0b00000, 0b10001, 0b10001, 0b01110, 0b00000) sad = (0b00000, 0b01010, 0b01010, 0b00000, 0b01110, 0b10001, 0b10001, 0b00000) lcd.create_char(0, sad) lcd.write_string(unichr(0)) lcd.create_char(1, happy) lcd.write_string(unichr(1)) input('You should now see a sad and a happy face next to each other. ') lcd.create_char(0, happy) lcd.home() lcd.write_string(unichr(0)) input('Now both faces should be happy. ') lcd.clear() lcd.write_string('1234567890123456\r\n2nd line') input( 'The first line should be filled with numbers, the second line should show "2nd line"' )
class LCD_SYS_1: def __init__(self): if gv.SYSTEM_MODE == 1 and (gv.USE_HD44780_16x2_LCD or gv.USE_HD44780_20x4_LCD or gv.USE_I2C_16X2DISPLAY): # Timing constants self.E_PULSE = 0.0005 self.E_DELAY = 0.0005 self.display_called = False self.temp_display = False if gv.IS_DEBIAN: self.thread_sleep = 0.05 else: self.thread_sleep = 0.1 self.timeout_init = 2 # default timeout reset time self.timeout_length = self.timeout_init # initial timeout length (timeout_custom will override) self.STRING_1 = '' self.STRING_2 = '' self.STRING_3 = '' self.STRING_4 = '' self.STRING_1_PRIORITY = '' self.STRING_2_PRIORITY = '' self.STRING_3_PRIORITY = '' self.STRING_4_PRIORITY = '' self.loop_alive = True if gv.IS_DEBIAN: import RPi.GPIO as GPIO import RPLCD if (gv.USE_HD44780_16x2_LCD or gv.USE_HD44780_20x4_LCD): from RPLCD.gpio import CharLCD self.lcd = CharLCD(pin_rs=gv.GPIO_LCD_RS, pin_rw=None, pin_e=gv.GPIO_LCD_E, pins_data=[gv.GPIO_LCD_D4, gv.GPIO_LCD_D5, gv.GPIO_LCD_D6, gv.GPIO_LCD_D7], numbering_mode=GPIO.BCM, cols=gv.LCD_COLS, rows=gv.LCD_ROWS, charmap='A00') elif (gv.USE_I2C_16X2DISPLAY): from RPLCD.i2c import CharLCD # print 'Addr: ' + str(gv.I2C_16x2DISPLAY_ADDR) # self.lcd = CharLCD('PCF8574', gv.I2C_16x2DISPLAY_ADDR) self.lcd = CharLCD(i2c_expander='PCF8574', address=gv.I2C_16x2DISPLAY_ADDR, port=1, cols=16, rows=2, dotsize=8, charmap='A00', auto_linebreaks=True, backlight_enabled=True) # self.lcd.write_string('Hello world') self.lcd.clear() # Hide the cursor # self.lcd._set_cursor_mode(CursorMode.hide) self.lcd.cursor_mode = 'hide' # Fill the display with blank spaces for i in xrange(1, gv.LCD_ROWS+1): self.lcd_string(' ', i) # Write custom codes to the LCD self.lcd.create_char(1, lcdcc.block) self.lcd.create_char(2, lcdcc.pause) self.lcd.create_char(3, lcdcc.voice_button_on) self.lcd.create_char(4, lcdcc.voice_button_off) self.lcd.create_char(5, lcdcc.block2) self.lcd.create_char(6, lcdcc.loading_1) self.lcd.create_char(7, lcdcc.loading_2) self.lcd.create_char(0, lcdcc.loading_3) self.LCDThread = threading.Thread(target=self.lcd_main) self.LCDThread.daemon = True self.LCDThread.start() def reset_after_timeout(self): self.display_called = False self.temp_display = False self.timeout_start = time.time() def lcd_main(self): if gv.USE_HD44780_20x4_LCD and gv.IS_DEBIAN: self.lcd.clear() # if gv.USE_HD44780_16x2_LCD: # self.lcd_string("WELCOME TO".center(gv.LCD_COLS, ' '), 1) # self.lcd_string("SAMPLERBOX".center(gv.LCD_COLS, ' '), 2) # elif gv.USE_HD44780_20x4_LCD: # self.lcd_string(unichr(1) * gv.LCD_COLS, 1) # self.lcd_string("WELCOME TO".center(gv.LCD_COLS, ' '), 2) # self.lcd_string("SAMPLERBOX".center(gv.LCD_COLS, ' '), 3) # self.lcd_string(unichr(1) * gv.LCD_COLS, 4) # time.sleep(0.6) self.timeout_start = time.time() print_message = '' while self.loop_alive: if self.display_called: now = time.time() if (now - self.timeout_start) > self.timeout_length: self.reset_after_timeout() if (self.temp_display or gv.displayer.menu_mode == gv.displayer.DISP_UTILS_MODE): self.lcd_string(self.STRING_1, 1) self.lcd_string(self.STRING_2, 2) print_message = "\r%s||%s" % (self.STRING_1[:gv.LCD_COLS], self.STRING_2[:gv.LCD_COLS]) if gv.USE_HD44780_20x4_LCD: self.lcd_string(self.STRING_3, 3) self.lcd_string(self.STRING_4, 4) print_message = "\r%s||%s||%s" % (print_message, self.STRING_3[:gv.LCD_COLS], self.STRING_4[:gv.LCD_COLS]) elif gv.displayer.menu_mode == gv.displayer.DISP_PRESET_MODE: self.lcd_string(self.STRING_1_PRIORITY, 1) self.lcd_string(self.STRING_2_PRIORITY, 2) print_message = "\r%s||%s" % (self.STRING_1_PRIORITY[:gv.LCD_COLS], self.STRING_2_PRIORITY[:gv.LCD_COLS]) if gv.USE_HD44780_20x4_LCD: self.lcd_string(self.STRING_3_PRIORITY, 3) self.lcd_string(self.STRING_4_PRIORITY, 4) print_message = "\r%s||%s||%s" % (print_message, self.STRING_3_PRIORITY[:gv.LCD_COLS], self.STRING_4_PRIORITY[:gv.LCD_COLS]) elif gv.displayer.menu_mode == gv.displayer.DISP_MENU_MODE: self.lcd_string(self.STRING_1_PRIORITY, 1) self.lcd_string(self.STRING_2_PRIORITY, 2) print_message = "\r%s||%s" % (self.STRING_1_PRIORITY[:gv.LCD_COLS], self.STRING_2_PRIORITY[:gv.LCD_COLS]) if gv.USE_HD44780_20x4_LCD: self.lcd_string(self.STRING_3_PRIORITY, 3) self.lcd_string(self.STRING_4_PRIORITY, 4) print_message = "\r%s||%s||%s" % (print_message, self.STRING_3_PRIORITY[:gv.LCD_COLS], self.STRING_4_PRIORITY[:gv.LCD_COLS]) if gv.PRINT_LCD_MESSAGES: sys.stdout.write(print_message) sys.stdout.flush() gui_message = print_message.replace('\r', '') gui_message = gui_message.replace('||', '\r') if gv.USE_GUI and not gv.IS_DEBIAN: gv.gui.output['text'] = gui_message time.sleep(self.thread_sleep) def lcd_string(self, message, line): message = message[:gv.LCD_COLS] message = message.ljust(gv.LCD_COLS, " ") if (gv.USE_HD44780_16x2_LCD or gv.USE_HD44780_20x4_LCD or gv.USE_I2C_16X2DISPLAY) and gv.IS_DEBIAN: self.lcd.write_string(message[:gv.LCD_COLS]) def display(self, message, line=1, is_priority=False, timeout_custom=None): message += ' ' * gv.LCD_COLS # Send string to display if line == 1: self.STRING_1 = message if is_priority: self.STRING_1_PRIORITY = message else: self.temp_display = True if line == 2: self.STRING_2 = message if is_priority: self.STRING_2_PRIORITY = message else: self.temp_display = True if line == 3: self.STRING_3 = message if is_priority: self.STRING_3_PRIORITY = message else: self.temp_display = True if line == 4: self.STRING_4 = message if is_priority: self.STRING_4_PRIORITY = message else: self.temp_display = True if timeout_custom != None: self.timeout_length = timeout_custom else: self.timeout_length = self.timeout_init self.timeout_start = time.time() self.display_called = True def stop(self): self.lcd.close() self.loop_alive = False