mirror of
				https://github.com/AquaMorph/Droplets.git
				synced 2025-10-30 16:13:20 +00:00 
			
		
		
		
	Seperated parts to new classes
This commit is contained in:
		
							
								
								
									
										2
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
									
									
									
									
								
							| @@ -1,6 +1,6 @@ | |||||||
| TARGET = main | TARGET = main | ||||||
|  |  | ||||||
| CPP_SOURCES = src/main.cpp | CPP_SOURCES = src/main.cpp src/menu.cpp src/util.cpp | ||||||
|  |  | ||||||
| LIBDAISY_DIR = ./lib/libDaisy | LIBDAISY_DIR = ./lib/libDaisy | ||||||
| DAISYSP_DIR = ./lib/daisySP | DAISYSP_DIR = ./lib/daisySP | ||||||
|   | |||||||
							
								
								
									
										87
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										87
									
								
								src/main.cpp
									
									
									
									
									
								
							| @@ -5,44 +5,15 @@ | |||||||
| #include <string> | #include <string> | ||||||
|  |  | ||||||
| #include "main.h" | #include "main.h" | ||||||
|  | #include "menu.h" | ||||||
|  | #include "util.h" | ||||||
|  |  | ||||||
| using namespace daisy; | using namespace daisy; | ||||||
| using namespace daisysp; | using namespace daisysp; | ||||||
|  |  | ||||||
| DaisyPatch patch; | DaisyPatch patch; | ||||||
|  | Util util(&patch); | ||||||
| const std::string MENU_ITEMS[] = {"VCO",  | Menu menu(&patch); | ||||||
| 				  "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); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| int main(void) { | int main(void) { | ||||||
|   patch.Init(); |   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() { | void ProcessControls() { | ||||||
|   patch.UpdateAnalogControls(); |   patch.UpdateAnalogControls(); | ||||||
|   patch.DebounceControls(); |   patch.DebounceControls(); | ||||||
|  |  | ||||||
|   if (inMenu) { |   if (menu.InMenu()) { | ||||||
|     selectedMenuItem -= patch.encoder.Increment(); |     menu.UpdateMenuPosition(); | ||||||
|     FilterMenuSelection(); |  | ||||||
|     if (patch.encoder.RisingEdge()) { |     if (patch.encoder.RisingEdge()) { | ||||||
|       inMenu = false; |       menu.SetInMenu(false); | ||||||
|     } |     } | ||||||
|   } else { |   } else { | ||||||
|     if (patch.encoder.Pressed()) { |     if (patch.encoder.Pressed()) { | ||||||
|       if (patch.encoder.TimeHeldMs() > 500 && patch.encoder.TimeHeldMs() < 505) { |       if (patch.encoder.TimeHeldMs() > 500 && patch.encoder.TimeHeldMs() < 505) { | ||||||
| 	inMenu = true; | 	menu.SetInMenu(true); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| @@ -90,33 +45,13 @@ void ProcessControls() { | |||||||
|  |  | ||||||
| void ProcessOutputs() {} | 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() { | void ProcessOled() { | ||||||
|   patch.display.Fill(false); |   patch.display.Fill(false); | ||||||
|  |  | ||||||
|   if (inMenu) { |   if (menu.InMenu()) { | ||||||
|     ProcessMenuOled(); |     menu.ProcessMenuOled(); | ||||||
|   } else { |   } else { | ||||||
|     WriteString(0, 0, MENU_ITEMS[selectedMenuItem]); |     util.WriteString(0, 0, menu.SelectedName()); | ||||||
|   } |   } | ||||||
|   patch.display.Update(); |   patch.display.Update(); | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										74
									
								
								src/menu.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								src/menu.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -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]; | ||||||
|  | } | ||||||
							
								
								
									
										20
									
								
								src/menu.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/menu.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | |||||||
|  | #include "daisy_patch.h" | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
|  | 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(); | ||||||
|  | }; | ||||||
							
								
								
									
										21
									
								
								src/util.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/util.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -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); | ||||||
|  | } | ||||||
							
								
								
									
										22
									
								
								src/util.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/util.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | |||||||
|  | #include "daisy_patch.h" | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
|  | 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); | ||||||
|  | }; | ||||||
		Reference in New Issue
	
	Block a user