mirror of
https://github.com/pimoroni/st7735-python.git
synced 2025-01-05 22:40:25 +03:00
Cleanup
This commit is contained in:
@@ -19,31 +19,27 @@
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
from PIL import Image
|
||||
import ST7735 as TFT
|
||||
import Adafruit_GPIO.SPI as SPI
|
||||
import ST7735
|
||||
import time
|
||||
import sys
|
||||
|
||||
image_file = sys.argv[1]
|
||||
if len(sys.argv) > 1:
|
||||
image_file = sys.argv[1]
|
||||
else:
|
||||
print("Usage: {} <filename.gif>".format(sys.argv[0]))
|
||||
sys.exit(0)
|
||||
|
||||
WIDTH = TFT.ST7735_TFTWIDTH
|
||||
HEIGHT = TFT.ST7735_TFTHEIGHT
|
||||
SPEED_HZ = 4000000
|
||||
|
||||
# Raspberry Pi configuration.
|
||||
DC = 5
|
||||
RST = 25
|
||||
SPI_PORT = 0
|
||||
SPI_DEVICE = 0
|
||||
WIDTH = ST7735.ST7735_TFTWIDTH
|
||||
HEIGHT = ST7735.ST7735_TFTHEIGHT
|
||||
|
||||
# Create TFT LCD display class.
|
||||
disp = TFT.ST7735(
|
||||
DC,
|
||||
rst=RST,
|
||||
spi=SPI.SpiDev(
|
||||
SPI_PORT,
|
||||
SPI_DEVICE,
|
||||
max_speed_hz=SPEED_HZ))
|
||||
disp = ST7735.ST7735(
|
||||
port=0,
|
||||
cs=0,
|
||||
dc=24,
|
||||
backlight=18,
|
||||
spi_speed_hz=4000000
|
||||
)
|
||||
|
||||
# Initialize display.
|
||||
disp.begin()
|
||||
@@ -52,7 +48,7 @@ disp.begin()
|
||||
print('Loading gif: {}...'.format(image_file))
|
||||
image = Image.open(image_file)
|
||||
|
||||
print('Drawing gif')
|
||||
print('Drawing gif, press Ctrl+C to exit!')
|
||||
|
||||
frame = 0
|
||||
|
||||
|
||||
@@ -18,13 +18,12 @@
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
from PIL import Image
|
||||
|
||||
import ST7735 as TFT
|
||||
import Adafruit_GPIO.SPI as SPI
|
||||
import time
|
||||
|
||||
import sys
|
||||
import signal
|
||||
|
||||
from PIL import Image
|
||||
import ST7735 as ST7735
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: {} <image_file>".format(sys.argv[0]))
|
||||
@@ -32,29 +31,17 @@ if len(sys.argv) < 2:
|
||||
|
||||
image_file = sys.argv[1]
|
||||
|
||||
WIDTH = TFT.ST7735_TFTWIDTH
|
||||
HEIGHT = TFT.ST7735_TFTHEIGHT
|
||||
SPEED_HZ = 4000000
|
||||
WIDTH = ST7735.ST7735_TFTWIDTH
|
||||
HEIGHT = ST7735.ST7735_TFTHEIGHT
|
||||
|
||||
|
||||
# Raspberry Pi configuration.
|
||||
DC = 5
|
||||
SPI_PORT = 0
|
||||
SPI_DEVICE = 0
|
||||
|
||||
# BeagleBone Black configuration.
|
||||
# DC = 'P9_15'
|
||||
# RST = 'P9_12'
|
||||
# SPI_PORT = 1
|
||||
# SPI_DEVICE = 0
|
||||
|
||||
# Create TFT LCD display class.
|
||||
disp = TFT.ST7735(
|
||||
DC,
|
||||
spi=SPI.SpiDev(
|
||||
SPI_PORT,
|
||||
SPI_DEVICE,
|
||||
max_speed_hz=SPEED_HZ))
|
||||
# Create ST7735 LCD display class.
|
||||
disp = ST7735.ST7735(
|
||||
port=0,
|
||||
cs=0,
|
||||
dc=24,
|
||||
backlight=18,
|
||||
spi_speed_hz=4000000
|
||||
)
|
||||
|
||||
# Initialize display.
|
||||
disp.begin()
|
||||
@@ -71,3 +58,6 @@ image = image.resize((WIDTH, HEIGHT))
|
||||
print('Drawing image')
|
||||
|
||||
disp.display(image)
|
||||
|
||||
print("Press Ctl+C to exit!")
|
||||
signal.pause()
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
# Copyright (c) 2014 Adafruit Industries
|
||||
# Author: Tony DiCola
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
from PIL import Image
|
||||
import time
|
||||
import ST7735 as TFT
|
||||
import Adafruit_GPIO as GPIO
|
||||
import Adafruit_GPIO.SPI as SPI
|
||||
|
||||
import sys
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: {} <image_file>".format(sys.argv[0]))
|
||||
sys.exit(1)
|
||||
|
||||
image_file = sys.argv[1]
|
||||
|
||||
|
||||
|
||||
WIDTH = 160
|
||||
HEIGHT = 80
|
||||
SPEED_HZ = 16000000
|
||||
|
||||
|
||||
# Raspberry Pi configuration.
|
||||
DC = 5
|
||||
RST = 25
|
||||
SPI_PORT = 0
|
||||
SPI_DEVICE = 0
|
||||
|
||||
# BeagleBone Black configuration.
|
||||
#DC = 'P9_15'
|
||||
#RST = 'P9_12'
|
||||
#SPI_PORT = 1
|
||||
#SPI_DEVICE = 0
|
||||
|
||||
# Create TFT LCD display class.
|
||||
disp = TFT.ST7735(
|
||||
DC,
|
||||
rst=RST,
|
||||
spi=SPI.SpiDev(
|
||||
SPI_PORT,
|
||||
SPI_DEVICE,
|
||||
max_speed_hz=SPEED_HZ))
|
||||
|
||||
# Initialize display.
|
||||
disp.begin()
|
||||
|
||||
# Load an image.
|
||||
print('Loading image...')
|
||||
image = Image.open(image_file)
|
||||
|
||||
# Resize the image and rotate it so matches the display.
|
||||
image = image.rotate(0).resize((WIDTH, HEIGHT))
|
||||
|
||||
print('Press Ctrl-C to exit')
|
||||
while(True):
|
||||
# Draw the image on the display hardware.
|
||||
print('Drawing image')
|
||||
start_time = time.time()
|
||||
disp.display(image)
|
||||
end_time = time.time()
|
||||
print('Time to draw image: ' + str(end_time - start_time))
|
||||
# disp.clear((0, 0, 0))
|
||||
# disp.display()
|
||||
@@ -18,37 +18,24 @@
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
import signal
|
||||
from PIL import Image
|
||||
from PIL import ImageDraw
|
||||
from PIL import ImageFont
|
||||
|
||||
import ST7735 as TFT
|
||||
import Adafruit_GPIO.SPI as SPI
|
||||
import ST7735
|
||||
|
||||
WIDTH = ST7735.ST7735_TFTWIDTH
|
||||
HEIGHT = ST7735.ST7735_TFTHEIGHT
|
||||
|
||||
WIDTH = 160
|
||||
HEIGHT = 80
|
||||
SPEED_HZ = 4000000
|
||||
|
||||
|
||||
# Raspberry Pi configuration.
|
||||
DC = 5
|
||||
SPI_PORT = 0
|
||||
SPI_DEVICE = 0
|
||||
|
||||
# BeagleBone Black configuration.
|
||||
# DC = 'P9_15'
|
||||
# RST = 'P9_12'
|
||||
# SPI_PORT = 1
|
||||
# SPI_DEVICE = 0
|
||||
|
||||
# Create TFT LCD display class.
|
||||
disp = TFT.ST7735(
|
||||
DC,
|
||||
spi=SPI.SpiDev(
|
||||
SPI_PORT,
|
||||
SPI_DEVICE,
|
||||
max_speed_hz=SPEED_HZ))
|
||||
# Create ST7735 LCD display class.
|
||||
disp = ST7735.ST7735(
|
||||
port=0,
|
||||
cs=0,
|
||||
dc=24,
|
||||
backlight=18,
|
||||
spi_speed_hz=4000000
|
||||
)
|
||||
|
||||
# Initialize display.
|
||||
disp.begin()
|
||||
@@ -108,3 +95,6 @@ draw_rotated_text(disp.buffer, 'This is a line of text.', (10, HEIGHT-10), 0, fo
|
||||
# Write buffer to display hardware, must be called to make things visible on the
|
||||
# display!
|
||||
disp.display()
|
||||
|
||||
print("Press Ctl+C to exit!")
|
||||
signal.pause()
|
||||
|
||||
332
ez_setup.py
332
ez_setup.py
@@ -1,332 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
"""Bootstrap setuptools installation
|
||||
|
||||
To use setuptools in your package's setup.py, include this
|
||||
file in the same directory and add this to the top of your setup.py::
|
||||
|
||||
from ez_setup import use_setuptools
|
||||
use_setuptools()
|
||||
|
||||
To require a specific version of setuptools, set a download
|
||||
mirror, or use an alternate download directory, simply supply
|
||||
the appropriate options to ``use_setuptools()``.
|
||||
|
||||
This file can also be run as a script to install or upgrade setuptools.
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import zipfile
|
||||
import optparse
|
||||
import subprocess
|
||||
import platform
|
||||
import textwrap
|
||||
import contextlib
|
||||
|
||||
from distutils import log
|
||||
|
||||
try:
|
||||
from site import USER_SITE
|
||||
except ImportError:
|
||||
USER_SITE = None
|
||||
|
||||
DEFAULT_VERSION = "3.5.1"
|
||||
DEFAULT_URL = "https://pypi.python.org/packages/source/s/setuptools/"
|
||||
|
||||
def _python_cmd(*args):
|
||||
"""
|
||||
Return True if the command succeeded.
|
||||
"""
|
||||
args = (sys.executable,) + args
|
||||
return subprocess.call(args) == 0
|
||||
|
||||
|
||||
def _install(archive_filename, install_args=()):
|
||||
with archive_context(archive_filename):
|
||||
# installing
|
||||
log.warn('Installing Setuptools')
|
||||
if not _python_cmd('setup.py', 'install', *install_args):
|
||||
log.warn('Something went wrong during the installation.')
|
||||
log.warn('See the error message above.')
|
||||
# exitcode will be 2
|
||||
return 2
|
||||
|
||||
|
||||
def _build_egg(egg, archive_filename, to_dir):
|
||||
with archive_context(archive_filename):
|
||||
# building an egg
|
||||
log.warn('Building a Setuptools egg in %s', to_dir)
|
||||
_python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)
|
||||
# returning the result
|
||||
log.warn(egg)
|
||||
if not os.path.exists(egg):
|
||||
raise IOError('Could not build the egg.')
|
||||
|
||||
|
||||
def get_zip_class():
|
||||
"""
|
||||
Supplement ZipFile class to support context manager for Python 2.6
|
||||
"""
|
||||
class ContextualZipFile(zipfile.ZipFile):
|
||||
def __enter__(self):
|
||||
return self
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.close
|
||||
return zipfile.ZipFile if hasattr(zipfile.ZipFile, '__exit__') else \
|
||||
ContextualZipFile
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def archive_context(filename):
|
||||
# extracting the archive
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
log.warn('Extracting in %s', tmpdir)
|
||||
old_wd = os.getcwd()
|
||||
try:
|
||||
os.chdir(tmpdir)
|
||||
with get_zip_class()(filename) as archive:
|
||||
archive.extractall()
|
||||
|
||||
# going in the directory
|
||||
subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
|
||||
os.chdir(subdir)
|
||||
log.warn('Now working in %s', subdir)
|
||||
yield
|
||||
|
||||
finally:
|
||||
os.chdir(old_wd)
|
||||
shutil.rmtree(tmpdir)
|
||||
|
||||
|
||||
def _do_download(version, download_base, to_dir, download_delay):
|
||||
egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
|
||||
% (version, sys.version_info[0], sys.version_info[1]))
|
||||
if not os.path.exists(egg):
|
||||
archive = download_setuptools(version, download_base,
|
||||
to_dir, download_delay)
|
||||
_build_egg(egg, archive, to_dir)
|
||||
sys.path.insert(0, egg)
|
||||
|
||||
# Remove previously-imported pkg_resources if present (see
|
||||
# https://bitbucket.org/pypa/setuptools/pull-request/7/ for details).
|
||||
if 'pkg_resources' in sys.modules:
|
||||
del sys.modules['pkg_resources']
|
||||
|
||||
import setuptools
|
||||
setuptools.bootstrap_install_from = egg
|
||||
|
||||
|
||||
def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
|
||||
to_dir=os.curdir, download_delay=15):
|
||||
to_dir = os.path.abspath(to_dir)
|
||||
rep_modules = 'pkg_resources', 'setuptools'
|
||||
imported = set(sys.modules).intersection(rep_modules)
|
||||
try:
|
||||
import pkg_resources
|
||||
except ImportError:
|
||||
return _do_download(version, download_base, to_dir, download_delay)
|
||||
try:
|
||||
pkg_resources.require("setuptools>=" + version)
|
||||
return
|
||||
except pkg_resources.DistributionNotFound:
|
||||
return _do_download(version, download_base, to_dir, download_delay)
|
||||
except pkg_resources.VersionConflict as VC_err:
|
||||
if imported:
|
||||
msg = textwrap.dedent("""
|
||||
The required version of setuptools (>={version}) is not available,
|
||||
and can't be installed while this script is running. Please
|
||||
install a more recent version first, using
|
||||
'easy_install -U setuptools'.
|
||||
|
||||
(Currently using {VC_err.args[0]!r})
|
||||
""").format(VC_err=VC_err, version=version)
|
||||
sys.stderr.write(msg)
|
||||
sys.exit(2)
|
||||
|
||||
# otherwise, reload ok
|
||||
del pkg_resources, sys.modules['pkg_resources']
|
||||
return _do_download(version, download_base, to_dir, download_delay)
|
||||
|
||||
def _clean_check(cmd, target):
|
||||
"""
|
||||
Run the command to download target. If the command fails, clean up before
|
||||
re-raising the error.
|
||||
"""
|
||||
try:
|
||||
subprocess.check_call(cmd)
|
||||
except subprocess.CalledProcessError:
|
||||
if os.access(target, os.F_OK):
|
||||
os.unlink(target)
|
||||
raise
|
||||
|
||||
def download_file_powershell(url, target):
|
||||
"""
|
||||
Download the file at url to target using Powershell (which will validate
|
||||
trust). Raise an exception if the command cannot complete.
|
||||
"""
|
||||
target = os.path.abspath(target)
|
||||
cmd = [
|
||||
'powershell',
|
||||
'-Command',
|
||||
"(new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)" % vars(),
|
||||
]
|
||||
_clean_check(cmd, target)
|
||||
|
||||
def has_powershell():
|
||||
if platform.system() != 'Windows':
|
||||
return False
|
||||
cmd = ['powershell', '-Command', 'echo test']
|
||||
devnull = open(os.path.devnull, 'wb')
|
||||
try:
|
||||
try:
|
||||
subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
|
||||
except Exception:
|
||||
return False
|
||||
finally:
|
||||
devnull.close()
|
||||
return True
|
||||
|
||||
download_file_powershell.viable = has_powershell
|
||||
|
||||
def download_file_curl(url, target):
|
||||
cmd = ['curl', url, '--silent', '--output', target]
|
||||
_clean_check(cmd, target)
|
||||
|
||||
def has_curl():
|
||||
cmd = ['curl', '--version']
|
||||
devnull = open(os.path.devnull, 'wb')
|
||||
try:
|
||||
try:
|
||||
subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
|
||||
except Exception:
|
||||
return False
|
||||
finally:
|
||||
devnull.close()
|
||||
return True
|
||||
|
||||
download_file_curl.viable = has_curl
|
||||
|
||||
def download_file_wget(url, target):
|
||||
cmd = ['wget', url, '--quiet', '--output-document', target]
|
||||
_clean_check(cmd, target)
|
||||
|
||||
def has_wget():
|
||||
cmd = ['wget', '--version']
|
||||
devnull = open(os.path.devnull, 'wb')
|
||||
try:
|
||||
try:
|
||||
subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
|
||||
except Exception:
|
||||
return False
|
||||
finally:
|
||||
devnull.close()
|
||||
return True
|
||||
|
||||
download_file_wget.viable = has_wget
|
||||
|
||||
def download_file_insecure(url, target):
|
||||
"""
|
||||
Use Python to download the file, even though it cannot authenticate the
|
||||
connection.
|
||||
"""
|
||||
try:
|
||||
from urllib.request import urlopen
|
||||
except ImportError:
|
||||
from urllib2 import urlopen
|
||||
src = dst = None
|
||||
try:
|
||||
src = urlopen(url)
|
||||
# Read/write all in one block, so we don't create a corrupt file
|
||||
# if the download is interrupted.
|
||||
data = src.read()
|
||||
dst = open(target, "wb")
|
||||
dst.write(data)
|
||||
finally:
|
||||
if src:
|
||||
src.close()
|
||||
if dst:
|
||||
dst.close()
|
||||
|
||||
download_file_insecure.viable = lambda: True
|
||||
|
||||
def get_best_downloader():
|
||||
downloaders = [
|
||||
download_file_powershell,
|
||||
download_file_curl,
|
||||
download_file_wget,
|
||||
download_file_insecure,
|
||||
]
|
||||
|
||||
for dl in downloaders:
|
||||
if dl.viable():
|
||||
return dl
|
||||
|
||||
def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
|
||||
to_dir=os.curdir, delay=15, downloader_factory=get_best_downloader):
|
||||
"""
|
||||
Download setuptools from a specified location and return its filename
|
||||
|
||||
`version` should be a valid setuptools version number that is available
|
||||
as an egg for download under the `download_base` URL (which should end
|
||||
with a '/'). `to_dir` is the directory where the egg will be downloaded.
|
||||
`delay` is the number of seconds to pause before an actual download
|
||||
attempt.
|
||||
|
||||
``downloader_factory`` should be a function taking no arguments and
|
||||
returning a function for downloading a URL to a target.
|
||||
"""
|
||||
# making sure we use the absolute path
|
||||
to_dir = os.path.abspath(to_dir)
|
||||
zip_name = "setuptools-%s.zip" % version
|
||||
url = download_base + zip_name
|
||||
saveto = os.path.join(to_dir, zip_name)
|
||||
if not os.path.exists(saveto): # Avoid repeated downloads
|
||||
log.warn("Downloading %s", url)
|
||||
downloader = downloader_factory()
|
||||
downloader(url, saveto)
|
||||
return os.path.realpath(saveto)
|
||||
|
||||
def _build_install_args(options):
|
||||
"""
|
||||
Build the arguments to 'python setup.py install' on the setuptools package
|
||||
"""
|
||||
return ['--user'] if options.user_install else []
|
||||
|
||||
def _parse_args():
|
||||
"""
|
||||
Parse the command line for options
|
||||
"""
|
||||
parser = optparse.OptionParser()
|
||||
parser.add_option(
|
||||
'--user', dest='user_install', action='store_true', default=False,
|
||||
help='install in user site package (requires Python 2.6 or later)')
|
||||
parser.add_option(
|
||||
'--download-base', dest='download_base', metavar="URL",
|
||||
default=DEFAULT_URL,
|
||||
help='alternative URL from where to download the setuptools package')
|
||||
parser.add_option(
|
||||
'--insecure', dest='downloader_factory', action='store_const',
|
||||
const=lambda: download_file_insecure, default=get_best_downloader,
|
||||
help='Use internal, non-validating downloader'
|
||||
)
|
||||
parser.add_option(
|
||||
'--version', help="Specify which version to download",
|
||||
default=DEFAULT_VERSION,
|
||||
)
|
||||
options, args = parser.parse_args()
|
||||
# positional arguments are ignored
|
||||
return options
|
||||
|
||||
def main():
|
||||
"""Install or upgrade setuptools and EasyInstall"""
|
||||
options = _parse_args()
|
||||
archive = download_setuptools(
|
||||
version=options.version,
|
||||
download_base=options.download_base,
|
||||
downloader_factory=options.downloader_factory,
|
||||
)
|
||||
return _install(archive, _build_install_args(options))
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -21,21 +21,22 @@
|
||||
import numbers
|
||||
import time
|
||||
import numpy as np
|
||||
import atexit
|
||||
|
||||
import spidev
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
from PIL import Image
|
||||
from PIL import ImageDraw
|
||||
|
||||
import Adafruit_GPIO as GPIO
|
||||
import Adafruit_GPIO.SPI as SPI
|
||||
GPIO.setwarnings(False)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
||||
|
||||
# SPI_CLOCK_HZ = 64000000 # 64 MHz
|
||||
SPI_CLOCK_HZ = 16000000 # 4 MHz
|
||||
|
||||
|
||||
# Constants for interacting with display registers.
|
||||
ST7735_TFTWIDTH = 160 # 128
|
||||
ST7735_TFTHEIGHT = 80 #64 #160
|
||||
ST7735_TFTWIDTH = 160
|
||||
ST7735_TFTHEIGHT = 80
|
||||
|
||||
ST7735_NOP = 0x00
|
||||
ST7735_SWRESET = 0x01
|
||||
@@ -123,30 +124,37 @@ def image_to_data(image):
|
||||
class ST7735(object):
|
||||
"""Representation of an ST7735 TFT LCD."""
|
||||
|
||||
def __init__(self, dc, spi, rst=None, gpio=None, width=ST7735_TFTWIDTH,
|
||||
height=ST7735_TFTHEIGHT):
|
||||
def __init__(self, port, cs, dc, backlight=None, rst=None, width=ST7735_TFTWIDTH,
|
||||
height=ST7735_TFTHEIGHT, spi_speed_hz=4000000):
|
||||
"""Create an instance of the display using SPI communication. Must
|
||||
provide the GPIO pin number for the D/C pin and the SPI driver. Can
|
||||
optionally provide the GPIO pin number for the reset pin as the rst
|
||||
parameter.
|
||||
|
||||
:param port: SPI port number
|
||||
:param cs: SPI CS number (0 or 1 for BCM
|
||||
"""
|
||||
self._spi = spidev.SpiDev(port, cs)
|
||||
self._spi.mode = 0
|
||||
self._spi.lsbfirst = False
|
||||
self._spi.max_speed_hz = spi_speed_hz
|
||||
|
||||
self._dc = dc
|
||||
self._rst = rst
|
||||
self._spi = spi
|
||||
self._gpio = gpio
|
||||
self.width = width
|
||||
self.height = height
|
||||
if self._gpio is None:
|
||||
self._gpio = GPIO.get_platform_gpio()
|
||||
|
||||
# Set DC as output.
|
||||
self._gpio.setup(dc, GPIO.OUT)
|
||||
GPIO.setup(dc, GPIO.OUT)
|
||||
|
||||
# Setup backlight as output (if provided).
|
||||
if backlight is not None:
|
||||
GPIO.setup(backlight, GPIO.OUT)
|
||||
|
||||
# Setup reset as output (if provided).
|
||||
if rst is not None:
|
||||
self._gpio.setup(rst, GPIO.OUT)
|
||||
# Set SPI to mode 0, MSB first.
|
||||
spi.set_mode(0)
|
||||
spi.set_bit_order(SPI.MSBFIRST)
|
||||
spi.set_clock_hz(SPI_CLOCK_HZ)
|
||||
GPIO.setup(rst, GPIO.OUT)
|
||||
|
||||
# Create an image buffer.
|
||||
self.buffer = Image.new('RGB', (width, height))
|
||||
|
||||
@@ -157,14 +165,14 @@ class ST7735(object):
|
||||
single SPI transaction, with a default of 4096.
|
||||
"""
|
||||
# Set DC low for command, high for data.
|
||||
self._gpio.output(self._dc, is_data)
|
||||
GPIO.output(self._dc, is_data)
|
||||
# Convert scalar argument to list so either can be passed as parameter.
|
||||
if isinstance(data, numbers.Number):
|
||||
data = [data & 0xFF]
|
||||
# Write data a chunk at a time.
|
||||
for start in range(0, len(data), chunk_size):
|
||||
end = min(start+chunk_size, len(data))
|
||||
self._spi.write(data[start:end])
|
||||
self._spi.xfer(data[start:end])
|
||||
|
||||
def command(self, data):
|
||||
"""Write a byte or array of bytes to the display as command data."""
|
||||
@@ -177,89 +185,87 @@ class ST7735(object):
|
||||
def reset(self):
|
||||
"""Reset the display, if reset pin is connected."""
|
||||
if self._rst is not None:
|
||||
self._gpio.set_high(self._rst)
|
||||
GPIO.output(self._rst, 1)
|
||||
time.sleep(0.500)
|
||||
self._gpio.set_low(self._rst)
|
||||
GPIO.output(self._rst, 0)
|
||||
time.sleep(0.500)
|
||||
self._gpio.set_high(self._rst)
|
||||
GPIO.output(self._rst, 1)
|
||||
time.sleep(0.500)
|
||||
|
||||
def _init(self):
|
||||
# Initialize the display. Broken out as a separate function so it can
|
||||
# be overridden by other displays in the future.
|
||||
|
||||
self.command(ST7735_SWRESET) # Software reset
|
||||
time.sleep(0.150) # delay 150 ms
|
||||
self.command(ST7735_SWRESET) # Software reset
|
||||
time.sleep(0.150) # delay 150 ms
|
||||
|
||||
self.command(ST7735_SLPOUT) # Out of sleep mode
|
||||
time.sleep(0.500) # delay 500 ms
|
||||
self.command(ST7735_SLPOUT) # Out of sleep mode
|
||||
time.sleep(0.500) # delay 500 ms
|
||||
|
||||
self.command(ST7735_FRMCTR1) # Frame rate ctrl - normal mode
|
||||
self.data(0x01) # Rate = fosc/(1x2+40) * (LINE+2C+2D)
|
||||
self.command(ST7735_FRMCTR1) # Frame rate ctrl - normal mode
|
||||
self.data(0x01) # Rate = fosc/(1x2+40) * (LINE+2C+2D)
|
||||
self.data(0x2C)
|
||||
self.data(0x2D)
|
||||
|
||||
self.command(ST7735_FRMCTR2) # Frame rate ctrl - idle mode
|
||||
self.data(0x01) # Rate = fosc/(1x2+40) * (LINE+2C+2D)
|
||||
self.command(ST7735_FRMCTR2) # Frame rate ctrl - idle mode
|
||||
self.data(0x01) # Rate = fosc/(1x2+40) * (LINE+2C+2D)
|
||||
self.data(0x2C)
|
||||
self.data(0x2D)
|
||||
|
||||
self.command(ST7735_FRMCTR3) # Frame rate ctrl - partial mode
|
||||
self.data(0x01) # Dot inversion mode
|
||||
self.command(ST7735_FRMCTR3) # Frame rate ctrl - partial mode
|
||||
self.data(0x01) # Dot inversion mode
|
||||
self.data(0x2C)
|
||||
self.data(0x2D)
|
||||
self.data(0x01) # Line inversion mode
|
||||
self.data(0x01) # Line inversion mode
|
||||
self.data(0x2C)
|
||||
self.data(0x2D)
|
||||
|
||||
self.command(ST7735_INVCTR) # Display inversion ctrl
|
||||
self.data(0x07) # No inversion
|
||||
self.command(ST7735_INVCTR) # Display inversion ctrl
|
||||
self.data(0x07) # No inversion
|
||||
|
||||
self.command(ST7735_PWCTR1) # Power control
|
||||
self.command(ST7735_PWCTR1) # Power control
|
||||
self.data(0xA2)
|
||||
self.data(0x02) # -4.6V
|
||||
self.data(0x84) # auto mode
|
||||
self.data(0x02) # -4.6V
|
||||
self.data(0x84) # auto mode
|
||||
|
||||
self.command(ST7735_PWCTR2) # Power control
|
||||
self.data(0x0A) # Opamp current small
|
||||
self.data(0x00) # Boost frequency
|
||||
self.command(ST7735_PWCTR2) # Power control
|
||||
self.data(0x0A) # Opamp current small
|
||||
self.data(0x00) # Boost frequency
|
||||
|
||||
self.command(ST7735_PWCTR4) # Power control
|
||||
self.data(0x8A) # BCLK/2, Opamp current small & Medium low
|
||||
self.command(ST7735_PWCTR4) # Power control
|
||||
self.data(0x8A) # BCLK/2, Opamp current small & Medium low
|
||||
self.data(0x2A)
|
||||
|
||||
self.command(ST7735_PWCTR5) # Power control
|
||||
self.command(ST7735_PWCTR5) # Power control
|
||||
self.data(0x8A)
|
||||
self.data(0xEE)
|
||||
|
||||
self.command(ST7735_VMCTR1) # Power control
|
||||
self.command(ST7735_VMCTR1) # Power control
|
||||
self.data(0x0E)
|
||||
|
||||
self.command(ST7735_INVON) # Don't invert display
|
||||
self.command(ST7735_INVON) # Don't invert display
|
||||
|
||||
self.command(ST7735_MADCTL) # Memory access control (directions)
|
||||
self.data(0xC8) # row addr/col addr, bottom to top refresh
|
||||
self.command(ST7735_MADCTL) # Memory access control (directions)
|
||||
self.data(0xC8) # row addr/col addr, bottom to top refresh
|
||||
|
||||
self.command(ST7735_COLMOD) # set color mode
|
||||
self.data(0x05) # 16-bit color
|
||||
self.command(ST7735_COLMOD) # set color mode
|
||||
self.data(0x05) # 16-bit color
|
||||
|
||||
#
|
||||
|
||||
self.command(ST7735_CASET) # Column addr set
|
||||
self.data(0x00) # XSTART = 0
|
||||
self.command(ST7735_CASET) # Column addr set
|
||||
self.data(0x00) # XSTART = 0
|
||||
self.data(132 - ST7735_TFTHEIGHT)
|
||||
self.data(0x00) # XEND = 127
|
||||
self.data(0x00) # XEND = 127
|
||||
self.data(ST7735_TFTHEIGHT - 1)
|
||||
|
||||
self.command(ST7735_RASET) # Row addr set
|
||||
self.data(0x00) # XSTART = 0
|
||||
self.command(ST7735_RASET) # Row addr set
|
||||
self.data(0x00) # XSTART = 0
|
||||
self.data(161 - ST7735_TFTWIDTH)
|
||||
self.data(0x00) # XEND = 159
|
||||
self.data(0x00) # XEND = 159
|
||||
self.data(ST7735_TFTWIDTH - 1)
|
||||
|
||||
#
|
||||
|
||||
self.command(ST7735_GMCTRP1) # Set Gamma
|
||||
self.command(ST7735_GMCTRP1) # Set Gamma
|
||||
self.data(0x02)
|
||||
self.data(0x1c)
|
||||
self.data(0x07)
|
||||
@@ -277,7 +283,7 @@ class ST7735(object):
|
||||
self.data(0x03)
|
||||
self.data(0x10)
|
||||
|
||||
self.command(ST7735_GMCTRN1) # Set Gamma
|
||||
self.command(ST7735_GMCTRN1) # Set Gamma
|
||||
self.data(0x03)
|
||||
self.data(0x1d)
|
||||
self.data(0x07)
|
||||
@@ -295,11 +301,11 @@ class ST7735(object):
|
||||
self.data(0x02)
|
||||
self.data(0x10)
|
||||
|
||||
self.command(ST7735_NORON) # Normal display on
|
||||
time.sleep(0.10) # 10 ms
|
||||
self.command(ST7735_NORON) # Normal display on
|
||||
time.sleep(0.10) # 10 ms
|
||||
|
||||
self.command(ST7735_DISPON) # Display on
|
||||
time.sleep(0.100) # 100 ms
|
||||
self.command(ST7735_DISPON) # Display on
|
||||
time.sleep(0.100) # 100 ms
|
||||
|
||||
def begin(self):
|
||||
"""Initialize the display. Should be called once before other calls that
|
||||
@@ -307,6 +313,7 @@ class ST7735(object):
|
||||
"""
|
||||
self.reset()
|
||||
self._init()
|
||||
atexit.register(self.clear_on_exit)
|
||||
|
||||
def set_window(self, x0=0, y0=0, x1=None, y1=None):
|
||||
"""Set the pixel address window for proceeding drawing commands. x0 and
|
||||
@@ -317,28 +324,27 @@ class ST7735(object):
|
||||
"""
|
||||
if x1 is None:
|
||||
x1 = self.width-1
|
||||
|
||||
if y1 is None:
|
||||
y1 = self.height-1
|
||||
|
||||
#y0 += 132 - 80
|
||||
#y1 += 132 - 80
|
||||
y0 += 26
|
||||
y1 += 26
|
||||
|
||||
x0 += 1
|
||||
x1 += 1
|
||||
|
||||
self.command(ST7735_CASET) # Column addr set
|
||||
self.command(ST7735_CASET) # Column addr set
|
||||
self.data(y0 >> 8)
|
||||
self.data(y0) # XSTART
|
||||
self.data(y1 >> 8)
|
||||
self.data(y1) # XEND
|
||||
self.command(ST7735_RASET) # Row addr set
|
||||
self.command(ST7735_RASET) # Row addr set
|
||||
self.data(x0 >> 8)
|
||||
self.data(x0) # YSTART
|
||||
self.data(x1 >> 8)
|
||||
self.data(x1) # YEND
|
||||
self.command(ST7735_RAMWR) # write to RAM
|
||||
self.command(ST7735_RAMWR) # write to RAM
|
||||
|
||||
def display(self, image=None):
|
||||
"""Write the display buffer or provided image to the hardware. If no
|
||||
@@ -367,3 +373,9 @@ class ST7735(object):
|
||||
def draw(self):
|
||||
"""Return a PIL ImageDraw instance for 2D drawing on the image buffer."""
|
||||
return ImageDraw.Draw(self.buffer)
|
||||
|
||||
def clear_on_exit(self):
|
||||
self.clear()
|
||||
self.display()
|
||||
|
||||
|
||||
21
setup.py
21
setup.py
@@ -1,18 +1,3 @@
|
||||
# Workaround for issue in Python 2.7.3
|
||||
# See http://bugs.python.org/issue15881#msg170215
|
||||
try:
|
||||
import multiprocessing
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
# Try using ez_setup to install setuptools if not already installed.
|
||||
from ez_setup import use_setuptools
|
||||
use_setuptools()
|
||||
except ImportError:
|
||||
# Ignore import error and assume Python 3 which already has setuptools.
|
||||
pass
|
||||
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
|
||||
@@ -27,10 +12,8 @@ classifiers = ['Development Status :: 4 - Beta',
|
||||
|
||||
setup(name = 'ST7735',
|
||||
version = '0.0.1',
|
||||
description = 'Library to control an ST7735 TFT LCD display.',
|
||||
description = 'Library to control an ST7735 168x80 TFT LCD display.',
|
||||
license = 'MIT',
|
||||
classifiers = classifiers,
|
||||
url = 'https://github.com/cskau/Python_ST7735/',
|
||||
dependency_links = ['https://github.com/adafruit/Adafruit_Python_GPIO/tarball/master#egg=Adafruit-GPIO-0.6.5'],
|
||||
install_requires = ['Adafruit-GPIO>=0.6.5'],
|
||||
url = 'https://github.com/pimoroni/st7735-160x80-python/',
|
||||
packages = find_packages())
|
||||
|
||||
Reference in New Issue
Block a user