Refactor open_barely out of Valve class to a child
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 18 Nov 2024 09:59:08 +0000 (10:59 +0100)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Mon, 18 Nov 2024 09:59:08 +0000 (10:59 +0100)
Because the open_barely method would only be used barely, I think it is
better to separate it.

CellValve.h [new file with mode: 0644]
CellValve.ino [new file with mode: 0644]
Makefile
Valve.h
Valve.ino

diff --git a/CellValve.h b/CellValve.h
new file mode 100644 (file)
index 0000000..ada5a2e
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef CELLVALVE_H
+# define CELLVALVE_H
+
+#include "Valve.h"
+
+class CellValve : public Valve
+{
+       private:
+               const int       m_usec_barely_opened;
+
+       public:
+               CellValve() = delete;
+               CellValve(const CellValve &other) = delete;
+               CellValve(pin pwm_pin, int usec_opened, int usec_barely_opened,
+                               int isec_closed);
+               ~CellValve();
+
+               CellValve       &operator=(const CellValve &other) = delete;
+
+               void    open_barely(float seconds = 5);
+};
+
+#endif // CELLVALVE_H
diff --git a/CellValve.ino b/CellValve.ino
new file mode 100644 (file)
index 0000000..ad2c8b0
--- /dev/null
@@ -0,0 +1,17 @@
+#include "CellValve.h"
+
+CellValve::CellValve(pin pwm_pin, int usec_opened, int usec_barely_opened,
+                               int usec_closed)
+       : Valve{pwm_pin, usec_opened, usec_closed}
+       , m_usec_barely_opened{usec_barely_opened}
+{
+}
+
+CellValve::~CellValve()
+{
+}
+
+void   CellValve::open_barely(float seconds)
+{
+       write_usec_timed(m_usec_barely_opened, seconds);
+}
index 4d945533e763b2f87ae00b9cbdd8f44dc4c6aa7a..97a5eea0fa165f97adb48c33598141078e2fe5d7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@ endif
 SRCS :=        Servomatic.ino          \
                PressureSensor.ino      \
                Valve.ino                       \
+               CellValve.ino           \
                Measurement.ino         \
 
 
diff --git a/Valve.h b/Valve.h
index f0f514fb81737a889df1c371f6bb8d66b444c71d..9ae0a8a64c844ea677a8d254e0822e0993328eff 100644 (file)
--- a/Valve.h
+++ b/Valve.h
@@ -6,29 +6,27 @@
 class Valve
 {
        private:
-               typedef int pin;
-
                const Servo     m_servo;
                const int       m_usec_opened;
-               const int       m_usec_barely_opened;
                const int       m_usec_closed;
                int                     m_usec_current;
 
-               void    write_usec_timed(int usec_wanted, float seconds);
                void    update_pos();
 
+       protected:
+               typedef int pin;
+
+               void    write_usec_timed(int usec_wanted, float seconds);
+
        public:
                Valve() = delete;
                Valve(const Valve &other) = delete;
-               Valve(pin pwm_pin, int usec_opened, int usec_barely_opened,
-                               int isec_closed);
+               Valve(pin pwm_pin, int usec_opened, int isec_closed);
                ~Valve();
 
                Valve   &operator=(const Valve &other) = delete;
 
                void    open(float seconds = 5);
-               void    open_barely(float seconds = 5);
                void    close(float seconds = 5);
 };
-
 #endif // VALVE_H
index 13cb83cc39ddf0dd3558e9a8bfc4f44d6df76e3e..1c18584fa9c11d9d797c481882d1f58fbbc13ff8 100644 (file)
--- a/Valve.ino
+++ b/Valve.ino
@@ -1,9 +1,8 @@
 #include "Valve.h"
 
-Valve::Valve(pin pwm_pin, int usec_opened, int usec_barely_opened, int usec_closed)
+Valve::Valve(pin pwm_pin, int usec_opened, int usec_closed)
        : m_servo{}
        , m_usec_opened{usec_opened}
-       , m_usec_barely_opened{usec_barely_opened}
        , m_usec_closed{usec_closed}
        , m_usec_current{usec_closed}
 {
@@ -52,11 +51,6 @@ void Valve::open(float seconds)
        write_usec_timed(m_usec_opened, seconds);
 }
 
-void   Valve::open_barely(float seconds)
-{
-       write_usec_timed(m_usec_barely_opened, seconds);
-}
-
 void   Valve::close(float seconds)
 {
        write_usec_timed(m_usec_closed, seconds);