Skip to content

Commit 9311a39

Browse files
Add files via upload
1 parent c16bf0d commit 9311a39

File tree

7 files changed

+403
-1
lines changed

7 files changed

+403
-1
lines changed

PCD8544.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# PCD8544.py
2+
3+
from machine import Pin, SPI
4+
from array import array
5+
import time
6+
7+
class PCD8544():
8+
def __init__(self, rst=14, ce=13, dc=12, dout=0, din=5, clk=4):
9+
self._rst = Pin(rst, Pin.OUT) # 14
10+
self._ce = Pin(ce, Pin.OUT) # 13
11+
self._ce.high()
12+
self._dc = Pin(dc, Pin.OUT) # 12
13+
self._dc.high()
14+
15+
self._dout = dout # 0 MISO - not connected
16+
self._din = din # 5 MOSI
17+
self._clk = clk # 4 SCLK
18+
self._row = 0
19+
self._col = 0
20+
self._x = 0
21+
self._y = 0
22+
self.clear()
23+
24+
# SPI
25+
26+
self._spi = SPI(baudrate=100000, polarity=1, phase=0, sck=Pin(self._clk), mosi=Pin(self._din), miso=Pin(self._dout))
27+
self._spi.init(baudrate=200000) # set the baudrate
28+
29+
def command(self,c):
30+
b = bytearray(1)
31+
b[0] = c
32+
self._dc.low()
33+
self._ce.low()
34+
self._spi.write(b) # write 1 byte on MOSI
35+
self._ce.high()
36+
37+
38+
def data(self, data):
39+
b = bytearray(1)
40+
b[0] = c
41+
self._dc.high()
42+
self._ce.low()
43+
self._spi.write(b) # write 1 byte on MOSI
44+
self._ce.high()
45+
46+
def reset(self):
47+
self._rst.low()
48+
time.sleep_ms(50) # sleep for 50 milliseconds
49+
self._rst.high()
50+
51+
# begin
52+
def begin(self):
53+
self.reset()
54+
self.command(0x21) # extended command
55+
self.command(0xB1) # set contrast
56+
self.command(0x13) # bias
57+
self.command(0x04) # temp coeff
58+
self.command(0x20) # normal moded._x
59+
self.command(0x0C) # not inverted
60+
self.display()
61+
62+
# display
63+
def display(self):
64+
self.command(0x40)
65+
self.command(0x80)
66+
self._dc.high()
67+
self._ce.low()
68+
self._spi.write(self._buf)
69+
self._ce.high
70+
71+
def p_char(self, ch):
72+
fp = (ord(ch)-0x20) * 5
73+
char_buf = array('b',[0,0,0,0,0])
74+
f = open('font5x7.fnt','rb')
75+
f.seek(fp)
76+
char_buf = f.read(5)
77+
bp = 84*self._row + 6*self._col
78+
for x in range (0,5):
79+
self._buf[bp+x] = char_buf[x]
80+
self._buf[bp+5] = 0 # put in inter char space
81+
self._col += 1
82+
if (self._col>13):
83+
self._col = 0
84+
self._row += 1
85+
if (self._row>5):
86+
self._row = 0
87+
88+
def p_string(self, str):
89+
for ch in (str):
90+
self.p_char(ch)
91+
92+
def clear(self):
93+
self._buf= bytearray(84 * int(48 / 8))
94+
self._row = 0
95+
self._col = 0
96+
97+
def pixel(self,x,y,fill):
98+
r = int(y/8)
99+
i = r * 84 + x
100+
b = y % 8
101+
self._buf[i] = self._buf[i] | ( 1 << b )
102+

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1-
# MicroPython_PCD8544
1+
ESP8266 Micropython driver for PCD8544. Typically used in the Nokia 5110 display.
2+
3+
**font5x7.fnt** is a 5 x 7 font file. It holds the font in pure binary and so uses only 480 bytes
4+
5+
When the display needs a character it reads the five bytes it needs from the file rather than loading the whole font into memory
6+
7+
invoke the display with:
8+
9+
from PCD8544 import PCD8544
10+
11+
if you use different pins then you will need to specify these in the inital call
12+
13+
d = PCD8544()
14+
15+
d.reset()
16+
17+
d.begin() # displays the Project Pages logo
18+
19+
d.clear() # clears the display buffer
20+
21+
d.display() # writes the buffer to the actual display
22+
23+
d._row is the character row
24+
25+
d._col is the character column
26+
27+
d.p_char('x')
28+
* puts the character into the display buffer
29+
* advances _row and _col accoringly. They will wrap back to the top of the screen
30+
* requires d.display() to show it
31+
32+
d.p_string('hello world')
33+
* prints the string to the display buffer
34+
* advances _row and _col accoringly. They will wrap back to the top of the screen
35+
* requires d.display() to show it
36+
37+
d.pixel(x,y,fill)
38+
* sets a pixel in the display buffer
39+
* this is for use by the lcd_gfx.py
40+
* this allows you to draw lines, rectangles, triangles and circles. Filled or not
41+
42+
I have added a main.py that demonstrates the string printing and drawing capabilities
43+

