|
| 1 | +import struct |
| 2 | +from time import sleep |
| 3 | + |
| 4 | +import serial # Install pyserial : pip install pyserial |
| 5 | +from PIL import Image # Install PIL or Pillow |
| 6 | + |
| 7 | +# Set your COM port e.g. COM3 for Windows, /dev/ttyACM0 for Linux... |
| 8 | +COM_PORT = "/dev/ttyACM1" |
| 9 | +# COM_PORT = "COM5" |
| 10 | + |
| 11 | +def SendReg(ser, reg, x, y, ex, ey): |
| 12 | + byteBuffer = bytearray(6) |
| 13 | + byteBuffer[0] = (x >> 2) |
| 14 | + byteBuffer[1] = (((x & 3) << 6) + (y >> 4)) |
| 15 | + byteBuffer[2] = (((y & 15) << 4) + (ex >> 6)) |
| 16 | + byteBuffer[3] = (((ex & 63) << 2) + (ey >> 8)) |
| 17 | + byteBuffer[4] = (ey & 255) |
| 18 | + byteBuffer[5] = reg |
| 19 | + ser.write(bytes(byteBuffer)) |
| 20 | + |
| 21 | + |
| 22 | +def Reset(ser): |
| 23 | + SendReg(ser, 101, 0, 0, 0, 0) |
| 24 | + |
| 25 | + |
| 26 | +def Clear(ser): |
| 27 | + SendReg(ser, 102, 0, 0, 0, 0) |
| 28 | + |
| 29 | + |
| 30 | +def ScreenOff(ser): |
| 31 | + SendReg(ser, 108, 0, 0, 0, 0) |
| 32 | + |
| 33 | + |
| 34 | +def ScreenOn(ser): |
| 35 | + SendReg(ser, 109, 0, 0, 0, 0) |
| 36 | + |
| 37 | + |
| 38 | +def SetBrightness(ser, level): |
| 39 | + # Level : 0 (bright) - 255 (darkest) |
| 40 | + SendReg(ser, 110, level, 0, 0, 0) |
| 41 | + |
| 42 | + |
| 43 | +def PrintImage(ser, image): |
| 44 | + im = Image.open(image) |
| 45 | + image_height = im.size[1] |
| 46 | + image_width = im.size[0] |
| 47 | + |
| 48 | + SendReg(ser, 197, 0, 0, image_width - 1, image_height - 1) |
| 49 | + |
| 50 | + pix = im.load() |
| 51 | + for h in range(image_height): |
| 52 | + line = bytes() |
| 53 | + for w in range(image_width): |
| 54 | + if w < image_width: |
| 55 | + R = pix[w, h][0] >> 3 |
| 56 | + G = pix[w, h][1] >> 2 |
| 57 | + B = pix[w, h][2] >> 3 |
| 58 | + |
| 59 | + rgb = (R << 11) | (G << 5) | B |
| 60 | + line += struct.pack('H', rgb) |
| 61 | + ser.write(line) |
| 62 | + |
| 63 | + sleep(0.01) # Wait 10 ms after picture display |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + ser = serial.Serial(COM_PORT, 115200, timeout=1, rtscts=1) |
| 68 | + |
| 69 | + # Clear screen (blank) |
| 70 | + Clear(ser) |
| 71 | + |
| 72 | + # Set brightness to max value |
| 73 | + SetBrightness(ser, 0) |
| 74 | + |
| 75 | + # Display sample picture |
| 76 | + PrintImage(ser, "res/example.png") |
| 77 | + |
| 78 | + ser.close() |
0 commit comments