Andrew Boyson / oldheating

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

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 "fram.h"
00009 #include "radiator.h"
00010 #include "clktime.h"
00011 #include "clk.h"
00012 #include "led.h"
00013 
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;            static int iWinter;
00022 static char*     hallRom;              static int iHallRom;
00023 static uint8_t   overrideCancelHour;   static int iOverrideCancelHour;
00024 static uint8_t   overrideCancelMinute; static int iOverrideCancelMinute;
00025 static  int16_t  nightTemperature;     static int iNightTemperature;
00026 static  int16_t  frostTemperature;     static int iFrostTemperature;
00027 
00028 bool     RadiatorGetWinter              () { return (bool)htgWinter;              } 
00029 bool     RadiatorGetOverride            () { return       htgOverride;            } 
00030 uint16_t RadiatorGetHallDS18B20Value    () { return DS18B20ValueFromRom(hallRom); }
00031 int      RadiatorGetOverrideCancelHour  () { return  (int)overrideCancelHour;     }
00032 int      RadiatorGetOverrideCancelMinute() { return  (int)overrideCancelMinute;   }
00033 int      RadiatorGetNightTemperature    () { return  (int)nightTemperature;       } 
00034 int      RadiatorGetFrostTemperature    () { return  (int)frostTemperature;       } 
00035 
00036 static void  setWinter              ( bool  v) {                             htgWinter            =     (char)v; FramWrite(iWinter,               1, &htgWinter           ); }
00037 static void  setHallRom             ( char* v) {                             memcpy(hallRom,  v, 8);             FramWrite(iHallRom,              8,  hallRom             ); }
00038 void RadiatorSetOverrideCancelHour  ( int   v) { if (v > 23 || v < 0) v = 0; overrideCancelHour   =  (uint8_t)v, FramWrite(iOverrideCancelHour,   1, &overrideCancelHour  ); }
00039 void RadiatorSetOverrideCancelMinute( int   v) { if (v > 59 || v < 0) v = 0; overrideCancelMinute =  (uint8_t)v, FramWrite(iOverrideCancelMinute, 1, &overrideCancelMinute); }
00040 void RadiatorSetNightTemperature    ( int   v) { if (v > 99 || v < 0) v = 0; nightTemperature     =  (int16_t)v; FramWrite(iNightTemperature,     2, &nightTemperature    ); }
00041 void RadiatorSetFrostTemperature    ( int   v) { if (v > 99 || v < 0) v = 0; frostTemperature     =  (int16_t)v; FramWrite(iFrostTemperature,     2, &frostTemperature    ); }
00042 
00043 static bool outputBeforeOverride = false;
00044 static void makeOutputBeforeOverride()
00045 {
00046     //See if the temperature is too low
00047     int  hallTemp16ths = DS18B20ValueFromRom(hallRom);
00048     int nightTemp16ths = nightTemperature << 4;
00049     int frostTemp16ths = frostTemperature << 4;
00050 
00051     static bool tooCold = false; //This is static to ride through invalid temperature reads
00052     
00053     static bool nightTooCold = false;
00054     static bool frostTooCold = false;
00055     
00056     if (DS18B20IsValidValue(hallTemp16ths))
00057     {
00058         if (hallTemp16ths < frostTemp16ths) frostTooCold = true;
00059         if (hallTemp16ths > frostTemp16ths) frostTooCold = false;
00060         if (hallTemp16ths < nightTemp16ths) nightTooCold = true; //Set   at 289 (18.06) rather than 288 (18.00)
00061         if (hallTemp16ths > nightTemp16ths) nightTooCold = false;//Reset at 287 (17.94). This prevent it following the flashing.
00062     }
00063     
00064     outputBeforeOverride = (htgWinter && ProgramTimerOutput) || (htgWinter && nightTooCold) || frostTooCold;
00065 }
00066 static void autoCancelOverride()
00067 {
00068     
00069     //Remove override at 11pm
00070     if (ClkTimeIsSet())
00071     {
00072         struct tm tm;
00073         ClkNowTmLocal(&tm);
00074         static bool cancelWasDue = false;
00075         bool cancelIsDue = tm.tm_hour == overrideCancelHour && tm.tm_min == overrideCancelMinute;
00076         if (cancelIsDue && !cancelWasDue && htgOverride) htgOverride = false;
00077         cancelWasDue = cancelIsDue;
00078     }
00079     
00080     //Remove override if no longer required
00081     static bool previousOutput = false;
00082     if (previousOutput != outputBeforeOverride && htgOverride) htgOverride = false;
00083     previousOutput = outputBeforeOverride;
00084 }
00085 bool RadiatorPump = false;
00086 static void makeOutputWithOverride()
00087 {
00088     RadiatorPump = htgOverride ? !outputBeforeOverride : outputBeforeOverride ;
00089 }
00090 
00091 void RadiatorSetWinter(bool value) //Summer is false, Winter is true
00092 {
00093     if (htgWinter == (char)value) return; //Ignore no change
00094     setWinter(value);                     //Change to the new value
00095     
00096     bool prevOutputBeforeOverride = outputBeforeOverride;
00097     makeOutputBeforeOverride();
00098     
00099     if (htgOverride) //Only deal with an override that is already set; if it wasn't set don't change it
00100     {
00101         if (htgWinter) //Summer -> Winter
00102         {
00103             if (outputBeforeOverride != prevOutputBeforeOverride) htgOverride = false; //Adjust the override to leave the heat as it was - off or on.
00104         }
00105         else //Winter -> Summer
00106         {
00107             htgOverride = false; //turn off the heat.
00108         }
00109     }
00110         
00111     makeOutputWithOverride();
00112 }
00113 void RadiatorSetOverride(bool value)
00114 {
00115     htgOverride = value;
00116     makeOutputBeforeOverride();
00117     makeOutputWithOverride(); }
00118 
00119 void RadiatorChgWinter  (){ RadiatorSetWinter  (!RadiatorGetWinter  ()); }
00120 void RadiatorChgOverride(){ RadiatorSetOverride(!RadiatorGetOverride()); }
00121 
00122 int RadiatorInit()
00123 {
00124     hallRom = DS18B20Roms + 8 * DS18B20RomCount;
00125     DS18B20RomSetters[DS18B20RomCount] = setHallRom;
00126     DS18B20RomNames[DS18B20RomCount] = "Hall";
00127     DS18B20RomCount++;
00128 
00129     int  address;
00130     int8_t  def1;
00131     int16_t def2;
00132     def1 =  0; address = FramLoad( 1, &htgWinter,            &def1); if (address < 0) return -1; iWinter               = address; 
00133                          FramAllocate(1); //Spare byte
00134                address = FramLoad( 8,  hallRom,                  0); if (address < 0) return -1; iHallRom              = address;
00135     def1 = 23; address = FramLoad( 1, &overrideCancelHour,   &def1); if (address < 0) return -1; iOverrideCancelHour   = address;
00136     def1 =  0; address = FramLoad( 1, &overrideCancelMinute, &def1); if (address < 0) return -1; iOverrideCancelMinute = address;
00137                          FramAllocate(2); //Spare two bytes
00138     def2 = 15; address = FramLoad( 2, &nightTemperature,     &def2); if (address < 0) return -1; iNightTemperature     = address; 
00139     def2 =  8; address = FramLoad( 2, &frostTemperature,     &def2); if (address < 0) return -1; iFrostTemperature     = address; 
00140     
00141     RADIATOR_PUMP_DIR = 1; //Set the direction to 1 == output
00142     
00143     return 0;
00144 }
00145 void RadiatorMain()
00146 {
00147     //Make the radiator output
00148     makeOutputBeforeOverride();
00149     autoCancelOverride(); //Do this after making the output as it uses that information
00150     makeOutputWithOverride();
00151     
00152     //Pump output
00153     if (RadiatorPump) RADIATOR_PUMP_SET;
00154     else              RADIATOR_PUMP_CLR;
00155 
00156 }