class LCD(): # All avalaible colors COLOR_RED = 0 COLOR_YELLOW = 1 COLOR_GREEN = 2 COLOR_TEAL = 3 COLOR_BLUE = 4 COLOR_VIOLET = 5 COLOR_ON = 6 COLOR_OFF = 7 def __init__(self): self.__lcd = Adafruit_CharLCDPlate() self.__buttons = ( self.__lcd.LEFT, self.__lcd.UP, self.__lcd.DOWN, self.__lcd.RIGHT, self.__lcd.SELECT, ) self.__col = ( self.__lcd.RED, self.__lcd.YELLOW, self.__lcd.GREEN, self.__lcd.TEAL, self.__lcd.BLUE, self.__lcd.VIOLET, self.__lcd.ON, self.__lcd.OFF ) def stop(self): self.change_color(7) self.__lcd.clear() def clear(self, clear_background): if clear_background: self.change_color(6) self.__lcd.clear() def change_color(self, index): self.__lcd.backlight(self.__col[index]) def set_message(self, msg): self.__lcd.message(msg) def button_pressed(self, index): return self.__lcd.buttonPressed(self.__buttons[index])
def lcdDisplay(): lcd = Adafruit_CharLCDPlate(busnum = 1, addr=0x24) lcd.backlight(lcd.TEAL) while True: altitude = resources.getRes("altitude").getState() heading = resources.getRes("heading").getState() direction = dirs[int((heading+11.25)%360/22.5)] outsideTemp = resources.getRes("outsideTemp").getState() coolantTemp = resources.getRes("coolantTemp").getState()*9/5+32 battery = resources.getRes("battery").getState() message = "%5d' %03s %03d\n%3dF %3dF %4.1fV" % (altitude, direction, heading, outsideTemp, coolantTemp, battery) lcd.clear() lcd.message(message) time.sleep(10)
# We add five extra spaces between each complete text scroll. if self.position > len(self.text) + 4 : self.position = 0 return scrolledText def setNewText(self, newText): self.text = newText self.position = 0 # Initialize the LCD plate. Should auto-detect correct I2C bus. If not, # pass '0' for early 256 MB Model B boards or '1' for all later versions lcd = Adafruit_CharLCDPlate() lcd.begin(16,2) lcd.clear() lcd.backlight(lcd.ON) tmr = None # Poll buttons btn = (lcd.LEFT, lcd.UP, lcd.DOWN, lcd.RIGHT, lcd.SELECT) lastArtistSong = '' scroller = TextScroller('', 16) while True: # Get current status and playtime process = subprocess.Popen('mpc', shell=True, stdout=subprocess.PIPE) status = process.communicate()[0] statusLines = status.split('\n') statusLines = map(_utf8_to_lcd, statusLines)
# We add five extra spaces between each complete text scroll. if self.position > len(self.text) + 4: self.position = 0 return scrolledText def setNewText(self, newText): self.text = newText self.position = 0 # Initialize the LCD plate. Should auto-detect correct I2C bus. If not, # pass '0' for early 256 MB Model B boards or '1' for all later versions lcd = Adafruit_CharLCDPlate() lcd.begin(16, 2) lcd.clear() lcd.backlight(lcd.ON) tmr = None # Poll buttons btn = (lcd.LEFT, lcd.UP, lcd.DOWN, lcd.RIGHT, lcd.SELECT) lastArtistSong = '' scroller = TextScroller('', 16) while True: # Get current status and playtime process = subprocess.Popen('mpc', shell=True, stdout=subprocess.PIPE) status = process.communicate()[0] statusLines = status.split('\n') statusLines = map(_utf8_to_lcd, statusLines)
class Device(object): def __init__(self): self.lcd = None self._color = RED self._lastbuttons = 0 self._lock = threading.Lock() def get_buttons(self): pressed = [] self._lock.acquire() try: if not self.lcd: return [] buttons = self.lcd.buttons() finally: self._lock.release() for button in BUTTONS: power = 1 << button if (buttons & power) == 0: if (self._lastbuttons & power) != 0: pressed.append(button) self._lastbuttons = buttons return pressed def color(self, color): if self.lcd: self._lock.acquire() self.lcd.backlight(color) self._lock.release() self._color = color def on(self): self._lock.acquire() self.lcd = Adafruit_CharLCDPlate() self.lcd.begin(WIDTH, 2) self.lcd.clear() self.lcd.backlight(self._color) self._lock.release() def off(self): if self.lcd: self._lock.acquire() self.lcd.clear() self.lcd.stop() self.lcd = None self._lock.release() def display(self, line1 = None, line2 = None): if not self.lcd: self.on() self._lock.acquire() if line1 is not None: line1 = str(line1).ljust(WIDTH) self.lcd.setCursor(0, 0) self.lcd.message(line1) if line2 is not None: line2 = str(line2).ljust(WIDTH) self.lcd.setCursor(0, 1) self.lcd.message(line2) self._lock.release()