Skip to content

Commit 91a20fa

Browse files
committed
Updated for ESP-IDF 6.0, added missing print() functions for ESP and enabled anti-aliased characters in 2-bit mode
1 parent a2c24e9 commit 91a20fa

5 files changed

Lines changed: 177 additions & 19 deletions

File tree

src/FastEPD.cpp

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,106 @@ uint8_t FASTEPD::ioRead(uint8_t u8Pin)
466466
return val;
467467
}
468468

469+
// Implement missing print functions
470+
#ifndef ARDUINO
471+
void FASTEPD::print(int value, int format)
472+
{
473+
char c, ucTemp[32];
474+
char *d = &ucTemp[31];
475+
476+
if (value) {
477+
d[0] = 0;
478+
switch(format) {
479+
case DEC:
480+
while (value) {
481+
d--;
482+
*d = '0' + (value % 10);
483+
value /= 10;
484+
}
485+
break;
486+
case HEX:
487+
while (value) {
488+
d--;
489+
c = value & 0xf;
490+
if (c < 10)
491+
*d = '0' + c;
492+
else
493+
*d = 'A' + (c-10);
494+
value >>= 4;
495+
}
496+
break;
497+
case OCT:
498+
while (value) {
499+
d--;
500+
*d = '0' + (value & 7);
501+
value >>= 3;
502+
}
503+
break;
504+
case BIN:
505+
while (value) {
506+
d--;
507+
*d = '0' + (value & 1);
508+
value >>= 1;
509+
}
510+
break;
511+
default:
512+
break;
513+
}
514+
} else { // if zero value
515+
d--;
516+
*d = '0';
517+
}
518+
print((const char *)d);
519+
} /* print() */
520+
521+
void FASTEPD::println(int value, int format)
522+
{
523+
char ucTemp[4];
524+
525+
print(value, format);
526+
ucTemp[0] = '\n';
527+
ucTemp[1] = '\r';
528+
ucTemp[2] = 0;
529+
print((const char *)ucTemp);
530+
} /* println() */
531+
532+
void FASTEPD::print(const std::string &str)
533+
{
534+
print(str.c_str());
535+
} /* print() */
536+
537+
void FASTEPD::println(const char *pString)
538+
{
539+
char ucTemp[4];
540+
541+
print(pString);
542+
ucTemp[0] = '\n';
543+
ucTemp[1] = '\r';
544+
ucTemp[2] = 0;
545+
print((const char *)ucTemp);
546+
} /* println() */
547+
void FASTEPD::println(const std::string &str)
548+
{
549+
char ucTemp[4];
550+
551+
print(str);
552+
ucTemp[0] = '\n';
553+
ucTemp[1] = '\r';
554+
ucTemp[2] = 0;
555+
print((const char *)ucTemp);
556+
} /* println() */
557+
558+
void FASTEPD::print(const char *pString)
559+
{
560+
uint8_t *s = (uint8_t *)pString;
561+
562+
while (*s != 0) {
563+
write(*s++);
564+
}
565+
} /* print() */
566+
567+
#endif // !ARDUINO
568+
469569
void FASTEPD::setBitBang(bool bBitBang)
470570
{
471571
_state.bit_bang = (uint8_t)bBitBang;
@@ -487,7 +587,9 @@ void FASTEPD::setFont(const void *pFont, bool bAntiAlias)
487587
{
488588
_state.iFont = -1;
489589
_state.pFont = (void *)pFont;
490-
if (_state.mode != BB_MODE_4BPP) bAntiAlias = false; // only works in grayscale mode
590+
if (_state.mode == BB_MODE_1BPP) {
591+
bAntiAlias = false; // only works in 2 or 4-bit grayscale mode
592+
}
491593
_state.anti_alias = (uint8_t)bAntiAlias;
492594
} /* setFont() */
493595

@@ -582,8 +684,13 @@ static uint8_t u8Unicode0, u8Unicode1;
582684
pGlyphSmall = &pBBFS->glyphs[c - first]; pGlyph = NULL;
583685
}
584686
if (c == '\n') {
687+
h = (pBBF) ? pBBF->height : pBBFS->height;
585688
_state.iCursorX = 0;
586-
_state.iCursorY += (pBBF) ? pBBF->height : pBBFS->height;
689+
if (_state.anti_alias) {
690+
_state.iCursorY += h/2;
691+
} else {
692+
_state.iCursorY += h;
693+
}
587694
} else if (c != '\r') {
588695
if (c >= first && c <= last) {
589696
if (pBBF) {
@@ -597,7 +704,11 @@ static uint8_t u8Unicode0, u8Unicode1;
597704
w += (pBBF) ? pGlyph->xOffset : pGlyphSmall->xOffset;
598705
if (_state.wrap && (_state.iCursorX + w) > _state.width) {
599706
_state.iCursorX = 0;
600-
_state.iCursorY += h;
707+
if (_state.anti_alias) {
708+
_state.iCursorY += h/2;
709+
} else {
710+
_state.iCursorY += h;
711+
}
601712
}
602713
bbepWriteStringCustom(&_state, _state.pFont, -1, -1, szTemp, _state.iFG);
603714
}

src/FastEPD.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
#include <stdio.h>
2525
#include <stdlib.h>
2626
#include <string.h>
27-
// for Print support
28-
#ifdef __LINUX__
27+
#ifdef __cplusplus
2928
#include <string>
29+
#endif // __cplusplus
30+
// for Print support
3031
using namespace std;
3132
#define DEC 10
3233
#define HEX 16
3334
#define OCT 8
3435
#define BIN 2
35-
#endif // __LINUX__
3636
#endif
3737

3838
#define BB_PANEL_FLAG_NONE 0x00
@@ -320,8 +320,8 @@ class FASTEPD
320320
void println(const char *pString);
321321
void print(int, int);
322322
void println(int, int);
323-
void print(const string &);
324-
void println(const string &);
323+
void print(const std::string &);
324+
void println(const std::string &);
325325
#endif
326326

327327
protected:

src/FastEPD.inl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,21 @@ static bool s3_notify_dma_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_pane
288288
// Maximum line width = 1024 * 4 = 4096 pixels
289289
#define MAX_TX_SIZE 1024
290290
static esp_lcd_i80_bus_config_t s3_bus_config = {
291-
.dc_gpio_num = 0,
292-
.wr_gpio_num = 0,
291+
.dc_gpio_num = (gpio_num_t)0,
292+
.wr_gpio_num = (gpio_num_t)0,
293293
.clk_src = /*LCD_CLK_SRC_DEFAULT,*/ LCD_CLK_SRC_PLL160M,
294-
.data_gpio_nums = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
294+
.data_gpio_nums = {(gpio_num_t)0},
295295
.bus_width = 0,
296296
.max_transfer_bytes = MAX_TX_SIZE,
297+
#if ESP_IDF_VERSION <= ESP_IDF_VERSION_VAL(5, 0, 0)
297298
.psram_trans_align = 0, // 0 = use default values
298299
.sram_trans_align = 0,
300+
#else
301+
.dma_burst_size = 32,
302+
#endif
299303
};
300304
static esp_lcd_panel_io_i80_config_t s3_io_config = {
301-
.cs_gpio_num = 0,
305+
.cs_gpio_num = (gpio_num_t)0,
302306
.pclk_hz = 12000000,
303307
.trans_queue_depth = 4,
304308
.on_color_trans_done = s3_notify_dma_ready,
@@ -1429,15 +1433,15 @@ int bbepIOInit(FASTEPDSTATE *pState)
14291433
s3_bus_config.wr_gpio_num = (gpio_num_t)pState->panelDef.ioCL;
14301434
s3_bus_config.bus_width = pState->panelDef.bus_width;
14311435
for (int i=0; i<pState->panelDef.bus_width; i++) {
1432-
s3_bus_config.data_gpio_nums[i] = pState->panelDef.data[i];
1436+
s3_bus_config.data_gpio_nums[i] = (gpio_num_t)pState->panelDef.data[i];
14331437
}
14341438
ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&s3_bus_config, &i80_bus));
14351439
s3_io_config.pclk_hz = pState->panelDef.bus_speed;
14361440
if (pState->panelDef.flags & BB_PANEL_FLAG_SLOW_SPH) {
14371441
bSlowSPH = 1;
14381442
u8SPH = (gpio_num_t)pState->panelDef.ioSPH;
14391443
u8CKV = (gpio_num_t)pState->panelDef.ioCKV;
1440-
s3_io_config.cs_gpio_num = -1; // disable hardware CS
1444+
s3_io_config.cs_gpio_num = (gpio_num_t)-1; // disable hardware CS
14411445
} else {
14421446
s3_io_config.cs_gpio_num = (gpio_num_t)pState->panelDef.ioSPH;
14431447
}

src/arduino_io.inl

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ static int iDelay = 0; //1;
3232
#ifndef ARDUINO
3333
#include "driver/gpio.h"
3434
#include "esp_timer.h"
35-
35+
#include "driver/i2c_master.h"
36+
i2c_master_bus_handle_t bus_handle;
37+
i2c_master_dev_handle_t dev_handle;
3638
// GPIO modes
3739
#define memcpy_P memcpy
3840
#define pgm_read_byte(a) (*(uint8_t *)a)
@@ -273,6 +275,17 @@ int bbepI2CInit(uint8_t sda, uint8_t scl, int bb)
273275
Wire.setClock(400000);
274276
Wire.setTimeout(100);
275277
#else
278+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
279+
i2c_master_bus_config_t conf;
280+
281+
conf.i2c_port = I2C_NUM_0;
282+
conf.sda_io_num = (gpio_num_t)sda;
283+
conf.scl_io_num = (gpio_num_t)scl;
284+
conf.clk_source = I2C_CLK_SRC_DEFAULT;
285+
conf.glitch_ignore_cnt = 7;
286+
conf.flags.enable_internal_pullup = true;
287+
ESP_ERROR_CHECK(i2c_new_master_bus(&conf, &bus_handle));
288+
#else // older esp-idff
276289
i2c_config_t conf;
277290
ESP_ERROR_CHECK(i2c_driver_delete());
278291
conf.mode = I2C_MODE_MASTER;
@@ -281,9 +294,10 @@ int bbepI2CInit(uint8_t sda, uint8_t scl, int bb)
281294
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
282295
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
283296
conf.master.clk_speed = 400000;
284-
conf.clk_flags = 0;
297+
conf.clk_flags = 0;
285298
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
286299
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
300+
#endif // older IDF
287301
#endif // ARDUINO
288302
} // !BitBang
289303
return BBEP_SUCCESS;
@@ -302,6 +316,16 @@ int bbepI2CWrite(unsigned char iAddr, unsigned char *pData, int iLen)
302316
rc = !Wire.endTransmission();
303317
return rc;
304318
#else
319+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
320+
i2c_device_config_t dev_config;
321+
dev_config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
322+
dev_config.device_address = iAddr;
323+
dev_config.scl_speed_hz = 400000;
324+
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_config, &dev_handle));
325+
esp_err_t ret = i2c_master_transmit(dev_handle, pData, iLen, 1000); // 1-second timeout
326+
i2c_master_bus_rm_device(dev_handle);
327+
return (ret == ESP_OK);
328+
#else // older idf version
305329
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
306330
if (cmd == NULL) {
307331
// ESP_LOGE("bb_epdiy", "insufficient memory for I2C transaction");
@@ -313,6 +337,7 @@ int bbepI2CWrite(unsigned char iAddr, unsigned char *pData, int iLen)
313337
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_PERIOD_MS);
314338
i2c_cmd_link_delete(cmd);
315339
return (ret == ESP_OK);
340+
#endif // older esp-idf
316341
#endif // ARDUINO
317342
} // !BitBang
318343
}
@@ -330,6 +355,16 @@ int i = 0;
330355
}
331356
#else
332357
esp_err_t ret;
358+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
359+
i2c_device_config_t dev_config;
360+
dev_config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
361+
dev_config.device_address = iAddr;
362+
dev_config.scl_speed_hz = 400000;
363+
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_config, &dev_handle));
364+
ret = i2c_master_transmit_receive(dev_handle, NULL, 0, pData, iLen, 1000); // 1-second timeout
365+
i2c_master_bus_rm_device(dev_handle);
366+
return (ret == ESP_OK);
367+
#else // older ESP-IDF
333368
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
334369
if (cmd == NULL) {
335370
// ESP_LOGE("epdiy", "insufficient memory for I2C transaction");
@@ -347,6 +382,7 @@ int i = 0;
347382
i = iLen;
348383
}
349384
i2c_cmd_link_delete(cmd);
385+
#endif // older esp-idf version
350386
#endif // ARDUINO
351387
} // !BitBang
352388
return i;

