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 old-radiator.c Source File

old-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 outputBeforeOverride = false;
00048 static void makeOutputBeforeOverride()
00049 {
00050     //See if the temperature is too low
00051     int  hallTemp16ths = DS18B20ValueFromRom(hallRom);
00052     int nightTemp16ths = nightTemperature << 4;
00053     int frostTemp16ths = frostTemperature << 4;
00054 
00055     static bool tooCold = false; //This is static to ride through invalid temperature reads
00056     
00057     static bool nightTooCold = false;
00058     static bool frostTooCold = false;
00059     
00060     if (DS18B20IsValidValue(hallTemp16ths))
00061     {
00062         if (hallTemp16ths < frostTemp16ths) frostTooCold = true;
00063         if (hallTemp16ths > frostTemp16ths) frostTooCold = false;
00064         if (hallTemp16ths < nightTemp16ths) nightTooCold = true; //Set   at 289 (18.06) rather than 288 (18.00)
00065         if (hallTemp16ths > nightTemp16ths) nightTooCold = false;//Reset at 287 (17.94). This prevent it following the flashing.
00066     }
00067     
00068     outputBeforeOverride = (htgWinter && ProgramTimerOutput) || (htgWinter && nightTooCold) || frostTooCold;
00069 }
00070 static void autoCancelOverride()
00071 {
00072     
00073     //Remove override at 11pm
00074     if (ClkTimeIsSet())
00075     {
00076         struct tm tm;
00077         ClkNowTmLocal(&tm);
00078         static bool cancelWasDue = false;
00079         bool cancelIsDue = tm.tm_hour == overrideCancelHour && tm.tm_min == overrideCancelMinute;
00080         if (cancelIsDue && !cancelWasDue && htgOverride) htgOverride = false;
00081         cancelWasDue = cancelIsDue;
00082     }
00083     
00084     //Remove override if no longer required
00085     static bool previousOutput = false;
00086     if (previousOutput != outputBeforeOverride && htgOverride) htgOverride = false;
00087     previousOutput = outputBeforeOverride;
00088 }
00089 bool RadiatorPump = false;
00090 static void makeOutputWithOverride()
00091 {
00092     RadiatorPump = htgOverride ? !outputBeforeOverride : outputBeforeOverride ;
00093     if (hotWaterProtectOn && BoilerGetTankDS18B20Value() < ((int16_t)hotWaterProtectTemp << 4)) RadiatorPump = false; //Prevent the heating from robbing all the hot water for showers
00094 }
00095 
00096 void RadiatorSetWinter(bool value) //Summer is false, Winter is true
00097 {
00098     if (htgWinter == (char)value) return; //Ignore no change
00099     setWinter(value);                     //Change to the new value
00100     
00101     bool prevOutputBeforeOverride = outputBeforeOverride;
00102     makeOutputBeforeOverride();
00103     
00104     if (htgOverride) //Only deal with an override that is already set; if it wasn't set don't change it
00105     {
00106         if (htgWinter) //Summer -> Winter
00107         {
00108             if (outputBeforeOverride != prevOutputBeforeOverride) htgOverride = false; //Adjust the override to leave the heat as it was - off or on.
00109         }
00110         else //Winter -> Summer
00111         {
00112             htgOverride = false; //turn off the heat.
00113         }
00114     }
00115         
00116     makeOutputWithOverride();
00117 }
00118 void RadiatorSetOverride(bool value)
00119 {
00120     htgOverride = value;
00121     makeOutputBeforeOverride();
00122     makeOutputWithOverride();
00123 }
00124 
00125 void RadiatorChgWinter  (){ RadiatorSetWinter  (!RadiatorGetWinter  ()); }
00126 void RadiatorChgOverride(){ RadiatorSetOverride(!RadiatorGetOverride()); }
00127 void RadiatorChgHotWaterProtectOn  ()       {                             hotWaterProtectOn   = !hotWaterProtectOn; makeOutputWithOverride(); SetHotWaterProtectOn  (&hotWaterProtectOn  ); }
00128 void RadiatorSetHotWaterProtectTemp( int v) { if (v > 99 || v < 0) v = 0; hotWaterProtectTemp = (int8_t)v;          makeOutputWithOverride(); SetHotWaterProtectTemp(&hotWaterProtectTemp); }
00129 int RadiatorInit()
00130 {
00131     hallRom = DS18B20Roms + 8 * DS18B20RomCount;
00132     DS18B20RomSetters[DS18B20RomCount] = setHallRom;
00133     DS18B20RomNames[DS18B20RomCount] = "Hall";
00134     DS18B20RomCount++;
00135 
00136     int  address;
00137     int8_t  def1;
00138     int16_t def2;
00139     GetRadiatorWinter      (&htgWinter           );
00140     GetHallRom             ( hallRom             );
00141     GetOverrideCancelHour  (&overrideCancelHour  );
00142     GetOverrideCancelMinute(&overrideCancelMinute);
00143     GetNightTemperature    (&nightTemperature    );
00144     GetFrostTemperature    (&frostTemperature    );
00145     GetHotWaterProtectOn   (&hotWaterProtectOn   );
00146     GetHotWaterProtectTemp (&hotWaterProtectTemp );
00147     
00148     RADIATOR_PUMP_DIR = 1; //Set the direction to 1 == output
00149     
00150     return 0;
00151 }
00152 void RadiatorMain()
00153 {
00154     //Make the radiator output
00155     makeOutputBeforeOverride();
00156     autoCancelOverride(); //Do this after making the output as it uses that information
00157     makeOutputWithOverride();
00158     
00159     //Pump output
00160     if (RadiatorPump) RADIATOR_PUMP_SET;
00161     else              RADIATOR_PUMP_CLR;
00162 
00163 }
00164 */