8000 ex9 working · sparkfun/qwiic_serlcd_py@4c7826a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c7826a

Browse files
author
Pete Lewis
committed
ex9 working
also added a 10ms delay to print function just like in the arduino library
1 parent 9299152 commit 4c7826a

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#!/usr/bin/env python
2+
#-----------------------------------------------------------------------------
3+
# ex9_qwiic_serlcd_custom_character.py
4+
#
5+
# This example prints "I <heart> SerLCD!" and a little dancing man
6+
# to the LCD.
7+
#
8+
# Custom characters are recorded to SerLCD and are remembered even after power is lost.
9+
# There is a maximum of 8 custom characters that can be recorded.
10+
#
11+
#------------------------------------------------------------------------
12+
#
13+
# Written by SparkFun Electronics, August 2020
14+
#
15+
# Ported from Arduino Library code with many contributions from
16+
# Gaston Williams - August 29, 2018
17+
#
18+
# Based on Adafruit's example at
19+
#
20+
# https://github.com/adafruit/SPI_VFD/blob/master/examples/createChar/createChar.pde
21+
#
22+
# This example code is in the public domain.
23+
# http://www.arduino.cc/en/Tutorial/LiquidCrystalCustomCharacter
24+
#
25+
# Also useful:
26+
# http://icontexto.com/charactercreator/
27+
#
28+
# This python library supports the SparkFun Electroncis qwiic
29+
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
30+
# board computers.
31+
#
32+
# More information on qwiic is at https://www.sparkfun.com/qwiic
33+
#
34+
# Do you like this library? Help support SparkFun. Buy a board!
35+
#
36+
#==================================================================================
37+
# Copyright (c) 2020 SparkFun Electronics
38+
#
39+
# Permission is hereby granted, free of charge, to any person obtaining a copy
40+
# of this software and associated documentation files (the "Software"), to deal
41+
# in the Software without restriction, including without limitation the rights
42+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43+
# copies of the Software, and to permit persons to whom the Software is
44+
# furnished to do so, subject to the following conditions:
45+
#
46+
# The above copyright notice and this permission notice shall be included in all
47+
# copies or substantial portions of the Software.
48+
#
49+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
55+
# SOFTWARE.
56+
#==================================================================================
57+
# Example 9
58+
#
59+
60+
from __future__ import print_function
61+
import qwiic_serlcd
62+
import time
63+
import sys
64+
65+
def runExample():
66+
67+
print("\nSparkFun Qwiic SerLCD Example 9\n")
68+
myLCD = qwiic_serlcd.QwiicSerlcd()
69+
70+
if myLCD.connected == False:
71+
print("The Qwiic SerLCD device isn't connected to the system. Please check your connection", \
72+
file=sys.stderr)
73+
return
74+
75+
myLCD.setBacklight(255, 255, 255) # Set backlight to bright white
76+
myLCD.setContrast(5) # set contrast. Lower to 0 for higher contrast.
77+
myLCD.begin() # call this for default settings (no
78+
myLCD.leftToRight()
79+
time.sleep(1) # give a sec for system messages to complete
80+
81+
# make some custom characters:
82+
heart = [
83+
0b00000,
84+
0b01010,
85+
0b11111,
86+
0b11111,
87+
0b11111,
88+
0b01110,
89+
0b00100,
90+
0b00000]
91+
92+
smiley = [
93+
0b00000,
94+
0b00000,
95+
0b01010,
96+
0b00000,
97+
0b00000,
98+
0b10001,
99+
0b01110,
100+
0b00000]
101+
102+
frownie = [
103+
0b00000,
104+
0b00000,
105+
0b01010,
106+
0b00000,
107+
0b00000,
108+
0b00000,
109+
0b01110,
110+
0b10001]
111+
112+
armsDown = [
113+
0b00100,
114+
0b01010,
115+
0b00100,
116+
0b00100,
117+
0b01110,
118+
0b10101,
119+
0b00100,
120+
0b01010]
121+
122+
armsUp = [
123< F438 /td>+
0b00100,
124+
0b01010,
125+
0b00100,
126+
0b10101,
127+
0b01110,
128+
0b00100,
129+
0b00100,
130+
0b01010]
131+
132+
myLCD.createChar(0, heart)
133+
myLCD.createChar(1, smiley)
134+
myLCD.createChar(2, frownie)
135+
myLCD.createChar(3, armsDown)
136+
myLCD.createChar(4, armsUp)
137+
138+
myLCD.setCursor(0,0) # set cursor to the top left
139+
140+
# Print a message to the LCD.
141+
myLCD.print("I ")
142+
myLCD.writeChar(0) # Print the heart character, stored in location 0
143+
myLCD.print(" SerLCD! ")
144+
myLCD.writeChar(1) # Print smiley
145+
146+
while True:
147+
148+
myLCD.setCursor(4,1) # column, row
149+
myLCD.writeChar(3) # print little man, arms down
150+
time.sleep(0.2)
151+
152+
myLCD.setCursor(4,1) # column, row
153+
myLCD.writeChar(4) # print little man, arms up
154+
time.sleep(0.2)
155+
156+
if __name__ == '__main__':
157+
try:
158+
runExample()
159+
except (KeyboardInterrupt, SystemExit) as exErr:
160+
print("\nEnding Example 9")
161+
sys.exit(0)
162+
163+

qwiic_serlcd.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def print(self, string):
242242
for c in string:
243243
if self._i2c.writeCommand(self.address, ord(c)) == False:
244244
return False
245+
time.sleep(0.01)
245246
return True
246247

247248
# ----------------------------------
@@ -596,3 +597,57 @@ def rightToLeft(self):
596597
"""
597598
self._displayControl &= ~LCD_ENTRYLEFT
598599
return self.specialCommand(LCD_ENTRYMODESET | self._displayControl)
600+
601+
# ----------------------------------
602+
# createChar()
603+
#
604+
# Create a customer character
605+
# byte location - character number 0 to 7
606+
# byte[] charmap - byte array for character
607+
def createChar(self, location, charmap):
608+
"""
609+
Create a customer character
610+
:param location: character number 0 to 7
611+
:param charmap: byte array for character
612+
613+
:return: Returns true if the I2C write was successful, otherwise False.
614+
:rtype: bool
615+
616+
"""
617+
location &= 0x7 # we only have 8 locations 0-7
618+
619+
# create a block of data bytes to send to the screen
620+
# This will include the location (with the addition of 27 to let the screen know)
621+
# and the 8 bytes of charmap
622+
block = [0,1,2,3,4,5,6,7,8,9]
623+
624+
block[0] = (27 + location) # command type/location
625+
626+
for i in range(1,9):
627+
block[i] = charmap[i-1]
628+
629+
# send the complete bytes (address, settings command , write char command (includes location), charmap)
630+
result = self._i2c.writeBlock(self.address, SETTING_COMMAND, block)
631+
time.sleep(0.05)
632+
return result
633+
634+
# ----------------------------------
635+
# writeChar()
636+
#
637+
# Write a customer character to the display
638+
# byte location - character number 0 to 7
639+
def writeChar(self, location):
640+
"""
641+
Write a customer character to the display
642+
:param location: character number 0 to 7
643+
644+
:return: Returns true if the I2C write was successful, otherwise False.
645+
:rtype: bool
646+
647+
"""
648+
location &= 0x7 # we only have 8 locations 0-7
649+
650+
# send command
651+
result = self.command(35 + location)
652+
time.sleep(0.05)
653+
return result

0 commit comments

Comments
 (0)
0