Compare commits
3 Commits
2bad84d2e6
...
2cb38b9fdc
Author | SHA1 | Date | |
---|---|---|---|
2cb38b9fdc | |||
612f6a9fbc | |||
1c01b17f4d |
162
eink.hpp
162
eink.hpp
@ -7,143 +7,155 @@
|
|||||||
#include "../clock.hpp"
|
#include "../clock.hpp"
|
||||||
#include "../io/io.hpp"
|
#include "../io/io.hpp"
|
||||||
|
|
||||||
template <typename Spi>
|
namespace eink {
|
||||||
class Epd {
|
|
||||||
static constexpr auto width = uint16_t{200};
|
|
||||||
static constexpr auto height = uint16_t{200};
|
|
||||||
|
|
||||||
io::Pin<io::P::C1> m_rst;
|
template <uint16_t Width, uint16_t Height, typename Spi, io::P RstPin, io::P DcPin, io::P BusyPin>
|
||||||
io::Pin<io::P::C0> m_dc;
|
class Eink {
|
||||||
io::Pin<io::P::C2> m_bsy;
|
struct Cmd {
|
||||||
|
static constexpr auto SW_RESET = uint8_t{0x12};
|
||||||
|
static constexpr auto DRIVER_OUTPUT_CONTROL = uint8_t{0x01};
|
||||||
|
static constexpr auto DATA_ENTRY_MODE = uint8_t{0x11};
|
||||||
|
static constexpr auto SET_RAM_X_ADDR_POSITIONS = uint8_t{0x44};
|
||||||
|
static constexpr auto SET_RAM_Y_ADDR_POSITIONS = uint8_t{0x45};
|
||||||
|
static constexpr auto BORDER_WAVEFORM_CONTROL = uint8_t{0x3C};
|
||||||
|
static constexpr auto READ_TEMPERATURE_SENSOR = uint8_t{0x18};
|
||||||
|
static constexpr auto SET_RAM_X_ADDR = uint8_t{0x4E};
|
||||||
|
static constexpr auto SET_RAM_Y_ADDR = uint8_t{0x4F};
|
||||||
|
static constexpr auto WRITE_RAM_BLACK = uint8_t{0x24};
|
||||||
|
static constexpr auto WRITE_RAM_RED = uint8_t{0x26};
|
||||||
|
static constexpr auto DISPLAY_UPDATE_CONTROL_2 = uint8_t{0x22};
|
||||||
|
static constexpr auto UPDATE_DISPLAY = uint8_t{0x20};
|
||||||
|
static constexpr auto DEEP_SLEEP_MODE = uint8_t{0x10};
|
||||||
|
};
|
||||||
|
|
||||||
|
static io::Pin<RstPin> m_rst;
|
||||||
|
static io::Pin<DcPin> m_dc;
|
||||||
|
static io::Pin<BusyPin> m_bsy;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Epd() = default;
|
static void init()
|
||||||
~Epd() = default;
|
|
||||||
|
|
||||||
int Init()
|
|
||||||
{
|
{
|
||||||
m_rst.dir(io::Dir::OUT);
|
m_rst.dir(io::Dir::OUT);
|
||||||
m_dc.dir(io::Dir::OUT);
|
m_dc.dir(io::Dir::OUT);
|
||||||
m_bsy.dir(io::Dir::IN);
|
m_bsy.dir(io::Dir::IN);
|
||||||
Spi::init();
|
Spi::init();
|
||||||
|
|
||||||
/* EPD hardware init start */
|
reset();
|
||||||
Reset();
|
|
||||||
|
|
||||||
WaitUntilIdle();
|
waitUntilIdle();
|
||||||
SendCommand(0x12); // SWRESET
|
sendCommand(Cmd::SW_RESET);
|
||||||
WaitUntilIdle();
|
waitUntilIdle();
|
||||||
|
|
||||||
SendCommand(0x01); // Driver output control
|
sendCommand(Cmd::DRIVER_OUTPUT_CONTROL);
|
||||||
SendData(0xC7);
|
sendData(0xC7);
|
||||||
SendData(0x00);
|
sendData(0x00);
|
||||||
SendData(0x01);
|
sendData(0x01);
|
||||||
|
|
||||||
SendCommand(0x11); // data entry mode
|
sendCommand(Cmd::DATA_ENTRY_MODE);
|
||||||
SendData(0x01);
|
sendData(0x01);
|
||||||
|
|
||||||
SendCommand(0x44); // set Ram-X address start/end position
|
sendCommand(Cmd::SET_RAM_X_ADDR_POSITIONS);
|
||||||
SendData(0x00);
|
sendData(0x00);
|
||||||
SendData(0x18); // 0x18-->(24+1)*8=200
|
sendData((Width / 8) - 1);
|
||||||
|
|
||||||
SendCommand(0x45); // set Ram-Y address start/end position
|
sendCommand(Cmd::SET_RAM_Y_ADDR_POSITIONS);
|
||||||
SendData(0xC7); // 0xC7-->(199+1)=200
|
sendData(Height - 1);
|
||||||
SendData(0x00);
|
sendData(0x00);
|
||||||
SendData(0x00);
|
sendData(0x00);
|
||||||
SendData(0x00);
|
sendData(0x00);
|
||||||
|
|
||||||
SendCommand(0x3C); // BorderWavefrom
|
sendCommand(Cmd::BORDER_WAVEFORM_CONTROL);
|
||||||
SendData(0x05);
|
sendData(0x05);
|
||||||
|
|
||||||
SendCommand(0x18); // Read built-in temperature sensor
|
sendCommand(Cmd::READ_TEMPERATURE_SENSOR);
|
||||||
SendData(0x80);
|
sendData(0x80);
|
||||||
|
|
||||||
SendCommand(0x4E); // set RAM x address count to 0;
|
sendCommand(Cmd::SET_RAM_X_ADDR);
|
||||||
SendData(0x00);
|
sendData(0x00);
|
||||||
SendCommand(0x4F); // set RAM y address count to 0X199;
|
|
||||||
SendData(0xC7);
|
|
||||||
SendData(0x00);
|
|
||||||
WaitUntilIdle();
|
|
||||||
|
|
||||||
/* EPD hardware init end */
|
sendCommand(Cmd::SET_RAM_Y_ADDR);
|
||||||
|
sendData(Height - 1);
|
||||||
|
sendData(0x00);
|
||||||
|
|
||||||
return 0;
|
waitUntilIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendCommand(unsigned char command)
|
static void sendCommand(const uint8_t command)
|
||||||
{
|
{
|
||||||
m_dc = false;
|
m_dc = false;
|
||||||
SpiTransfer(command);
|
spiTransfer(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SendData(unsigned char data)
|
static void sendData(const uint8_t data)
|
||||||
{
|
{
|
||||||
m_dc = true;
|
m_dc = true;
|
||||||
SpiTransfer(data);
|
spiTransfer(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WaitUntilIdle()
|
static void waitUntilIdle()
|
||||||
{
|
{
|
||||||
while (m_bsy) {
|
while (m_bsy) {
|
||||||
_delay_ms(100);
|
_delay_ms(100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Reset()
|
static void reset()
|
||||||
{
|
{
|
||||||
m_rst = true;
|
m_rst = true;
|
||||||
_delay_ms(200);
|
_delay_ms(200);
|
||||||
m_rst = false; // module reset
|
m_rst = false;
|
||||||
_delay_ms(10);
|
_delay_ms(10);
|
||||||
m_rst = true;
|
m_rst = true;
|
||||||
_delay_ms(200);
|
_delay_ms(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DisplayFrame(const unsigned char *frame_buffer_black, const unsigned char *frame_buffer_red)
|
static void draw(const uint8_t *blackFrame, const uint8_t *redFrame)
|
||||||
{
|
{
|
||||||
SendCommand(0x24);
|
sendCommand(Cmd::WRITE_RAM_BLACK);
|
||||||
for (auto i = uint16_t{0}; i < width * height / 8; i++) {
|
for (auto i = uint16_t{0}; i < Width * Height / 8; i++) {
|
||||||
SendData(pgm_read_byte(&frame_buffer_black[i]));
|
sendData(pgm_read_byte(&blackFrame[i]));
|
||||||
}
|
}
|
||||||
SendCommand(0x26);
|
sendCommand(Cmd::WRITE_RAM_RED);
|
||||||
for (auto i = uint16_t{0}; i < width * height / 8; i++) {
|
for (auto i = uint16_t{0}; i < Width * Height / 8; i++) {
|
||||||
SendData(~pgm_read_byte(&frame_buffer_red[i]));
|
sendData(~pgm_read_byte(&redFrame[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
SendCommand(0x22);
|
sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
|
||||||
SendData(0xF7);
|
sendData(0xF7);
|
||||||
SendCommand(0x20);
|
sendCommand(Cmd::UPDATE_DISPLAY);
|
||||||
WaitUntilIdle();
|
waitUntilIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DisplayClear()
|
static void clear()
|
||||||
{
|
{
|
||||||
SendCommand(0x24);
|
sendCommand(Cmd::WRITE_RAM_BLACK);
|
||||||
for (auto i = uint16_t{0}; i < width * height / 8; i++) {
|
for (auto i = uint16_t{0}; i < Width * Height / 8; i++) {
|
||||||
SendData(0xff);
|
sendData(0xff);
|
||||||
}
|
}
|
||||||
SendCommand(0x26);
|
sendCommand(Cmd::WRITE_RAM_RED);
|
||||||
for (auto i = uint16_t{0}; i < width * height / 8; i++) {
|
for (auto i = uint16_t{0}; i < Width * Height / 8; i++) {
|
||||||
SendData(0x00);
|
sendData(0x00);
|
||||||
}
|
}
|
||||||
|
|
||||||
SendCommand(0x22);
|
sendCommand(Cmd::DISPLAY_UPDATE_CONTROL_2);
|
||||||
SendData(0xF7);
|
sendData(0xF7);
|
||||||
SendCommand(0x20);
|
sendCommand(Cmd::UPDATE_DISPLAY);
|
||||||
WaitUntilIdle();
|
waitUntilIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sleep()
|
static void sleep()
|
||||||
{
|
{
|
||||||
SendCommand(0x10); // power setting
|
sendCommand(Cmd::DEEP_SLEEP_MODE);
|
||||||
SendData(0x01); // gate switch to external
|
sendData(0x01);
|
||||||
_delay_ms(100);
|
_delay_ms(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpiTransfer(unsigned char data)
|
static void spiTransfer(const uint8_t data)
|
||||||
{
|
{
|
||||||
Spi::select(true);
|
Spi::select(true);
|
||||||
Spi::transfer(data);
|
Spi::transfer(data);
|
||||||
Spi::select(false);
|
Spi::select(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace eink
|
||||||
|
Loading…
Reference in New Issue
Block a user