Change to constexpr and add const where possible
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 26 Nov 2024 10:21:33 +0000 (11:21 +0100)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 26 Nov 2024 10:21:33 +0000 (11:21 +0100)
Measurement.h
PressureSensor.h
PressureSensor.ino
Servomatic.ino

index a0813e6c1505bb95fd7c4683a9bafcbfb1cda5e2..56743651e81d6b5aa83a26b915eb2526b179b9ca 100644 (file)
@@ -6,8 +6,8 @@
 class Measurement
 {
        private:
-               static const size_t             MAX_ELEMENTS = 100;
-               static const unsigned long      START_INTERVAL_MS = 5000;
+               static constexpr size_t         MAX_ELEMENTS = 100;
+               static constexpr unsigned long  START_INTERVAL_MS = 5000;
 
                const PressureSensor    &m_sensor;
                unsigned long                   m_interval_ms;
index f8a857b0600c340a02e99423377470d4b98f0881..ea72baa1cf3d04078298ef544337641bb5a18b4d 100644 (file)
@@ -7,18 +7,17 @@ class PressureSensor
                typedef int pin;
 
                const pin       m_analog_pin;
-               const float m_resistance;       // in Ohms
+               const float     m_resistance;   // in Ohms
+               const float     m_max_pressure;
 
                static const float      MIN_CURRENT = 4e-3;                     // in A
                static const float      MAX_CURRENT = 20e-3;            // in A
                static const float      REFERENCE_VOLTAGE = 5;          // in V
                static const int        CONVERSION_BIT_NUM = 10;
 
-               float   signal_to_pressure(int ad_signal);
+               float   signal_to_pressure(int ad_signal) const;
 
        public:
-               const float     m_max_pressure;
-
                PressureSensor() = delete;
                PressureSensor(pin analog_pin, float max_pressure = 10, float resistance = 240);
                PressureSensor(const PressureSensor &other) = delete;
@@ -26,7 +25,7 @@ class PressureSensor
 
                PressureSensor  &operator=(const PressureSensor &other) = delete;
 
-               float   get_pressure();
+               float   get_pressure() const;
 };
 
 #endif // PRESSURESENSOR_H
index 8a717f17f251f52400c24e928ee021b686126ad7..fe0fa7d2e27744660f762fc383bae995fa840094 100644 (file)
@@ -35,7 +35,7 @@ PressureSensor::~PressureSensor()
 // And p = |  ----------------------------------------- - MIN_CURRENT  | * --------------
 //          \ m_resistance * (2^CONVERSION_BIT_NUM - 1)               /         I_R
 
-float  PressureSensor::signal_to_pressure(int ad_signal)
+float  PressureSensor::signal_to_pressure(int ad_signal) const
 {
        float   signal_str;
        float   voltage;
@@ -47,7 +47,7 @@ float PressureSensor::signal_to_pressure(int ad_signal)
        return ((current - MIN_CURRENT) / (MAX_CURRENT - MIN_CURRENT) * m_max_pressure);
 }
 
-float  PressureSensor::get_pressure()
+float  PressureSensor::get_pressure() const
 {
        return (signal_to_pressure(analogRead(m_analog_pin)));
 }
index 7e59678f34b7d274da8dce53d25ac4e0e8959df6..2e79d28f201cc6b9b89981c57c57bfc876ea7c2c 100644 (file)
@@ -5,25 +5,25 @@
 #include "Measurement.h"
 #include <avr/sleep.h>
 
-static const int       PRESSURE_SENSOR1_PIN = A1;
-static const int       PRESSURE_SENSOR2_PIN = A2;
-static const int       PRESSURE_SENSOR3_PIN = A3;
+static constexpr int   PRESSURE_SENSOR1_PIN = A1;
+static constexpr int   PRESSURE_SENSOR2_PIN = A2;
+static constexpr int   PRESSURE_SENSOR3_PIN = A3;
 
-static const int       SWITCH_PIN = 2; // Only pins 2; 3 have external interrupts?
+static constexpr int   SWITCH_PIN = 2; // Only pins 2; 3 have external interrupts?
 
-static const int       GAS_VALVE_PIN = 9;
-static const int       CELL_VALVE_PIN = 10;
-static const int       PRESSURE_SENSOR_VALVE_PIN = 11;
-static const int       VACUUM_VALVE_PIN = 12;
+static constexpr int   GAS_VALVE_PIN = 9;
+static constexpr int   CELL_VALVE_PIN = 10;
+static constexpr int   PRESSURE_SENSOR_VALVE_PIN = 11;
+static constexpr int   VACUUM_VALVE_PIN = 12;
 
-static const unsigned long     CELL_BARELY_OPEN_EQUILIBRATION_MS = 10000;
-static const unsigned long     EVACUATION_LONG_MS = 600000;
-static const unsigned long     EVACUATION_MS = 60000;
+static constexpr unsigned long CELL_BARELY_OPEN_EQUILIBRATION_MS = 10000;
+static constexpr unsigned long EVACUATION_LONG_MS = 600000;
+static constexpr unsigned long EVACUATION_MS = 60000;
 
-static const unsigned int      THREEWAY_TURNS_TO_FILL = 3;
+static constexpr unsigned int  THREEWAY_TURNS_TO_FILL = 3;
 
-static const float     MEASUREMENT_UPPER_LIMIT = 8;
-static const float     MEASUREMENT_LOWER_LIMIT = 0.5;
+static constexpr float MEASUREMENT_UPPER_LIMIT = 8;
+static constexpr float MEASUREMENT_LOWER_LIMIT = 0.5;
 
 void   threeway_fill(Valve &threeway_valve, unsigned int count)
 {