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 
19c51ba919SBonnie Lo     /** Get Current Zone ID */
20c51ba919SBonnie Lo     virtual int64_t getZoneID(void) const = 0;
21c51ba919SBonnie Lo 
227a98c19aSPatrick Venture     /** If the zone implementation supports logging, initialize the log. */
237a98c19aSPatrick Venture     virtual void initializeLog(void) = 0;
247a98c19aSPatrick Venture     /** If the zone implementation supports logging, write string to log. */
257a98c19aSPatrick Venture     virtual void writeLog(const std::string& value) = 0;
267a98c19aSPatrick Venture 
277a98c19aSPatrick Venture     /** Return a pointer to the sensor specified by name. */
281a153794SPatrick Venture     virtual Sensor* getSensor(const std::string& name) = 0;
297a98c19aSPatrick Venture 
307a98c19aSPatrick Venture     /* updateFanTelemetry() and updateSensors() both clear the failsafe state
317a98c19aSPatrick Venture      * for a sensor if it's no longer in that state.
327a98c19aSPatrick Venture      */
337a98c19aSPatrick Venture     /** For each fan input in the zone, read each to update the cachedValue and
347a98c19aSPatrick Venture      * check if the fan is beyond its timeout to trigger a failsafe condition.
357a98c19aSPatrick Venture      */
367a98c19aSPatrick Venture     virtual void updateFanTelemetry(void) = 0;
377a98c19aSPatrick Venture     /** For each thermal input in the zone, read each to update the cachedValue
387a98c19aSPatrick Venture      * and check if the sensor is beyond its timeout to trigger a failsafe
397a98c19aSPatrick Venture      * condition.
407a98c19aSPatrick Venture      */
417a98c19aSPatrick Venture     virtual void updateSensors(void) = 0;
427a98c19aSPatrick Venture     /** For each fan and thermal input in the zone, set the cachedValue to 0 and
437a98c19aSPatrick Venture      * set the input as failsafe - to default the zone to failsafe before it
447a98c19aSPatrick Venture      * starts processing values to control fans.
457a98c19aSPatrick Venture      */
467a98c19aSPatrick Venture     virtual void initializeCache(void) = 0;
47a4146eb1SJosh Lehan 
48b300575eSJosh Lehan     /** Optionally adds fan outputs to an output cache, which is different
49b300575eSJosh Lehan      * from the input cache accessed by getCachedValue(), so it is possible
50b300575eSJosh Lehan      * to have entries with the same name in both the output cache and
51b300575eSJosh Lehan      * the input cache. The output cache is used for logging, to show
52b300575eSJosh Lehan      * the PWM values determined by the PID loop, next to the resulting RPM.
53b300575eSJosh Lehan      */
54b300575eSJosh Lehan     virtual void setOutputCache(std::string_view name,
55b300575eSJosh Lehan                                 const ValueCacheEntry& values) = 0;
56b300575eSJosh Lehan 
577a98c19aSPatrick Venture     /** Return cached value for sensor by name. */
587a98c19aSPatrick Venture     virtual double getCachedValue(const std::string& name) = 0;
59b300575eSJosh Lehan     /** Return cached values, both scaled and original unscaled values,
60b300575eSJosh Lehan      * for sensor by name. Subclasses can add trivial return {value, value},
61b300575eSJosh Lehan      * for subclasses that only implement getCachedValue() and do not care
62b300575eSJosh Lehan      * about maintaining the distinction between scaled and unscaled values.
63b300575eSJosh Lehan      */
64b300575eSJosh Lehan     virtual ValueCacheEntry getCachedValues(const std::string& name) = 0;
657a98c19aSPatrick Venture 
667a98c19aSPatrick Venture     /** Add a set point value for the Max Set Point computation. */
67ccc8bb62SNirav Shah     virtual void addSetPoint(double setpoint, const std::string& name) = 0;
687a98c19aSPatrick Venture     /** Clear all set points specified via addSetPoint */
697a98c19aSPatrick Venture     virtual void clearSetPoints(void) = 0;
707a98c19aSPatrick Venture 
717a98c19aSPatrick Venture     /** Add maximum RPM value to drive fan pids. */
727a98c19aSPatrick Venture     virtual void addRPMCeiling(double ceiling) = 0;
737a98c19aSPatrick Venture     /** Clear any RPM value set with addRPMCeiling. */
747a98c19aSPatrick Venture     virtual void clearRPMCeilings(void) = 0;
757a98c19aSPatrick Venture 
767a98c19aSPatrick Venture     /** Compute the value returned by getMaxSetPointRequest - called from the
777a98c19aSPatrick Venture      * looping mechanism before triggering any Fan PIDs. The value computed is
787a98c19aSPatrick Venture      * used by each fan PID.
797a98c19aSPatrick Venture      */
807a98c19aSPatrick Venture     virtual void determineMaxSetPointRequest(void) = 0;
817a98c19aSPatrick Venture     /** Given the set points added via addSetPoint, return the maximum value -
827a98c19aSPatrick Venture      * called from the PID loop that uses that value to drive the fans.
837a98c19aSPatrick Venture      */
847a98c19aSPatrick Venture     virtual double getMaxSetPointRequest() const = 0;
857a98c19aSPatrick Venture 
867a98c19aSPatrick Venture     /** Return if the zone has any sensors in fail safe mode. */
877a98c19aSPatrick Venture     virtual bool getFailSafeMode() const = 0;
887a98c19aSPatrick Venture     /** Return the rpm or pwm percent value to drive fan pids when zone is in
897a98c19aSPatrick Venture      * fail safe.
907a98c19aSPatrick Venture      */
917a98c19aSPatrick Venture     virtual double getFailSafePercent() const = 0;
927a98c19aSPatrick Venture 
930e8fc398SBonnie Lo     /** Return the zone's cycle time settings */
940e8fc398SBonnie Lo     virtual uint64_t getCycleIntervalTime(void) const = 0;
950e8fc398SBonnie Lo     virtual uint64_t getUpdateThermalsCycle(void) const = 0;
960e8fc398SBonnie Lo 
977a98c19aSPatrick Venture     /** Return if the zone is set to manual mode.  false equates to automatic
987a98c19aSPatrick Venture      * mode (the default).
997a98c19aSPatrick Venture      */
1007a98c19aSPatrick Venture     virtual bool getManualMode(void) const = 0;
1017a98c19aSPatrick Venture 
102a4146eb1SJosh Lehan     /** Returns true if a redundant fan PWM write is needed. Redundant write
103a4146eb1SJosh Lehan      * is used when returning the fan to automatic mode from manual mode.
104a4146eb1SJosh Lehan      */
105a4146eb1SJosh Lehan     virtual bool getRedundantWrite(void) const = 0;
106a4146eb1SJosh Lehan 
107*9788963cSDelphine CC Chiu     /** Returns true if user wants to accumulate the output PWM of different
108*9788963cSDelphine CC Chiu      * controllers with same sensor
109*9788963cSDelphine CC Chiu      */
110*9788963cSDelphine CC Chiu     virtual bool getAccSetPoint(void) const = 0;
111*9788963cSDelphine CC Chiu 
1127a98c19aSPatrick Venture     /** For each fan pid, do processing. */
1137a98c19aSPatrick Venture     virtual void processFans(void) = 0;
1147a98c19aSPatrick Venture     /** For each thermal pid, do processing. */
1157a98c19aSPatrick Venture     virtual void processThermals(void) = 0;
11637180062SHarvey Wu 
11737180062SHarvey Wu     /** Update thermal/power debug dbus properties */
11837180062SHarvey Wu     virtual void updateThermalPowerDebugInterface(std::string pidName,
11937180062SHarvey Wu                                                   std::string leader,
12037180062SHarvey Wu                                                   double input,
12137180062SHarvey Wu                                                   double output) = 0;
1221a153794SPatrick Venture };
1231a153794SPatrick Venture 
1241a153794SPatrick Venture } // namespace pid_control
125