-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisp.py
242 lines (191 loc) · 7.34 KB
/
disp.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from machine import Pin, SPI
import framebuf
import utime
import os
# Display resolution
EPD_WIDTH = 800
EPD_HEIGHT = 480
RST_PIN = 12
DC_PIN = 8
CS_PIN = 9
BUSY_PIN = 13
class EPD_7in5(framebuf.FrameBuffer):
def __init__(self):
self.reset_pin = Pin(RST_PIN, Pin.OUT)
self.busy_pin = Pin(BUSY_PIN, Pin.IN, Pin.PULL_UP)
self.cs_pin = Pin(CS_PIN, Pin.OUT)
self.width = EPD_WIDTH
self.height = EPD_HEIGHT
self.spi = SPI(1)
self.spi.init(baudrate=4000_000)
self.dc_pin = Pin(DC_PIN, Pin.OUT)
self.buffer = bytearray(self.height * self.width // 8)
super().__init__(self.buffer, self.width, self.height, framebuf.MONO_HLSB)
self.init()
def digital_write(self, pin, value):
pin.value(value)
def digital_read(self, pin):
return pin.value()
def delay_ms(self, delaytime):
utime.sleep(delaytime / 1000.0)
def spi_writebyte(self, data):
self.spi.write(bytearray(data))
def module_exit(self):
self.digital_write(self.reset_pin, 0)
# Hardware reset
def reset(self):
self.digital_write(self.reset_pin, 1)
self.delay_ms(50)
self.digital_write(self.reset_pin, 0)
self.delay_ms(2)
self.digital_write(self.reset_pin, 1)
self.delay_ms(50)
def send_command(self, command):
self.digital_write(self.dc_pin, 0)
self.digital_write(self.cs_pin, 0)
self.spi_writebyte([command])
self.digital_write(self.cs_pin, 1)
def send_data(self, data):
self.digital_write(self.dc_pin, 1)
self.digital_write(self.cs_pin, 0)
self.spi_writebyte([data])
self.digital_write(self.cs_pin, 1)
def WaitUntilIdle(self):
print("e-Paper busy")
while(self.digital_read(self.busy_pin) == 0): # Wait until the busy_pin goes LOW
self.send_command(0x71)
self.delay_ms(20)
self.delay_ms(20)
print("e-Paper busy release")
def TurnOnDisplay(self):
self.send_command(0x12) # DISPLAY REFRESH
self.delay_ms(100) #!!!The delay here is necessary, 200uS at least!!!
self.WaitUntilIdle()
def init(self):
# EPD hardware init start
self.reset()
self.send_command(0x01) # POWER SETTING
self.send_data(0x07)
self.send_data(0x07) # VGH=20V,VGL=-20V
self.send_data(0x3f) # VDH=15V
self.send_data(0x3f) # VDL=-15V
self.send_command(0x04) # POWER ON
self.delay_ms(100)
self.WaitUntilIdle()
self.send_command(0X00) # PANNEL SETTING
self.send_data(0x1F) # KW-3f KWR-2F BWROTP 0f BWOTP 1f
self.send_command(0x61) # tres
self.send_data(0x03) # source 800
self.send_data(0x20)
self.send_data(0x01) # gate 480
self.send_data(0xE0)
self.send_command(0X15)
self.send_data(0x00)
self.send_command(0X50) # VCOM AND DATA INTERVAL SETTING
self.send_data(0x10)
self.send_data(0x00)
self.send_command(0X60) # TCON SETTING
self.send_data(0x22)
self.send_command(0x65) # Resolution setting
self.send_data(0x00)
self.send_data(0x00) # 800*480
self.send_data(0x00)
self.send_data(0x00)
return 0;
def Clear(self):
high = self.height
if( self.width % 8 == 0) :
wide = self.width // 8
else :
wide = self.width // 8 + 1
self.send_command(0x10)
for j in range(0, high):
for i in range(0, wide):
self.send_data(0xff)
self.send_command(0x13)
for j in range(0, high):
for i in range(0, wide):
self.send_data(0x00)
self.TurnOnDisplay()
def ClearBlack(self):
high = self.height
if( self.width % 8 == 0) :
wide = self.width // 8
else :
wide = self.width // 8 + 1
self.send_command(0x10)
for j in range(0, high):
for i in range(0, wide):
self.send_data(0x00)
self.send_command(0x13)
for j in range(0, high):
for i in range(0, wide):
self.send_data(0xff)
self.TurnOnDisplay()
def display(self,blackimage):
high = self.height
if( self.width % 8 == 0) :
wide = self.width // 8
else :
wide = self.width // 8 + 1
self.send_command(0x10)
for j in range(0, high):
for i in range(0, wide):
self.send_data(blackimage[i + j * wide])
self.send_command(0x13)
for j in range(0, high):
for i in range(0, wide):
self.send_data(blackimage[i + j * wide])
self.TurnOnDisplay()
def sleep(self):
self.send_command(0x02) # power off
self.WaitUntilIdle()
self.send_command(0x07) # deep sleep
self.send_data(0xa5)
if __name__=='__main__':
epd = EPD_7in5()
epd.Clear()
# Create a framebuffer object for the display
buf = bytearray(EPD_WIDTH * EPD_HEIGHT // 8)
fbuf = framebuf.FrameBuffer(buf, EPD_WIDTH, EPD_HEIGHT, framebuf.MONO_HLSB)
rows = EPD_HEIGHT // 160
cols = EPD_WIDTH // 160
input_files = os.listdir("/")
# Iterate over the cropped images
for filename in input_files:
for i in range(rows):
for j in range(cols):
with open(f'{filename}/image_{i}_{j}.bmp', 'rb') as f:
# Read the bmp file
bmp_data = f.read()
# Verify that the file is a BMP file
if bmp_data[:2] != b'BM':
raise ValueError("Invalid BMP file")
# Get the offset of the pixel data
offset = int.from_bytes(bmp_data[10:14], "little")
# Get the width and height of the image
width = int.from_bytes(bmp_data[18:22], "little")
height = int.from_bytes(bmp_data[22:26], "little")
# Get the pixel data
pixels = bmp_data[offset:]
# Copy the pixel data to the framebuffer
for sub_y in range(height-1, -1, -1):
for sub_x in range(width):
# Get the index of the pixel in the pixel data
index = (width * sub_y + sub_x) // 8
# Get the bit value of the pixel
bit = (pixels[index] >> (sub_x % 8)) & 1
# Invert the color of the pixel
inverted_bit = bit ^ 1
# Set the pixel value in the framebuffer
fbuf.pixel(j*width+sub_x, i*height+height-sub_y-1, inverted_bit)
# Update the e-paper display with the new framebuffer
epd.blit(fbuf, 0, 0)
#epd.display(epd.buffer)
epd.display(epd.buffer)
epd.delay_ms(500)
print(filename)
#epd.Clear()
epd.delay_ms(2000)
print("sleep")
epd.sleep()