Skip to content

Commit 9299152

Browse files
committed
ex8 working, also added delays to match arduino library
I noticed that sometimes the entry mode was being unintentionally set to "rightToLeft", so I added a call to "leftToRight" in the example 8 to make this more consistent.
1 parent 4b7ec11 commit 9299152

File tree

3 files changed

+181
-9
lines changed

3 files changed

+181
-9
lines changed

examples/ex7_qwiic_serlcd_scroll.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def runExample():
6464
myLCD.setBacklight(255, 255, 255) # Set backlight to bright white
6565
myLCD.setContrast(5) # set contrast. Lower to 0 for higher contrast.
6666
myLCD.clearScreen()
67-
myLCD.cursor() # Turn on the underline cursor
6867

6968
time.sleep(1) # give a sec for system messages to complete
7069
myLCD.print("Hello World!")
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python
2+
#-----------------------------------------------------------------------------
3+
# ex8_qwiic_serlcd_autoscroll_with_text.py
4+
#
5+
# Simple example demonstrating the autoscroll feature on the SerLCD (Qwiic).
6+
#
7+
# This example demonstrates the use of the autoscroll()
8+
# and noAutoscroll() functions to make new text scroll or not.
9+
#------------------------------------------------------------------------
10+
#
11+
# Written by SparkFun Electronics, August 2020
12+
#
13+
# Ported from Arduino Library code with many contributions from
14+
# Gaston Williams - August 29, 2018
15+
#
16+
# This python library supports the SparkFun Electroncis qwiic
17+
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
18+
# board computers.
19+
#
20+
# More information on qwiic is at https://www.sparkfun.com/qwiic
21+
#
22+
# Do you like this library? Help support SparkFun. Buy a board!
23+
#
24+
#==================================================================================
25+
# Copyright (c) 2020 SparkFun Electronics
26+
#
27+
# Permission is hereby granted, free of charge, to any person obtaining a copy
28+
# of this software and associated documentation files (the "Software"), to deal
29+
# in the Software without restriction, including without limitation the rights
30+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31+
# copies of the Software, and to permit persons to whom the Software is
32+
# furnished to do so, subject to the following conditions:
33+
#
34+
# The above copyright notice and this permission notice shall be included in all
35+
# copies or substantial portions of the Software.
36+
#
37+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43+
# SOFTWARE.
44+
#==================================================================================
45+
# Example 8
46+
#
47+
48+
from __future__ import print_function
49+
import qwiic_serlcd
50+
import time
51+
import sys
52+
53+
def runExample():
54+
55+
print("\nSparkFun Qwiic SerLCD Example 8\n")
56+
myLCD = qwiic_serlcd.QwiicSerlcd()
57+
58+
if myLCD.connected == False:
59+
print("The Qwiic SerLCD device isn't connected to the system. Please check your connection", \
60+
file=sys.stderr)
61+
return
62+
63+
myLCD.setBacklight(255, 255, 255) # Set backlight to bright white
64+
myLCD.setContrast(5) # set contrast. Lower to 0 for higher contrast.
65+
myLCD.begin() # call this for default settings (no
66+
myLCD.leftToRight()
67+
time.sleep(1) # give a sec for system messages to complete
68+
69+
while True:
70+
myLCD.setCursor(0, 0) # set the cursor to (0,0)
71+
72+
for thisChar in range(10): # print from 0 to 9
73+
myLCD.print(str(thisChar))
74+
time.sleep(0.5)
75+
76+
myLCD.autoscroll() # set the display to automatically scroll
77+
78+
for thisChar in range(0,10): # print from 0 to 9
79+
myLCD.setCursor(10+thisChar,1)
80+
myLCD.print(str(thisChar))
81+
time.sleep(0.5)
82+
83+
myLCD.noAutoscroll() # turn off automatic scrolling
84+
myLCD.clearScreen() # clear screen for the next loop
85+
86+
if __name__ == '__main__':
87+
try:
88+
runExample()
89+
except (KeyboardInterrupt, SystemExit) as exErr:
90+
print("\nEnding Example 8")
91+
sys.exit(0)
92+
93+

qwiic_serlcd.py

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#-----------------------------------------------------------------------------
7171
from __future__ import print_function
7272
import struct
73+
import time
7374

7475
import qwiic_i2c
7576

@@ -201,6 +202,7 @@ def is_connected(self):
201202
return qwiic_i2c.isDeviceConnected(self.address)
202203

