Andrew Boyson / Mbed 2 deprecated heating

Dependencies:   1-wire clock crypto fram log lpc1768 net web wiz mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers radiator.c Source File

radiator.c

00001 #include <stdint.h>
00002 #include <string.h>
00003 #include <stdbool.h>
00004 
00005 #include "gpio.h"
00006 #include "program.h"
00007 #include "ds18b20.h"
00008 #include "settings.h"
00009 #include "boiler.h"
00010 #include "radiator.h"
00011 #include "clktime.h"
00012 #include "clk.h"
00013 #include "led.h"
00014 
00015 #define RADIATOR_PUMP_DIR FIO2DIR(03) // P2.03 == p23;
00016 #define RADIATOR_PUMP_PIN FIO2PIN(03)
00017 #define RADIATOR_PUMP_SET FIO2SET(03)
00018 #define RADIATOR_PUMP_CLR FIO2CLR(03)
00019 
00020 static bool      htgOverride = false;
00021 static char      htgWinter;
00022 static char*     hallRom;
00023 static uint8_t   overrideCancelHour;
00024 static uint8_t   overrideCancelMinute;
00025 static  int16_t  nightTemperature;
00026 static  int16_t  frostTemperature;
00027 static char      hotWaterProtectOn;
00028 static  int8_t   hotWaterProtectTemp;
00029 
00030 bool     RadiatorGetWinter              () { return (bool)htgWinter;              } 
00031 bool     RadiatorGetOverride            () { return       htgOverride;            } 
00032 bool     RadiatorGetHotWaterProtectOn   () { return (bool)hotWaterProtectOn;      }
00033 uint16_t RadiatorGetHallDS18B20Value    () { return DS18B20ValueFromRom(hallRom); }
00034 int      RadiatorGetOverrideCancelHour  () { return  (int)overrideCancelHour;     }
00035 int      RadiatorGetOverrideCancelMinute() { return  (int)overrideCancelMinute;   }
00036 int      RadiatorGetNightTemperature    () { return  (int)nightTemperature;       } 
00037 int      RadiatorGetFrostTemperature    () { return  (int)frostTemperature;       } 
00038 int      RadiatorGetHotWaterProtectTemp () { return  (int)hotWaterProtectTemp;    }
00039 
00040 static void  setWinter              ( bool  v) {                             htgWinter            =     (char)v; SetRadiatorWinter      (&htgWinter           ); }
00041 static void  setHallRom             ( char* v) {                             memcpy(hallRom,  v, 8);             SetHallRom             ( hallRom             ); }
00042 void RadiatorSetOverrideCancelHour  ( int   v) { if (v > 23 || v < 0) v = 0; overrideCancelHour   =  (uint8_t)v; SetOverrideCancelHour  (&overrideCancelHour  ); }
00043 void RadiatorSetOverrideCancelMinute( int   v) { if (v > 59 || v < 0) v = 0; overrideCancelMinute =  (uint8_t)v; SetOverrideCancelMinute(&overrideCancelMinute); }
00044 void RadiatorSetNightTemperature    ( int   v) { if (v > 99 || v < 0) v = 0; nightTemperature     =  (int16_t)v; SetNightTemperature    (&nightTemperature    ); }
00045 void RadiatorSetFrostTemperature    ( int   v) { if (v > 99 || v < 0) v = 0; frostTemperature     =  (int16_t)v; SetFrostTemperature    (&frostTemperature    ); }
00046 
00047 static bool getProgramRequest()
00048 {
00049     return htgWinter && ProgramTimerOutput;
00050 }
00051 static bool getTooCold()
00052 {
00053     //See if the temperature is too low
00054     int  hallTemp16ths = DS18B20ValueFromRom(hallRom);
00055     int nightTemp16ths = nightTemperature << 4;
00056     int frostTemp16ths = frostTemperature << 4;
00057     
00058     static bool nightTooCold = false; //This is static to ride through invalid temperature reads
00059     static bool frostTooCold = false; //This is static to ride through invalid temperature reads
00060     
00061     if (DS18B20IsValidValue(hallTemp16ths))
00062     {
00063         if (hallTemp16ths < frostTemp16ths) frostTooCold = true;
00064         if (hallTemp16ths > frostTemp16ths) frostTooCold = false;
00065         if (hallTemp16ths < nightTemp16ths) nightTooCold = true; //Set   at 289 (18.06) rather than 288 (18.00)
00066         if (hallTemp16ths > nightTemp16ths) nightTooCold = false;//Reset at 287 (17.94). This prevent it following the flashing.
00067     }
00068     
00069     return (htgWinter && nightTooCold) || frostTooCold;
00070 }
00071 static bool getTankTooColdForShowers()
00072 {
00073     return hotWaterProtectOn && BoilerGetTankDS18B20Value() < ((int16_t)hotWaterProtectTemp << 4); //Prevent the heating from robbing all the hot water for showers
00074 }
00075 static void autoCancelOverride()
00076 {
00077     
00078     //Remove override at 11pm
00079     if (ClkTimeIsSet())
00080     {
00081         struct tm tm;
00082         ClkNowTmLocal(&tm);
00083         static bool cancelWasDue = false;
00084         bool cancelIsDue = tm.tm_hour == overrideCancelHour && tm.tm_min == overrideCancelMinute;
00085         if (cancelIsDue && !cancelWasDue && htgOverride) htgOverride = false;
00086         cancelWasDue = cancelIsDue;
00087     }
00088     
00089     //Remove override if no longer required
00090     bool thisProgramRequest = getProgramRequest();
00091     static bool previousProgramRequest = false;
00092     if (previousProgramRequest != thisProgramRequest && htgOverride) htgOverride = false;
00093     previousProgramRequest = thisProgramRequest;
00094 }
00095 bool RadiatorsOn = false;
00096 bool RadiatorPump = false;
00097 static void makeOutputs()
00098 {
00099     bool programRequest = getProgramRequest();
00100     RadiatorsOn = htgOverride ? !programRequest : programRequest ;
00101     RadiatorPump = RadiatorsOn && !getTankTooColdForShowers();
00102 }
00103 
00104 void RadiatorSetWinter(bool value) //Summer is false, Winter is true
00105 {
00106     if (htgWinter == (char)value) return; //Ignore no change
00107     
00108     bool programRequestBeforeSettingWinter = getProgramRequest();
00109     setWinter(value);                     //Change to the new value
00110     bool programRequestAfterSettingWinter = getProgramRequest();
00111     
00112     
00113     if (htgOverride) //Only deal with an override that is already set; if it wasn't set don't change it
00114     {
00115         if (htgWinter) //Summer -> Winter
00116         {
00117             if (programRequestAfterSettingWinter != programRequestBeforeSettingWinter) htgOverride = false; //Adjust the override to leave the heat as it was - off or on.
00118         }
00119         else //Winter -> Summer
00120         {
00121             htgOverride = false; //turn off the heat.
00122         }
00123     }
00124     makeOutputs();
00125 }
00126 void RadiatorSetOverride(bool value)
00127 {
00128     htgOverride = value;
00129     makeOutputs();
00130 }
00131 
00132 void RadiatorChgWinter  (){ RadiatorSetWinter  (!RadiatorGetWinter  ()); }
00133 void RadiatorChgOverride(){ RadiatorSetOverride(!RadiatorGetOverride()); }
00134 void RadiatorChgHotWaterProtectOn  ()       {                             hotWaterProtectOn   = !hotWaterProtectOn; SetHotWaterProtectOn  (&hotWaterProtectOn  ); }
00135 void RadiatorSetHotWaterProtectTemp( int v) { if (v > 99 || v < 0) v = 0; hotWaterProtectTemp = (int8_t)v;          SetHotWaterProtectTemp(&hotWaterProtectTemp); }
00136 int RadiatorInit()
00137 {
00138     hallRom = DS18B20Roms + 8 * DS18B20RomCount;
00139     DS18B20RomSetters[DS18B20RomCount] = setHallRom;
00140     DS18B20RomNames[DS18B20RomCount] = "Hall";
00141     DS18B20RomCount++;
00142 
00143     int  address;
00144     int8_t  def1;
00145     int16_t def2;
00146     GetRadiatorWinter      (&htgWinter           );
00147     GetHallRom             ( hallRom             );
00148     GetOverrideCancelHour  (&overrideCancelHour  );
00149     GetOverrideCancelMinute(&overrideCancelMinute);
00150     GetNightTemperature    (&nightTemperature    );
00151     GetFrostTemperature    (&frostTemperature    );
00152     GetHotWaterProtectOn   (&hotWaterProtectOn   );
00153     GetHotWaterProtectTemp (&hotWaterProtectTemp );
00154     
00155     RADIATOR_PUMP_DIR = 1; //Set the direction to 1 == output
00156     
00157     return 0;
00158 }
00159 void RadiatorMain()
00160 {
00161     autoCancelOverride();
00162     makeOutputs();
00163     
00164     if (RadiatorPump) RADIATOR_PUMP_SET;
00165     else              RADIATOR_PUMP_CLR;
00166 
00167 }