Last active
March 9, 2024 09:38
-
-
Save julien123123/9ec01d29f1dbf63ccfee3fc34e533984 to your computer and use it in GitHub Desktop.
Example for the use of Phoreglad's Micropython Pico e-Paper library for power saving. The same method should work with any e-Paper display driven by the SSD1677 IC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Pico_ePaper partial update/deepsleep demo | |
#by Julien Croteau | |
# 08/03/2024 | |
# This file should work with the files of the examples filder of the Pico e-Paper(https://github.com/phoreglad/pico-epaper) library downloaded to your board | |
# Rename this file 'main.py' if you want the deepsleep part to work. | |
from Pico_ePaper import Eink | |
from writer import Writer | |
from machine import Pin, SPI, deepsleep, reset_cause, | |
import time, json, ubinascii, freesans20, framebuf | |
# EPD setup | |
p = Pin(2, Pin.OUT) #To restet the epd | |
epdSPI = SPI(2, sck=Pin(12), baudrate=400000, mosi=Pin(13), miso=None) #SPI instance fpr E-paper display (miso Pin necessary for SoftSPI, but not needed) | |
epd = Eink(rotation=90, spi=epdSPI, cs_pin=Pin(10), dc_pin=Pin(09), reset_pin=p, busy_pin=Pin(11), use_partial_buffer=True) #Epaper setup (instance of EINK) | |
class Dum(framebuf.FrameBuffer): | |
def __init__(self,width,height,buf_format): | |
self.width = width | |
self.height = height | |
self._buf = bytearray(self.width * self.height // 8) | |
super().__init__(self._buf, self.width, self.height, buf_format) | |
self.fill(1) | |
dummy = Dum(epd.width, epd.height, framebuf.MONO_HLSB) #Dummy is an instance of DUM | |
dummyPart = Dum(epd.width, epd.height, framebuf.MONO_HLSB) #DummyPart for partial refresh | |
fake = Dum(epd.width, epd.height, framebuf.MONO_VLSB) #This is Mono_VLSB because I found that it's the format that works for horizontal orientation | |
running = True | |
while running: | |
if reset_cause() == 4: # If esp32 just woke up from deep sleep | |
### Update after deep sleep ### | |
try: | |
with open('buf.json', 'r') as fp: | |
json_data = json.load(fp) | |
fp.close() | |
except: | |
print("did not work!") | |
lb = bytearray(ubinascii.a2b_base64(json_data['last_buf'])) #load last partial update buffer and decode it | |
epd.partial_mode_on() | |
epd._send_command(0x26) | |
epd._send_buffer(lb) # send last partial update buffer so that it can do differential update | |
msg = "Well this has been fun!" | |
w = Writer(dummy, freesans20) | |
Writer.set_textpos(dummy, 30, int((epd.width-w.stringlen(msg, oh=False))/2)) | |
w.printstring(msg, invert=True) | |
epd.blit(dummy, 0, 0) | |
epd.show() | |
epd.sleep() | |
running = False | |
else: | |
### Normal Update ### | |
w = Writer(dummy, freesans20) | |
Writer.set_textpos(dummy, 55, 230) | |
w.printstring("Hello, I'm normal update", invert=True) | |
epd.blit(dummy, 0, 0) | |
epd.show() | |
### Normal Partial Update ### | |
wr = Writer(dummyPart, freesans20) | |
Writer.set_textpos(dummyPart, 100, 230) | |
wr.printstring("Hello, I'm partial update", invert=True) | |
Writer.set_textpos(dummyPart, 130, 230) | |
wr.printstring('Let the epd go to sleep', invert=True) | |
epd.partial_mode_on() | |
dummyPart.ellipse(100, 100, 100, 100, 0b00, True) | |
epd.blit(dummyPart, 0,0) | |
epd.show() | |
### Epd Sleep ### | |
epd.sleep() | |
time.sleep(4) | |
### Update with re-constrcuted buffer ### | |
# Creating the 'erasing' buffer' | |
wri = Writer(fake, freesans20) | |
message = "Yes!\nI can do\nthis!" | |
message = message.splitlines() | |
for index, line in enumerate(message): | |
Writer.set_textpos(fake, 30+index*30, 50) | |
wri.printstring(line, invert = True) | |
Writer.set_textpos(fake, 55, 230) | |
wri.printstring("Hello, I'm normal update", invert=True) | |
Writer.set_textpos(fake, 100, 230) | |
wri.printstring("Hello, I'm partial update", invert=True) | |
Writer.set_textpos(fake, 130, 230) | |
wri.printstring('Let the epd go to sleep', invert=True) | |
epd.reinit() | |
epd.partial_mode_on() | |
epd._send_command(0x26) | |
epd._send_buffer(fake._buf) #This action tels the epd to make the content of that buffer white on next partial update | |
#Creating the 'print in black' buffer | |
dummyPart.fill(1) | |
Writer.set_textpos(dummyPart, 160, 230) | |
wr.printstring("yep, I can erase too!", invert=True) | |
epd.blit(dummyPart, 0, 0) # The blitted part gets shown as black | |
epd.show() | |
epd.partial_mode_off() | |
### Normal update, and saving the buffer in JSON ### | |
fake.fill(1) | |
msg = "Lets save this last buffer and go into deepsleep" | |
Writer.set_textpos(fake, 30, int((epd.width-wri.stringlen(msg, oh=False))/2)) | |
wri.printstring(msg, invert= True) | |
epd.blit(fake, 0, 0) | |
epd.show() | |
b = ubinascii.b2a_base64(fake._buf).decode() # Encode buffer byte array to make it compatible with Json | |
# Here if you've been using partial update, you can also save epd._buffer_red for the next partial update | |
saved_buf = {'last_buf': b } | |
json_file = open('buf.json','w') | |
try: | |
json.dump(saved_buf, json_file) | |
json_file.close() | |
except: | |
print('Error!! Error!! could not save JSON') | |
deepsleep(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment