From 2bad84d2e6cfc9b4f332f351e16534c1472094e2 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Thu, 26 May 2022 15:11:00 +0200 Subject: [PATCH] Refactor eink driver --- eink.hpp | 149 ++++++++++++++++++++++++++++++++ epd1in54b_V2.h | 224 ------------------------------------------------- 2 files changed, 149 insertions(+), 224 deletions(-) create mode 100644 eink.hpp delete mode 100644 epd1in54b_V2.h diff --git a/eink.hpp b/eink.hpp new file mode 100644 index 0000000..dddb7ad --- /dev/null +++ b/eink.hpp @@ -0,0 +1,149 @@ +#pragma once + +#include + +#include + +#include "../clock.hpp" +#include "../io/io.hpp" + +template +class Epd { + static constexpr auto width = uint16_t{200}; + static constexpr auto height = uint16_t{200}; + + io::Pin m_rst; + io::Pin m_dc; + io::Pin m_bsy; + + public: + Epd() = default; + ~Epd() = default; + + int Init() + { + m_rst.dir(io::Dir::OUT); + m_dc.dir(io::Dir::OUT); + m_bsy.dir(io::Dir::IN); + Spi::init(); + + /* EPD hardware init start */ + Reset(); + + WaitUntilIdle(); + SendCommand(0x12); // SWRESET + WaitUntilIdle(); + + SendCommand(0x01); // Driver output control + SendData(0xC7); + SendData(0x00); + SendData(0x01); + + SendCommand(0x11); // data entry mode + SendData(0x01); + + SendCommand(0x44); // set Ram-X address start/end position + SendData(0x00); + SendData(0x18); // 0x18-->(24+1)*8=200 + + SendCommand(0x45); // set Ram-Y address start/end position + SendData(0xC7); // 0xC7-->(199+1)=200 + SendData(0x00); + SendData(0x00); + SendData(0x00); + + SendCommand(0x3C); // BorderWavefrom + SendData(0x05); + + SendCommand(0x18); // Read built-in temperature sensor + SendData(0x80); + + SendCommand(0x4E); // set RAM x address count to 0; + SendData(0x00); + SendCommand(0x4F); // set RAM y address count to 0X199; + SendData(0xC7); + SendData(0x00); + WaitUntilIdle(); + + /* EPD hardware init end */ + + return 0; + } + + void SendCommand(unsigned char command) + { + m_dc = false; + SpiTransfer(command); + } + + void SendData(unsigned char data) + { + m_dc = true; + SpiTransfer(data); + } + + void WaitUntilIdle() + { + while (m_bsy) { + _delay_ms(100); + } + } + + void Reset() + { + m_rst = true; + _delay_ms(200); + m_rst = false; // module reset + _delay_ms(10); + m_rst = true; + _delay_ms(200); + } + + void DisplayFrame(const unsigned char *frame_buffer_black, const unsigned char *frame_buffer_red) + { + SendCommand(0x24); + for (auto i = uint16_t{0}; i < width * height / 8; i++) { + SendData(pgm_read_byte(&frame_buffer_black[i])); + } + SendCommand(0x26); + for (auto i = uint16_t{0}; i < width * height / 8; i++) { + SendData(~pgm_read_byte(&frame_buffer_red[i])); + } + + SendCommand(0x22); + SendData(0xF7); + SendCommand(0x20); + WaitUntilIdle(); + } + + void DisplayClear() + { + SendCommand(0x24); + for (auto i = uint16_t{0}; i < width * height / 8; i++) { + SendData(0xff); + } + SendCommand(0x26); + for (auto i = uint16_t{0}; i < width * height / 8; i++) { + SendData(0x00); + } + + SendCommand(0x22); + SendData(0xF7); + SendCommand(0x20); + WaitUntilIdle(); + } + + void Sleep() + { + SendCommand(0x10); // power setting + SendData(0x01); // gate switch to external + _delay_ms(100); + } + + void SpiTransfer(unsigned char data) + { + Spi::select(true); + Spi::transfer(data); + Spi::select(false); + } +}; diff --git a/epd1in54b_V2.h b/epd1in54b_V2.h deleted file mode 100644 index 6557f30..0000000 --- a/epd1in54b_V2.h +++ /dev/null @@ -1,224 +0,0 @@ -/** - * @filename : epd1in54b.h - * @brief : Header file for e-paper display library epd1in54b.cpp - * @author : Yehui from Waveshare - * - * Copyright (C) Waveshare August 10 2017 - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documnetation 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 - * furished 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 OR 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. - */ - -#ifndef EPD1IN54B_H -#define EPD1IN54B_H - -#include - -#include "../clock.hpp" -#include "../io/io.hpp" - -// Display resolution -#define EPD_WIDTH 200 -#define EPD_HEIGHT 200 - -// EPD1IN54B commands -#define PANEL_SETTING 0x00 -#define POWER_SETTING 0x01 -#define POWER_OFF 0x02 -#define POWER_OFF_SEQUENCE_SETTING 0x03 -#define POWER_ON 0x04 -#define POWER_ON_MEASURE 0x05 -#define BOOSTER_SOFT_START 0x06 -#define DEEP_SLEEP 0x07 -#define DATA_START_TRANSMISSION_1 0x10 -#define DATA_STOP 0x11 -#define DISPLAY_REFRESH 0x12 -#define DATA_START_TRANSMISSION_2 0x13 -#define PLL_CONTROL 0x30 -#define TEMPERATURE_SENSOR_COMMAND 0x40 -#define TEMPERATURE_SENSOR_CALIBRATION 0x41 -#define TEMPERATURE_SENSOR_WRITE 0x42 -#define TEMPERATURE_SENSOR_READ 0x43 -#define VCOM_AND_DATA_INTERVAL_SETTING 0x50 -#define LOW_POWER_DETECTION 0x51 -#define TCON_SETTING 0x60 -#define TCON_RESOLUTION 0x61 -#define SOURCE_AND_GATE_START_SETTING 0x62 -#define GET_STATUS 0x71 -#define AUTO_MEASURE_VCOM 0x80 -#define VCOM_VALUE 0x81 -#define VCM_DC_SETTING_REGISTER 0x82 -#define PROGRAM_MODE 0xA0 -#define ACTIVE_PROGRAM 0xA1 -#define READ_OTP_DATA 0xA2 - -// Pin definition -#define RST_PIN io::P::C1 -#define DC_PIN io::P::C0 -#define BUSY_PIN io::P::C2 - -template -class Epd { - public: - unsigned long width; - unsigned long height; - - Epd() - { - width = EPD_WIDTH; - height = EPD_HEIGHT; - } - - ~Epd() = default; - - int Init() - { - /* this calls the peripheral hardware interface, see epdif */ - io::Pin::dir(io::Dir::OUT); - io::Pin::dir(io::Dir::OUT); - io::Pin::dir(io::Dir::IN); - Spi::init(); - - /* EPD hardware init start */ - Reset(); - - WaitUntilIdle(); - SendCommand(0x12); // SWRESET - WaitUntilIdle(); - - SendCommand(0x01); // Driver output control - SendData(0xC7); - SendData(0x00); - SendData(0x01); - - SendCommand(0x11); // data entry mode - SendData(0x01); - - SendCommand(0x44); // set Ram-X address start/end position - SendData(0x00); - SendData(0x18); // 0x18-->(24+1)*8=200 - - SendCommand(0x45); // set Ram-Y address start/end position - SendData(0xC7); // 0xC7-->(199+1)=200 - SendData(0x00); - SendData(0x00); - SendData(0x00); - - SendCommand(0x3C); // BorderWavefrom - SendData(0x05); - - SendCommand(0x18); // Read built-in temperature sensor - SendData(0x80); - - SendCommand(0x4E); // set RAM x address count to 0; - SendData(0x00); - SendCommand(0x4F); // set RAM y address count to 0X199; - SendData(0xC7); - SendData(0x00); - WaitUntilIdle(); - - /* EPD hardware init end */ - - return 0; - } - - void SendCommand(unsigned char command) - { - io::Pin::write(false); - SpiTransfer(command); - } - - void SendData(unsigned char data) - { - io::Pin::write(true); - SpiTransfer(data); - } - - void WaitUntilIdle() - { - while (io::Pin::read()) { - _delay_ms(100); - } - } - - void Reset() - { - io::Pin::write(true); - _delay_ms(200); - io::Pin::write(false); // module reset - _delay_ms(10); - io::Pin::write(true); - _delay_ms(200); - } - - void DisplayFrame(const unsigned char *frame_buffer_black, const unsigned char *frame_buffer_red) - { - unsigned int i; - - SendCommand(0x24); - for (i = 0; i < this->width * this->height / 8; i++) { - SendData(pgm_read_byte(&frame_buffer_black[i])); - } - SendCommand(0x26); - for (i = 0; i < this->width * this->height / 8; i++) { - SendData(~pgm_read_byte(&frame_buffer_red[i])); - } - - SendCommand(0x22); - SendData(0xF7); - SendCommand(0x20); - WaitUntilIdle(); - } - - void DisplayClear() - { - unsigned int i; - - SendCommand(0x24); - for (i = 0; i < this->width * this->height / 8; i++) { - SendData(0xff); - } - SendCommand(0x26); - for (i = 0; i < this->width * this->height / 8; i++) { - SendData(0x00); - } - - SendCommand(0x22); - SendData(0xF7); - SendCommand(0x20); - WaitUntilIdle(); - } - - void Sleep() - { - SendCommand(0x10); // power setting - SendData(0x01); // gate switch to external - _delay_ms(100); - } - - void SpiTransfer(unsigned char data) - { - Spi::select(true); - Spi::transfer(data); - Spi::select(false); - } -}; - -#endif /* EPD1IN54B_H */ - -/* END OF FILE */