|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +#---------------------------------------------------------------- |
| 4 | +# Author: Chris Crumpacker |
| 5 | +# Date: May 2013 |
| 6 | +# |
| 7 | +# A demo of some of the built in helper functions of |
| 8 | +# the Adafruit_CharLCDPlate.py This is 20x4 display specific for now. |
| 9 | +# I'll add conditions to get it to run on 16x2, etc. |
| 10 | +# |
| 11 | +# Using Adafruit_CharLCD code with the I2C and MCP230xx code aswell |
| 12 | +#---------------------------------------------------------------- |
| 13 | + |
| 14 | +numcolumns = 20 |
| 15 | +numrows = 4 |
| 16 | + |
| 17 | +from time import sleep |
| 18 | +from Adafruit_CharLCDPlate1 import Adafruit_CharLCDPlate |
| 19 | + |
| 20 | +lcd = Adafruit_CharLCDPlate() |
| 21 | + |
| 22 | +lcd.begin(numcolumns, numrows) |
| 23 | + |
| 24 | +lcd.backlight(lcd.ON) |
| 25 | +lcd.message("LCD 20x4\nDemonstration") |
| 26 | +sleep(2) |
| 27 | + |
| 28 | +while True: |
| 29 | + #Text on each line alone. |
| 30 | + lcd.clear() |
| 31 | + lcd.setCursor(0,0) |
| 32 | + lcd.message("Line 1") |
| 33 | + sleep(1) |
| 34 | + |
| 35 | + lcd.clear() |
| 36 | + lcd.setCursor(0,1) |
| 37 | + lcd.message("Line 2") |
| 38 | + sleep(1) |
| 39 | + |
| 40 | + lcd.clear() |
| 41 | + lcd.setCursor(0,2) |
| 42 | + lcd.message("Line 3") |
| 43 | + sleep(1) |
| 44 | + |
| 45 | + lcd.clear() |
| 46 | + lcd.setCursor(0,3) |
| 47 | + lcd.message("Line 4") |
| 48 | + sleep(1) |
| 49 | + |
| 50 | + # Using the "\n" new line marker |
| 51 | + lcd.clear() |
| 52 | + lcd.setCursor(0,0) |
| 53 | + lcd.message("Line 1") |
| 54 | + sleep(1) |
| 55 | + |
| 56 | + lcd.clear() |
| 57 | + lcd.setCursor(0,0) |
| 58 | + lcd.message("Line 1\nLine 2") |
| 59 | + sleep(1) |
| 60 | + |
| 61 | + lcd.clear() |
| 62 | + lcd.setCursor(0,0) |
| 63 | + lcd.message("Line 1\nLine 2\nLine 3") |
| 64 | + sleep(1) |
| 65 | + |
| 66 | + lcd.clear() |
| 67 | + lcd.setCursor(0,0) |
| 68 | + lcd.message("Line 1\nLine 2\nLine 3\nLine 4") |
| 69 | + sleep(1) |
| 70 | + |
| 71 | + # Auto line limiting by length as to not overflow the display |
| 72 | + # This is line by line and does not to any caraige returns |
| 73 | + lcd.clear() |
| 74 | + lcd.setCursor(0,0) |
| 75 | + lcd.message("This String is 33 Characters long",1) |
| 76 | + sleep(2) |
| 77 | + |
| 78 | + lcd.clear() |
| 79 | + lcd.setCursor(0,0) |
| 80 | + lcd.message("This String has elpise",2) |
| 81 | + sleep(2) |
| 82 | + |
| 83 | + #Scroll text to the right |
| 84 | + messageToPrint = "Scrolling Right" |
| 85 | + i=0 |
| 86 | + while i<20: |
| 87 | + lcd.clear() |
| 88 | + lcd.setCursor(0,0) |
| 89 | + suffix = " " * i |
| 90 | + lcd.message(suffix + messageToPrint,1) |
| 91 | + sleep(.25) |
| 92 | + i += 1 |
| 93 | + |
| 94 | + # Scroll test in from the Left |
| 95 | + messageToPrint = "Scrolling Left" |
| 96 | + i=20 |
| 97 | + while i>=0: |
| 98 | + lcd.clear() |
| 99 | + lcd.setCursor(0,0) |
| 100 | + suffix = " " * i |
| 101 | + lcd.message(suffix + messageToPrint,1) |
| 102 | + sleep(.25) |
| 103 | + i -= 1 |
| 104 | + sleep(2) |
| 105 | + |
| 106 | + # Printing text backwards, NOT right justified |
| 107 | + lcd.clear() |
| 108 | + lcd.setCursor(0,0) |
| 109 | + lcd.message("Right to left:") |
| 110 | + lcd.setCursor(10,1) |
| 111 | + lcd.rightToLeft() |
| 112 | + lcd.message("Testing") |
| 113 | + sleep(2) |
| 114 | + |
| 115 | + # Printing normally from the middle of the line |
| 116 | + lcd.clear() |
| 117 | + lcd.setCursor(0,0) |
| 118 | + lcd.message("Left to Right:") |
| 119 | + lcd.setCursor(10,1) |
| 120 | + lcd.message("Testing") |
| 121 | + sleep(2) |
| 122 | + |
| 123 | + # Enabling the cursor and having it blink |
| 124 | + lcd.clear() |
| 125 | + lcd.setCursor(0,0) |
| 126 | + lcd.cursor() |
| 127 | + lcd.blink() |
| 128 | + lcd.message("Cursor is blinking") |
| 129 | + lcd.setCursor(0,1) |
| 130 | + sleep(3) |
| 131 | + lcd.noCursor() |
| 132 | + lcd.noBlink() |
| 133 | + |
| 134 | + # Turning the backlight off and showing a simple count down |
| 135 | + lcd.clear() |
| 136 | + lcd.setCursor(0,0) |
| 137 | + lcd.message("Backlight off in") |
| 138 | + lcd.setCursor(0,3) |
| 139 | + lcd.message("Back on in 3sec") |
| 140 | + lcd.setCursor(17,0) #Reseting the cursor here keeps us from having to clear the screen, this over writes the previous character |
| 141 | + lcd.message("3") |
| 142 | + sleep(1) |
| 143 | + |
| 144 | + lcd.setCursor(17,0) |
| 145 | + lcd.message("2") |
| 146 | + sleep(1) |
| 147 | + |
| 148 | + lcd.setCursor(17,0) |
| 149 | + lcd.message("1") |
| 150 | + sleep(1) |
| 151 | + |
| 152 | + lcd.backlight(lcd.OFF) |
| 153 | + lcd.clear() |
| 154 | + lcd.setCursor(0,0) |
| 155 | + sleep(3) |
| 156 | + lcd.backlight(lcd.ON) |
| 157 | + lcd.message("Backlight on") |
0 commit comments