forked from HenningAust/py_LCD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcdSOM.py
executable file
·220 lines (186 loc) · 4.26 KB
/
lcdSOM.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
from pyA20EVB.gpio import gpio as GPIO
from pyA20EVB.gpio import port
#import A20_GPIO as GPIO
from time import sleep
#from PIL import Image
from font import default_FONT
import struct
# Some constants / parameter
SEND_CHR = 1
SEND_CMD = 2
X_RANGE = 84
#Y_RANGE = 48
Y_RANGE = 50
DEFAULT_CONTRAST = 0xC0
LCD_CACHE_SIZE = X_RANGE*Y_RANGE/8
LCD_Memory = [0 for i in range(LCD_CACHE_SIZE)]
LCD_TEMP = [0 for i in range(X_RANGE)],[0 for i in range(Y_RANGE)]
GPIO.init();
# UEXT2
#pin_CS = port.PI16 # SPI1-CS
#pin_MOSI = port.PI18 #SPI1- MOSI
#pin_MISO = port.PI19 #SPI1 MISO
#pin_CLK = port.PI17 #SPI1 CLK
#pin_RST = port.PB18 #UEXT PIN 5 LCD RESET
#pin_DC = port.PB19 #UEXT PIN 6 LCD DC
# UEXT1
pin_CS = port.PC19 # SPI2-CS
pin_MOSI = port.PC21 #SPI2- MOSI
pin_MISO = port.PC22 #SPI2 MISO
pin_CLK = port.PC20 #SPI2 CLK
pin_RST = port.PB20 #UEXT PIN 5 LCD RESET
pin_DC = port.PB21 #UEXT PIN 6 LCD DC
GPIO.setcfg(pin_CS,GPIO.OUTPUT) # SPI1-CS
GPIO.setcfg(pin_MOSI,GPIO.OUTPUT) #SPI1- MOSI
GPIO.setcfg(pin_MISO,GPIO.INPUT) #SPI1 MISO
GPIO.setcfg(pin_CLK,GPIO.OUTPUT) #SPI1 CLK
GPIO.setcfg(pin_RST,GPIO.OUTPUT) #UEXT PIN 5 LCD RESET
GPIO.setcfg(pin_DC,GPIO.OUTPUT) #UEXT PIN 6 LCD DC
def send(cmd,type):
GPIO.output(pin_CS, GPIO.LOW)
if(type == SEND_CHR):
GPIO.output(pin_DC, GPIO.HIGH)
else:
GPIO.output(pin_DC, GPIO.LOW)
SPIsendByte(cmd)
GPIO.output(pin_CS, GPIO.HIGH)
def SPIsendByte(byteTosend):
i = 0
for i in range(1,9):
if (byteTosend & 0x80):
GPIO.output(pin_MOSI, GPIO.HIGH)
else:
GPIO.output(pin_MOSI, GPIO.LOW)
#sleep(0.00001)
GPIO.output(pin_CLK, GPIO.LOW)
#sleep(0.00001)
GPIO.output(pin_CLK, GPIO.HIGH)
byteTosend = byteTosend << 1
return
def update():
x = 0
y = 0
for y in range(0,Y_RANGE/8):
send(0x80,SEND_CMD)
# send(0x40 | y, SEND_CMD)
send(0x40 | (y+1), SEND_CMD)
for x in range(X_RANGE):
send(LCD_Memory[y*X_RANGE + x],SEND_CHR)
def clear():
for i in range(len(LCD_Memory)):
LCD_Memory[i]=0
def drawPoint(x,y):
row = y/8
i = x + row * 84
LCD_Memory[i] |= 1 << (y % 8)
def drawLine(x1,y1,x2,y2):
dx=abs(x2-x1)
dy=abs(y2-y1)
if(x1 < x2): sx=1
else: sx=-1
if(y1 < y2): sy=1
else: sy=-1
err=dx-dy
while(1):
drawPoint(x1,y1)
if((x1==x2) & (y1==y2)):
break
e2 = 2*err
if(e2 > -dy):
err = err-dy
x1 = x1+sx
if(e2 < dx):
err=err+dx
y1=y1+sy
def drawRect(x1,y1,x2,y2):
drawLine(x1,y1,x1,y2)
drawLine(x1,y1,x2,y1)
drawLine(x2,y1,x2,y2)
drawLine(x1,y2,x2,y2)
def setContrast(contrast):
send(0x21,SEND_CMD)
send(0x14,SEND_CMD)
send(contrast,SEND_CMD)
send(0x20,SEND_CMD)
send(0x0C,SEND_CMD)
def write(string):
font = default_FONT
i=0
for i in string:
tmp = font[i]
for x in tmp:
send(x,SEND_CHR)
def chrXY(x, y, chr):
index=0
offset=0
i=0
font = default_FONT
if(x > X_RANGE): return
if(y > Y_RANGE): return
#index=x*6+y*84
index=x*5+y*84
for i in range(6):
if(i==5):
LCD_Memory[index]=0
break
offset = font[chr][i]
LCD_Memory[index]=offset
index= index +1
def chrXYInverse(x, y, chr):
index=0
offset=0
i=0
font = default_FONT
if(x > X_RANGE): return
if(y > Y_RANGE): return
index=x*5+y*84
for i in range(6):
if(i==5):
LCD_Memory[index]=255
break
offset = ~(font[chr][i])
LCD_Memory[index]=offset
index= index +1
def str(row,string,inv=0):
x = 0
for i in string:
if(inv):
chrXYInverse(x,row,i)
else:
chrXY(x,row,i)
x=x+1
#update()
def longstr(row,string,inv=0):
x = 0
for i in string:
if(inv):
chrXYInverse(x,row,i)
else:
chrXY(x,row,i)
x=x+1
if(x==16):
row=row+1
x=0
# Reset LCD
def init():
# SET CS HIGH
GPIO.output(pin_CS, GPIO.HIGH)
#Reset LCD
GPIO.output(pin_RST, GPIO.LOW)
sleep(1)
GPIO.output(pin_RST, GPIO.HIGH)
sleep(1)
send(0x21,SEND_CMD)
send(0xC8,SEND_CMD)
send(0x04,SEND_CMD)
send(0x40,SEND_CMD)
send(0x12,SEND_CMD)
send(0xE4,SEND_CMD)# Set Display offset line 1
send(0x45,SEND_CMD)# Set Display offset line 2: shiftet 5 pixels up and then use lines 1 to 6
send(0x20,SEND_CMD)
send(0x08,SEND_CMD)
send(0x0C,SEND_CMD)
setContrast(DEFAULT_CONTRAST)
clear()
update()