11a153794SPatrick Venture #pragma once
21a153794SPatrick Venture 
31a153794SPatrick Venture #include "sensors/sensor.hpp"
41a153794SPatrick Venture 
51a153794SPatrick Venture #include <string>
61a153794SPatrick Venture 
71a153794SPatrick Venture namespace pid_control
81a153794SPatrick Venture {
91a153794SPatrick Venture 
107a98c19aSPatrick Venture /**
117a98c19aSPatrick Venture  * In a Zone you have a set of PIDs which feed each other.  Fan PIDs are fed set
127a98c19aSPatrick Venture  * points from Thermal PIDs.
137a98c19aSPatrick Venture  */
141a153794SPatrick Venture class ZoneInterface
151a153794SPatrick Venture {
161a153794SPatrick Venture   public:
171a153794SPatrick Venture     virtual ~ZoneInterface() = default;
181a153794SPatrick Venture 
197a98c19aSPatrick Venture     /** If the zone implementation supports logging, initialize the log. */
207a98c19aSPatrick Venture     virtual void initializeLog(void) = 0;
217a98c19aSPatrick Venture     /** If the zone implementation supports logging, write string to log. */
227a98c19aSPatrick Venture     virtual void writeLog(const std::string& value) = 0;
237a98c19aSPatrick Venture 
247a98c19aSPatrick Venture     /** Return a pointer to the sensor specified by name. */
251a153794SPatrick Venture     virtual Sensor* getSensor(const std::string& name) = 0;
267a98c19aSPatrick Venture 
277a98c19aSPatrick Venture     /* updateFanTelemetry() and updateSensors() both clear the failsafe state
287a98c19aSPatrick Venture      * for a sensor if it's no longer in that state.
297a98c19aSPatrick Venture      */
307a98c19aSPatrick Venture     /** For each fan input in the zone, read each to update the cachedValue and
317a98c19aSPatrick Venture      * check if the fan is beyond its timeout to trigger a failsafe condition.
327a98c19aSPatrick Venture      */
337a98c19aSPatrick Venture     virtual void updateFanTelemetry(void) = 0;
347a98c19aSPatrick Venture     /** For each thermal input in the zone, read each to update the cachedValue
357a98c19aSPatrick Venture      * and check if the sensor is beyond its timeout to trigger a failsafe
367a98c19aSPatrick Venture      * condition.
377a98c19aSPatrick Venture      */
387a98c19aSPatrick Venture     virtual void updateSensors(void) = 0;
397a98c19aSPatrick Venture     /** For each fan and thermal input in the zone, set the cachedValue to 0 and
407a98c19aSPatrick Venture      * set the input as failsafe - to default the zone to failsafe before it
417a98c19aSPatrick Venture      * starts processing values to control fans.
427a98c19aSPatrick Venture      */
437a98c19aSPatrick Venture     virtual void initializeCache(void) = 0;
44a4146eb1SJosh Lehan 
45b300575eSJosh Lehan     /** Optionally adds fan outputs to an output cache, which is different
46b300575eSJosh Lehan      * from the input cache accessed by getCachedValue(), so it is possible
47b300575eSJosh Lehan      * to have entries with the same name in both the output cache and
48b300575eSJosh Lehan      * the input cache. The output cache is used for logging, to show
49b300575eSJosh Lehan      * the PWM values determined by the PID loop, next to the resulting RPM.
50b300575eSJosh Lehan      */
51b300575eSJosh Lehan     virtual void setOutputCache(std::string_view name,
52b300575eSJosh Lehan                                 const ValueCacheEntry& values) = 0;
53b300575eSJosh Lehan 
547a98c19aSPatrick Venture     /** Return cached value for sensor by name. */
557a98c19aSPatrick Venture     virtual double getCachedValue(const std::string& name) = 0;
56b300575eSJosh Lehan     /** Return cached values, both scaled and original unscaled values,
57b300575eSJosh Lehan      * for sensor by name. Subclasses can add trivial return {value, value},
58b300575eSJosh Lehan      * for subclasses that only implement getCachedValue() and do not care
59b300575eSJosh Lehan      * about maintaining the distinction between scaled and unscaled values.
60b300575eSJosh Lehan      */
61b300575eSJosh Lehan     virtual ValueCacheEntry getCachedValues(const std::string& name) = 0;
627a98c19aSPatrick Venture 
637a98c19aSPatrick Venture     /** Add a set point value for the Max Set Point computation. */
64ccc8bb62SNirav Shah     virtual void addSetPoint(double setpoint, const std::string& name) = 0;
657a98c19aSPatrick Venture     /** Clear all set points specified via addSetPoint */
667a98c19aSPatrick Venture     virtual void clearSetPoints(void) = 0;
677a98c19aSPatrick Venture 
687a98c19aSPatrick Venture     /** Add maximum RPM value to drive fan pids. */
697a98c19aSPatrick Venture     virtual void addRPMCeiling(double ceiling) = 0;
707a98c19aSPatrick Venture     /** Clear any RPM value set with addRPMCeiling. */
717a98c19aSPatrick Venture     virtual void clearRPMCeilings(void) = 0;
727a98c19aSPatrick Venture 
737a98c19aSPatrick Venture     /** Compute the value returned by getMaxSetPointRequest - called from the
747a98c19aSPatrick Venture      * looping mechanism before triggering any Fan PIDs. The value computed is
757a98c19aSPatrick Venture      * used by each fan PID.
767a98c19aSPatrick Venture      */
777a98c19aSPatrick Venture     virtual void determineMaxSetPointRequest(void) = 0;
787a98c19aSPatrick Venture     /** Given the set points added via addSetPoint, return the maximum value -
797a98c19aSPatrick Venture      * called from the PID loop that uses that value to drive the fans.
807a98c19aSPatrick Venture      */
817a98c19aSPatrick Venture     virtual double getMaxSetPointRequest() const = 0;
827a98c19aSPatrick Venture 
837a98c19aSPatrick Venture     /** Return if the zone has any sensors in fail safe mode. */
847a98c19aSPatrick Venture     virtual bool getFailSafeMode() const = 0;
857a98c19aSPatrick Venture     /** Return the rpm or pwm percent value to drive fan pids when zone is in
867a98c19aSPatrick Venture      * fail safe.
877a98c19aSPatrick Venture      */
887a98c19aSPatrick Venture     virtual double getFailSafePercent() const = 0;
897a98c19aSPatrick Venture 
90*0e8fc398SBonnie Lo     /** Return the zone's cycle time settings */
91*0e8fc398SBonnie Lo     virtual uint64_t getCycleIntervalTime(void) const = 0;
92*0e8fc398SBonnie Lo     virtual uint64_t getUpdateThermalsCycle(void) const = 0;
93*0e8fc398SBonnie Lo 
947a98c19aSPatrick Venture     /** Return if the zone is set to manual mode.  false equates to automatic
957a98c19aSPatrick Venture      * mode (the default).
967a98c19aSPatrick Venture      */
977a98c19aSPatrick Venture     virtual bool getManualMode(void) const = 0;
987a98c19aSPatrick Venture 
99a4146eb1SJosh Lehan     /** Returns true if a redundant fan PWM write is needed. Redundant write
100a4146eb1SJosh Lehan      * is used when returning the fan to automatic mode from manual mode.
101a4146eb1SJosh Lehan      */
102a4146eb1SJosh Lehan     virtual bool getRedundantWrite(void) const = 0;
103a4146eb1SJosh Lehan 
1047a98c19aSPatrick Venture     /** For each fan pid, do processing. */
1057a98c19aSPatrick Venture     virtual void processFans(void) = 0;
1067a98c19aSPatrick Venture     /** For each thermal pid, do processing. */
1077a98c19aSPatrick Venture     virtual void processThermals(void) = 0;
1081a153794SPatrick Venture };
1091a153794SPatrick Venture 
1101a153794SPatrick Venture } // namespace pid_control
111