class LCD: line1 = "" line2 = "" def __init__(self): self.lcd = CharLCD(pin_rs=26, pin_e=19, pins_data=[13, 6, 5, 11], numbering_mode=GPIO.BCM) def writeLine1(self, text: str): self.line1 = text self._toDisplay() def writeLine2(self, text: str): self.line2 = text self._toDisplay() def _toDisplay(self): #self.clear() self.lcd.write_string(f"{self.line1}\n{self.line2}") def clear(self): self.lcd.clear() def close(self): self.lcd.close(clear=True)
class Wrapper: lcd = 1 text = 'Initializing' oldtext = text shutDownLCD = False def __init__(self): self.activate() workerThread = threading.Thread(target=self.updateScreenWorker, args=(), daemon=True) workerThread.start() def activate(self): # Initialize LCD self.lcd = CharLCD(pin_rs=16, pin_e=18, pins_data=[11, 12, 13, 15], numbering_mode=GPIO.BOARD, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=True, pin_backlight=22) self.lcd.clear() GPIO.output(22, GPIO.HIGH) #turn on LCD backlight # Main loop def updateScreenWorker(self): self.text = "\nReady." while not self.shutDownLCD: if self.text != self.oldtext: self.lcd.clear() self.lcd.home() self.lcd.write_string(self.text) self.oldtext = self.text time.sleep(1) self.cleanShutdown() # If clean shut down is requested, shut down cleanly, and display optional message. def cleanShutdown(self, closeMessage=''): print('\nshutting down LCD') if GPIO.getmode() != None: # If screen can be accessed self.lcd.clear() self.lcd.home() self.lcd.write_string(closeMessage) self.lcd.close(clear=False) else: # If screen can not be accessed, activate self.activate() self.lcd.clear() self.lcd.home() self.lcd.write_string(closeMessage) self.lcd.close(clear=False) print('complete')
class Wrapper: i = 0 lcd = 1 text = 'Initializing' oldtext = text shutDownLCD = False def __init__(self): self.activate() workerThread = threading.Thread(target=self.updateScreenWorker, args=(), daemon=True) workerThread.start() def activate(self): self.lcd = CharLCD(pin_rs=16, pin_e=18, pins_data=[11, 12, 13, 15], numbering_mode=GPIO.BOARD, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=True, pin_backlight=22) self.lcd.clear() GPIO.output(22, GPIO.HIGH) #turns on backlight def updateScreenWorker(self): self.text = "Ready." while not self.shutDownLCD: #print("Text = " + self.text + " Oldtext = " + self.oldtext) if self.text != self.oldtext: self.lcd.clear() self.lcd.home() self.lcd.write_string(self.text) self.i = self.i + 1 self.oldtext = self.text time.sleep(1) self.cleanShutdown() def cleanShutdown(self, closeMessage=''): print('\nshutting down LCD') if GPIO.getmode() != None: self.lcd.clear() self.lcd.home() self.lcd.write_string(closeMessage) self.lcd.close(clear=False) else: self.activate() self.lcd.clear() self.lcd.home() self.lcd.write_string(closeMessage) self.lcd.close(clear=False) print('complete')
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(cols=16, rows=2, pin_rs=16, pin_e=36, pins_data=[38, 32, 11, 37], numbering_mode=GPIO.BOARD) def show_message(self, text): self.lcd.clear() self.lcd.write_string(text) def clear(self): self.lcd.clear() def close(self): self.lcd.close(clear=True)
class Display(): def __init__(self): # Configure the LCD self.lcd = CharLCD(pin_rs=38, pin_rw=None, pin_e=40, pins_data=[36, 18, 16, 12], numbering_mode=GPIO.BOARD, auto_linebreaks=False, cols=COLS, rows=ROWS) def print(self, string): if len(string) > COLS * ROWS: print('WARNING: String \'{}\' too big and overflows the display.'. format(string)) self.lcd.clear() self.lcd.write_string(string) def __del__(self): self.lcd.close()
class CommonHardwarePIHD44780GPIO(object): """ Class for interfacing with pi lcd HD44780 """ def __init__(self): self.lcd_inst = CharLCDGPIO(pin_rs=15, pin_rw=18, pin_e=16, pins_data=[21, 22, 23, 24], numbering_mode=GPIO.BOARD, cols=20, rows=4, dotsize=8, charmap='A02', auto_linebreaks=True) def com_hard_pi_hd44780_write(self, lcd_string, x_pos=None, y_pos=None): if x_pos is not None: self.lcd_inst.cursor_pos = (x_pos, y_pos) self.lcd_inst.write_string(lcd_string) def com_hard_pi_hd44780_clear(self): self.lcd_inst.clear() def com_hard_pi_hd44780_close(self, clear_screen=True): self.lcd_inst.close(clear=clear_screen)
lcd.cursor_pos = (0, 0) lcd.write_string(center(ip)) lcd.cursor_pos = (1, 0) lcd.write_string(center(ssid)) last_ip = ip last_ssid = ssid if __name__ == '__main__': try: lcd = CharLCD(pin_rs=8, pin_e=10, pin_rw=None, pins_data=[12, 16, 18, 22], cols=16, rows=2, numbering_mode=GPIO.BOARD, auto_linebreaks=True) while True: refresh(lcd) time.sleep(2) print('SSID: ' + wlan_ssid()) print('IP: ' + str(wlan_ip())) except Exception as e: print("Exception occurred: " + str(e)) finally: lcd.clear() lcd.close(True)
else: cput = open("/sys/class/thermal/thermal_zone0/temp") cpuf = open( "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq") lcd.clear() read = float(cput.read(8)) / 1000.0 freq = float(cpuf.read( 16)) / 1000000.0 # freq in GHz since read is in KHz already # print(freq) total += read # add newest read to total to be averaged if (read < min_): min_ = read if (read > max_): max_ = read # output to display lcd.write_string("T:" + str(round(read, 2)) + "C ") lcd.write_string("F:" + str(round(freq, 2))) lcd.write_string("\n\r" + str(round(min_, 1)) + "<" + str(round(total / reads, 3))) lcd.write_string("<" + str(round(max_, 1))) reads += 1 # increment for averaging time.sleep(POLLING_INTERVAL) except SystemExit: print("Exiting background-temp.py") finally: lcd.clear() lcd.write_string("exiting\n\rbg-temp.py") time.sleep(3) lcd.clear() lcd.close()
LCD_D6 = 5 LCD_D7 = 11 lcd = CharLCD(numbering_mode=GPIO.BCM, cols=16, rows=2, pin_rs=LCD_RS, pin_e=LCD_E, pins_data=[LCD_D4, LCD_D5, LCD_D6, LCD_D7]) lcd.cursor_pos = (0, 0) lcd.write_string(u" Raspberry Pi") lcd.cursor_pos = (1, 0) lcd.write_string(u" Model 4") time.sleep(1) lcd.clear() lcd.cursor_pos = (0, 0) lcd.write_string(u" Welcome\r\n") time.sleep(1) lcd.clear() try: while True: lcd.cursor_pos = (0, 0) lcd.write_string(datetime.now().strftime("%d/%m/%Y %H:%M")) humidity, temperature = Adafruit_DHT.read_retry(11, 17) lcd.cursor_pos = (1, 0) lcd.write_string("T:%sC H:%s%%" % (temperature, humidity)) except: lcd.close(clear=True) GPIO.cleanup()
#from RPLCD import CharLCD from RPLCD.gpio import CharLCD import RPi.GPIO as GPIO import time # Logical Pins = Physical Pins D4 = 33 D5 = 31 D6 = 29 D7 = 32 pin_e = 35 pin_rs = 37 print('Pins Set') lcd = CharLCD(cols=16, rows=2, pin_rs=pin_rs, pin_e=pin_e, pins_data=[D4, D5, D6, D7], numbering_mode=GPIO.BOARD) lcd.cursor_pos = (0, 0) #row,col starting with 0 #while True: lcd.write_string( u'Yo Mama!') #\n starts new line at same pos, \r returns line at 0 pos time.sleep(6) lcd.close(clear=True) #closes display and doesnt give warning #lcd.clear() #clears display but gives warning on restart of display #time.sleep(2) print('end of script') #GPIO.cleanup()
class RpiInterface: def __init__(self, music_logic): self.music_logic = music_logic def draw_screen(self): self.clear_screen() self.write_to_screen(self.music_logic.get_current_bank().name, line=0) self.write_to_screen(f"RS: {self.music_logic.repeat_style.name}", line=1) def draw_screen_async(self): p = Process(target=self.draw_screen) p.start() def write_to_screen(self, text, line=None): written_characters = 0 total_available_characters = 0 if line in range(SCREEN_ROWS): total_available_characters = SCREEN_COLUMNS self._lcd.cursor_pos = (line, 0) elif line is None: total_available_characters = SCREEN_COLUMNS * SCREEN_ROWS self._lcd.home() else: raise AttributeError( f"line must be between 0 and {SCREEN_COLUMNS}") trimmed_text = text[:total_available_characters] self._lcd.write_string(trimmed_text) def clear_screen(self): self._lcd.clear() def start(self): os.putenv('SDL_VIDEODRIVER', 'fbcon') pygame.display.init() GPIO.setmode(GPIO.BCM) self._lcd = CharLCD(pin_rs=17, pin_e=27, pins_data=[22, 10, 9, 11], numbering_mode=GPIO.BCM, cols=SCREEN_COLUMNS, rows=SCREEN_ROWS) self._lcd.clear() self._lcd.write_string('Welcome to\r\nSoundFloored') time.sleep(1) self.draw_screen_async() control_button_1_pin = 2 control_button_2_pin = 26 GPIO.setup(control_button_1_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect( control_button_1_pin, GPIO.FALLING, callback=lambda channel: self.music_logic.decrement_bank(), bouncetime=200) GPIO.setup(control_button_2_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect( control_button_2_pin, GPIO.FALLING, callback=lambda channel: self.music_logic.increment_bank(), bouncetime=200) audio_clip_button_pins = [14, 15, 20, 21] # Used to discard the channel value that is automatically provided by # add_event_detect to the callback function callback_wrapper = lambda index, channel: self.music_logic.play_clip( index) for index, audio_clip_button_pin in enumerate(audio_clip_button_pins): GPIO.setup(audio_clip_button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(audio_clip_button_pin, GPIO.FALLING, callback=partial(callback_wrapper, index), bouncetime=200) self.music_logic.after_state_change_functions.append( self.draw_screen_async) try: while True: for index, audio_clip_button_pin in enumerate( audio_clip_button_pins): if GPIO.input(audio_clip_button_pin ) == INPUT_VALUE_WHEN_BUTTON_PRESSED: # Sleep for just long enough to give the event detector a chance to # execute the callback, rather than stacking multiple calls # to play_clip at the same time time.sleep(0.01) self.music_logic.play_clip(index, is_distinct_trigger=False) except KeyboardInterrupt: pass finally: self._lcd.close(clear=True) GPIO.cleanup()
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