Pārlūkot izejas kodu

Added UTF8 text support.

Vladimir N. Shilov 1 gadu atpakaļ
vecāks
revīzija
8c60f24d00
4 mainītis faili ar 33 papildinājumiem un 9 dzēšanām
  1. 1 0
      lib/st7735/fonts.h
  2. 29 6
      lib/st7735/st7735.c
  3. 1 1
      lib/st7735/st7735.h
  4. 2 2
      lib/stab/stab.c

+ 1 - 0
lib/st7735/fonts.h

@@ -14,5 +14,6 @@ extern FontDef Font_7x10;
 extern FontDef Font_11x18;
 extern FontDef Font_16x26;
 extern FontDef LiberM_7x10;
+#define USE_UTF8
 
 #endif // __FONTS_H__

+ 29 - 6
lib/st7735/st7735.c

@@ -264,7 +264,8 @@ static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint
 void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor) {
   ST7735_Select();
 
-  while (*str) {
+  char ch = *str;
+  while (ch) {
     if (x + font.width >= ST7735_WIDTH) {
       x = 0;
       y += font.height;
@@ -272,16 +273,37 @@ void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, u
         break;
       }
 
-      if (*str == ' ') {
+      if (ch == ' ') {
         // skip spaces in the beginning of the new line
         str ++;
+        ch = *str;
         continue;
       }
     }
-
-    ST7735_WriteChar(x, y, *str, font, color, bgcolor);
+#ifdef USE_UTF8
+    if (ch >= 0xC0) {
+      switch (ch) {
+        case 0xD0: {
+          str ++;
+          ch = *str;
+          if (ch == 0x81) { ch = 0xA8; break; }
+          if (ch >= 0x90 && ch <= 0xBF) ch += 0x2F;
+          break;
+        }
+        case 0xD1: {
+          str ++;
+          ch = *str;
+          if (ch == 0x91) { ch = 0xB8; break; }
+          if (ch >= 0x7E && ch <= 0x8F) ch += 0x6F;
+          break;
+        }
+      }
+    }
+#endif /* USE_UTF8 */
+    ST7735_WriteChar(x, y, ch, font, color, bgcolor);
     x += font.width;
     str ++;
+    ch = *str;
   }
 
   ST7735_Unselect();
@@ -380,7 +402,7 @@ void ST7735_FillRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16
 
   ST7735_Unselect();
 }
-#ifdef USER_MALLOC
+#ifdef USE_MALLOC
 void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) {
   // clipping
   if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) {
@@ -398,7 +420,8 @@ void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, ui
 
   // Prepare whole line in a single buffer
   uint8_t pixel[] = {color >> 8, color & 0xFF};
-  uint8_t *line = malloc(w * sizeof(pixel));
+//  uint8_t *line = malloc(w * sizeof(pixel));
+  uint8_t *line = chHeapAlloc(NULL, w * sizeof(pixel));
   for (x = 0; x < w; ++x) {
     memcpy(line + x * sizeof(pixel), pixel, sizeof(pixel));
   }

+ 1 - 1
lib/st7735/st7735.h

@@ -254,7 +254,7 @@ void ST7735_DrawImage(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const uint
 void ST7735_InvertColors(bool invert);
 void ST7735_SetGamma(GammaDef gamma);
 void ST7735_Test(void);
-#ifdef USER_MALLOC
+#ifdef USE_MALLOC
 void ST7735_FillRectangleFast(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color);
 void ST7735_FillScreenFast(uint16_t color);
 #endif

+ 2 - 2
lib/stab/stab.c

@@ -673,7 +673,7 @@ static void gpt_cb(GPTDriver *drv) {
 static void ADC_cb(ADCDriver *adcp) {
   (void)adcp;
 
-  static uint16_t TM2_current;
+  static uint16_t TM2_current = 0;
   static int16_t Ufir = 0;   // Буферная переменная для НЧ-фильтрации
   static int16_t Udelta = 0; // Буферная переменная для НЧ-фильтрации
   {
@@ -707,7 +707,7 @@ static void ADC_cb(ADCDriver *adcp) {
         sum = 0;
         cnt_P_sum = 0; // Сбрасываем счетчик, сумматор и счетчик полупериодов
       }
-      TM2_current = TM2_tmp;  // Запомним значение для дальнейшей обработки
+      // !!!TM2_current = TM2_tmp;  // Запомним значение для дальнейшей обработки
       fl.zero = 1;
     } else {
       // переход через ноль NOT детектед