bmp.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def bmp(fname, display):
2+
f=open(fname,'rb')
3+
b=bytearray(54)
4+
b=f.read(54)
5+
# header check
6+
if b[0]==0x42 and b[1]==0x4D:
7+
# is bitmap
8+
size = b[2] + (b[3]<<8) + (b[4]<<16) +(b[5]<<24)
9+
offset = b[10] + (b[11]<<8) + (b[12]<<16) +(b[13]<<24)
10+
width = b[18] + (b[19]<<8) + (b[20]<<16) +(b[21]<<24)
11+
height = b[22] + (b[23]<<8) + (b[24]<<16) +(b[25]<<24)
12+
color_planes = b[26] + (b[27]<<8)
13+
bits_per_pixel = b[28] + (b[29]<<8)
14+
compression = b[30] + (b[31]<<8) + (b[32]<<16) +(b[33]<<24)
15+
image_size = b[34] + (b[35]<<8) + (b[36]<<16) +(b[37]<<24)
16+
17+
f.seek(offset)
18+
19+
row_bytes = int(bits_per_pixel/8) * width
20+
# Add up to multiple of 4
21+
if row_bytes % 4 > 0:
22+
row_bytes += 4 - row_bytes % 4
23+
24+
buffer = bytearray(row_bytes)
25+
for row in range(height):
26+
# print(row)
27+
# read in a whole row
28+
buffer=f.read(row_bytes)
29+
index = 0
30+
for index in range(width):
31+
y = (height-1) - row
32+
x = index
33+
if buffer[index*3]!=0xff:
34+
display.pixel(x,y,1)
35+
f.close()
36+

font5x7.fnt

480 Bytes
Binary file not shown.

icon.bmp

11.5 KB
Binary file not shown.