src/bb_ep_gfx.inl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,9 +1141,16 @@ int bbepWriteStringCustom(FASTEPDSTATE *pBBEP, const void *pFont, int x, int y,
11411141
}
11421142
tw = w;
11431143
if (pBBEP->anti_alias) { // draw half-size anti-aliased characters
1144-
const uint8_t grayColors[4] = {0xf, 0xc, 0x6, 0};
1145-
const uint8_t invertedGrays[4] = {0, 0x6, 0xc, 0xf};
1146-
uint8_t *pGrays = (iBG == 0) ? (uint8_t*)invertedGrays : (uint8_t*)grayColors;
1144+
const uint8_t grayColors4bpp[4] = {0xf, 0xc, 0x6, 0};
1145+
const uint8_t invertedGrays4bpp[4] = {0, 0x6, 0xc, 0xf};
1146+
const uint8_t grayColors2bpp[4] = {3, 2, 1, 0};
1147+
const uint8_t invertedGrays2bpp[4] = {0, 1, 2, 3};
1148+
uint8_t *pGrays;
1149+
if (pBBEP->mode == BB_MODE_4BPP) {
1150+
pGrays = (iBG == 0) ? (uint8_t*)invertedGrays4bpp : (uint8_t*)grayColors4bpp;
1151+
} else { // must be 2bpp
1152+
pGrays = (iBG == 0) ? (uint8_t*)invertedGrays2bpp : (uint8_t*)grayColors2bpp;
1153+
}
11471154
int iLineSize = (tw+7)/8;
11481155
memset(u8Cache, 0, iLineSize*2); // start with 2 lines of white (gray table is inverted)
11491156
for (ty=dy; ty<end_y+1 && ty+1 < height; ty++) {

0 commit comments

Comments
 (0)