-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathOLED_2in42_test.py
More file actions
69 lines (60 loc) · 1.97 KB
/
OLED_2in42_test.py
File metadata and controls
69 lines (60 loc) · 1.97 KB
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
#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os
import pdb
dir = os.getcwd()
libdir = os.path.join(dir,'waveshare_OLED/lib')
picdir = os.path.join(dir,'waveshare_OLED/pic')
fontdir = os.path.join(dir,'waveshare_OLED/fonts')
print(libdir)
print(picdir)
print(fontdir)
if os.path.exists(libdir):
sys.path.append(libdir)
import logging
import time
import traceback
from waveshare_OLED.lib import OLED_2in42
from PIL import Image,ImageDraw,ImageFont
logging.basicConfig(level=logging.DEBUG)
try:
#pdb.set_trace()
disp = OLED_2in42.OLED_2in42(spi_freq = 1000000)
logging.info("\r2.42inch OLED ")
# Initialize library.
disp.Init()
# Clear display.
logging.info("clear display")
disp.clear()
# Create blank image for drawing.
image1 = Image.new('1', (disp.width, disp.height), "WHITE")
draw = ImageDraw.Draw(image1)
font1 = ImageFont.truetype(os.path.join(fontdir, 'Font.ttc'), 18)
font2 = ImageFont.truetype(os.path.join(fontdir, 'Font.ttc'), 24)
logging.info ("***draw line")
draw.line([(0,0),(127,0)], fill = 0)
draw.line([(0,0),(0,63)], fill = 0)
draw.line([(0,63),(127,63)], fill = 0)
draw.line([(127,0),(127,63)], fill = 0)
logging.info ("***draw text")
draw.text((20,0), 'Waveshare ', font = font1, fill = 0)
draw.text((20,24), u'微雪电子 ', font = font2, fill = 0)
image1 = image1.rotate(180)
disp.ShowImage(disp.getbuffer(image1))
time.sleep(3)
logging.info ("***draw image")
Himage2 = Image.new('1', (disp.width, disp.height), 255) # 255: clear the frame
#bmp = Image.open(os.path.join(picdir, 'waveshare.bmp'))
bmp = Image.open(os.path.join(picdir, 'raspberry-pi-logo.bmp'))
Himage2.paste(bmp, (0,0))
Himage2=Himage2.rotate(180)
disp.ShowImage(disp.getbuffer(Himage2))
time.sleep(3)
disp.clear()
except IOError as e:
logging.info(e)
except KeyboardInterrupt:
logging.info("ctrl + c:")
disp.module_exit()
exit()