lcd_gfx.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# http://forum.micropython.org/viewtopic.php?f=5&t=195&p=873&hilit=lcd_gfx#p873
2+
#adaptiert von Adafruit_GFX.cpp
3+
#for LCD and touch-sensor skin
4+
5+
6+
def drawLine(x0,y0,x1,y1,thelcd,fill):
7+
steep= abs(y1 - y0) > abs(x1 - x0);
8+
if steep:
9+
x0, y0 = y0, x0
10+
x1, y1 = y1, x1
11+
if x0>x1:
12+
x0, x1 = x1, x0
13+
y0, y1 = y1, y0
14+
dx=x1-x0
15+
dy=abs(y1-y0)
16+
err=dx/2
17+
ystep=-1
18+
if y0 < y1: ystep = 1
19+
for xx in range(x0, x1):
20+
if steep:
21+
thelcd.pixel(y0,xx,fill)
22+
else:
23+
thelcd.pixel(xx,y0,fill)
24+
err-=dy
25+
if err<0:
26+
y0+=ystep
27+
err+=dx
28+
29+
def drawTrie(x0,y0,x1,y1,x2,y2,thelcd,fill):
30+
drawLine(x0,y0,x1,y1,thelcd,fill)
31+
drawLine(x2,y2,x1,y1,thelcd,fill)
32+
drawLine(x2,y2,x0,y0,thelcd,fill)
33+
34+
def drawFillTrie(x0,y0,x1,y1,x2,y2,thelcd,fill):
35+
if y0 > y1:
36+
y0, y1=y1, y0
37+
x0, x1=x1, x0
38+
39+
if y1 > y2:
40+
y2, y1=y1,y2
41+
x2, x1=x1,x2
42+
43+
if y0 > y1:
44+
y0, y1=y1,y0
45+
x0, x1=x1,x0
46+
47+
if y0 == y2:
48+
a = x0
49+
b = x0
50+
if x1 < a:
51+
a = x1
52+
else:
53+
if x1 > b:
54+
b = x1
55+
if x2 < a:
56+
a = x2
57+
else:
58+
if x2 > b:
59+
b = x2
60+
drawLine(a, y0,b+1,y0,thelcd,fill)
61+
return
62+
63+
dx01 = x1 - x0
64+
dy01 = y1 - y0
65+
dx02 = x2 - x0
66+
dy02 = y2 - y0
67+
dx12 = x2 - x1
68+
dy12 = y2 - y1
69+
sa = 0
70+
sb = 0
71+
72+
if y1 == y2:
73+
last = y1
74+
else:
75+
last = y1-1
76+
77+
y=y0
78+
79+
for y in range(y0, last+1):
80+
a= x0 + sa / dy01
81+
b= x0 + sb / dy02
82+
sa += dx01
83+
sb += dx02
84+
if a > b:
85+
a,b=b,a
86+
drawLine(int(a), y,int(b+1),y,thelcd,fill)
87+
88+
sa = dx12 * (y - y1)
89+
sb = dx02 * (y - y0)
90+
91+
for y in range(last+1, y2+1):
92+
a = x1 + sa / dy12
93+
b = x0 + sb / dy02
94+
sa += dx12
95+
sb += dx02
96+
if a > b:
97+
a,b=b,a
98+
drawLine(int(a), y,int(b+1),y,thelcd,fill)
99+
100+
101+
def drawRect(x,y,w,h,thelcd,fill):
102+
drawLine(x,y,x+w,y,thelcd,fill)
103+
drawLine(x+w-1,y,x+w-1,y+h,thelcd,fill)
104+
drawLine(x+w,y+h-1,x,y+h-1,thelcd,fill)
105+
drawLine(x,y+h,x,y,thelcd,fill)
106+
107+
def drawFillRect(x,y,w,h,thelcd,fill):
108+
xa=x
109+
xe=x+w
110+
ya=y
111+
ye=y+h
112+
if xa>xe:
113+
xa,xe = xe,xa
114+
115+
if ya>ye:
116+
ya,ye=ye,ya
117+
118+
for yy in range(ya, ye):
119+
for xx in range(xa,xe):
120+
thelcd.pixel(xx,yy,fill)
121+
122+
def drawCircle(x0,y0,r,thelcd,fill):
123+
f=1-r
124+
ddF_x = 1
125+
ddF_y = -2 * r
126+
x = 0
127+
y = r
128+
thelcd.pixel(x0 , y0+r,fill)
129+
thelcd.pixel(x0 , y0-r,fill)
130+
thelcd.pixel(x0+r, y0 ,fill)
131+
thelcd.pixel(x0-r, y0 ,fill)
132+
while x<y:
133+
if f>=0:
134+
y-=1
135+
ddF_y+=2
136+
f+=ddF_y
137+
x+=1
138+
ddF_x+=2
139+
f+=ddF_x
140+
thelcd.pixel(x0+x, y0+y, fill);
141+
thelcd.pixel(x0-x, y0+y, fill);
142+
thelcd.pixel(x0+x, y0-y, fill);
143+
thelcd.pixel(x0-x, y0-y, fill);
144+
thelcd.pixel(x0+y, y0+x, fill);
145+
thelcd.pixel(x0-y, y0+x, fill);
146+
thelcd.pixel(x0+y, y0-x, fill);
147+
thelcd.pixel(x0-y, y0-x, fill);
148+
149+
def drawfillCircle(x0,y0,r,thelcd,fill):
150+
drawLine(x0, y0-r, x0,y0-r+2*r+1,thelcd, fill)
151+
f = 1 - r
152+
ddF_x = 1
153+
ddF_y = -2 * r
154+
x = 0
155+
y = r
156+
while x<y:
157+
if f>=0:
158+
y-=1
159+
ddF_y+=2
160+
f+=ddF_y
161+
x+=1
162+
ddF_x+=2
163+
f+=ddF_x
164+
drawLine(x0+x, y0-y, x0+x, y0-y+2*y+1,thelcd, fill);
165+
drawLine(x0+y, y0-x, x0+y, y0-x+2*x+1,thelcd, fill);
166+
drawLine(x0-x, y0-y, x0-x, y0-y+2*y+1,thelcd, fill);
167+
drawLine(x0-y, y0-x, x0-y, y0-x+2*x+1,thelcd, fill);
168+

0 commit comments

Comments
 (0)