Log

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Jun 29 19:41:08 2017 +0000
Revision:
6:b98aa0ec75a1
Parent:
5:a1f0298e0995
Child:
7:417a6a65e942
Added serial usb uart so that log could be viewed on pc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:9907e344c82a 1 #include "mbed.h"
andrewboyson 1:4d81afe3859e 2 #include "time.h"
andrewboyson 0:9907e344c82a 3
andrewboyson 0:9907e344c82a 4 #define BUFFER_LENGTH 4096
andrewboyson 0:9907e344c82a 5 static char buffer[BUFFER_LENGTH];
andrewboyson 0:9907e344c82a 6 static char* pPush; //Initialised in init
andrewboyson 0:9907e344c82a 7 static char* pPull; //Initialised in init
andrewboyson 0:9907e344c82a 8 static int enable = true;
andrewboyson 6:b98aa0ec75a1 9
andrewboyson 6:b98aa0ec75a1 10 Serial LogUart(USBTX, USBRX);
andrewboyson 6:b98aa0ec75a1 11
andrewboyson 0:9907e344c82a 12 static char* incrementPushPullPointer(char* p, char* buffer, int bufferLength)
andrewboyson 0:9907e344c82a 13 {
andrewboyson 0:9907e344c82a 14 p++; //increment the pointer by one
andrewboyson 0:9907e344c82a 15 if (p == buffer + bufferLength) p = buffer; //if the pointer is now beyond the end then point it back to the start
andrewboyson 0:9907e344c82a 16 return p;
andrewboyson 0:9907e344c82a 17 }
andrewboyson 6:b98aa0ec75a1 18 static void push(char c)
andrewboyson 6:b98aa0ec75a1 19 {
andrewboyson 6:b98aa0ec75a1 20 //Move the pull position if about to run into it
andrewboyson 6:b98aa0ec75a1 21 char* pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
andrewboyson 6:b98aa0ec75a1 22 if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH);
andrewboyson 6:b98aa0ec75a1 23
andrewboyson 6:b98aa0ec75a1 24 //Add the character at the push position
andrewboyson 6:b98aa0ec75a1 25 *pPush = c;
andrewboyson 6:b98aa0ec75a1 26 pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
andrewboyson 6:b98aa0ec75a1 27
andrewboyson 6:b98aa0ec75a1 28 if (LogUart) LogUart.putc(c);
andrewboyson 6:b98aa0ec75a1 29 }
andrewboyson 0:9907e344c82a 30 void LogPush(char c)
andrewboyson 0:9907e344c82a 31 {
andrewboyson 0:9907e344c82a 32 //Only add if allowed
andrewboyson 0:9907e344c82a 33 if (!enable) return;
andrewboyson 0:9907e344c82a 34
andrewboyson 6:b98aa0ec75a1 35 //Work out if the character needs to be delimited
andrewboyson 2:ee4ec2d72525 36 bool delimited = false;
andrewboyson 2:ee4ec2d72525 37 if (c < ' ' && c != '\r' && c != '\n') delimited = true;
andrewboyson 2:ee4ec2d72525 38 if (c > 126) delimited = true;
andrewboyson 2:ee4ec2d72525 39
andrewboyson 6:b98aa0ec75a1 40 //Push the delimiter or the character
andrewboyson 6:b98aa0ec75a1 41 if (delimited) push('^');
andrewboyson 6:b98aa0ec75a1 42 else push(c);
andrewboyson 2:ee4ec2d72525 43
andrewboyson 6:b98aa0ec75a1 44 //Stop if its not delimited
andrewboyson 2:ee4ec2d72525 45 if (!delimited) return;
andrewboyson 2:ee4ec2d72525 46
andrewboyson 6:b98aa0ec75a1 47 //Push the first digit
andrewboyson 2:ee4ec2d72525 48 char h = c >> 4;
andrewboyson 2:ee4ec2d72525 49 if (h < 10) h += '0';
andrewboyson 2:ee4ec2d72525 50 else h += 'A' - 10;
andrewboyson 6:b98aa0ec75a1 51 push(h);
andrewboyson 6:b98aa0ec75a1 52
andrewboyson 6:b98aa0ec75a1 53 //Push the second digit
andrewboyson 2:ee4ec2d72525 54 h = c & 0x0F;
andrewboyson 2:ee4ec2d72525 55 if (h < 10) h += '0';
andrewboyson 2:ee4ec2d72525 56 else h += 'A' - 10;
andrewboyson 6:b98aa0ec75a1 57 push(h);
andrewboyson 2:ee4ec2d72525 58
andrewboyson 0:9907e344c82a 59 }
andrewboyson 0:9907e344c82a 60 static char *pEnumerate;
andrewboyson 0:9907e344c82a 61 void LogEnumerateStart()
andrewboyson 0:9907e344c82a 62 {
andrewboyson 0:9907e344c82a 63 pEnumerate = pPull;
andrewboyson 0:9907e344c82a 64 }
andrewboyson 0:9907e344c82a 65 int LogEnumerate()
andrewboyson 0:9907e344c82a 66 {
andrewboyson 0:9907e344c82a 67 if (pEnumerate == pPush) return EOF;
andrewboyson 0:9907e344c82a 68 char c = *pEnumerate;
andrewboyson 0:9907e344c82a 69 pEnumerate = incrementPushPullPointer(pEnumerate, buffer, BUFFER_LENGTH);
andrewboyson 2:ee4ec2d72525 70 return c;
andrewboyson 0:9907e344c82a 71 }
andrewboyson 0:9907e344c82a 72 void LogEnable(int on)
andrewboyson 0:9907e344c82a 73 {
andrewboyson 0:9907e344c82a 74 enable = on;
andrewboyson 0:9907e344c82a 75 }
andrewboyson 0:9907e344c82a 76 int LogInit()
andrewboyson 0:9907e344c82a 77 {
andrewboyson 6:b98aa0ec75a1 78 LogUart.baud(115200);
andrewboyson 0:9907e344c82a 79 pPush = buffer;
andrewboyson 0:9907e344c82a 80 pPull = buffer;
andrewboyson 0:9907e344c82a 81 return 0;
andrewboyson 0:9907e344c82a 82 }
andrewboyson 0:9907e344c82a 83 void LogV(char *fmt, va_list argptr)
andrewboyson 0:9907e344c82a 84 {
andrewboyson 0:9907e344c82a 85 int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required
andrewboyson 0:9907e344c82a 86 char snd[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null
andrewboyson 0:9907e344c82a 87 vsprintf(snd, fmt, argptr); //Fill the new buffer
andrewboyson 0:9907e344c82a 88 for (char* ptr = snd; *ptr; ptr++) LogPush(*ptr); //Send the string to the log buffer
andrewboyson 0:9907e344c82a 89 }
andrewboyson 0:9907e344c82a 90 void LogF(char *fmt, ...)
andrewboyson 0:9907e344c82a 91 {
andrewboyson 0:9907e344c82a 92 va_list argptr;
andrewboyson 0:9907e344c82a 93 va_start(argptr, fmt);
andrewboyson 0:9907e344c82a 94 LogV(fmt, argptr);
andrewboyson 0:9907e344c82a 95 va_end(argptr);
andrewboyson 0:9907e344c82a 96 }
andrewboyson 0:9907e344c82a 97 void LogCrLf (char * text)
andrewboyson 0:9907e344c82a 98 {
andrewboyson 0:9907e344c82a 99 LogF("%s\r\n", text);
andrewboyson 0:9907e344c82a 100 }
andrewboyson 0:9907e344c82a 101 static void pushuint4(int value)
andrewboyson 0:9907e344c82a 102 {
andrewboyson 0:9907e344c82a 103 if (value > 9999) { LogPush('+'); LogPush('+'); LogPush('+'); LogPush('+'); }
andrewboyson 0:9907e344c82a 104 else if (value < 0) { LogPush('-'); LogPush('-'); LogPush('-'); LogPush('-'); }
andrewboyson 0:9907e344c82a 105 else
andrewboyson 0:9907e344c82a 106 {
andrewboyson 0:9907e344c82a 107 div_t divres;
andrewboyson 0:9907e344c82a 108 int k, c, t, u;
andrewboyson 0:9907e344c82a 109 divres = div(value , 10); u = divres.rem;
andrewboyson 0:9907e344c82a 110 divres = div(divres.quot, 10); t = divres.rem;
andrewboyson 0:9907e344c82a 111 divres = div(divres.quot, 10); c = divres.rem;
andrewboyson 0:9907e344c82a 112 k = divres.quot;
andrewboyson 0:9907e344c82a 113 LogPush(k + '0'); LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
andrewboyson 0:9907e344c82a 114 }
andrewboyson 0:9907e344c82a 115 }
andrewboyson 0:9907e344c82a 116 static void pushuint3(int value)
andrewboyson 0:9907e344c82a 117 {
andrewboyson 0:9907e344c82a 118 if (value > 999) { LogPush('+'); LogPush('+'); LogPush('+'); }
andrewboyson 0:9907e344c82a 119 else if (value < 0) { LogPush('-'); LogPush('-'); LogPush('-'); }
andrewboyson 0:9907e344c82a 120 else
andrewboyson 0:9907e344c82a 121 {
andrewboyson 0:9907e344c82a 122 div_t divres;
andrewboyson 0:9907e344c82a 123 int c, t, u;
andrewboyson 0:9907e344c82a 124 divres = div(value , 10); u = divres.rem;
andrewboyson 0:9907e344c82a 125 divres = div(divres.quot, 10); t = divres.rem;
andrewboyson 0:9907e344c82a 126 c = divres.quot;
andrewboyson 0:9907e344c82a 127 LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
andrewboyson 0:9907e344c82a 128 }
andrewboyson 0:9907e344c82a 129 }
andrewboyson 0:9907e344c82a 130 static void pushuint2(int value)
andrewboyson 0:9907e344c82a 131 {
andrewboyson 0:9907e344c82a 132 if (value > 99) { LogPush('+'); LogPush('+'); }
andrewboyson 0:9907e344c82a 133 else if (value < 0) { LogPush('-'); LogPush('-'); }
andrewboyson 0:9907e344c82a 134 else
andrewboyson 0:9907e344c82a 135 {
andrewboyson 0:9907e344c82a 136 div_t divres;
andrewboyson 0:9907e344c82a 137 int t, u;
andrewboyson 0:9907e344c82a 138 divres = div(value , 10); u = divres.rem;
andrewboyson 0:9907e344c82a 139 t = divres.quot;
andrewboyson 0:9907e344c82a 140 LogPush(t + '0'); LogPush(u + '0');
andrewboyson 0:9907e344c82a 141 }
andrewboyson 0:9907e344c82a 142 }
andrewboyson 0:9907e344c82a 143 void LogTime()
andrewboyson 0:9907e344c82a 144 {
andrewboyson 0:9907e344c82a 145 struct tm tm;
andrewboyson 1:4d81afe3859e 146 TimeToTmUtc(time(NULL), &tm);
andrewboyson 0:9907e344c82a 147
andrewboyson 0:9907e344c82a 148 pushuint4(tm.tm_year + 1900);
andrewboyson 0:9907e344c82a 149 LogPush('-');
andrewboyson 0:9907e344c82a 150 pushuint3(tm.tm_yday + 1);
andrewboyson 0:9907e344c82a 151 LogPush(' ');
andrewboyson 0:9907e344c82a 152 pushuint2(tm.tm_hour);
andrewboyson 0:9907e344c82a 153 LogPush(':');
andrewboyson 0:9907e344c82a 154 pushuint2(tm.tm_min);
andrewboyson 0:9907e344c82a 155 LogPush(':');
andrewboyson 0:9907e344c82a 156 pushuint2(tm.tm_sec);
andrewboyson 0:9907e344c82a 157 }
andrewboyson 0:9907e344c82a 158 void LogTimeCrLf(char * text)
andrewboyson 0:9907e344c82a 159 {
andrewboyson 0:9907e344c82a 160 LogTime();
andrewboyson 0:9907e344c82a 161 LogF(" %s\r\n", text);
andrewboyson 0:9907e344c82a 162 }
andrewboyson 0:9907e344c82a 163 void LogTimeF(char *fmt, ...)
andrewboyson 0:9907e344c82a 164 {
andrewboyson 0:9907e344c82a 165 va_list argptr;
andrewboyson 0:9907e344c82a 166 va_start(argptr, fmt);
andrewboyson 0:9907e344c82a 167 LogTime();
andrewboyson 0:9907e344c82a 168 LogPush(' ');
andrewboyson 0:9907e344c82a 169 LogV(fmt, argptr);
andrewboyson 0:9907e344c82a 170 va_end(argptr);
andrewboyson 0:9907e344c82a 171 }
andrewboyson 3:7806ff3f1a2d 172 void LogSave(FILE *fp)
andrewboyson 0:9907e344c82a 173 {
andrewboyson 0:9907e344c82a 174 LogEnumerateStart();
andrewboyson 0:9907e344c82a 175 while(1)
andrewboyson 0:9907e344c82a 176 {
andrewboyson 0:9907e344c82a 177 int c = LogEnumerate();
andrewboyson 0:9907e344c82a 178 if (c == EOF) break;
andrewboyson 0:9907e344c82a 179 putc(c, fp);
andrewboyson 0:9907e344c82a 180 }
andrewboyson 0:9907e344c82a 181 }