Skip to content

Commit f448d32

Browse files
author
Vincent Pillet
committed
First commit
1 parent 0526fdc commit f448d32

12 files changed

+384
-0
lines changed

Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python
2+
RUN apt-get update && apt-get -y install python3-pil python3-numpy python3-smbus
3+
WORKDIR /data
4+
COPY . .
5+
RUN pip install -r requirements.txt
6+
CMD ["python", "example/OLED_1in5_rgb_test.py"]

example/OLED_1in5_rgb_test.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/python
2+
# -*- coding:utf-8 -*-
3+
4+
import sys
5+
import os
6+
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
7+
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
8+
if os.path.exists(libdir):
9+
sys.path.append(libdir)
10+
11+
import logging
12+
import time
13+
import traceback
14+
from waveshare_OLED import OLED_1in5_rgb
15+
from PIL import Image,ImageDraw,ImageFont
16+
logging.basicConfig(level=logging.DEBUG)
17+
18+
try:
19+
disp = OLED_1in5_rgb.OLED_1in5_rgb()
20+
21+
logging.info("\r 1.5inch rgb OLED ")
22+
# Initialize library.
23+
disp.Init()
24+
# Clear display.
25+
logging.info("clear display")
26+
disp.clear()
27+
28+
# Create blank image for drawing.
29+
image1 = Image.new('RGB', (disp.width, disp.height), 0)
30+
draw = ImageDraw.Draw(image1)
31+
font = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 12)
32+
font1 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
33+
font2 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
34+
logging.info ("***draw line")
35+
draw.line([(0,0),(127,0)], fill = "RED")
36+
draw.line([(0,0),(0,127)], fill = "RED")
37+
draw.line([(0,127),(127,127)], fill = "RED")
38+
draw.line([(127,0),(127,127)], fill = "RED")
39+
logging.info ("***draw text")
40+
draw.text((20,0), 'Waveshare ', font = font1, fill = "BLUE")
41+
draw.text((20,24), u'微雪电子 ', font = font2, fill = "MAGENTA")
42+
draw.text((20,64), 'Waveshare ', font = font1, fill = "CYAN")
43+
draw.text((20,92), u'微雪电子 ', font = font2, fill = "GREEN")
44+
image1 = image1.rotate(0)
45+
disp.ShowImage(disp.getbuffer(image1))
46+
time.sleep(3)
47+
48+
logging.info ("***draw rectangle")
49+
image1 = Image.new("RGB", (disp.width, disp.height), "BLACK")
50+
draw = ImageDraw.Draw(image1)
51+
draw.line([(0,8), (127,8)], fill = "RED", width = 16)
52+
draw.line([(0,24),(127,24)], fill = "YELLOW", width = 16)
53+
draw.line([(0,40),(127,40)], fill = "GREEN", width = 16)
54+
draw.line([(0,56),(127,56)], fill = "CYAN", width = 16)
55+
draw.line([(0,72),(127,72)], fill = "BLUE", width = 16)
56+
draw.line([(0,88),(127,88)], fill = "MAGENTA",width = 16)
57+
draw.line([(0,104),(127,104)],fill = "BLACK", width = 16)
58+
draw.line([(0,120),(127,120)],fill = "WHITE", width = 16)
59+
disp.ShowImage(disp.getbuffer(image1))
60+
time.sleep(3)
61+
62+
logging.info ("***draw image")
63+
Himage2 = Image.new('RGB', (disp.width, disp.height), 0) # 0: clear the frame
64+
bmp = Image.open(os.path.join(picdir, '1in5_rgb.bmp'))
65+
Himage2.paste(bmp, (0,0))
66+
Himage2=Himage2.rotate(0)
67+
disp.ShowImage(disp.getbuffer(Himage2))
68+
time.sleep(3)
69+
70+
disp.clear()
71+
72+
except IOError as e:
73+
logging.info(e)
74+
75+
except KeyboardInterrupt:
76+
logging.info("ctrl + c:")
77+
disp.module_exit()
78+
exit()

