def print_to_LCDScreen (message): try: lcd = Adafruit_CharLCD() lcd.begin(16,2) for x in range(0, 16): for y in range(0, 2): lcd.setCursor(x, y) lcd.message('>') time.sleep(.025) lcd.noDisplay() lcd.clear() lcd.message(str(message)) for x in range(0, 16): lcd.DisplayLeft() lcd.display() for x in range(0, 16): lcd.scrollDisplayRight() time.sleep(.05) # lcd.noDisplay() # lcd.display() # lcd.blink() # lcd.noCursor() # lcd.clear() # lcd.noBlink() # lcd.begin(16, 2) # lcd.setCursor(7, 0) # lcd.message('123') # lcd.message('x') # lcd.clear() return 'ok' except Exception,e: return e
class Display(Thread): # TODO: move this to a config file COLS = 16 ROWS = 2 COL_OFFSET = 2 # You may choose to add an offset to the original position DURATION = 15 PREFIX = "WARNING: " def __init__(self, msg): Thread.__init__(self) LCDReactor.Display.START_POSITION = LCDReactor.Display.COLS - LCDReactor.Display.COL_OFFSET self.msgs = [LCDReactor.Display.PREFIX, msg] self.lcd = CharLCD(pin_rs=2, pin_e=4, pins_db=[3, 14, 25, 24]) def init_display(self): self.lcd.clear() self.lcd.begin(LCDReactor.Display.COLS, LCDReactor.Display.ROWS) def display_message(self): self.lcd.setCursor(LCDReactor.Display.START_POSITION, 0) self.lcd.message(self.msgs[0]) self.lcd.setCursor(LCDReactor.Display.START_POSITION, 1) self.lcd.message(self.msgs[1]) def shift_text(self): self.lcd.DisplayLeft() time.sleep(0.3) def loop_message(self): # Calculate the maximum length and the start position # Needed for when the time runs out and the message is in the # middle of the LCD position = LCDReactor.Display.START_POSITION max_len = max(len(self.msgs[0]), len(self.msgs[1])) start_time = time.time() # Display for n seconds while time.time() < start_time + LCDReactor.Display.DURATION: self.shift_text() position = (position + 1) % max_len # If the text is in the middle of the screen, we want to shift it # off. The best way is to take the current position and move it # until the the position is out of the display. # "Out of display" is given by max_len (maximum image size) + # START_POSITION, since it starts in the right side of the LCD for x in range(position, LCDReactor.Display.START_POSITION + max_len): self.shift_text() self.lcd.clear() def display(self): self.init_display() self.display_message() self.loop_message() def run(self): self.display()
class LCD: __line1 = "" __line2 = "" def __init__(self): self.lcd = Adafruit_CharLCD() self.lcd.clear() self.logger = LoggerModule.Logger("LCD Module") def hello(self): self.lcd.message(" Welcome to \n Kinderbox ") def turn_off(self): self.lcd.noDisplay() def display_pause(self): self.message("", "Pause") def display_ready(self): self.message("", "Ready") def display_volume(self, message): self.message("", message) def message(self, line1, line2): if self.__line1 == line1 and self.__line2 == line2: return try: n_line1 = normalization.remove_unicode(line1) except: n_line1 = "unkown" try: n_line2 = normalization.remove_unicode(line2) except: n_line2 = "unkown" self.lcd.clear() sleep(0.5) message = "%s\n%s" %(n_line1,n_line2) self.lcd.message(message) self.__line1 = line1 self.__line2 = line2 def scroll_to_left(self): #Check size message. If over 16 character --> move if len(self.__line1) > 16 or len(self.__line2) > 16: self.lcd.DisplayLeft()
DURATION = 30 START_POSITION = 14 # Have the text scroll left. Adjust for LCD size current_time = lambda: int(round(time.time())) start_time = current_time() msg = 'CODERDOJO\nMINHO' split = msg.split('\n') position = START_POSITION max_len = max(len(split[0]), len(split[1])) lcd = CharLCD(pin_rs=2, pin_e=4, pins_db=[3, 14, 25, 24]) lcd.clear() lcd.begin(16, 2) lcd.setCursor(14, 0) lcd.message(split[0]) lcd.setCursor(14, 1) lcd.message(split[1]) while current_time() < start_time + DURATION: lcd.DisplayLeft() position = (position + 1) % max_len time.sleep(.3) for x in range(position, START_POSITION + max_len): lcd.DisplayLeft() time.sleep(.3) lcd.clear()