|
| 1 | +#!/usr/bin/env python |
| 2 | +#coding: utf-8 |
| 3 | + |
| 4 | +''' |
| 5 | +部分函数取自https://github.com/yy502/ePaperDisplay.git |
| 6 | +由于不太习惯原作的行文风格,故重造一遍。。。 |
| 7 | +''' |
| 8 | + |
| 9 | +import sys, os |
| 10 | +import serial |
| 11 | +import struct |
| 12 | +import time |
| 13 | + |
| 14 | +FONT_SIZE_32 = 0x01 |
| 15 | +FONT_SIZE_48 = 0x02 |
| 16 | +FONT_SIZE_64 = 0x03 |
| 17 | + |
| 18 | +MEM_FLASH = 0x00 |
| 19 | +MEM_SD = 0x01 |
| 20 | + |
| 21 | +ROTATION_NORMAL = 0x00 |
| 22 | +ROTATION_180 = 0x01 |
| 23 | + |
| 24 | +# commands |
| 25 | +CMD_HANDSHAKE = 0x00 # handshake |
| 26 | +CMD_SET_MEMORY = 0x07 # get memory mode |
| 27 | +CMD_SET_ROTATION = 0x0d |
| 28 | +CMD_UPDATE = 0x0A # update |
| 29 | +CMD_LOAD_FONT = 0x0E # copy font files from SD card to NandFlash. |
| 30 | + # Font files include GBK32/48/64.FON |
| 31 | + # 48MB allocated in NandFlash for fonts |
| 32 | + # LED will flicker 3 times when starts and ends. |
| 33 | +CMD_LOAD_PIC = 0x0F # Import the image files from SD card to the NandFlash. |
| 34 | + # LED will flicker 3 times when starts and ends. |
| 35 | + # 80MB allocated in NandFlash for images |
| 36 | +CMD_SET_COLOR = 0x10 # set colour |
| 37 | +CMD_SET_EN_FONT = 0x1E # set English font |
| 38 | +CMD_SET_CH_FONT = 0x1F # set Chinese font |
| 39 | + |
| 40 | +CMD_DRAW_LINE = 0x22 # draw line |
| 41 | +CMD_CLEAR = 0x2E # clear screen use back colour |
| 42 | +CMD_DRAW_STRING = 0x30 # draw string |
| 43 | +CMD_DRAW_BITMAP = 0x70 # draw bitmap |
| 44 | + |
| 45 | +COLOR_BLACK = 0x00 |
| 46 | +COLOR_DARK_GRAY = 0x01 |
| 47 | +COLOR_GRAY = 0x02 |
| 48 | +COLOR_WHITE = 0x03 |
| 49 | + |
| 50 | + |
| 51 | +class Screen: |
| 52 | + def __init__(self, tty): |
| 53 | + self.tty = tty |
| 54 | + |
| 55 | + def _build_frame(self, cmd, args=None): |
| 56 | + length = 9 |
| 57 | + if args is not None: |
| 58 | + length += len(args) |
| 59 | + frame = '\xA5' + struct.pack('>h', length) + chr(cmd) |
| 60 | + if args is not None: |
| 61 | + frame += args |
| 62 | + frame += '\xCC\x33\xC3\x3C' |
| 63 | + parity = 0x00 |
| 64 | + for i in xrange(0, len(frame)): |
| 65 | + parity = parity ^ ord(frame[i]) |
| 66 | + frame += chr(parity) |
| 67 | + return frame |
| 68 | + |
| 69 | + def _send(self, frame): |
| 70 | + self.socket.write(frame) |
| 71 | + rt = self.socket.read(10) |
| 72 | + |
| 73 | + def connect(self): |
| 74 | + self.socket = serial.Serial(port=self.tty, \ |
| 75 | + baudrate=115200, \ |
| 76 | + stopbits=serial.STOPBITS_ONE, \ |
| 77 | + bytesize=serial.EIGHTBITS, \ |
| 78 | + timeout=0.03) |
| 79 | + |
| 80 | + def disconnect(self): |
| 81 | + self.socket.close() |
| 82 | + |
| 83 | + def handshake(self): |
| 84 | + self._send(self._build_frame(CMD_HANDSHAKE)) |
| 85 | + |
| 86 | + def set_memory(self, mem): |
| 87 | + self._send(self._build_frame(CMD_SET_MEMORY, chr(mem))) |
| 88 | + |
| 89 | + def set_rotation(self, r): |
| 90 | + self._send(self._build_frame(CMD_SET_ROTATION, chr(r))) |
| 91 | + |
| 92 | + def clear(self): |
| 93 | + self._send(self._build_frame(CMD_CLEAR)) |
| 94 | + |
| 95 | + def update(self): |
| 96 | + self._send(self._build_frame(CMD_UPDATE)) |
| 97 | + |
| 98 | + def line(self, x0, y0, x1, y1): |
| 99 | + args = struct.pack('>hhhh', x0, y0, x1, y1) |
| 100 | + self._send(self._build_frame(CMD_DRAW_LINE, args)) |
| 101 | + |
| 102 | + def set_color(self, front, background): |
| 103 | + self._send(self._build_frame(CMD_SET_COLOR, chr(front) + chr(background))) |
| 104 | + |
| 105 | + def set_en_font_size(self, size): |
| 106 | + self._send(self._build_frame(CMD_SET_EN_FONT, chr(size))) |
| 107 | + |
| 108 | + def set_ch_font_size(self, size): |
| 109 | + self._send(self._build_frame(CMD_SET_CH_FONT, chr(size))) |
| 110 | + |
| 111 | + def _get_real_font_size(self, font_size): |
| 112 | + return [0, 32, 48, 64][font_size] |
| 113 | + |
| 114 | + def get_text_width(self, txt, size=FONT_SIZE_32): |
| 115 | + size = self._get_real_font_size(size) |
| 116 | + width = 0 |
| 117 | + for c in txt: |
| 118 | + if c in "'": |
| 119 | + width += 5 |
| 120 | + elif c in "ijl|": |
| 121 | + width += 6 |
| 122 | + elif c in "f": |
| 123 | + width += 7 |
| 124 | + elif c in " It![].,;:/\\": |
| 125 | + width += 8 |
| 126 | + elif c in "r-`(){}": |
| 127 | + width += 9 |
| 128 | + elif c in '"': |
| 129 | + width += 10 |
| 130 | + elif c in "*": |
| 131 | + width += 11 |
| 132 | + elif c in "x^": |
| 133 | + width += 12 |
| 134 | + elif c in "Jvz": |
| 135 | + width += 13 |
| 136 | + elif c in "cksy": |
| 137 | + width += 14 |
| 138 | + elif c in "Labdeghnopqu$#?_1234567890": |
| 139 | + width += 15 |
| 140 | + elif c in "T+<>=~": |
| 141 | + width += 16 |
| 142 | + elif c in "FPVXZ": |
| 143 | + width += 17 |
| 144 | + elif c in "ABEKSY&": |
| 145 | + width += 18 |
| 146 | + elif c in "HNUw": |
| 147 | + width += 19 |
| 148 | + elif c in "CDR": |
| 149 | + width += 20 |
| 150 | + elif c in "GOQ": |
| 151 | + width += 21 |
| 152 | + elif c in "m": |
| 153 | + width += 22 |
| 154 | + elif c in "M": |
| 155 | + width += 23 |
| 156 | + elif c in "%": |
| 157 | + width += 24 |
| 158 | + elif c in "@": |
| 159 | + width += 27 |
| 160 | + elif c in "W": |
| 161 | + width += 28 |
| 162 | + else: # non-ascii or Chinese character |
| 163 | + width += 32 |
| 164 | + return int(width * (size / 32.0)) |
| 165 | + |
| 166 | + |
| 167 | + def text(self, x0, y0, text): |
| 168 | + args = struct.pack('>hh', x0, y0) |
| 169 | + if isinstance(text, str): |
| 170 | + text = text.decode('utf-8') |
| 171 | + text = text.encode('gb2312') |
| 172 | + args = args + text + '\x00' |
| 173 | + self._send(self._build_frame(CMD_DRAW_STRING, args)) |
| 174 | + |
| 175 | + def wrap_text(self, x0, y0, limit, text, font_size=FONT_SIZE_32, line_space=10): |
| 176 | + |
| 177 | + line_height = self._get_real_font_size(font_size) |
| 178 | + line = '' |
| 179 | + width = 0 |
| 180 | + cy = y0 |
| 181 | + |
| 182 | + if not isinstance(text, unicode): |
| 183 | + text = text.decode('utf-8') |
| 184 | + |
| 185 | + for c in text: |
| 186 | + line += c |
| 187 | + width += self.get_text_width(c, font_size) |
| 188 | + if width + font_size * 32 > limit: |
| 189 | + self.text(x0, cy, line) |
| 190 | + cy += line_height + line_space |
| 191 | + line = '' |
| 192 | + width = 0 |
| 193 | + |
| 194 | + if len(line): |
| 195 | + self.text(x0, cy, line) |
| 196 | + |
| 197 | + def load_pic(self): |
| 198 | + self._send(self._build_frame(CMD_LOAD_PIC)) |
| 199 | + |
| 200 | + def bitmap(self, x0, y0, image): |
| 201 | + if isinstance(image, str): |
| 202 | + image = image.decode('utf-8') |
| 203 | + args = struct.pack('>hh', x0, y0) |
| 204 | + args = args + image.encode('ascii') + '\x00' |
| 205 | + self._send(self._build_frame(CMD_DRAW_BITMAP, args)) |
| 206 | + |
| 207 | + |
0 commit comments