Skip to content

[验证成功] 增加屏幕旋转功能,适配老王144x72_JDI_Memory LCD屏幕,增加屏幕旋转测试例子 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions Display_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,43 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#define DISPLAY_WIDTH 400 // Display width in pixel
#define DISPLAY_HEIGHT 240 // Display height in pixel
// #define DISPLAY_WIDTH 400 // Display width in pixel
// #define DISPLAY_HEIGHT 240 // Display height in pixel

#define USE_ESP32_DMA // 使用ESP32 DMA
#define DIFF_LINE_UPDATE // 差异行更新
//#define USE_ESP32_DMA // 使用ESP32 DMA
//#define DIFF_LINE_UPDATE // 差异行更新

//=================================================================
// Wiring details: https://github.com/Gbertaz/JDI_MIP_Display#wiring
//=================================================================

// #define SPI_FREQUENCY 4000000 // SPI frequency in Hz
// #define SPI_CHANNEL spi0 // SPI channel number
// #define PIN_MISO D9 // SPI Data Signal pin
// #define PIN_MOSI D10 // SPI Data Signal pin
// #define PIN_SCK D8 // SPI Clock Signal pin
// #define PIN_SCS D7 // SPI Chip Select Signal pin
// #define PIN_DISP D0 // Display ON/OFF Switching Signal pin
// #define PIN_FRONTLIGHT -1 // Frontlight pin. Optional depending on the display model

//=================================================================
// 适配老王JDI_Memory LCD屏幕,采用MEGA2560进行驱动
// 分辨率为:144x72
//=================================================================

#define USE_144_72_LCD

#define DISPLAY_WIDTH 144 // Display width in pixel
#define DISPLAY_HEIGHT 72 // Display height in pixel

//控制引脚
#define PIN_SCS A0 // SPI Chip Select Signal pin
#define PIN_DISP A1 // Display ON/OFF Switching Signal pin

//SPI设置,不用关注
#define SPI_FREQUENCY 4000000 // SPI frequency in Hz
#define SPI_CHANNEL spi0 // SPI channel number
#define PIN_MISO D9 // SPI Data Signal pin
#define PIN_MOSI D10 // SPI Data Signal pin
#define PIN_SCK D8 // SPI Clock Signal pin
#define PIN_SCS D7 // SPI Chip Select Signal pin
#define PIN_DISP D0 // Display ON/OFF Switching Signal pin
#define PIN_MISO 50 // SPI Data Signal pin
#define PIN_MOSI 51 // SPI Data Signal pin
#define PIN_SCK 52 // SPI Clock Signal pin
#define PIN_FRONTLIGHT -1 // Frontlight pin. Optional depending on the display model
129 changes: 124 additions & 5 deletions JDI_MIP_Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
spi_host_device_t spi_host = (spi_host_device_t)1; // 绘制一次然后冻结
#endif

JDI_MIP_Display::JDI_MIP_Display() : Adafruit_GFX(DISPLAY_WIDTH, DISPLAY_HEIGHT), _pSPIx(&SPI), _spi_settings(SPI_FREQUENCY, MSBFIRST, SPI_MODE0)
JDI_MIP_Display::JDI_MIP_Display(uint16_t rot) : Adafruit_GFX(DISPLAY_WIDTH, DISPLAY_HEIGHT), _pSPIx(&SPI), _spi_settings(SPI_FREQUENCY, MSBFIRST, SPI_MODE0)
{
_mosi = PIN_MOSI;
_miso = PIN_MISO;
Expand All @@ -37,6 +37,7 @@ JDI_MIP_Display::JDI_MIP_Display() : Adafruit_GFX(DISPLAY_WIDTH, DISPLAY_HEIGHT)
_disp = PIN_DISP;
_freq = SPI_FREQUENCY;
_frontlight = PIN_FRONTLIGHT;
set_direction(rot);
}

void JDI_MIP_Display::selectSPI(SPIClass& spi, SPISettings spi_settings)
Expand All @@ -45,6 +46,16 @@ void JDI_MIP_Display::selectSPI(SPIClass& spi, SPISettings spi_settings)
_spi_settings = spi_settings;
}


void JDI_MIP_Display::set_direction(uint16_t rot){
_rotation = rot;
if((rot == 0) || (rot == 180)){
setRotation(1); //等效 setRotation(3);
}else{
setRotation(0); //等效 setRotation(2);
}
}

void JDI_MIP_Display::begin()
{
_background = COLOR_BLACK;
Expand All @@ -65,9 +76,117 @@ void JDI_MIP_Display::begin()
#endif
}

#ifdef USE_144_72_LCD
void JDI_MIP_Display::refresh() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function must be generic and not tied to a specific display resolution.

