865|2

1万

帖子

14

资源

版主

IL0389 电子纸(墨水屏)驱动 [复制链接]

image.png

 

项目网站

 

'''
Copyright (c) 2020 Thomas Wilkinson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Written by Thomas Wilkinson - github.com/T-Wilko
'''

from micropython import const
from machine import SPI, Pin
from time import sleep_ms
import ustruct

## Display resolution
EPD_WIDTH  = const(400)
EPD_HEIGHT = const(300)
# EPD_WIDTH must be divisible by 8 for this driver to work

## Display command addresses
PANEL_SETTING                       = const(0x00)
POWER_SETTING                       = const(0x01)
POWER_OFF                           = const(0x02)
POWER_OFF_SEQ_SET                   = const(0x03)
POWER_ON                            = const(0x04)
POWER_ON_MEASURE                    = const(0x05)
BOOSTER_SOFT_START_CONTROL          = const(0x06)
DEEP_SLEEP_MODE                     = const(0x07)
START_TRANSMISSION_1                = const(0x10)
DATA_STOP                           = const(0x11)
DISP_REFRESH                        = const(0x12)
START_TRANSMISSION_2                = const(0x13)
PLL_CONTROL                         = const(0x30)
TEMP_CALIBRATION                    = const(0x40)
TEMP_SENSOR_SELECTION               = const(0x41)
TEMP_SENSOR_WRITE                   = const(0x42)
TEMP_SENSOR_READ                    = const(0x43)
VCOM_DATA_INT_SETTING               = const(0x50)
LOW_POWER_DETECT                    = const(0x51)
TCON_SETTING                        = const(0x60)
RES_SETTING                         = const(0x61)
GSST_SETTING                        = const(0x65)
REVISION                            = const(0x70)
GET_STATUS                          = const(0x71)
AUTO_MEASURE_VCOM                   = const(0x80)
VCOM_READ                           = const(0x81)
VCM_DC_SETTING                      = const(0x82)
#PARTIAL_WINDOW                     = const(0x90)
PARTIAL_IN                          = const(0x91)
PARTIAL_OUT                         = const(0x92)
PROGRAM_MODE                        = const(0xA0)
ACTIVE_PROG                         = const(0xA1)
READ_OTP                            = const(0xA2)
CASCADE_SETTING                     = const(0xE0)
POWER_SAVING                        = const(0xE3)
FORCE_TEMP                          = const(0xE5)

class EPD:
    def __init__(self):
        self.spi = SPI(1, baudrate=20000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
        #self.spi = SPI(1, 10000000, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
        self.spi.init()

        dc = Pin(27)
        cs = Pin(5)
        rst = Pin(14)
        busy = Pin(4)
        
        self.cs = cs
        self.dc = dc
        self.rst = rst
        self.busy = busy
        self.cs.init(self.cs.OUT, value=1)
        self.dc.init(self.dc.OUT, value=0)
        self.rst.init(self.rst.OUT, value=0)
        self.busy.init(self.busy.IN)
        self.width = EPD_WIDTH
        self.height = EPD_HEIGHT

        self.size = self.width * self.height // 8
        self.buf = bytearray(self.size)

    def clearBuffer(self):
        self._command(b'\x24')
        for i in range(0, len(self.buf)):
            self.buf = 255
            self._data(bytearray([self.buf]))

    def displayBuffer(self, buf):
        self._command(bytearray([START_TRANSMISSION_2]))
        for i in range(0, len(buf)):
            self._data(bytearray([buf]))
        self._command(bytearray([DATA_STOP]))
        self._command(bytearray([DISP_REFRESH]))
        self.wait_until_idle()

    def _command(self, command, data=None):
        self.cs(1) # according to LOLIN_EPD
        self.dc(0)
        self.cs(0)
        self.spi.write(command)
        self.cs(1)
        if data is not None:
            self._data(data)

    def _data(self, data):
        self.cs(1) # according to LOLIN_EPD
        self.dc(1)
        self.cs(0)
        self.spi.write(data)
        self.cs(1)

    def init(self):
        #Reset EPD Driver IC
        self.reset()
        
        #Booster soft start
        self._command(bytearray([BOOSTER_SOFT_START_CONTROL]), b'\x17\x17\x17')
        
        #Power setting
        self._command(bytearray([POWER_SETTING]), b'\x03\x00\x2B\x2B\x09')
        
        #Power on
        self._command(bytearray([POWER_ON]))
        
        #Check busy pin and proceed if idle
        self.wait_until_idle()
        
        #Panel setting - B&W and full resolution
        self._command(bytearray([PANEL_SETTING]), b'\x1F')
        
        #PLL control - Set to 100Hz, default is 50Hz
        self._command(bytearray([PLL_CONTROL]), b'\x3A')
        
        #Resolution setting
        self._command(bytearray([RES_SETTING]), b'\x01\x90\x01\x2C')
        
        #VCM_DC setting - Currently set to -1V, default is -0.1V i.e. b'\x00'
        self._command(bytearray([VCM_DC_SETTING]), b'\x12')
        
        #VCOM and Data Interval setting
        self._command(bytearray([VCOM_DATA_INT_SETTING]), b'\x87')
        
        
        
    def wait_until_idle(self):
        while self.busy == 0:
            pass
        return

    def reset(self):
        self.rst(1)
        sleep_ms(1)

        self.rst(0)
        sleep_ms(10)

        self.rst(1)

    # draw the current frame memory and switch to the next memory area
    def display_frame(self):
        self._command(bytearray([DISPLAY_UPDATE_CONTROL_2]), b'\xC7')
        self._command(bytearray([MASTER_ACTIVATION]))
        self._command(bytearray([TERMINATE_FRAME_READ_WRITE]))
        self.wait_until_idle()

    # to wake call reset() or init()
    def sleep(self):
        self._command(bytearray([DEEP_SLEEP_MODE]))
        self.wait_until_idle()

 


回复

86

帖子

1

资源

一粒金砂(中级)

感谢版主。代码非常漂亮。


回复

25

帖子

0

资源

一粒金砂(中级)

听起来觉得很厉害,不明觉厉的那一种


回复
您需要登录后才可以回帖 登录 | 注册

最新文章 更多>>
    关闭
    站长推荐上一条 1/9 下一条

    About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

    站点相关: 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

    北京市海淀区知春路23号集成电路设计园量子银座1305 电话:(010)82350740 邮编:100191

    电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2021 EEWORLD.com.cn, Inc. All rights reserved
    快速回复 返回顶部 返回列表