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