Controls the central heating system and the lights.

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

Committer:
andrewboyson
Date:
Sat Nov 12 10:03:38 2022 +0000
Revision:
8:8ac076ce51af
Parent:
0:22b158d3c76f
Updated LPC1768 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:22b158d3c76f 1 #include <stdint.h>
andrewboyson 0:22b158d3c76f 2 #include <string.h>
andrewboyson 0:22b158d3c76f 3 #include <stdbool.h>
andrewboyson 0:22b158d3c76f 4
andrewboyson 0:22b158d3c76f 5 #include "tftp.h"
andrewboyson 0:22b158d3c76f 6 #include "dnslabel.h"
andrewboyson 0:22b158d3c76f 7 //#include "fram.h"
andrewboyson 0:22b158d3c76f 8 #include "clk.h"
andrewboyson 0:22b158d3c76f 9 #include "clktime.h"
andrewboyson 0:22b158d3c76f 10 #include "mstimer.h"
andrewboyson 0:22b158d3c76f 11 #include "clktm.h"
andrewboyson 0:22b158d3c76f 12 #include "ds18b20.h"
andrewboyson 0:22b158d3c76f 13 #include "radiator.h"
andrewboyson 0:22b158d3c76f 14 #include "boiler.h"
andrewboyson 0:22b158d3c76f 15 #include "hot-water.h"
andrewboyson 0:22b158d3c76f 16 #include "log.h"
andrewboyson 0:22b158d3c76f 17 #include "net.h"
andrewboyson 0:22b158d3c76f 18 #include "settings.h"
andrewboyson 0:22b158d3c76f 19
andrewboyson 0:22b158d3c76f 20 bool ValuesTrace = false;
andrewboyson 0:22b158d3c76f 21
andrewboyson 0:22b158d3c76f 22 static char serverName[DNS_MAX_LABEL_LENGTH+1];
andrewboyson 0:22b158d3c76f 23 static char fileName [DNS_MAX_LABEL_LENGTH+1];
andrewboyson 0:22b158d3c76f 24 static int32_t writeSize;
andrewboyson 0:22b158d3c76f 25 static int32_t readInterval;
andrewboyson 0:22b158d3c76f 26 static int64_t startTime;
andrewboyson 0:22b158d3c76f 27 static int32_t count;
andrewboyson 0:22b158d3c76f 28
andrewboyson 0:22b158d3c76f 29 char* ValuesGetServerName ( ) { return serverName; }
andrewboyson 0:22b158d3c76f 30 char* ValuesGetFileName ( ) { return fileName; }
andrewboyson 0:22b158d3c76f 31 int ValuesGetWriteSize ( ) { return (int) writeSize; }
andrewboyson 0:22b158d3c76f 32 int ValuesGetReadInterval ( ) { return (int) readInterval; }
andrewboyson 0:22b158d3c76f 33 void ValuesGetStartTm (struct tm* ptm) { ClkTimeToTmUtc(startTime, ptm); }
andrewboyson 0:22b158d3c76f 34 int64_t ValuesGetStartTime ( ) { return startTime >> CLK_TIME_ONE_SECOND_SHIFT;}
andrewboyson 0:22b158d3c76f 35 int ValuesGetCount ( ) { return (int) count; }
andrewboyson 0:22b158d3c76f 36
andrewboyson 0:22b158d3c76f 37 void ValuesSetServerName (char* value) { DnsLabelCopy(serverName, value); SetValuesServerName ( value); }
andrewboyson 0:22b158d3c76f 38 void ValuesSetFileName (char* value) { DnsLabelCopy( fileName, value); SetValuesFileName ( value); }
andrewboyson 0:22b158d3c76f 39 void ValuesSetWriteSize (int value) { writeSize = value ; SetValuesWriteSize (&value); }
andrewboyson 0:22b158d3c76f 40 void ValuesSetReadInterval (int value) { readInterval = value ; SetValuesReadInterval(&value); }
andrewboyson 0:22b158d3c76f 41 static void setStartTime (int64_t value) { startTime = value ; SetValuesStartTime (&value); }
andrewboyson 0:22b158d3c76f 42 static void setCount (int value) { count = value ; SetValuesCount (&value); }
andrewboyson 0:22b158d3c76f 43
andrewboyson 0:22b158d3c76f 44 static int readValuesFromFram()
andrewboyson 0:22b158d3c76f 45 {
andrewboyson 0:22b158d3c76f 46 int address;
andrewboyson 0:22b158d3c76f 47 int32_t def4;
andrewboyson 0:22b158d3c76f 48 GetValuesServerName ( serverName);
andrewboyson 0:22b158d3c76f 49 GetValuesFileName ( fileName);
andrewboyson 0:22b158d3c76f 50 GetValuesWriteSize ( &writeSize);
andrewboyson 0:22b158d3c76f 51 GetValuesReadInterval(&readInterval);
andrewboyson 0:22b158d3c76f 52 GetValuesStartTime ( &startTime);
andrewboyson 0:22b158d3c76f 53 GetValuesCount ( &count);
andrewboyson 0:22b158d3c76f 54 return 0;
andrewboyson 0:22b158d3c76f 55 }
andrewboyson 0:22b158d3c76f 56
andrewboyson 0:22b158d3c76f 57
andrewboyson 0:22b158d3c76f 58 static uint32_t readStartMs;
andrewboyson 0:22b158d3c76f 59
andrewboyson 0:22b158d3c76f 60 static int writeIndex;
andrewboyson 0:22b158d3c76f 61
andrewboyson 0:22b158d3c76f 62 static int nextByteOfWriteStream()
andrewboyson 0:22b158d3c76f 63 {
andrewboyson 0:22b158d3c76f 64 int byteAfterData = count * 8;
andrewboyson 0:22b158d3c76f 65 if (writeIndex >= byteAfterData)
andrewboyson 0:22b158d3c76f 66 {
andrewboyson 0:22b158d3c76f 67 setCount(0);
andrewboyson 0:22b158d3c76f 68 return -1;
andrewboyson 0:22b158d3c76f 69 }
andrewboyson 0:22b158d3c76f 70 char c;
andrewboyson 0:22b158d3c76f 71 GetValuesData(writeIndex, &c);
andrewboyson 0:22b158d3c76f 72 writeIndex++;
andrewboyson 0:22b158d3c76f 73 return c;
andrewboyson 0:22b158d3c76f 74 }
andrewboyson 0:22b158d3c76f 75
andrewboyson 0:22b158d3c76f 76 static void readValues()
andrewboyson 0:22b158d3c76f 77 {
andrewboyson 0:22b158d3c76f 78 uint64_t record = 0;
andrewboyson 0:22b158d3c76f 79 uint16_t value;
andrewboyson 0:22b158d3c76f 80 value = RadiatorGetHallDS18B20Value();
andrewboyson 0:22b158d3c76f 81 value &= 0x0FFF;
andrewboyson 0:22b158d3c76f 82 record |= value; //0000 0000 0000 0AAA
andrewboyson 0:22b158d3c76f 83 record <<= 12; //0000 0000 00AA A000
andrewboyson 0:22b158d3c76f 84 value = BoilerGetTankDS18B20Value();
andrewboyson 0:22b158d3c76f 85 value &= 0x0FFF;
andrewboyson 0:22b158d3c76f 86 record |= value; //0000 0000 00AA ABBB
andrewboyson 0:22b158d3c76f 87 record <<= 12; //0000 000A AABB B000
andrewboyson 0:22b158d3c76f 88 value = BoilerGetOutputDS18B20Value();
andrewboyson 0:22b158d3c76f 89 value &= 0x0FFF;
andrewboyson 0:22b158d3c76f 90 record |= value; //0000 000A AABB BCCC
andrewboyson 0:22b158d3c76f 91 record <<= 12; //0000 AAAB BBCC C000
andrewboyson 0:22b158d3c76f 92 //value = BoilerGetReturnDS18B20Value();
andrewboyson 0:22b158d3c76f 93 value = BoilerGetRtnDelDS18B20Value();
andrewboyson 0:22b158d3c76f 94 value &= 0x0FFF;
andrewboyson 0:22b158d3c76f 95 record |= value; //0000 AAAB BBCC CDDD
andrewboyson 0:22b158d3c76f 96 record <<= 12; //0AAA BBBC CCDD D000
andrewboyson 0:22b158d3c76f 97 value = HotWaterGetDS18B20Value();
andrewboyson 0:22b158d3c76f 98 value &= 0x0FFF;
andrewboyson 0:22b158d3c76f 99 record |= value; //0AAA BBBC CCDD DEEE
andrewboyson 0:22b158d3c76f 100 record <<= 4; //AAAB BBCC CDDD EEE0
andrewboyson 0:22b158d3c76f 101
andrewboyson 0:22b158d3c76f 102 record |= RadiatorPump << 0;
andrewboyson 0:22b158d3c76f 103 record |= BoilerCall << 1;
andrewboyson 0:22b158d3c76f 104 record |= BoilerPump << 2; //AAAB BBCC CDDD EEEF
andrewboyson 0:22b158d3c76f 105
andrewboyson 0:22b158d3c76f 106 if (count <= 0)
andrewboyson 0:22b158d3c76f 107 {
andrewboyson 0:22b158d3c76f 108 count = 0;
andrewboyson 0:22b158d3c76f 109 setStartTime(ClkNowTai());
andrewboyson 0:22b158d3c76f 110 }
andrewboyson 0:22b158d3c76f 111
andrewboyson 0:22b158d3c76f 112 SetValuesData(count, &record);
andrewboyson 0:22b158d3c76f 113 setCount(count + 1);
andrewboyson 0:22b158d3c76f 114 }
andrewboyson 0:22b158d3c76f 115
andrewboyson 0:22b158d3c76f 116 static void writeValues()
andrewboyson 0:22b158d3c76f 117 {
andrewboyson 0:22b158d3c76f 118 if (!serverName[0] ) return; //Do nothing if have no server name
andrewboyson 0:22b158d3c76f 119 if ( !fileName[0] ) return; //Do nothing if have no file name
andrewboyson 0:22b158d3c76f 120 if (TftpWriteStatus) return; //Do nothing if the TFTP client is busy
andrewboyson 0:22b158d3c76f 121
andrewboyson 0:22b158d3c76f 122 strcpy(TftpServerName, serverName);
andrewboyson 0:22b158d3c76f 123 struct tm tm;
andrewboyson 0:22b158d3c76f 124 ClkTimeToTmUtc(startTime, &tm);
andrewboyson 0:22b158d3c76f 125 int len = strftime(TftpFileName, DNS_MAX_LABEL_LENGTH+1, fileName, &tm);
andrewboyson 0:22b158d3c76f 126 if (len == 0)
andrewboyson 0:22b158d3c76f 127 {
andrewboyson 0:22b158d3c76f 128 LogTimeF("Values - cannot make filename from template '%s'\r\n", count, fileName);
andrewboyson 0:22b158d3c76f 129 return;
andrewboyson 0:22b158d3c76f 130 }
andrewboyson 0:22b158d3c76f 131
andrewboyson 0:22b158d3c76f 132 if (ValuesTrace)
andrewboyson 0:22b158d3c76f 133 {
andrewboyson 0:22b158d3c76f 134 if (NetTraceNewLine) Log("\r\n");
andrewboyson 0:22b158d3c76f 135 LogTimeF("Values - requesting backup of %d values to %s\r\n", count, TftpFileName);
andrewboyson 0:22b158d3c76f 136 }
andrewboyson 0:22b158d3c76f 137
andrewboyson 0:22b158d3c76f 138 writeIndex = 0;
andrewboyson 0:22b158d3c76f 139 TftpWriteStatus = TFTP_WRITE_STATUS_REQUEST; //This is reset by TFTP when finished
andrewboyson 0:22b158d3c76f 140 }
andrewboyson 0:22b158d3c76f 141
andrewboyson 0:22b158d3c76f 142 void ValuesMain()
andrewboyson 0:22b158d3c76f 143 {
andrewboyson 0:22b158d3c76f 144 if (!readInterval) readStartMs = MsTimerCount;
andrewboyson 0:22b158d3c76f 145 if (writeSize && count < writeSize && readInterval)
andrewboyson 0:22b158d3c76f 146 {
andrewboyson 0:22b158d3c76f 147 if (MsTimerRelative(readStartMs, readInterval * 1000))
andrewboyson 0:22b158d3c76f 148 {
andrewboyson 0:22b158d3c76f 149 readValues(); //Only read values if they are going to be backed up
andrewboyson 0:22b158d3c76f 150 readStartMs = MsTimerCount;
andrewboyson 0:22b158d3c76f 151 }
andrewboyson 0:22b158d3c76f 152 }
andrewboyson 0:22b158d3c76f 153 else
andrewboyson 0:22b158d3c76f 154 {
andrewboyson 0:22b158d3c76f 155 readStartMs = MsTimerCount;
andrewboyson 0:22b158d3c76f 156 }
andrewboyson 0:22b158d3c76f 157 if (writeSize && count >= writeSize) writeValues(); //Backup the values once the backup size is reached
andrewboyson 0:22b158d3c76f 158 }
andrewboyson 0:22b158d3c76f 159
andrewboyson 0:22b158d3c76f 160 int ValuesInit()
andrewboyson 0:22b158d3c76f 161 {
andrewboyson 0:22b158d3c76f 162 if (readValuesFromFram()) return -1;
andrewboyson 0:22b158d3c76f 163 readStartMs = MsTimerCount;
andrewboyson 0:22b158d3c76f 164 TftpGetNextByteFunction = nextByteOfWriteStream;
andrewboyson 0:22b158d3c76f 165 if (count > 0) writeValues(); //Backup the values if there are any
andrewboyson 0:22b158d3c76f 166 return 0;
andrewboyson 0:22b158d3c76f 167 }