From dc226ea139074fb62fcaf2b44cc78aa719cea1c5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Tue, 26 Nov 2024 11:21:33 +0100 Subject: [PATCH] Change to constexpr and add const where possible --- Measurement.h | 4 ++-- PressureSensor.h | 9 ++++----- PressureSensor.ino | 4 ++-- Servomatic.ino | 28 ++++++++++++++-------------- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/Measurement.h b/Measurement.h index a0813e6..5674365 100644 --- a/Measurement.h +++ b/Measurement.h @@ -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; diff --git a/PressureSensor.h b/PressureSensor.h index f8a857b..ea72baa 100644 --- a/PressureSensor.h +++ b/PressureSensor.h @@ -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 diff --git a/PressureSensor.ino b/PressureSensor.ino index 8a717f1..fe0fa7d 100644 --- a/PressureSensor.ino +++ b/PressureSensor.ino @@ -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))); } diff --git a/Servomatic.ino b/Servomatic.ino index 7e59678..2e79d28 100644 --- a/Servomatic.ino +++ b/Servomatic.ino @@ -5,25 +5,25 @@ #include "Measurement.h" #include -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) { -- 2.30.2