Added Unicode (codepage1252) support and additional esp-idf C++ example

This commit is contained in:
Larry Bank
2025-05-29 14:15:27 +01:00
parent 419f07afaf
commit 3fdba9282b
8 changed files with 5217 additions and 5 deletions
+10
View File
@@ -0,0 +1,10 @@
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS "../../")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(Spectra6)
+2
View File
@@ -0,0 +1,2 @@
idf_component_register(SRCS "Spectra6.cpp"
INCLUDE_DIRS ".")
File diff suppressed because it is too large Load Diff
+43
View File
@@ -0,0 +1,43 @@
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <bb_epaper.h>
#include "Roboto_Black_40.h"
BBEPAPER bbep(EP81_SPECTRA_1024x576);
#define DC_PIN 14
#define BUSY_PIN 13
#define RESET_PIN 9
#define CS_PIN 10
#define CS_PIN2 8
#define SCK_PIN 12
#define MOSI_PIN 11
extern "C" void app_main(void)
{
bbep.initIO(DC_PIN, RESET_PIN, BUSY_PIN, CS_PIN, MOSI_PIN, SCK_PIN, 8000000);
bbep.setCS2(CS_PIN2);
bbep.setRotation(0);
bbep.allocBuffer();
bbep.fillScreen(BBEP_WHITE);
bbep.setFont(&Roboto_Black_40);
bbep.setTextColor(BBEP_BLACK);
bbep.setCursor(0, 75);
bbep.print("Mañana");
bbep.setTextColor(BBEP_RED);
bbep.setCursor(100, 150);
bbep.print("Ótimo!");
bbep.setTextColor(BBEP_BLUE);
bbep.setCursor(200, 225);
bbep.print("Non è vero?");
bbep.setTextColor(BBEP_YELLOW);
bbep.setCursor(300, 300);
bbep.print("Não foi eu!");
bbep.writePlane();
bbep.refresh(REFRESH_FULL);
bbep.sleep(1); // deep sleep
while (1) {
vTaskDelay(1);
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+134 -4
View File
@@ -833,6 +833,130 @@ void bbepSetTextWrap(BBEPDISP *pBBEP, int bWrap)
pBBEP->wrap = bWrap;
} /* bbepSetTextWrap() */
//
// Convert a single Unicode character into codepage 1252 (extended ASCII)
//
uint8_t bbepUnicodeTo1252(uint16_t u16CP)
{
// convert supported Unicode values to codepage 1252
switch(u16CP) { // they're all over the place, so check each
case 0x20ac:
u16CP = 0x80; // euro sign
break;
case 0x201a: // single low quote
u16CP = 0x82;
break;
case 0x192: // small F with hook
u16CP = 0x83;
break;
case 0x201e: // double low quote
u16CP = 0x84;
break;
case 0x2026: // hor ellipsis
u16CP = 0x85;
break;
case 0x2020: // dagger
u16CP = 0x86;
break;
case 0x2021: // double dagger
u16CP = 0x87;
break;
case 0x2c6: // circumflex
u16CP = 0x88;
break;
case 0x2030: // per mille
u16CP = 0x89;
break;
case 0x160: // capital S with caron
u16CP = 0x8a;
break;
case 0x2039: // single left pointing quote
u16CP = 0x8b;
break;
case 0x152: // capital ligature OE
u16CP = 0x8c;
break;
case 0x17d: // captial Z with caron
u16CP = 0x8e;
break;
case 0x2018: // left single quote
u16CP = 0x91;
break;
case 0x2019: // right single quote
u16CP = 0x92;
break;
case 0x201c: // left double quote
u16CP = 0x93;
break;
case 0x201d: // right double quote
u16CP = 0x94;
break;
case 0x2022: // bullet
u16CP = 0x95;
break;
case 0x2013: // en dash
u16CP = 0x96;
break;
case 0x2014: // em dash
u16CP = 0x97;
break;
case 0x2dc: // small tilde
u16CP = 0x98;
break;
case 0x2122: // trademark
u16CP = 0x99;
break;
case 0x161: // small s with caron
u16CP = 0x9a;
break;
case 0x203a: // single right quote
u16CP = 0x9b;
break;
case 0x153: // small ligature oe
u16CP = 0x9c;
break;
case 0x17e: // small z with caron
u16CP = 0x9e;
break;
case 0x178: // capital Y with diaeresis
u16CP = 0x9f;
break;
default:
if (u16CP > 0xff) u16CP = 32; // something went wrong
break;
} // switch on character
return (uint8_t)u16CP;
} /* bbepUnicodeTo1252() */
//
// Convert a Unicode string into our extended ASCII set (codepage 1252)
//
void bbepUnicodeString(const char *szMsg, uint8_t *szExtMsg)
{
int i, j;
uint8_t c;
uint16_t u16CP; // 16-bit codepoint encoded by the multi-byte sequence
i = j = 0;
while (szMsg[i]) {
c = szMsg[i++];
if (c < 0x80) { // normal 7-bit ASCII
u16CP = c;
} else { // multibyte
if (c < 0xe0) { // first 0x800 characters
u16CP = (c & 0x3f) << 6;
u16CP += (szMsg[i++] & 0x3f);
} else if (c < 0xf0) { // 0x800 to 0x10000
u16CP = (c & 0x3f) << 12;
u16CP += ((szMsg[i++] & 0x3f)<<6);
u16CP += (szMsg[i++] & 0x3f);
} else { // 0x10001 to 0x20000
u16CP = 32; // convert to spaces (nothing supported here)
}
} // multibyte
szExtMsg[j++] = bbepUnicodeTo1252(u16CP);
} // while szMsg[i]
szExtMsg[j++] = 0; // zero terminate it
} /* bbepUnicodeString() */
//
// Draw a string of BB_FONT characters directly into the EPD framebuffer
//
int bbepWriteStringCustom(BBEPDISP *pBBEP, BB_FONT *pFont, int x, int y, char *szMsg, int iColor, uint8_t iPlane)
@@ -843,6 +967,7 @@ int bbepWriteStringCustom(BBEPDISP *pBBEP, BB_FONT *pFont, int x, int y, char *s
uint8_t *s, uc0, uc1;
BB_GLYPH *pGlyph;
uint8_t *pBits, u8CMD1, u8CMD2, u8CMD, u8EndMask;
uint8_t szExtMsg[256]; // translated extended ASCII message text
uint8_t first, last;
if (pBBEP == NULL) return BBEP_ERROR_BAD_PARAMETER;
@@ -853,6 +978,11 @@ int bbepWriteStringCustom(BBEPDISP *pBBEP, BB_FONT *pFont, int x, int y, char *s
if (iColor != BBEP_TRANSPARENT) {
iColor = pBBEP->pColorLookup[iColor & 0xf]; // translate the color for this display type
}
if (szMsg[1] == 0 && (szMsg[0] & 0x80)) { // single byte means we're coming from the Arduino write() method with pre-converted extended ASCII
szExtMsg[0] = szMsg[0]; szExtMsg[1] = 0;
} else {
bbepUnicodeString(szMsg, szExtMsg); // convert to extended ASCII
}
iBG = pBBEP->iBG;
if (iBG == -1) iBG = BBEP_TRANSPARENT; // -1 = don't care
if (iBG != BBEP_TRANSPARENT) {
@@ -866,8 +996,8 @@ int bbepWriteStringCustom(BBEPDISP *pBBEP, BB_FONT *pFont, int x, int y, char *s
last = pgm_read_byte(&pFont->last);
if (x == CENTER_X) { // center the string on the e-paper
dx = i = 0;
while (szMsg[i]) {
c = szMsg[i++];
while (szExtMsg[i]) {
c = szExtMsg[i++];
if (c < first || c > last) // undefined character
continue; // skip it
c -= first; // first char of font defined
@@ -882,8 +1012,8 @@ int bbepWriteStringCustom(BBEPDISP *pBBEP, BB_FONT *pFont, int x, int y, char *s
pBits += sizeof(BB_FONT);
pBits += (last - first + 1) * sizeof(BB_GLYPH);
i = 0;
while (szMsg[i] && x < pBBEP->width && y < pBBEP->height) {
c = szMsg[i++];
while (szExtMsg[i] && x < pBBEP->width && y < pBBEP->height) {
c = szExtMsg[i++];
if (c < first || c > last) // undefined character
continue; // skip it
c -= first; // first char of font defined
+29 -1
View File
@@ -369,8 +369,36 @@ char ucTemp[4];
size_t BBEPAPER::write(uint8_t c) {
char szTemp[2]; // used to draw 1 character at a time to the C methods
int w=8, h=8;
static int iUnicodeCount = 0;
static uint8_t u8Unicode0, u8Unicode1;
szTemp[0] = c; szTemp[1] = 0;
if (iUnicodeCount == 0) {
if (c >= 0x80) { // start of a multi-byte character
iUnicodeCount++;
u8Unicode0 = c;
return 1;
}
} else { // middle/end of a multi-byte character
uint16_t u16Code;
if (u8Unicode0 < 0xe0) { // 2 byte char, 0-0x7ff
u16Code = (u8Unicode0 & 0x3f) << 6;
u16Code += (c & 0x3f);
c = bbepUnicodeTo1252(u16Code);
iUnicodeCount = 0;
} else { // 3 byte character 0x800 and above
if (iUnicodeCount == 1) {
iUnicodeCount++; // save for next byte to arrive
u8Unicode1 = c;
return 1;
}
u16Code = (u8Unicode0 & 0x3f) << 12;
u16Code += (u8Unicode1 & 0x3f) << 6;
u16Code += (c & 0x3f);
c = bbepUnicodeTo1252(u16Code);
iUnicodeCount = 0;
}
}
szTemp[0] = c; szTemp[1] = 0;
if (_bbep.pFont == NULL) { // use built-in fonts
if (_bbep.iFont == FONT_8x8 || _bbep.iFont == FONT_6x8) {
h = 8;