mirror of
https://github.com/pimoroni/st7735-python.git
synced 2025-01-05 22:40:25 +03:00
Code QA tweaks
This commit is contained in:
@@ -19,14 +19,13 @@
|
|||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
# THE SOFTWARE.
|
# THE SOFTWARE.
|
||||||
import time
|
import time
|
||||||
import sys
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from PIL import ImageDraw
|
from PIL import ImageDraw
|
||||||
import ST7735 as ST7735
|
import ST7735 as ST7735
|
||||||
|
|
||||||
SPI_SPEED_MHZ = 10 # Higher speed = higher framerate
|
SPI_SPEED_MHZ = 10 # Higher speed = higher framerate
|
||||||
|
|
||||||
# Create ST7735 LCD display class.
|
# Create ST7735 LCD display class.
|
||||||
disp = ST7735.ST7735(
|
disp = ST7735.ST7735(
|
||||||
@@ -66,4 +65,7 @@ while True:
|
|||||||
count += 1
|
count += 1
|
||||||
time_current = time.time() - time_start
|
time_current = time.time() - time_start
|
||||||
if count % 120 == 0:
|
if count % 120 == 0:
|
||||||
print("Time: {}, Frames: {}, FPS: {}".format(time_current, count, count / time_current))
|
print("Time: {}, Frames: {}, FPS: {}".format(
|
||||||
|
time_current,
|
||||||
|
count,
|
||||||
|
count / time_current))
|
||||||
|
|||||||
@@ -58,6 +58,6 @@ while True:
|
|||||||
disp.display(image.resize((WIDTH, HEIGHT)))
|
disp.display(image.resize((WIDTH, HEIGHT)))
|
||||||
frame += 1
|
frame += 1
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
|
|
||||||
except EOFError:
|
except EOFError:
|
||||||
frame = 0
|
frame = 0
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@
|
|||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
# 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
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
# THE SOFTWARE.
|
# THE SOFTWARE.
|
||||||
import time
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|||||||
@@ -50,46 +50,48 @@ img = Image.new('RGB', (WIDTH, HEIGHT), color=(255, 0, 0))
|
|||||||
draw = ImageDraw.Draw(img)
|
draw = ImageDraw.Draw(img)
|
||||||
|
|
||||||
# Draw a purple rectangle with yellow outline.
|
# Draw a purple rectangle with yellow outline.
|
||||||
draw.rectangle((10, 10, WIDTH-10, HEIGHT-10), outline=(255,255,0), fill=(255,0,255))
|
draw.rectangle((10, 10, WIDTH - 10, HEIGHT - 10), outline=(255, 255, 0), fill=(255, 0, 255))
|
||||||
|
|
||||||
# Draw some shapes.
|
# Draw some shapes.
|
||||||
# Draw a blue ellipse with a green outline.
|
# Draw a blue ellipse with a green outline.
|
||||||
draw.ellipse((10, 10, WIDTH-10, HEIGHT-10), outline=(0,255,0), fill=(0,0,255))
|
draw.ellipse((10, 10, WIDTH - 10, HEIGHT - 10), outline=(0, 255, 0), fill=(0, 0, 255))
|
||||||
|
|
||||||
# Draw a white X.
|
# Draw a white X.
|
||||||
draw.line((10, 10, WIDTH-10, HEIGHT-10), fill=(255,255,255))
|
draw.line((10, 10, WIDTH - 10, HEIGHT - 10), fill=(255, 255, 255))
|
||||||
draw.line((10, HEIGHT-10, WIDTH-10, 10), fill=(255,255,255))
|
draw.line((10, HEIGHT - 10, WIDTH - 10, 10), fill=(255, 255, 255))
|
||||||
|
|
||||||
# Draw a cyan triangle with a black outline.
|
# Draw a cyan triangle with a black outline.
|
||||||
draw.polygon([(WIDTH/2, 10), (WIDTH-10, HEIGHT-10), (10, HEIGHT-10)], outline=(0,0,0), fill=(0,255,255))
|
draw.polygon([(WIDTH / 2, 10), (WIDTH - 10, HEIGHT - 10), (10, HEIGHT - 10)], outline=(0, 0, 0), fill=(0, 255, 255))
|
||||||
|
|
||||||
# Load default font.
|
# Load default font.
|
||||||
font = ImageFont.load_default()
|
font = ImageFont.load_default()
|
||||||
|
|
||||||
# Alternatively load a TTF font.
|
# Alternatively load a TTF font.
|
||||||
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
|
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
|
||||||
#font = ImageFont.truetype('Minecraftia.ttf', 16)
|
# font = ImageFont.truetype('Minecraftia.ttf', 16)
|
||||||
|
|
||||||
|
|
||||||
# Define a function to create rotated text. Unfortunately PIL doesn't have good
|
# Define a function to create rotated text. Unfortunately PIL doesn't have good
|
||||||
# native support for rotated fonts, but this function can be used to make a
|
# native support for rotated fonts, but this function can be used to make a
|
||||||
# text image and rotate it so it's easy to paste in the buffer.
|
# text image and rotate it so it's easy to paste in the buffer.
|
||||||
def draw_rotated_text(image, text, position, angle, font, fill=(255,255,255)):
|
def draw_rotated_text(image, text, position, angle, font, fill=(255, 255, 255)):
|
||||||
# Get rendered font width and height.
|
# Get rendered font width and height.
|
||||||
draw = ImageDraw.Draw(image)
|
draw = ImageDraw.Draw(image)
|
||||||
width, height = draw.textsize(text, font=font)
|
width, height = draw.textsize(text, font=font)
|
||||||
# Create a new image with transparent background to store the text.
|
# Create a new image with transparent background to store the text.
|
||||||
textimage = Image.new('RGBA', (width, height), (0,0,0,0))
|
textimage = Image.new('RGBA', (width, height), (0, 0, 0, 0))
|
||||||
# Render the text.
|
# Render the text.
|
||||||
textdraw = ImageDraw.Draw(textimage)
|
textdraw = ImageDraw.Draw(textimage)
|
||||||
textdraw.text((0,0), text, font=font, fill=fill)
|
textdraw.text((0, 0), text, font=font, fill=fill)
|
||||||
# Rotate the text image.
|
# Rotate the text image.
|
||||||
rotated = textimage.rotate(angle, expand=1)
|
rotated = textimage.rotate(angle, expand=1)
|
||||||
# Paste the text into the image, using it as a mask for transparency.
|
# Paste the text into the image, using it as a mask for transparency.
|
||||||
image.paste(rotated, position, rotated)
|
image.paste(rotated, position, rotated)
|
||||||
|
|
||||||
|
|
||||||
# Write two lines of white text on the buffer, rotated 90 degrees counter clockwise.
|
# Write two lines of white text on the buffer, rotated 90 degrees counter clockwise.
|
||||||
draw_rotated_text(img, 'Hello World!', (0, 0), 90, font, fill=(255,255,255))
|
draw_rotated_text(img, 'Hello World!', (0, 0), 90, font, fill=(255, 255, 255))
|
||||||
draw_rotated_text(img, 'This is a line of text.', (10, HEIGHT-10), 0, font, fill=(255,255,255))
|
draw_rotated_text(img, 'This is a line of text.', (10, HEIGHT - 10), 0, font, fill=(255, 255, 255))
|
||||||
|
|
||||||
# Write buffer to display hardware, must be called to make things visible on the
|
# Write buffer to display hardware, must be called to make things visible on the
|
||||||
# display!
|
# display!
|
||||||
|
|||||||
Reference in New Issue
Block a user