example/setup.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
from setuptools import setup
3+
setup(
4+
name='waveshare-OLED',
5+
description='Waveshare OLED Display',
6+
author='Waveshare',
7+
package_dir={'': 'lib'},
8+
packages=['waveshare_OLED'],
9+
)
10+
+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# /*****************************************************************************
2+
# * | File : OLED_1in5_rgb.py
3+
# * | Author : Waveshare team
4+
# * | Function : Driver for OLED_1in5_rgb
5+
# * | Info :
6+
# *----------------
7+
# * | This version: V2.0
8+
# * | Date : 2020-08-17
9+
# * | Info :
10+
# ******************************************************************************/
11+
# Permission is hereby granted, free of charge, to any person obtaining a copy
12+
# of this software and associated documnetation files (the "Software"), to deal
13+
# in the Software without restriction, including without limitation the rights
14+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
# copies of the Software, and to permit persons to whom the Software is
16+
# furished to do so, subject to the following conditions:
17+
#
18+
# The above copyright notice and this permission notice shall be included in
19+
# all copies or substantial portions of the Software.
20+
#
21+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
# THE SOFTWARE.
28+
#
29+
30+
from . import config
31+
import time
32+
import numpy as np
33+
34+
Device_SPI = config.Device_SPI
35+
Device_I2C = config.Device_I2C
36+
37+
OLED_WIDTH = 128 #OLED width
38+
OLED_HEIGHT = 128 #OLED height
39+
40+
class OLED_1in5_rgb(config.RaspberryPi):
41+
42+
""" Write register address and data """
43+
def command(self, cmd):
44+
self.digital_write(self.DC_PIN,False)
45+
self.spi_writebyte([cmd])
46+
47+
""" Write data """
48+
def data(self, data):
49+
self.digital_write(self.DC_PIN,True)
50+
self.spi_writebyte([data])
51+
52+
def Init(self):
53+
if (self.module_init() != 0):
54+
return -1
55+
56+
self.width = OLED_WIDTH
57+
self.height = OLED_HEIGHT
58+
59+
"""Initialize dispaly"""
60+
self.reset()
61+
62+
if(self.Device == Device_I2C):
63+
print ("Only Device_SPI, Please revise config.py !!!")
64+
exit()
65+
66+
self.command(0xfd) # command lock
67+
self.data(0x12)
68+
self.command(0xfd) # command lock
69+
self.data(0xB1)
70+
71+
self.command(0xae) # display off
72+
self.command(0xa4) # Normal Display mode
73+
74+
self.command(0x15) # set column address
75+
self.data(0x00) # column address start 00
76+
self.data(0x7f) # column address end 127
77+
self.command(0x75) # set row address
78+
self.data(0x00) # row address start 00
79+
self.data(0x7f) # row address end 127
80+
81+
self.command(0xB3)
82+
self.data(0xF1)
83+
84+
self.command(0xCA)
85+
self.data(0x7F)
86+
87+
self.command(0xa0) # set re-map & data format
88+
self.data(0x74) # Horizontal address increment
89+
90+
self.command(0xa1) # set display start line
91+
self.data(0x00) # start 00 line
92+
93+
self.command(0xa2) # set display offset
94+
self.data(0x00)
95+
96+
self.command(0xAB)
97+
self.command(0x01)
98+
99+
self.command(0xB4)
100+
self.data(0xA0)
101+
self.data(0xB5)
102+
self.data(0x55)
103+
104+
self.command(0xC1)
105+
self.data(0xC8)
106+
self.data(0x80)
107+
self.data(0xC0)
108+
109+
self.command(0xC7)
110+
self.data(0x0F)
111+
112+
self.command(0xB1)
113+
self.data(0x32)
114+
115+
self.command(0xB2)
116+
self.data(0xA4)
117+
self.data(0x00)
118+
self.data(0x00)
119+
120+
self.command(0xBB)
121+
self.data(0x17)
122+
123+
self.command(0xB6)
124+
self.data(0x01)
125+
126+
self.command(0xBE)
127+
self.data(0x05)
128+
129+
self.command(0xA6)
130+
131+
time.sleep(0.1)
132+
self.command(0xAF);#--turn on oled panel
133+
134+
135+
def reset(self):
136+
"""Reset the display"""
137+
self.digital_write(self.RST_PIN,True)
138+
time.sleep(0.1)
139+
self.digital_write(self.RST_PIN,False)
140+
time.sleep(0.1)
141+
self.digital_write(self.RST_PIN,True)
142+
time.sleep(0.1)
143+
144+
def clear(self):
145+
_buffer = [0x00]*(self.width * self.height * 2)
146+
self.ShowImage(_buffer)
147+
148+
def getbuffer(self, image):
149+
buf = [0x00] * ((self.width*2) * self.height)
150+
imwidth, imheight = image.size
151+
pixels = image.load()
152+
for y in range(imheight):
153+
for x in range(imwidth):
154+
# Set the bits for the column of pixels at the current position.
155+
buf[x*2 + y*imwidth*2] = ((pixels[x,y][0] & 0xF8) | (pixels[x,y][1] >> 5))
156+
buf[x*2+1 + y*imwidth*2] = (((pixels[x,y][1]<<3) & 0xE0) | (pixels[x,y][2] >> 3))
157+
return buf
158+
159+
def ShowImage(self, pBuf):
160+
self.command(0x15) # set column address
161+
self.data(0x00) # column address start 00
162+
self.data(0x7f) # column address end 127
163+
self.command(0x75) # set row address
164+
self.data(0x00) # row address start 00
165+
self.data(0x7f) # row address end 127
166+
self.command(0x5C);
167+
for i in range(0, self.height):
168+
for j in range(0, self.width*2):
169+
self.data(pBuf[j + self.width*2*i])
170+
return
171+
172+

