From 048e2d2cbd0e45832a6fd5d26b4bddd727fce13e Mon Sep 17 00:00:00 2001
From: William Emfinger <waemfinger@gmail.com>
Date: Wed, 9 Apr 2025 13:55:48 -0500
Subject: [PATCH 1/4] feat(joystick/vector): Update to return `const ref` where
 possible * Update `espp::Vector2d` and `espp::Joystick` to return const
 references where possible

Reduces the amount of unnecessary copies in application code

Build and run `math/example` and `joystick/example`
---
 components/joystick/include/joystick.hpp | 4 ++--
 components/joystick/src/joystick.cpp     | 4 ++--
 components/math/include/vector2d.hpp     | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/components/joystick/include/joystick.hpp b/components/joystick/include/joystick.hpp
index dbd1d47a2..8ffac7e93 100644
--- a/components/joystick/include/joystick.hpp
+++ b/components/joystick/include/joystick.hpp
@@ -203,7 +203,7 @@ class Joystick : public BaseComponent {
    * @brief Get the most recently updated calibrated position.
    * @return The most recent position (from when update() was last called).
    */
-  espp::Vector2f position() const;
+  const espp::Vector2f &position() const;
 
   /**
    * @brief Get the most recently updated raw / uncalibrated readings. This
@@ -213,7 +213,7 @@ class Joystick : public BaseComponent {
    * @return The most recent raw measurements (from when update() was last
    *         called).
    */
-  espp::Vector2f raw() const;
+  const espp::Vector2f &raw() const;
 
   friend struct fmt::formatter<Joystick>;
 
diff --git a/components/joystick/src/joystick.cpp b/components/joystick/src/joystick.cpp
index 64cd02f48..42ba06048 100644
--- a/components/joystick/src/joystick.cpp
+++ b/components/joystick/src/joystick.cpp
@@ -74,9 +74,9 @@ float espp::Joystick::x() const { return position_.x(); }
 
 float espp::Joystick::y() const { return position_.y(); }
 
-espp::Vector2f espp::Joystick::position() const { return position_; }
+const espp::Vector2f &espp::Joystick::position() const { return position_; }
 
-espp::Vector2f espp::Joystick::raw() const { return raw_; }
+const espp::Vector2f &espp::Joystick::raw() const { return raw_; }
 
 void espp::Joystick::recalculate(float raw_x, float raw_y) {
   raw_.x(raw_x);
diff --git a/components/math/include/vector2d.hpp b/components/math/include/vector2d.hpp
index bb99f906b..0343ac382 100644
--- a/components/math/include/vector2d.hpp
+++ b/components/math/include/vector2d.hpp
@@ -76,7 +76,7 @@ template <typename T> class Vector2d {
    * @brief Getter for the x value.
    * @return The current x value.
    */
-  T x() const { return x_; }
+  const T &x() const { return x_; }
 
   /**
    * @brief Setter for the x value.

From c196f85af81bdf93a375a772833cfb38a1ac5e2e Mon Sep 17 00:00:00 2001
From: William Emfinger <waemfinger@gmail.com>
Date: Wed, 9 Apr 2025 13:59:17 -0500
Subject: [PATCH 2/4] add missing const ref to vector

---
 components/math/include/vector2d.hpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/components/math/include/vector2d.hpp b/components/math/include/vector2d.hpp
index 0343ac382..acbe9dde8 100644
--- a/components/math/include/vector2d.hpp
+++ b/components/math/include/vector2d.hpp
@@ -88,7 +88,7 @@ template <typename T> class Vector2d {
    * @brief Getter for the y value.
    * @return The current y value.
    */
-  T y() const { return y_; }
+  const T &y() const { return y_; }
 
   /**
    * @brief Setter for the y value.

From 2593d1ed6162ca8790a2a1c774faefee4db75674 Mon Sep 17 00:00:00 2001
From: William Emfinger <waemfinger@gmail.com>
Date: Wed, 9 Apr 2025 14:01:07 -0500
Subject: [PATCH 3/4] Update components/joystick/include/joystick.hpp

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---
 components/joystick/include/joystick.hpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/components/joystick/include/joystick.hpp b/components/joystick/include/joystick.hpp
index 8ffac7e93..7cf9eeb7a 100644
--- a/components/joystick/include/joystick.hpp
+++ b/components/joystick/include/joystick.hpp
@@ -203,6 +203,11 @@ class Joystick : public BaseComponent {
    * @brief Get the most recently updated calibrated position.
    * @return The most recent position (from when update() was last called).
    */
+  /**
+   * @brief Get the most recently updated calibrated position.
+   * @return The most recent position (from when update() was last called).
+   * @note The returned reference is valid as long as the Joystick object is alive.
+   */
   const espp::Vector2f &position() const;
 
   /**

From 2f83b9da3f592ec53fd70cc2c2ea05ed44e3ba01 Mon Sep 17 00:00:00 2001
From: William Emfinger <waemfinger@gmail.com>
Date: Wed, 9 Apr 2025 14:02:27 -0500
Subject: [PATCH 4/4] fix comments

---
 components/joystick/include/joystick.hpp | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/components/joystick/include/joystick.hpp b/components/joystick/include/joystick.hpp
index 7cf9eeb7a..327131405 100644
--- a/components/joystick/include/joystick.hpp
+++ b/components/joystick/include/joystick.hpp
@@ -199,10 +199,6 @@ class Joystick : public BaseComponent {
    */
   float y() const;
 
-  /**
-   * @brief Get the most recently updated calibrated position.
-   * @return The most recent position (from when update() was last called).
-   */
   /**
    * @brief Get the most recently updated calibrated position.
    * @return The most recent position (from when update() was last called).
@@ -217,6 +213,7 @@ class Joystick : public BaseComponent {
    *        structures.
    * @return The most recent raw measurements (from when update() was last
    *         called).
+   * @note The returned reference is valid as long as the Joystick object is alive.
    */
   const espp::Vector2f &raw() const;