10000 ex4 working, also moved to command/special command functions · sparkfun/qwiic_serlcd_py@dfad153 · GitHub
[go: up one dir, main page]

Skip to content

Commit dfad153

Browse files
author
Pete Lewis
committed
ex4 working, also moved to command/special command functions
1 parent 8973b99 commit dfad153

File tree

2 files changed

+205
-1
lines changed

2 files changed

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

qwiic_serlcd.py

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def clearScreen(self):
247247
:rtype: bool
248248
249249
"""
250-
return self._i2c.writeByte(self.address, SETTING_COMMAND, CLEAR_COMMAND)
250+
return self.command(CLEAR_COMMAND)
251251

252252
# ----------------------------------
253253
# setCursor()
@@ -354,3 +354,103 @@ def setBacklight(self, r, g, b):
354354
# send the complete bytes (address, settings command , contrast command, contrast value)
355355
return self._i2c.writeBlock(self.address, SETTING_COMMAND, block)
356356

357+
# ----------------------------------
358+
# specialCommand()
359+
#
360+
# Send one (or multiples of) special command to the display.
361+
# Used by other functions.
362+
def specialCommand(self, command, count = 1):
363+
"""
364+
Send one (or multiple) special commands to the display.
365+
Used by other functions.
366+
367+
:param command: Command to send (a single byte)
368+
:param count: Number of times to send the command (if ommited, then default is once)
369+
370+
:return: Returns true if the I2C write was successful, otherwise False.
371+
:rtype: bool
372+
373+
"""
374+
for i in range(0, count):
375+
# send the complete bytes (special command + command)
376+
return self._i2c.writeByte(self.address, SPECIAL_COMMAND, command)
377+
378+
# ----------------------------------
379+
# command()
380+
#
381+
# Send one setting command to the display.
382+
# Used by other functions.
383+
def command(self, command):
384+
"""
385+
Send one setting command to the display.
386+
Used by other functions.
387+
388+
:param command: Command to send (a single byte)
389+
390+
:return: Returns true if the I2C write was successful, otherwise False.
391+
:rtype: bool
392+
393+
"""
394+
return self._i2c.writeByte(self.address, SETTING_COMMAND, command)
395+
396+
# ----------------------------------
397+
# moveCursorLeft()
398+
#
399+
# Move the cursor one or more characters to the left.
400+
def moveCursorLeft(self, count = 1):
401+
"""
402+
Move the cursor one or more characters to the left.
403+
404+
:param count: Number of character spaces you'd like to move
405+
406+
:return: Returns true if the I2C write was successful, otherwise False.
407+
:rtype: bool
408+
409+
"""
410+
return self.specialCommand(LCD_CURSORSHIFT | LCD_CURSORMOVE | LCD_MOVELEFT, count)
411+
412+
# ----------------------------------
413+
# moveCursorRight()
414+
#
415+
# Move the cursor one or more characters to the right.
416+
def moveCursorRight(self, count = 1):
417+
"""
418+
Move the cursor one or more characters to the right.
419+
420+
:param count: Number of character spaces you'd like to move
421+
422+
:return: Returns true if the I2C write was successful, otherwise False.
423+
:rtype: bool
424+
425+
"""
426+
return self.specialCommand(LCD_CURSORSHIFT | LCD_CURSORMOVE | LCD_MOVERIGHT, count)
427+
428+
# ----------------------------------
429+
# cursor()
430+
#
431+
# Turn the underline cursor on.
432+
def cursor(self):
433+
"""
434+
Turn the underline cursor on.
435+
436+
:return: Returns true if the I2C write was successful, otherwise False.
437+
:rtype: bool
438+
439+
"""
440+
self._displayControl |= LCD_CURSORON
441+
return self.specialCommand(LCD_DISPLAYCONTROL | self._displayControl)
442+
443+
# ----------------------------------
444+
# noCursor()
445+
#
446+
# Turn the underline cursor off.
447+
def noCursor(self):
448+
"""
449+
Turn the underline cursor off.
450+
451+
:return: Returns true if the I2C write was successful, otherwise False.
452+
:rtype: bool
453+
454+
"""
455+
self._displayControl &= ~LCD_CURSORON
456+
return self.specialCommand(LCD_DISPLAYCONTROL | self._displayControl)

0 commit comments

Comments
 (0)
0