example/waveshare_OLED/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.

example/waveshare_OLED/config.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# /*****************************************************************************
2+
# * | File : config.py
3+
# * | Author : Waveshare team
4+
# * | Function : Hardware underlying interface,for Raspberry pi
5+
# * | Info :
6+
# *----------------
7+
# * | This version: V1.0
8+
# * | Date : 2020-06-17
9+
# * | Info :
10+
# ******************************************************************************/
11+
# Permission is hereby granted, free of charge, to any person obtaining a copy
12+
# of this software and associated documnetation files (the "Software"), to deal
13+
# in the Software without restriction, including without limitation the rights
14+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
# copies of the Software, and to permit persons to whom the Software is
16+
# furished to do so, subject to the following conditions:
17+
#
18+
# The above copyright notice and this permission notice shall be included in
19+
# all copies or substantial portions of the Software.
20+
#
21+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
# THE SOFTWARE.
28+
#
29+
30+
31+
import time
32+
from smbus import SMBus
33+
import spidev
34+
import ctypes
35+
from gpiozero import *
36+
37+
Device_SPI = 1
38+
Device_I2C = 0
39+
40+
class RaspberryPi:
41+
def __init__(self,spi=spidev.SpiDev(0,0),spi_freq=10000000,rst = 27,dc = 25,bl = 18,bl_freq=1000,i2c=None):
42+
self.INPUT = False
43+
self.OUTPUT = True
44+
45+
self.SPEED =spi_freq
46+
47+
if(Device_SPI == 1):
48+
self.Device = Device_SPI
49+
self.spi = spi
50+
else :
51+
self.Device = Device_I2C
52+
self.address = 0x3c
53+
self.bus = SMBus(1)
54+
55+
self.RST_PIN = self.gpio_mode(rst,self.OUTPUT)
56+
self.DC_PIN = self.gpio_mode(dc,self.OUTPUT)
57+
58+
59+
def delay_ms(self,delaytime):
60+
time.sleep(delaytime / 1000.0)
61+
62+
def gpio_mode(self,Pin,Mode,pull_up = None,active_state = True):
63+
if Mode:
64+
return DigitalOutputDevice(Pin,active_high = True,initial_value =False)
65+
else:
66+
return DigitalInputDevice(Pin,pull_up=pull_up,active_state=active_state)
67+
68+
def digital_write(self, Pin, value):
69+
if value:
70+
Pin.on()
71+
else:
72+
Pin.off()
73+
74+
def digital_read(self, Pin):
75+
return Pin.value
76+
77+
def spi_writebyte(self,data):
78+
self.spi.writebytes([data[0]])
79+
80+
def i2c_writebyte(self,reg, value):
81+
self.bus.write_byte_data(self.address, reg, value)
82+
83+
def module_init(self):
84+
self.digital_write(self.RST_PIN,False)
85+
if(self.Device == Device_SPI):
86+
self.spi.max_speed_hz = self.SPEED
87+
self.spi.mode = 0b11
88+
self.digital_write(self.DC_PIN,False)
89+
return 0
90+
91+
def module_exit(self):
92+
if(self.Device == Device_SPI):
93+
self.spi.close()
94+
else :
95+
self.bus.close()
96+
self.digital_write(self.RST_PIN,False)
97+
self.digital_write(self.DC_PIN,False)
98+
99+
### END OF FILE ###

example/waveshare_OLED/setup.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
from setuptools import setup
3+
setup(
4+
name='waveshare-OLED',
5+
description='Waveshare OLED Display',
6+
author='Waveshare',
7+
package_dir={'': 'lib'},
8+
packages=['waveshare_OLED'],
9+
)
10+

go.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
docker build -t oled .

requirements.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
smbus
2+
spidev
3+
gpiozero
4+
numpy
5+
pillow
6+
lgpio
7+
RPi.GPIO

0 commit comments

Comments
 (0)