瀏覽代碼

Variable width font output do motwork properli.

Vladimir N. Shilov 1 年之前
父節點
當前提交
4289d4fe7c
共有 5 個文件被更改,包括 29 次插入14 次删除
  1. 3 2
      lib/lib.mk
  2. 3 1
      lib/st7735/arial_8_ukr.c
  3. 2 1
      lib/st7735/fonts.h
  4. 19 8
      lib/st7735/st7735.c
  5. 2 2
      main.c

+ 3 - 2
lib/lib.mk

@@ -4,8 +4,9 @@ USERLIB = ./lib
 # List of all the Userlib files
 USERSRC =  $(USERLIB)/st7735/st7735.c \
 	$(USERLIB)/st7735/fonts.c \
-	$(USERLIB)/st7735/arial_8_ukr.c \
-	$(USERLIB)/stab/stab.c
+	$(USERLIB)/st7735/arial_8_ukr.c
+# \
+#	$(USERLIB)/stab/stab.c
 # \
 #	$(USERLIB)/eeprom/eeprom.c
           

+ 3 - 1
lib/st7735/arial_8_ukr.c

@@ -1,4 +1,5 @@
 #include "arial_8_ukr.h"
+#include "fonts.h"
 
 /*
  *  Font data for Arial 8pt
@@ -7,7 +8,7 @@
 /* Character bitmaps for Arial 8pt */
 // Height = 10 bit
 
-static const uint16_t Arial8x10 [1670] =
+static const uint16_t Arial8x10[1670] =
 {
 	/* @0 ' ' (2 pixels wide) */
 	0x0000, //
@@ -2357,3 +2358,4 @@ static const uint8_t ArialFontWidthTable[167] =
 };
 
 FontDefV Arial_8x10 = {10, ArialFontWidthTable, Arial8x10};
+FontDef Arial_8x10f = {8,10,Arial8x10};

+ 2 - 1
lib/st7735/fonts.h

@@ -5,7 +5,7 @@
 
 typedef struct {
   const uint8_t width;
-  uint8_t height;
+  const uint8_t height;
   const uint16_t *data;
 } FontDef;
 
@@ -13,5 +13,6 @@ typedef struct {
 extern FontDef Font_7x10;
 extern FontDef Font_11x18;
 extern FontDef Font_16x26;
+extern FontDef Arial_8x10f;
 
 #endif // __FONTS_H__

+ 19 - 8
lib/st7735/st7735.c

@@ -239,17 +239,26 @@ static void ST7735_WriteChar(uint16_t x, uint16_t y, char ch, FontDef font, uint
 
   ST7735_SetAddressWindow(x, y, x+font.width-1, y+font.height-1);
 
+  LCD_DC_DATA;
+  SPI1->CR1 |= SPI_CR1_DFF;
+
+  uint16_t ih = (ch - 32) * font.height;
   for(i = 0; i < font.height; i++) {
-    b = font.data[(ch - 32) * font.height + i];
+    b = font.data[ih + i];
     for(j = 0; j < font.width; j++) {
       if (b & 0x8000) {
-        ST7735_WriteData16(color);
+        while (!(SPI1->SR & SPI_SR_TXE));
+        SPI1->DR = color;
       } else {
-        ST7735_WriteData16(bgcolor);
+        while (!(SPI1->SR & SPI_SR_TXE));
+        SPI1->DR = bgcolor;
       }
       b <<= 1;
     }
   }
+  while (!(SPI1->SR & SPI_SR_TXE));
+  while ((SPI1->SR & SPI_SR_BSY));
+  SPI1->CR1 &= ~(SPI_CR1_DFF);
 }
 
 void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, uint16_t color, uint16_t bgcolor) {
@@ -289,7 +298,7 @@ void ST7735_WriteString(uint16_t x, uint16_t y, const char* str, FontDef font, u
  * @param bgcolor background color
  */
 static void ST7735_WriteCharV(uint16_t x, uint16_t y, char ch, FontDefV font, uint16_t color, uint16_t bgcolor) {
-  uint32_t i, b, j;
+  uint16_t i, b, j;
   uint8_t c, w, h;
 
   c = ch - 32;
@@ -301,8 +310,9 @@ static void ST7735_WriteCharV(uint16_t x, uint16_t y, char ch, FontDefV font, ui
   LCD_DC_DATA;
   SPI1->CR1 |= SPI_CR1_DFF;
 
+  uint16_t ih = c * h;
   for(i = 0; i < h; i++) {
-    b = font.data[c * h + i]; // ??? замінити на масив позицій
+    b = font.data[ih+i]; // ??? замінити на масив позицій
     for(j = 0; j < w; j++) {
       if (b & 0x8000) {
         while (!(SPI1->SR & SPI_SR_TXE));
@@ -314,7 +324,6 @@ static void ST7735_WriteCharV(uint16_t x, uint16_t y, char ch, FontDefV font, ui
       b <<= 1;
     }
   }
-
   while (!(SPI1->SR & SPI_SR_TXE));
   while ((SPI1->SR & SPI_SR_BSY));
   SPI1->CR1 &= ~(SPI_CR1_DFF);
@@ -323,8 +332,10 @@ static void ST7735_WriteCharV(uint16_t x, uint16_t y, char ch, FontDefV font, ui
 void ST7735_WriteStringV(uint16_t x, uint16_t y, const char* str, FontDefV font, uint16_t color, uint16_t bgcolor) {
   ST7735_Select();
 
+  uint8_t w;
   while (*str) {
-    if (x + font.width[(uint8_t)*str] >= ST7735_WIDTH) {
+    w = font.width[(uint8_t)*str];
+    if (x + w >= ST7735_WIDTH) {
       x = 0;
       y += font.height;
       if (y + font.height >= ST7735_HEIGHT) {
@@ -339,7 +350,7 @@ void ST7735_WriteStringV(uint16_t x, uint16_t y, const char* str, FontDefV font,
     }
 
     ST7735_WriteCharV(x, y, *str, font, color, bgcolor);
-    x += font.width[(uint8_t)*str];
+    x += w;
     str ++;
   }
 

+ 2 - 2
main.c

@@ -92,9 +92,9 @@ int main(void) {
 
   ST7735_FillScreen(ST7735_BLACK);
   ST7735_WriteString(0, 0, "Font_7x10, red on black, lorem ipsum dolor sit amet", Font_7x10, ST7735_RED, ST7735_BLACK);
-  ST7735_WriteStringV(0, 32, "Arial_8x10, green on black, lorem ipsum dolor sit amet", Arial_8x10, ST7735_GREEN, ST7735_BLACK);
+  ST7735_WriteString(0, 32, "Arial_8x10, green on black, lorem ipsum dolor sit amet", Arial_8x10f, ST7735_GREEN, ST7735_BLACK);
   ST7735_WriteString(0, 64, "Font_7x10, blue on black, lorem ipsum dolor sit amet", Font_7x10, ST7735_BLUE, ST7735_BLACK);
-  ST7735_WriteStringV(0, 96, "Arial_8x10, yellow on black, lorem ipsum dolor sit amet", Arial_8x10, ST7735_YELLOW, ST7735_BLACK);
+  ST7735_WriteString(0, 96, "Arial_8x10, yellow on black, lorem ipsum dolor sit amet", Arial_8x10f, ST7735_YELLOW, ST7735_BLACK);
 
   /*
    * Normal main() thread activity.