203204
connected = property(is_connected)
205+
204206
# ----------------------------------
205207
# begin()
206208
#
@@ -213,8 +215,15 @@ def begin(self):
213215
:rtype: bool
214216
215217
"""
216-
# Basically return True if we are connected...
217-
return self.is_connected()
218+
# set default settings, as defined in constructor
219+
result0 = self.specialCommand(LCD_DISPLAYCONTROL | self._displayControl)
220+
time.sleep(1)
221+
result1 = self.specialCommand(LCD_ENTRYMODESET | self._displayMode)
222+
time.sleep(1)
223+
result2 = self.clearScreen()
224+
time.sleep(1)
225+
226+
return (bool(result0) & bool(result1) & bool(result2))
218227

219228
# ----------------------------------
220229
# print()
@@ -247,7 +256,9 @@ def clearScreen(self):
247256
:rtype: bool
248257
249258
"""
250-
return self.command(CLEAR_COMMAND)
259+
result = self.command(CLEAR_COMMAND)
260+
time.sleep(0.01)
261+
return result
251262

252263
# ----------------------------------
253264
# setCursor()
@@ -303,7 +314,9 @@ def setContrast(self, contrast):
303314
block = [CONTRAST_COMMAND, contrast]
304315

305316
# send the complete bytes (address, settings command , contrast command, contrast value)
306-
return self._i2c.writeBlock(self.address, SETTING_COMMAND, block)
317+
result = self._i2c.writeBlock(self.address, SETTING_COMMAND, block)
318+
time.sleep(0.01)
319+
return result
307320

308321
# ----------------------------------
309322
# setBacklight()
@@ -352,7 +365,9 @@ def setBacklight(self, r, g, b):
352365
block[9] = (LCD_DISPLAYCONTROL | self._displayControl)
353366

354367
# send the complete bytes (address, settings command , contrast command, contrast value)
355-
return self._i2c.writeBlock(self.address, SETTING_COMMAND, block)
368+
result = self._i2c.writeBlock(self.address, SETTING_COMMAND, block)
369+
time.sleep(0.05)
370+
return result
356371

357372
# ----------------------------------
358373
# specialCommand()
@@ -373,7 +388,9 @@ def specialCommand(self, command, count = 1):
373388
"""
374389
for i in range(0, count):
375390
# send the complete bytes (special command + command)
376-
return self._i2c.writeByte(self.address, SPECIAL_COMMAND, command)
391+
result = self._i2c.writeByte(self.address, SPECIAL_COMMAND, command)
392+
time.sleep(0.05)
393+
return result
377394

378395
# ----------------------------------
379396
# command()
@@ -391,7 +408,9 @@ def command(self, command):
391408
:rtype: bool
392409
393410
"""
394-
return self._i2c.writeByte(self.address, SETTING_COMMAND, command)
411+
result = self._i2c.writeByte(self.address, SETTING_COMMAND, command)
412+
time.sleep(0.01)
413+
return result
395414

396415
# ----------------------------------
397416
# moveCursorLeft()
@@ -515,4 +534,65 @@ def scrollDisplayRight(self, count = 1):
515534
:rtype: bool
516535
517536
"""
518-
return self.specialCommand(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT, count)
537+
return self.specialCommand(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT, count)
538+
539+
# ----------------------------------
540+
# autoscroll()
541+
#
542+
# Turn autoscrolling on. This will right-justify text from the cursor.
543+
def autoscroll(self):
544+
"""
545+
Turn autoscrolling on. This will right-justify text from the cursor.
546+
547+
:return: Returns true if the I2C write was successful, otherwise False.
548+
:rtype: bool
549+
550+
"""
551+
self._displayControl |= LCD_ENTRYSHIFTINCREMENT
552+
return self.specialCommand(LCD_ENTRYMODESET | self._displayControl)
553+
554+
# ----------------------------------
555+
# noAutoscroll()
556+
#
557+
# Turn autoscrolling off.
558+
def noAutoscroll(self):
559+
"""
560+
Turn autoscrolling off.
561+
562+
:return: Returns true if the I2C write was successful, otherwise False.
563+
:rtype: bool
564+
565+
"""
566+
self._displayControl &= ~LCD_ENTRYSHIFTINCREMENT
567+
return self.specialCommand(LCD_ENTRYMODESET | self._displayControl)
568+
569+
# ----------------------------------
570+
# leftToRight()
571+
#
572+
# Set the text to flow from left to right. This is the direction
573+
# that is common to most Western languages.
574+
def leftToRight(self):
575+
"""
576+
Set the text to flow from left to right.
577+
578+
:return: Returns true if the I2C write was successful, otherwise False.
579+
:rtype: bool
580+
581+
"""
582+
self._displayControl |= LCD_ENTRYLEFT
583+
return self.specialCommand(LCD_ENTRYMODESET | self._displayControl)
584+
585+
# ----------------------------------
586+
# rightToLeft()
587+
#
588+
# Set the text to flow from right to left.
589+
def rightToLeft(self):
590+
"""
591+
Set the text to flow from right to left
592+
593+
:return: Returns true if the I2C write was successful, otherwise False.
594+
:rtype: bool
595+
596+
"""
597+
self._displayControl &= ~LCD_ENTRYLEFT
598+
return self.specialCommand(LCD_ENTRYMODESET | self._displayControl)

0 commit comments

Comments
 (0)