-
Notifications
You must be signed in to change notification settings - Fork 14
/
ssd1306_i2c.py
82 lines (60 loc) · 2.83 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# https://learn.adafruit.com/micropython-hardware-ssd1306-oled-display/software
import time
from machine import I2C, Pin
from .ssd1306 import SSD1306_I2C
class Display:
def __init__(self,
width = 128, height = 64,
scl_pin_id = 15, sda_pin_id = 4,
freq = 400000):
self.width = width
self.height = height
self.poweron()
self.i2c = I2C(scl = Pin(scl_pin_id, Pin.OUT),
sda = Pin(sda_pin_id),
freq = freq)
self.display = SSD1306_I2C(width, height, self.i2c)
self.show = self.display.show
def poweron(self, pin=16):
pin_reset = Pin(pin, mode=Pin.OUT)
pin_reset.value(0)
time.sleep_ms(50)
pin_reset.value(1)
def poweroff(self, pin=16):
pin_reset = Pin(pin, mode=Pin.OUT)
pin_reset.value(0)
def clear(self):
self.display.fill(0)
self.display.show()
def show_text(self, text, x = 0, y = 0, clear_first = True, show_now = True, hold_seconds = 0):
if clear_first: self.display.fill(0)
self.display.text(text, x, y)
if show_now:
self.display.show()
if hold_seconds > 0: time.sleep(hold_seconds)
def wrap(self, text, start_line = 0,
height_per_line = 8, width_per_char = 8,
start_pixel_each_line = 0):
chars_per_line = self.width//width_per_char
max_lines = self.height//height_per_line - start_line
lines = [(text[chars_per_line*line: chars_per_line*(line+1)], start_pixel_each_line, height_per_line*(line+start_line))
for line in range(max_lines)]
return lines
def show_text_wrap(self, text,
start_line = 0, height_per_line = 8, width_per_char = 8, start_pixel_each_line = 0,
clear_first = True, show_now = True, hold_seconds = 0):
if clear_first: self.clear()
for line, x, y in self.wrap(text, start_line, height_per_line, width_per_char, start_pixel_each_line):
self.show_text(line, x, y, clear_first = False, show_now = False)
if show_now:
self.display.show()
if hold_seconds > 0: time.sleep(hold_seconds)
def show_datetime(self, year, month, day, hour, minute, second):
datetime = [year, month, day, hour, minute, second]
datetime_str = ["{0:0>2}".format(d) for d in datetime]
self.show_text(text = '-'.join(datetime_str[:3]),
x = 0, y = 0, clear_first = True, show_now = False)
self.show_text(text = ':'.join(datetime_str[3:6]),
x = 0, y = 10, clear_first = False, show_now = True)
def show_time(self, year, month, day, hour, minute, second):
self.show_datetime(year, month, day, hour, minute, second)