From 4c274421d9bcf5f903e307f1fd61e52174362975 Mon Sep 17 00:00:00 2001
From: mark9064 <30447455+mark9064@users.noreply.github.com>
Date: Fri, 9 Feb 2024 00:04:31 +0000
Subject: [PATCH] Remove GFX (and unused GFX dependencies)

---
 src/CMakeLists.txt                  |   2 -
 src/components/gfx/Gfx.cpp          | 196 ----------------------------
 src/components/gfx/Gfx.h            |  62 ---------
 src/displayapp/DisplayAppRecovery.h |   1 -
 src/drivers/St7789.cpp              |  21 ---
 src/drivers/St7789.h                |   2 -
 src/recoveryLoader.cpp              |   3 -
 7 files changed, 287 deletions(-)
 delete mode 100644 src/components/gfx/Gfx.cpp
 delete mode 100644 src/components/gfx/Gfx.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 0f872f46..8d0b792c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -542,7 +542,6 @@ list(APPEND RECOVERY_SOURCE_FILES
         systemtask/SystemTask.cpp
         systemtask/SystemMonitor.cpp
         drivers/TwiMaster.cpp
-        components/gfx/Gfx.cpp
         components/rle/RleDecoder.cpp
         components/heartrate/HeartRateController.cpp
         heartratetask/HeartRateTask.cpp
@@ -572,7 +571,6 @@ list(APPEND RECOVERYLOADER_SOURCE_FILES
 
         components/rle/RleDecoder.cpp
 
-        components/gfx/Gfx.cpp
         drivers/St7789.cpp
         components/brightness/BrightnessController.cpp
 
diff --git a/src/components/gfx/Gfx.cpp b/src/components/gfx/Gfx.cpp
deleted file mode 100644
index baa6486a..00000000
--- a/src/components/gfx/Gfx.cpp
+++ /dev/null
@@ -1,196 +0,0 @@
-#include "components/gfx/Gfx.h"
-#include "drivers/St7789.h"
-using namespace Pinetime::Components;
-
-Gfx::Gfx(Pinetime::Drivers::St7789& lcd) : lcd {lcd} {
-}
-
-void Gfx::Init() {
-}
-
-void Gfx::ClearScreen() {
-  SetBackgroundColor(0x0000);
-
-  state.remainingIterations = 240 + 1;
-  state.currentIteration = 0;
-  state.busy = true;
-  state.action = Action::FillRectangle;
-  state.taskToNotify = xTaskGetCurrentTaskHandle();
-
-  lcd.DrawBuffer(0, 0, width, height, reinterpret_cast<const uint8_t*>(buffer), width * 2);
-  WaitTransferFinished();
-}
-
-void Gfx::FillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color) {
-  SetBackgroundColor(color);
-
-  state.remainingIterations = h;
-  state.currentIteration = 0;
-  state.busy = true;
-  state.action = Action::FillRectangle;
-  state.color = color;
-  state.taskToNotify = xTaskGetCurrentTaskHandle();
-
-  lcd.DrawBuffer(x, y, w, h, reinterpret_cast<const uint8_t*>(buffer), width * 2);
-
-  WaitTransferFinished();
-}
-
-void Gfx::FillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t* b) {
-  state.remainingIterations = h;
-  state.currentIteration = 0;
-  state.busy = true;
-  state.action = Action::FillRectangle;
-  state.color = 0x00;
-  state.taskToNotify = xTaskGetCurrentTaskHandle();
-
-  lcd.DrawBuffer(x, y, w, h, reinterpret_cast<const uint8_t*>(b), width * 2);
-
-  WaitTransferFinished();
-}
-
-void Gfx::DrawString(uint8_t x, uint8_t y, uint16_t color, const char* text, const FONT_INFO* p_font, bool wrap) {
-  if (y > (height - p_font->height)) {
-    // Not enough space to write even single char.
-    return;
-  }
-
-  uint8_t current_x = x;
-  uint8_t current_y = y;
-
-  for (size_t i = 0; text[i] != '\0'; i++) {
-    if (text[i] == '\n') {
-      current_x = x;
-      current_y += p_font->height + p_font->height / 10;
-    } else {
-      DrawChar(p_font, (uint8_t) text[i], &current_x, current_y, color);
-    }
-
-    uint8_t char_idx = text[i] - p_font->startChar;
-    uint16_t char_width = text[i] == ' ' ? (p_font->height / 2) : p_font->charInfo[char_idx].widthBits;
-
-    if (current_x > (width - char_width)) {
-      if (wrap) {
-        current_x = x;
-        current_y += p_font->height + p_font->height / 10;
-      } else {
-        break;
-      }
-
-      if (y > (height - p_font->height)) {
-        break;
-      }
-    }
-  }
-}
-
-void Gfx::DrawChar(const FONT_INFO* font, uint8_t c, uint8_t* x, uint8_t y, uint16_t color) {
-  uint8_t char_idx = c - font->startChar;
-  uint16_t bytes_in_line = CEIL_DIV(font->charInfo[char_idx].widthBits, 8);
-  uint16_t bg = 0x0000;
-
-  if (c == ' ') {
-    *x += font->height / 2;
-    return;
-  }
-
-  // Build first line
-  for (uint16_t j = 0; j < bytes_in_line; j++) {
-    for (uint8_t k = 0; k < 8; k++) {
-      if ((1 << (7 - k)) & font->data[font->charInfo[char_idx].offset + j]) {
-        buffer[(j * 8) + k] = color;
-      } else {
-        buffer[(j * 8) + k] = bg;
-      }
-    }
-  }
-
-  state.remainingIterations = font->height + 0;
-  state.currentIteration = 0;
-  state.busy = true;
-  state.action = Action::DrawChar;
-  state.font = const_cast<FONT_INFO*>(font);
-  state.character = c;
-  state.color = color;
-  state.taskToNotify = xTaskGetCurrentTaskHandle();
-
-  lcd.DrawBuffer(*x, y, bytes_in_line * 8, font->height, reinterpret_cast<const uint8_t*>(&buffer), bytes_in_line * 8 * 2);
-  WaitTransferFinished();
-
-  *x += font->charInfo[char_idx].widthBits + font->spacePixels;
-}
-
-void Gfx::pixel_draw(uint8_t x, uint8_t y, uint16_t color) {
-  lcd.DrawPixel(x, y, color);
-}
-
-void Gfx::Sleep() {
-  lcd.Sleep();
-}
-
-void Gfx::Wakeup() {
-  lcd.Wakeup();
-}
-
-void Gfx::SetBackgroundColor(uint16_t color) {
-  for (int i = 0; i < width; i++) {
-    buffer[i] = color;
-  }
-}
-
-bool Gfx::GetNextBuffer(uint8_t** data, size_t& size) {
-  if (!state.busy)
-    return false;
-  state.remainingIterations = state.remainingIterations - 1;
-  if (state.remainingIterations == 0) {
-    state.busy = false;
-    NotifyEndOfTransfer(state.taskToNotify);
-    return false;
-  }
-
-  if (state.action == Action::FillRectangle) {
-    *data = reinterpret_cast<uint8_t*>(buffer);
-    size = width * 2;
-  } else if (state.action == Action::DrawChar) {
-    uint16_t bg = 0x0000;
-    uint8_t char_idx = state.character - state.font->startChar;
-    uint16_t bytes_in_line = CEIL_DIV(state.font->charInfo[char_idx].widthBits, 8);
-
-    for (uint16_t j = 0; j < bytes_in_line; j++) {
-      for (uint8_t k = 0; k < 8; k++) {
-        if ((1 << (7 - k)) & state.font->data[state.font->charInfo[char_idx].offset + ((state.currentIteration + 1) * bytes_in_line) + j]) {
-          buffer[(j * 8) + k] = state.color;
-        } else {
-          buffer[(j * 8) + k] = bg;
-        }
-      }
-    }
-
-    *data = reinterpret_cast<uint8_t*>(buffer);
-    size = bytes_in_line * 8 * 2;
-  }
-
-  state.currentIteration = state.currentIteration + 1;
-
-  return true;
-}
-
-void Gfx::NotifyEndOfTransfer(TaskHandle_t task) {
-  if (task != nullptr) {
-    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
-    vTaskNotifyGiveFromISR(task, &xHigherPriorityTaskWoken);
-    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
-  }
-}
-
-void Gfx::WaitTransferFinished() const {
-  ulTaskNotifyTake(pdTRUE, 500);
-}
-
-void Gfx::SetScrollArea(uint16_t topFixedLines, uint16_t scrollLines, uint16_t bottomFixedLines) {
-  lcd.VerticalScrollDefinition(topFixedLines, scrollLines, bottomFixedLines);
-}
-
-void Gfx::SetScrollStartLine(uint16_t line) {
-  lcd.VerticalScrollStartAddress(line);
-}
diff --git a/src/components/gfx/Gfx.h b/src/components/gfx/Gfx.h
deleted file mode 100644
index 17c248f7..00000000
--- a/src/components/gfx/Gfx.h
+++ /dev/null
@@ -1,62 +0,0 @@
-#pragma once
-#include <FreeRTOS.h>
-#include <nrf_font.h>
-#include <task.h>
-#include <cstddef>
-#include <cstdint>
-#include "drivers/BufferProvider.h"
-
-namespace Pinetime {
-  namespace Drivers {
-    class St7789;
-  }
-
-  namespace Components {
-    class Gfx : public Pinetime::Drivers::BufferProvider {
-    public:
-      explicit Gfx(Drivers::St7789& lcd);
-      void Init();
-      void ClearScreen();
-      void DrawString(uint8_t x, uint8_t y, uint16_t color, const char* text, const FONT_INFO* p_font, bool wrap);
-      void DrawChar(const FONT_INFO* font, uint8_t c, uint8_t* x, uint8_t y, uint16_t color);
-      void FillRectangle(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint16_t color);
-      void FillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t* b);
-      void SetScrollArea(uint16_t topFixedLines, uint16_t scrollLines, uint16_t bottomFixedLines);
-      void SetScrollStartLine(uint16_t line);
-
-      void Sleep();
-      void Wakeup();
-      bool GetNextBuffer(uint8_t** buffer, size_t& size) override;
-      void pixel_draw(uint8_t x, uint8_t y, uint16_t color);
-
-    private:
-      static constexpr uint8_t width = 240;
-      static constexpr uint8_t height = 240;
-
-      enum class Action { None, FillRectangle, DrawChar };
-
-      struct State {
-        State() : busy {false}, action {Action::None}, remainingIterations {0}, currentIteration {0} {
-        }
-
-        volatile bool busy;
-        volatile Action action;
-        volatile uint16_t remainingIterations;
-        volatile uint16_t currentIteration;
-        volatile FONT_INFO* font;
-        volatile uint16_t color;
-        volatile uint8_t character;
-        volatile TaskHandle_t taskToNotify = nullptr;
-      };
-
-      volatile State state;
-
-      uint16_t buffer[width]; // 1 line buffer
-      Drivers::St7789& lcd;
-
-      void SetBackgroundColor(uint16_t color);
-      void WaitTransferFinished() const;
-      void NotifyEndOfTransfer(TaskHandle_t task);
-    };
-  }
-}
diff --git a/src/displayapp/DisplayAppRecovery.h b/src/displayapp/DisplayAppRecovery.h
index 3a5c78d9..c1bf6243 100644
--- a/src/displayapp/DisplayAppRecovery.h
+++ b/src/displayapp/DisplayAppRecovery.h
@@ -5,7 +5,6 @@
 #include <drivers/SpiMaster.h>
 #include <bits/unique_ptr.h>
 #include <queue.h>
