ソースを参照

Added X_centered().

Vladimir N. Shilov 1 年間 前
コミット
59b65ba76b
2 ファイル変更20 行追加3 行削除
  1. 1 0
      lib/main.h
  2. 19 3
      main.c

+ 1 - 0
lib/main.h

@@ -50,5 +50,6 @@ static btn_hndlr bha[Button_Num] = {
 
 static void lcd_MainScreen(void);
 static void lcd_ClearMain(void);
+static uint8_t X_centered(const uint8_t len, const uint8_t pix);
 
 #endif /* __MAIN_H__ */

+ 19 - 3
main.c

@@ -127,9 +127,9 @@ int main(void) {
 static void lcd_MainScreen(void) {
   ST7735_FillScreen(Black);
 
-  ST7735_WriteString(47, LCD_LINE_2, "Heater", Font_11x18, Yellow, Black);
-  ST7735_WriteString(52, LCD_LINE_4, "Power", Font_11x18, Yellow, Black);
-  ST7735_WriteString(25, LCD_LINE_6, "Stabilizer", Font_11x18, Yellow, Black);
+  ST7735_WriteString(X_centered(6, 11), LCD_LINE_2, "Heater", Font_11x18, Yellow, Black);
+  ST7735_WriteString(X_centered(5, 11), LCD_LINE_4, "Power", Font_11x18, Yellow, Black);
+  ST7735_WriteString(X_centered(10, 11), LCD_LINE_6, "Stabilizer", Font_11x18, Yellow, Black);
   chThdSleepMilliseconds(1000);
 
   /* top info line */
@@ -186,3 +186,19 @@ static void btn4_handler(const button_state_t state) {
     ST7735_WriteString(120, 118, "  S  ", LiberM_7x10, Silver, Black);
   }
 }
+
+/**
+ * @brief The function returns the starting X position for text of len characters long,
+ * to place it in the center of the display.
+ * @param len Number of characters in the text,
+ * @param pix Font width in pixels,
+ */
+static uint8_t X_centered(const uint8_t len, const uint8_t pix) {
+  unsigned wdt = ST7735_WIDTH;
+  unsigned pl = len * pix;
+  if (pl > wdt) {
+    return 0;
+  } else {
+    return (uint8_t)((wdt - pl) / 2);
+  }
+}