1212// Store last Blink Timestamp.
1313unsigned long previousMillis = 0 ;
1414
15+ // Store last Scan Timestamp.
16+ unsigned long scanMillis = 0 ;
17+
1518// Store last Relais Duration Timestamp.
1619unsigned long ch1 = -1 ;
1720unsigned long ch2 = -1 ;
@@ -37,6 +40,13 @@ float minV = 0.0;
3740// Store Tank Volume.
3841float tankVolume = 1000.0 ;
3942
43+ // Store scanned Values (Caching to save CPU)
44+ float scanCurrent = 0.00 ;
45+ float scanTemperature = 0.00 ;
46+ float scanWaterLevel = 0.00 ;
47+ float scanWaterVolume = 0.00 ;
48+
49+
4050/* *
4151 * Monitors and manages the state of relays based on their configured timers.
4252 *
@@ -109,10 +119,60 @@ void DeviceHandler::handleBlink()
109119 }
110120}
111121
122+
123+ void DeviceHandler::scanSensors ()
124+ {
125+ // Read ADC First = latestVoltage as ref.
126+ getADCValue ();
127+
128+ // Read Sensor Values.
129+ scanCurrent = getCurrent (false );
130+ scanTemperature = getCPUTemperature ();
131+ scanWaterLevel = getLevel ();
132+ scanWaterVolume = getVolume ();
133+ }
134+
135+ void DeviceHandler::handleScan ()
136+ {
137+ unsigned long currentMillis = millis ();
138+
139+ // Check if the interval has passed
140+ if (currentMillis - scanMillis >= SCAN_INTERVAL)
141+ {
142+ scanMillis = currentMillis;
143+
144+ scanSensors ();
145+ }
146+ }
147+
148+ /* *
149+ * Executes periodic tasks required for the proper functioning of the device.
150+ *
151+ * This method acts as the main loop for the `DeviceHandler` class, invoking
152+ * specific subroutines in a predefined sequence. Each subroutine manages
153+ * a distinct functionality of the device, such as blinking an LED, handling
154+ * relay states, and performing scanning operations.
155+ *
156+ * Behavior:
157+ * - Calls `handleBlink()` to manage the LED blinking operation based on the specified
158+ * blink interval.
159+ * - Calls `handleRelais()` to monitor and control the state of relays based on their
160+ * configured timers.
161+ * - Calls `handleScan()` to handle scanning-related tasks.
162+ *
163+ * Preconditions:
164+ * - The device and its components (e.g., relays, LED) must be properly initialized and
165+ * functional.
166+ * - Any required configurations (e.g., timers, intervals) must be defined and valid.
167+ *
168+ * Postconditions:
169+ * - Executes the subroutines sequentially, ensuring periodic tasks are handled as needed.
170+ */
112171void DeviceHandler::loop ()
113172{
114173 handleBlink ();
115174 handleRelais ();
175+ handleScan ();
116176}
117177
118178/* *
@@ -192,7 +252,6 @@ void DeviceHandler::setup()
192252 // Set ADC Attenuation.
193253 analogSetPinAttenuation (SENSE, ADC_11db);
194254
195-
196255 // Input Pins.
197256 pinMode (SENSE, INPUT);
198257
@@ -488,3 +547,117 @@ float DeviceHandler::getVolume()
488547{
489548 return tankVolume * (getLevel () / 100 .0f );
490549}
550+
551+ /* *
552+ * Retrieves the cached value of the water volume measurement.
553+ *
554+ * This method returns the most recently scanned water volume, stored in the `scanWaterVolume`
555+ * variable. The value represents the last known measurement without triggering a new
556+ * volume scanning operation. It is useful for applications where frequent or real-time
557+ * volume updates are not required, thus saving computational resources.
558+ *
559+ * @return The cached water volume as a floating-point value. The value is the last recorded
560+ * measurement of the water volume or the default initialized value if no update
561+ * has occurred.
562+ */
563+ float DeviceHandler::getVolumeCached ()
564+ {
565+ return scanWaterVolume;
566+ }
567+
568+ /* *
569+ * Retrieves the cached value of the CPU temperature.
570+ *
571+ * This method provides the most recently scanned value of the CPU temperature,
572+ * stored in the `scanTemperature` variable. The value is updated periodically
573+ * by other components or processes, ensuring low-latency access to temperature
574+ * data without performing a new scan.
575+ *
576+ * Preconditions:
577+ * - The `scanTemperature` variable must contain an updated temperature reading.
578+ *
579+ * Postconditions:
580+ * - Returns the cached CPU temperature value as a floating-point number.
581+ *
582+ * @return The cached value of the CPU temperature in degrees Celsius.
583+ */
584+ float DeviceHandler::getCPUTemperatureCached ()
585+ {
586+ return scanTemperature;
587+ }
588+
589+ /* *
590+ * Retrieves the cached value of the water level measurement.
591+ *
592+ * This method returns the most recently stored value of the water level percentage,
593+ * which was previously measured and cached. The cached value provides quick access
594+ * without requiring a new measurement or sensor scan.
595+ *
596+ * Preconditions:
597+ * - The `scanWaterLevel` variable must hold a valid cached water level percentage, as
598+ * updated by the appropriate scanning or sensing process.
599+ *
600+ * Postconditions:
601+ * - The cached water level percentage remains unchanged after this method call.
602+ *
603+ * Behavior:
604+ * - The method simply fetches and returns the current cached value of water level
605+ * from the `scanWaterLevel` variable without initiating any additional sensor reads.
606+ *
607+ * @return The cached water level percentage as a floating-point value.
608+ */
609+ float DeviceHandler::getLevelCached ()
610+ {
611+ return scanWaterLevel;
612+ }
613+
614+ /* *
615+ * Retrieves the most recently cached current value measured by the device.
616+ *
617+ * This method returns the current measurement previously scanned and stored
618+ * in the internal cache. It provides quick access to the last recorded value
619+ * without initiating a new sensor reading.
620+ *
621+ * Preconditions:
622+ * - The `scanCurrent` variable must have been updated with a valid measurement
623+ * from a prior sensor scan operation.
624+ *
625+ * Postconditions:
626+ * - No sensor is queried or updated as this method only fetches the cached value.
627+ *
628+ * Behavior:
629+ * - The method simply returns the value of the `scanCurrent` variable, representing
630+ * the last recorded current measurement in floating-point format.
631+ *
632+ * @return The cached current measurement as a float value.
633+ */
634+ float DeviceHandler::getCurrentCached ()
635+ {
636+ return scanCurrent;
637+ }
638+
639+ /* *
640+ * Retrieves the most recently cached ADC voltage value.
641+ *
642+ * This method provides the last stored analog-to-digital converter (ADC)
643+ * voltage reading without initiating a new measurement. It serves as a
644+ * faster alternative to obtaining the latest ADC value when repeated
645+ * readings are unnecessary or when the cached value suffices.
646+ *
647+ * Preconditions:
648+ * - The `latestVoltage` variable must have been updated previously
649+ * through a measurement or initialization process.
650+ *
651+ * Postconditions:
652+ * - The returned value reflects the last stored ADC voltage reading,
653+ * which may not represent the current state of the ADC.
654+ *
655+ * Behavior:
656+ * - Always returns the cached ADC voltage value stored in `latestVoltage`.
657+ *
658+ * @return The cached ADC voltage value as a floating-point number.
659+ */
660+ float DeviceHandler::getADCValueCached ()
661+ {
662+ return latestVoltage;
663+ }
0 commit comments