-#include "components/gfx/Gfx.h"
 #include "drivers/Cst816s.h"
 #include <drivers/Watchdog.h>
 #include <components/motor/MotorController.h>
diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp
index 17d14ce6..e583aac8 100644
--- a/src/drivers/St7789.cpp
+++ b/src/drivers/St7789.cpp
@@ -140,16 +140,6 @@ void St7789::DisplayOff() {
   nrf_delay_ms(500);
 }
 
-void St7789::VerticalScrollDefinition(uint16_t topFixedLines, uint16_t scrollLines, uint16_t bottomFixedLines) {
-  WriteCommand(static_cast<uint8_t>(Commands::VerticalScrollDefinition));
-  WriteData(topFixedLines >> 8u);
-  WriteData(topFixedLines & 0x00ffu);
-  WriteData(scrollLines >> 8u);
-  WriteData(scrollLines & 0x00ffu);
-  WriteData(bottomFixedLines >> 8u);
-  WriteData(bottomFixedLines & 0x00ffu);
-}
-
 void St7789::VerticalScrollStartAddress(uint16_t line) {
   verticalScrollingStartAddress = line;
   WriteCommand(static_cast<uint8_t>(Commands::VerticalScrollStartAddress));
@@ -160,17 +150,6 @@ void St7789::VerticalScrollStartAddress(uint16_t line) {
 void St7789::Uninit() {
 }
 
-void St7789::DrawPixel(uint16_t x, uint16_t y, uint32_t color) {
-  if (x >= Width || y >= Height) {
-    return;
-  }
-
-  SetAddrWindow(x, y, x + 1, y + 1);
-
-  nrf_gpio_pin_set(pinDataCommand);
-  WriteSpi(reinterpret_cast<const uint8_t*>(&color), 2);
-}
-
 void St7789::DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t* data, size_t size) {
   SetAddrWindow(x, y, x + width - 1, y + height - 1);
   nrf_gpio_pin_set(pinDataCommand);
diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h
index 68e1da44..b00bee03 100644
--- a/src/drivers/St7789.h
+++ b/src/drivers/St7789.h
@@ -16,9 +16,7 @@ namespace Pinetime {
 
       void Init();
       void Uninit();
-      void DrawPixel(uint16_t x, uint16_t y, uint32_t color);
 
-      void VerticalScrollDefinition(uint16_t topFixedLines, uint16_t scrollLines, uint16_t bottomFixedLines);
       void VerticalScrollStartAddress(uint16_t line);
 
       void DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t* data, size_t size);
diff --git a/src/recoveryLoader.cpp b/src/recoveryLoader.cpp
index 723977e3..a0b4d784 100644
--- a/src/recoveryLoader.cpp
+++ b/src/recoveryLoader.cpp
@@ -10,7 +10,6 @@
 #include <libraries/gpiote/app_gpiote.h>
 #include <hal/nrf_wdt.h>
 #include <cstring>
-#include <components/gfx/Gfx.h>
 #include <drivers/St7789.h>
 #include <components/brightness/BrightnessController.h>
 #include <algorithm>
@@ -48,7 +47,6 @@ Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi};
 Pinetime::Drivers::Spi lcdSpi {spi, Pinetime::PinMap::SpiLcdCsn};
 Pinetime::Drivers::St7789 lcd {lcdSpi, Pinetime::PinMap::LcdDataCommand, Pinetime::PinMap::LcdReset};
 
-Pinetime::Components::Gfx gfx {lcd};
 Pinetime::Controllers::BrightnessController brightnessController;
 
 void DisplayProgressBar(uint8_t percent, uint16_t color);
@@ -92,7 +90,6 @@ void Process(void* /*instance*/) {
   spiNorFlash.Wakeup();
   brightnessController.Init();
   lcd.Init();
-  gfx.Init();
 
   NRF_LOG_INFO("Display logo")
   DisplayLogo();