From 49a842aa37baa390fe8ccbf3907cdafbf2578e52 Mon Sep 17 00:00:00 2001
From: David Huss <dh@atoav.com>
Date: Wed, 15 Nov 2023 22:06:06 +0100
Subject: [PATCH] More generic functions for ui setup

---
 code/daisy-looper/button_grid.h    | 33 ++++++------
 code/daisy-looper/daisy-looper.ino |  5 +-
 code/daisy-looper/ui.h             | 84 +++++++++++++-----------------
 3 files changed, 55 insertions(+), 67 deletions(-)

diff --git a/code/daisy-looper/button_grid.h b/code/daisy-looper/button_grid.h
index d183739..4a7ddcf 100644
--- a/code/daisy-looper/button_grid.h
+++ b/code/daisy-looper/button_grid.h
@@ -93,36 +93,33 @@ class ButtonGrid {
     void setup() {
       Serial.println("Running Setup for ButtonGrid");
       for (int n=0; n<6; n++) {
-        // If this is the home button setup callbacks
-        if (grid_buttons_[n].is_home) {
-          Serial.print("Home button ");
-          Serial.println(n);
-          // // When homebutton is held switch to this UI Mode
-          // grid_buttons_[n].button.onHold([ui, n](){ ui->setMode(ui_mode); });
-          // // When homebutton is released hide ALL button descriptions
-          // grid_buttons_[n].button.onReleased([ui, n](){ 
-          //     ui->setMode(UI_MODE_DEFAULT);
-          //     for (int i=0; i<6; i++) {
-          //       grid_buttons_[i].should_render_description = false;
-          //     }
-          //   });
-        } else {
+        if (!grid_buttons_[n].is_home) {
           Serial.println(n);
           // Not a home button, display help on long hold and hide on release
           grid_buttons_[n].button->onLongHold([this, n](){
-            // TODO: Doesn't register for some reason?
-            Serial.println("Show DESCROOOTT");
             grid_buttons_[n].should_render_description = true;
           });
           grid_buttons_[n].button->onReleased([this, n](){
-            // TODO: Doesn't register for some reason?
-            Serial.println("NOPE");
             grid_buttons_[n].should_render_description = false;
           });
         }
       }
     }
 
+    int homeButtonIndex() {
+      for (int i=0; i<6; i++) {
+        if (grid_buttons_[i].is_home) {
+          return i;
+        }
+      }
+    }
+
+    void hideAllDescriptions() {
+      for (int i=0; i<6; i++) {
+        grid_buttons_[i].should_render_description = false;
+      }
+    }
+
     void render(int button_enum) {
       int width = display.width();
       int height = display.height();
diff --git a/code/daisy-looper/daisy-looper.ino b/code/daisy-looper/daisy-looper.ino
index f0c5a6e..24a57a3 100644
--- a/code/daisy-looper/daisy-looper.ino
+++ b/code/daisy-looper/daisy-looper.ino
@@ -20,7 +20,10 @@
 #include "helpers.h"
 #include "ui.h"
 
-static const size_t buffer_length = 48000 * 5;
+
+#define BUFFER_LENGTH_SECONDS 5
+
+static const size_t buffer_length = 48000 * BUFFER_LENGTH_SECONDS;
 static float DSY_SDRAM_BSS buffer[buffer_length];
 
 // Create instances of audio stuff
diff --git a/code/daisy-looper/ui.h b/code/daisy-looper/ui.h
index 0cceec3..c26f25c 100644
--- a/code/daisy-looper/ui.h
+++ b/code/daisy-looper/ui.h
@@ -4,6 +4,15 @@
 #ifndef Ui_h
 #define Ui_h
 
+#ifdef DEBUG
+#define DEBUG_PRINT(x) Serial.print (x)
+#define DEBUG_PRINTLN(x) Serial.println (x)
+#else
+#define DEBUG_PRINT(x)
+#define DEBUG_PRINTLN(x)
+#endif
+
+
 #include "Adafruit_SH110X.h"
 #include "Adafruit_GFX.h"
 #include "potentiometers.h"
@@ -224,18 +233,39 @@ class Ui {
       button_6.reset();
     }
 
+    Button* setupButtonGrid(int n) {
+      Serial.println("[UI] Setup Rec Menu");
+
+      // Find the index of the home button
+      int home_button_index = button_grids[n].homeButtonIndex();
+
+      // Create a pointer to the hoime button
+      Button* home_button = button_grids[n].grid_buttons_[home_button_index].button;
+
+      // Reset the controls
+      resetControls();
+
+      // Setup the button grid
+      button_grids[n].setup();
+
+      // Return to default mode on release
+      home_button->onReleased([this, n](){
+        this->setMode(UI_MODE_DEFAULT);
+        this->button_grids[n].hideAllDescriptions();
+      });
+
+      // Return pointer to the home button
+      return home_button;
+    }
+
     // Setup the Recording Menu
     void setupRecMenu() {
       // Only run once when the ui_mode changed
       if (ui_mode == UI_MODE_REC_MENU && last_ui_mode != UI_MODE_REC_MENU) {
         int n = 0;
-        Serial.println("[UI] Setup Rec Menu");
-
-        // Reset controls
-        resetControls();
 
-        // Setup
-        button_grids[n].setup();
+        // Setup button Grid
+        Button* home_button = setupButtonGrid(n);
 
         // Toggle between momentary and toggle recording modes
         button_2.onPress([this, n](){       
@@ -280,48 +310,6 @@ class Ui {
           looper.setRecStartMode((atoav::RecStartMode) button_grids[n].grid_buttons_[5].active);
         });
 
-        // // Display descriptions on Long Hold:
-        // button_2.onLongHold([this, n](){
-        //   button_grids[n].grid_buttons_[1].should_render_description = true;
-        // });
-
-        // button_2.onReleased([this, n](){
-        //   button_grids[n].grid_buttons_[1].should_render_description = false;
-        // });
-
-        // button_3.onLongHold([this, n](){
-        //   button_grids[n].grid_buttons_[2].should_render_description = true;
-        // });
-
-        // button_3.onReleased([this, n](){
-        //   button_grids[n].grid_buttons_[2].should_render_description = false;
-        // });
-
-        // button_4.onLongHold([this, n](){
-        //   button_grids[n].grid_buttons_[3].should_render_description = true;
-        // });
-
-        // button_4.onReleased([this, n](){
-        //   button_grids[n].grid_buttons_[3].should_render_description = false;
-        // });
-
-        // button_5.onLongHold([this, n](){
-        //   button_grids[n].grid_buttons_[4].should_render_description = true;
-        // });
-
-        // button_5.onReleased([this, n](){
-        //   button_grids[n].grid_buttons_[4].should_render_description = false;
-        // });
-
-        // button_6.onLongHold([this, n](){
-        //   button_grids[n].grid_buttons_[5].should_render_description = true;
-        // });
-
-        // button_6.onReleased([this, n](){
-        //   button_grids[n].grid_buttons_[5].should_render_description = false;
-        // });
-
-
         // Store the last ui mode, for the check on top
         last_ui_mode = ui_mode;
       }
-- 
GitLab