forked from execuc/u2if
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd_time_ssd1306_i2c.py
66 lines (53 loc) · 2.11 KB
/
lcd_time_ssd1306_i2c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Example and ssd1306 driver derived from https://github.com/adafruit/Adafruit_CircuitPython_SSD1306, which is licensed :
# SPDX-FileCopyrightText: Melissa LeBlanc-Williams for Adafruit Industries
# SPDX-License-Identifier: MIT
import time
from PIL import Image, ImageDraw, ImageFont
from external.ssd1306 import SSD1306_I2C, SET_DISP_START_LINE
from machine import I2C
# Connect an I2C SSD1306 LCD (128x64)
def show_image(display, image, draw, font, font2):
# write the current time to the display after each scroll
draw.rectangle((0, 0, display.width, display.height * 2), outline=0, fill=0)
text = time.strftime("%A")
draw.text((0, 0), text, font=font, fill=255)
text = time.strftime("%e %b %Y")
draw.text((0, 14), text, font=font, fill=255)
text = time.strftime("%X")
draw.text((0, 36), text, font=font2, fill=255)
display.image(image)
display.show()
# Use second I2C port
i2c = I2C(i2c_index=1, frequency=400000) # , pullup=True
display = SSD1306_I2C(128, 64, i2c)
# Clear the display. Always call show after changing pixels to make the display
# update visible!
display.fill(0)
display.show()
time.sleep(0.5)
display.fill(1)
display.show()
time.sleep(0.5)
display.fill(0)
display.show()
# # Set a pixel in the origin 0,0 position.
# display.pixel(0, 0, 1)
# # Set a pixel in the middle 64, 16 position.
# display.pixel(64, 32, 1)
# # Set a pixel in the opposite 127, 31 position.
# display.pixel(127, 63, 1)
# display.show()
image = Image.new("1", (display.width, display.height))
draw = ImageDraw.Draw(image)
# Load a font in 2 different sizes.
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 16)
font2 = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 24)
offset = 0 # flips between 0 and 32 for double buffering
while True:
for i in range(0, display.height // 2):
offset = (offset + 1) % display.height
display.write_cmd(SET_DISP_START_LINE | offset)
show_image(display, image, draw, font, font2)
for i in range(0, display.height // 2):
show_image(display, image, draw, font, font2)
time.sleep(0.05)