From 6ce1007ba988872f3c7f33bc94255813b4283249 Mon Sep 17 00:00:00 2001 From: Christian Colglazier Date: Sat, 1 Aug 2020 16:22:57 -0400 Subject: [PATCH] Seperated parts to new classes --- Makefile | 2 +- src/main.cpp | 87 +++++++--------------------------------------------- src/menu.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++ src/menu.h | 20 ++++++++++++ src/util.cpp | 21 +++++++++++++ src/util.h | 22 +++++++++++++ 6 files changed, 149 insertions(+), 77 deletions(-) create mode 100644 src/menu.cpp create mode 100644 src/menu.h create mode 100644 src/util.cpp create mode 100644 src/util.h diff --git a/Makefile b/Makefile index 0c65321..597a87a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ TARGET = main -CPP_SOURCES = src/main.cpp +CPP_SOURCES = src/main.cpp src/menu.cpp src/util.cpp LIBDAISY_DIR = ./lib/libDaisy DAISYSP_DIR = ./lib/daisySP diff --git a/src/main.cpp b/src/main.cpp index 8f8d68c..f3f9606 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,44 +5,15 @@ #include #include "main.h" +#include "menu.h" +#include "util.h" using namespace daisy; using namespace daisysp; DaisyPatch patch; - -const std::string MENU_ITEMS[] = {"VCO", - "VCA", - "Envelope", - "LFO", - "Logic", - "Noise", - "Delay", - "Reverb", - "Turing", - "Quantizer"}; -const int MENU_SIZE = sizeof(MENU_ITEMS)/sizeof(*MENU_ITEMS); -const int MAX_CHAR_LENGTH = 15; -const int MENU_X[] = {0, 5, 10, 5, 0}; -const int MENU_Y[] = {0, 11, 22, 41, 52}; -int selectedMenuItem = 0; -bool inMenu = false; - -void DrawSolidRect(uint8_t x1, - uint8_t y1, - uint8_t x2, - uint8_t y2, - bool on) { - for (int i = std::min(y1, y2); i <= std::max(y1, y2); i++) { - patch.display.DrawLine(x1, i, x2, i, on); - } -} - -void WriteString(int x, int y, std::string text) { - patch.display.SetCursor(x, y); - char* cstr = &text[0]; - patch.display.WriteString(cstr, Font_6x8, true); -} +Util util(&patch); +Menu menu(&patch); int main(void) { patch.Init(); @@ -54,35 +25,19 @@ int main(void) { } } -void FilterMenuSelection() { - if (selectedMenuItem >= MENU_SIZE) { - selectedMenuItem = MENU_SIZE - 1; - } else if (selectedMenuItem < 0) { - selectedMenuItem = 0; - } -} - -std::string FilterMenuText(int position) { - if (position >= MENU_SIZE || position < 0) { - return ""; - } else { - return MENU_ITEMS[position]; - } -} void ProcessControls() { patch.UpdateAnalogControls(); patch.DebounceControls(); - if (inMenu) { - selectedMenuItem -= patch.encoder.Increment(); - FilterMenuSelection(); + if (menu.InMenu()) { + menu.UpdateMenuPosition(); if (patch.encoder.RisingEdge()) { - inMenu = false; + menu.SetInMenu(false); } } else { if (patch.encoder.Pressed()) { if (patch.encoder.TimeHeldMs() > 500 && patch.encoder.TimeHeldMs() < 505) { - inMenu = true; + menu.SetInMenu(true); } } } @@ -90,33 +45,13 @@ void ProcessControls() { void ProcessOutputs() {} -void CreateMenuItem(std::string text, int position, bool highlighted) { - char* cstr = &text[0]; - text.insert(text.end(), MAX_CHAR_LENGTH-text.size(), ' '); - patch.display.SetCursor(MENU_X[position-1], MENU_Y[position-1]); - if (highlighted) { - DrawSolidRect(0, MENU_Y[2], SSD1309_WIDTH, MENU_Y[2]+17, true); - patch.display.WriteString(cstr, Font_11x18, !highlighted); - } else { - patch.display.WriteString(cstr, Font_7x10, !highlighted); - } -} - -void ProcessMenuOled() { - CreateMenuItem(FilterMenuText(selectedMenuItem-2), 1, false); - CreateMenuItem(FilterMenuText(selectedMenuItem-1), 2, false); - CreateMenuItem(FilterMenuText(selectedMenuItem), 3, true); - CreateMenuItem(FilterMenuText(selectedMenuItem+1), 4, false); - CreateMenuItem(FilterMenuText(selectedMenuItem+2), 5, false); -} - void ProcessOled() { patch.display.Fill(false); - if (inMenu) { - ProcessMenuOled(); + if (menu.InMenu()) { + menu.ProcessMenuOled(); } else { - WriteString(0, 0, MENU_ITEMS[selectedMenuItem]); + util.WriteString(0, 0, menu.SelectedName()); } patch.display.Update(); } diff --git a/src/menu.cpp b/src/menu.cpp new file mode 100644 index 0000000..2e481c0 --- /dev/null +++ b/src/menu.cpp @@ -0,0 +1,74 @@ +#include "menu.h" + +Menu::Menu(DaisyPatch* m_patch) { + patch = m_patch; +} +const std::string MENU_ITEMS[] = {"VCO", + "VCA", + "Envelope", + "LFO", + "Logic", + "Noise", + "Delay", + "Reverb", + "Turing", + "Quantizer"}; +const int MENU_SIZE = sizeof(MENU_ITEMS)/sizeof(*MENU_ITEMS); +const int MAX_CHAR_LENGTH = 15; +const int MENU_X[] = {0, 5, 10, 5, 0}; +const int MENU_Y[] = {0, 11, 22, 41, 52}; +int selectedMenuItem = 0; +bool inMenu = false; + +bool Menu::InMenu() { + return inMenu; +} + +void Menu::SetInMenu(bool menuState) { + inMenu = menuState; +} + +void Menu::FilterMenuSelection() { + if (selectedMenuItem >= MENU_SIZE) { + selectedMenuItem = MENU_SIZE - 1; + } else if (selectedMenuItem < 0) { + selectedMenuItem = 0; + } +} + +std::string Menu::FilterMenuText(int position) { + if (position >= MENU_SIZE || position < 0) { + return ""; + } else { + return MENU_ITEMS[position]; + } +} + +void Menu::CreateMenuItem(std::string text, int position, bool highlighted) { + char* cstr = &text[0]; + text.insert(text.end(), MAX_CHAR_LENGTH-text.size(), ' '); + patch->display.SetCursor(MENU_X[position-1], MENU_Y[position-1]); + if (highlighted) { + //util.DrawSolidRect(0, MENU_Y[2], SSD1309_WIDTH, MENU_Y[2]+17, true); + patch->display.WriteString(cstr, Font_11x18, !highlighted); + } else { + patch->display.WriteString(cstr, Font_7x10, !highlighted); + } +} + +void Menu::ProcessMenuOled() { + CreateMenuItem(FilterMenuText(selectedMenuItem-2), 1, false); + CreateMenuItem(FilterMenuText(selectedMenuItem-1), 2, false); + CreateMenuItem(FilterMenuText(selectedMenuItem), 3, true); + CreateMenuItem(FilterMenuText(selectedMenuItem+1), 4, false); + CreateMenuItem(FilterMenuText(selectedMenuItem+2), 5, false); +} + +void Menu::UpdateMenuPosition() { + selectedMenuItem -= patch->encoder.Increment(); + FilterMenuSelection(); +} + +std::string Menu::SelectedName() { + return MENU_ITEMS[selectedMenuItem]; +} diff --git a/src/menu.h b/src/menu.h new file mode 100644 index 0000000..71c16ee --- /dev/null +++ b/src/menu.h @@ -0,0 +1,20 @@ +#include "daisy_patch.h" + +#include + +using namespace daisy; + +class Menu { + private: + DaisyPatch* patch; + public: + Menu(DaisyPatch*); + bool InMenu(); + void SetInMenu(bool); + void FilterMenuSelection(); + std::string FilterMenuText(int); + void CreateMenuItem(std::string, int, bool); + void ProcessMenuOled(); + void UpdateMenuPosition(); + std::string SelectedName(); +}; diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000..c64343b --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,21 @@ +#include "util.h" + +Util::Util(DaisyPatch* m_patch) { + patch = m_patch; +} + +void Util::DrawSolidRect(uint8_t x1, + uint8_t y1, + uint8_t x2, + uint8_t y2, + bool on) { + for (int i = std::min(y1, y2); i <= std::max(y1, y2); i++) { + patch->display.DrawLine(x1, i, x2, i, on); + } +} + +void Util::WriteString(int x, int y, std::string text) { + patch->display.SetCursor(x, y); + char* cstr = &text[0]; + patch->display.WriteString(cstr, Font_6x8, true); +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..99b7130 --- /dev/null +++ b/src/util.h @@ -0,0 +1,22 @@ +#include "daisy_patch.h" + +#include + +using namespace daisy; + +class Util { + private: + DaisyPatch* patch; + public: + Util(DaisyPatch*); + + void DrawSolidRect(uint8_t x1, + uint8_t y1, + uint8_t x2, + uint8_t y2, + bool on); + + void WriteString(int x, + int y, + std::string text); +};