char buf = 0;
switch (_rotation) {
case 270: // 遍历所有行
for (int i = 0; i < 144; i++) {
// 片选拉高,准备通信
digitalWrite(_scs, HIGH);
_pSPIx->transfer(CMD_UPDATE);
_pSPIx->transfer(i + 1);

// 处理行数据
for (int a = 70; a >= 0; a -= 2) {
if (i % 2 == 0) {
// 偶数行:提取高 4 位并合并
buf = ((_backBuffer[(a + 1) * 72 + i / 2] & 0xF0)) |
((_backBuffer[(a + 0) * 72 + i / 2] & 0xF0) >> 4);
} else {
// 奇数行:提取低 4 位并合并
buf = ((_backBuffer[(a + 1) * 72 + i / 2] & 0x0F) << 4) |
(_backBuffer[(a + 0) * 72 + i / 2] & 0x0F);
}

// 通过 SPI 发送数据
_pSPIx->transfer(buf);
}
// 结束数据传输,发送两个 0x00 字节
_pSPIx->transfer(0x00);
_pSPIx->transfer(0x00);

// 片选拉低,结束通信
digitalWrite(_scs, LOW);
}
break;
case 90:
for (int i = 0; i < 144; i++) {
// 片选拉高,准备通信
digitalWrite(_scs, HIGH);
_pSPIx->transfer(CMD_UPDATE);
_pSPIx->transfer(i + 1);

// 处理行数据
for (int a = 0; a < 72; a += 2) {
if ((143 - i) % 2 == 0) {
// 偶数行:提取高 4 位并合并
buf = ((_backBuffer[(a + 0) * 72 + (143 - i) / 2] & 0xF0)) |
((_backBuffer[(a + 1) * 72 + (143 - i) / 2] & 0xF0) >>
4);

} else {
// 奇数行:提取低 4 位并合并
buf = ((_backBuffer[(a + 0) * 72 + (143 - i) / 2] & 0x0F)
<< 4) |
(_backBuffer[(a + 1) * 72 + (143 - i) / 2] & 0x0F);
}

// 通过 SPI 发送数据
_pSPIx->transfer(buf);
}
// 结束数据传输,发送两个 0x00 字节
_pSPIx->transfer(0x00);
_pSPIx->transfer(0x00);

// 片选拉低,结束通信
digitalWrite(_scs, LOW);
}
break;
case 180:
for (int i = 0; i < 144; i++)
{
int lineIdx = 36 * (143 - i);
char *line_cmd;
_pSPIx->beginTransaction(_spi_settings);
digitalWrite(_scs, HIGH);
_pSPIx->transfer(CMD_UPDATE);
_pSPIx->transfer(i + 1);

for(int a = 0; a < 36; a++){

//交换高四位和低四位,因为反过来读的
buf = _backBuffer[lineIdx + 35 - a];
buf = ((buf & 0xF0) >> 4) | ((buf & 0x0F) << 4);

_pSPIx->transfer(buf);
}

_pSPIx->transfer(0x00);
_pSPIx->transfer(0x00);
digitalWrite(_scs, LOW);
_pSPIx->endTransaction();
}
break;
case 0:
for (int i = 0; i < height(); i++)
{
int lineIdx = width()/2 * i;
char *line_cmd;
line_cmd = &_backBuffer[lineIdx];
sendLineCommand(line_cmd, i);
}
break;
default:
break;
}
}

#else

void JDI_MIP_Display::refresh()
{
for (int i = 0; i < HEIGHT; i++)
for (int i = 0; i < height(); i++)
{
int lineIdx = HALF_WIDTH * i;
char *line_cmd;
Expand All @@ -81,7 +200,7 @@ void JDI_MIP_Display::refresh()
sendLineCommand(line_cmd, i);
}
}

#endif

bool JDI_MIP_Display::compareBuffersLine(int lineIndex)
{
Expand Down Expand Up @@ -122,7 +241,7 @@ void JDI_MIP_Display::clearScreen()

void JDI_MIP_Display::sendLineCommand(char *line_cmd, int line)
{
if ((line < 0) || (line >= HEIGHT))
if ((line < 0) || (line >= height()))
{
return;
}
Expand All @@ -149,7 +268,7 @@ void JDI_MIP_Display::sendLineCommand(char *line_cmd, int line)
_pSPIx->transfer(CMD_UPDATE);
_pSPIx->transfer(line + 1);

for(int i = 0; i < HALF_WIDTH; i++){
for(int i = 0; i < width()/2; i++){
_pSPIx->transfer(line_cmd[i]);
}

Expand Down
4 changes: 3 additions & 1 deletion JDI_MIP_Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
class JDI_MIP_Display : public Adafruit_GFX
{
public:
JDI_MIP_Display();
JDI_MIP_Display(uint16_t rot = 1);
void begin();
void refresh();
void refresh2();
Expand All @@ -64,6 +64,7 @@ class JDI_MIP_Display : public Adafruit_GFX
void setBackgroundColor(uint16_t color);
void drawBufferedPixel(int16_t x, int16_t y, uint16_t color);
void pushPixelsDMA(uint8_t *image, uint32_t len);
void set_direction(uint16_t n);
private:
uint8_t _sck; // 时钟信号 Clock signal
uint8_t _miso; // 主输入从输出 Master input from output
Expand All @@ -73,6 +74,7 @@ class JDI_MIP_Display : public Adafruit_GFX
uint8_t _frontlight; // 前置灯 Front lights
uint16_t _background; // 背景 background
uint32_t _freq; // SPI频率 SPI frequency
uint16_t _rotation; // 屏幕旋转方向 0 90 180 270

SPIClass* _pSPIx;
SPISettings _spi_settings;
Expand Down
Loading