Created
June 8, 2024 20:26
-
-
Save c3c/b7f3631f945ad337d8f416adef311a44 to your computer and use it in GitHub Desktop.
Snipppet for a T8-C3 driving a WaveShare 7.3in ACeP epaper display
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
# Snipppet for a T8-C3 driving a WaveShare 7.3in ACeP epaper display | |
# Retrieves pictures from wifi and stream them to the display | |
# Pictures must be preformatted in GS4_HMSB format | |
# Updates screen every 4 hours with a new picture | |
from machine import deepsleep, Pin, SPI | |
import time | |
import network | |
import requests | |
import random | |
import esp32 | |
import gc | |
import struct | |
SLEEPTIME = 4*60*60 * 1000 | |
PIN_CS = 10 | |
PIN_MOSI = 7 | |
PIN_SCK = 6 | |
PIN_BUSY = 5 | |
PIN_DIN = 7 | |
PIN_DC = 4 | |
PIN_RST = 8 | |
class Epaper73(): | |
def __init__(self): | |
self.rst_pin = Pin(PIN_RST, Pin.OUT) | |
self.busy_pin = Pin(PIN_BUSY, Pin.IN, Pin.PULL_UP) | |
self.cs_pin = Pin(PIN_CS, Pin.OUT) | |
self.spi = SPI(1) | |
self.spi.init(baudrate=1_000_000) | |
self.dc_pin = Pin(PIN_DC, Pin.OUT) | |
self.width = 800 | |
self.height = 480 | |
self.Black = 0x00 | |
self.White = 0x01 | |
self.Green = 0x02 | |
self.Blue = 0x03 | |
self.Red = 0x04 | |
self.Yellow = 0x05 | |
self.Orange = 0x06 | |
self.Clean = 0x07 | |
self.Epaper73_Init() | |
def delay_ms(self, delaytime): | |
time.sleep_ms(delaytime) | |
def spi_writebyte(self, data): | |
self.spi.write(bytearray(data)) | |
def digital_read(self, pin): | |
return pin.value() | |
def digital_write(self, pin, value): | |
pin.value(value) | |
def reset(self): | |
self.digital_write(self.rst_pin, 1) | |
self.delay_ms(20) | |
self.digital_write(self.rst_pin, 0) | |
self.delay_ms(5) | |
self.digital_write(self.rst_pin, 1) | |
self.delay_ms(20) | |
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 send_data1(self, buf): | |
self.digital_write(self.dc_pin, 1) | |
self.digital_write(self.cs_pin, 0) | |
self.spi.write(bytearray(buf)) | |
self.digital_write(self.cs_pin, 1) | |
def BusyHigh(self): | |
while(self.digital_read(self.busy_pin) == 0): | |
self.delay_ms(10) | |
def BusyLow(self): | |
while(self.digital_read(self.busy_pin) == 1): | |
self.delay_ms(10) | |
def Epaper73_Init(self): | |
self.reset() | |
self.BusyHigh() | |
self.delay_ms(30) | |
self.send_command(0xaa) | |
self.send_data(0x49) | |
self.send_data(0x55) | |
self.send_data(0x20) | |
self.send_data(0x08) | |
self.send_data(0x09) | |
self.send_data(0x18) | |
self.send_command(0x01) | |
self.send_data(0x3f) | |
self.send_data(0x00) | |
self.send_data(0x32) | |
self.send_data(0x2a) | |
self.send_data(0x0e) | |
self.send_data(0x2a) | |
self.send_command(0x00) | |
self.send_data(0x5f) | |
self.send_data(0x69) | |
self.send_command(0x03) | |
self.send_data(0x00) | |
self.send_data(0x54) | |
self.send_data(0x00) | |
self.send_data(0x44) | |
self.send_command(0x05) | |
self.send_data(0x40) | |
self.send_data(0x1f) | |
self.send_data(0x1f) | |
self.send_data(0x2c) | |
self.send_command(0x06) | |
self.send_data(0x6f) | |
self.send_data(0x1f) | |
self.send_data(0x1f) | |
self.send_data(0x22) | |
self.send_command(0x08) | |
self.send_data(0x6f) | |
self.send_data(0x1f) | |
self.send_data(0x1f) | |
self.send_data(0x22) | |
self.send_command(0x13) | |
self.send_data(0x00) | |
self.send_data(0x04) | |
self.send_command(0x30) | |
self.send_data(0x3c) | |
self.send_command(0x41) | |
self.send_data(0x00) | |
self.send_command(0x50) | |
self.send_data(0x3f) | |
self.send_command(0x60) | |
self.send_data(0x02) | |
self.send_data(0x00) | |
self.send_command(0x61) | |
self.send_data(0x03) | |
self.send_data(0x20) | |
self.send_data(0x01) | |
self.send_data(0xe0) | |
self.send_command(0x82) | |
self.send_data(0x1e) | |
self.send_command(0x84) | |
self.send_data(0x00) | |
self.send_command(0x86) | |
self.send_data(0x00) | |
self.send_command(0xe3) | |
self.send_data(0x2f) | |
self.send_command(0xe0) | |
self.send_data(0x00) | |
self.send_command(0xe6) | |
self.send_data(0x00) | |
def Epaper73_Clear(self, color): | |
self.send_command(0x10) | |
for _ in range(0, int(self.width // 2)): | |
self.send_data1([color<<4 | color] * self.height) | |
self.Epaper73_TurnOnDisplay() | |
self.delay_ms(500) | |
def Epaper73_Display(self, pic_stream): | |
self.send_command(0x10) | |
for _ in range(self.width // 2): | |
bytes_read = pic_stream.read(self.height) | |
self.send_data1(bytes_read) | |
self.Epaper73_TurnOnDisplay() | |
self.delay_ms(200) | |
def Epaper73_TurnOnDisplay(self): | |
self.send_command(0x04) | |
self.BusyHigh() | |
self.send_command(0x12) | |
self.send_data(0x00) | |
self.BusyHigh() | |
self.send_command(0x02) | |
self.send_data(0x00) | |
self.BusyLow() | |
def Epaper73_Sleep(self): | |
self.delay_ms(100); | |
self.send_command(0x07); | |
self.send_data(0xA5); | |
self.delay_ms(100); | |
self.digital_write(self.rst_pin, 1) | |
led = Pin(3, Pin.OUT) | |
led.value(1) | |
time.sleep(1) | |
led.value(0) | |
nvs = esp32.NVS("photo") | |
def connect_wifi(): | |
sta_if = network.WLAN(network.STA_IF) | |
sta_if.active(True) | |
sta_if.connect('wifinetwork', 'wifipassword') | |
tries = 0 | |
while tries < 10 and not sta_if.isconnected(): | |
time.sleep(1) | |
tries += 1 | |
if not sta_if.isconnected(): | |
raise Exception("wifi fail") | |
def set_nvs_last(x): | |
nvs.set_blob(b"last", x.encode()) | |
def get_nvs_last(): | |
x = bytearray(256) | |
try: | |
nvs.get_blob(b"last", x) | |
except OSError: | |
return b"" | |
return x.strip(b"\x00").decode() | |
def get_timed_file_list(): | |
file_list = requests.get("http://epaper.your.lan/epaper.txt") # list of files, new line separated | |
assert file_list.status_code == 200 | |
hour = int(file_list.headers['Date'].strip().split(' ')[-2].split(':')[0]) | |
pics = file_list.text.strip().splitlines() | |
return hour, pics | |
def select_next_pic(pic_list): | |
last_pic = get_nvs_last() | |
filtered = list(filter(lambda b: b!=last_pic, pic_list)) | |
return random.choice(filtered) | |
def get_picture_stream(the_pic): | |
gs4 = the_pic.replace(".bmp", ".gs4") # obtain gs4 formatted picture (GS4_HMSB) | |
pic = requests.get(f"http://epaper.your.lan/{gs4}", stream=True) | |
assert pic.status_code == 200 | |
raw_len = int(pic.headers['Content-Length']) | |
assert raw_len == 192000 | |
return pic.raw | |
quiet_time = False | |
try: | |
print("[ ] Wifi connecting") | |
connect_wifi() | |
print("[+] Wifi connected") | |
print("[ ] Pic list fetch") | |
hour, pic_list = get_timed_file_list() | |
print("[+] Pic list retrieved: %d pics" % len(pic_list)) | |
quiet_time = hour >= 20 | |
print("[+] Time = %dh GMT. Quiet time starting: %s" % (hour, str(quiet_time))) | |
next_pic = select_next_pic(pic_list) | |
print("[*] Next up: %s" % next_pic) | |
set_nvs_last(next_pic) | |
print("[+] Next pic in NVS") | |
print("[ ] Fetching picture") | |
pic_stream = get_picture_stream(next_pic) | |
print("[+] Fetching picture OK") | |
ep = Epaper73() | |
ep.Epaper73_Clear(0x01) | |
ep.Epaper73_Display(pic_stream) | |
ep.Epaper73_Sleep() | |
print("[+] Done") | |
except Exception as e: | |
print(e) | |
pass | |
if quiet_time: | |
deepsleep(SLEEPTIME*2) | |
else: | |
deepsleep(SLEEPTIME) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Following scripts can be used to convert images to gs4 format, improve brightness/contrast/saturation, with optional rotation.
First convert any input image to bmp with palette (this one is WaveShare's):
Post process then using: