Andrew Boyson / log

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Thu Jan 11 17:39:18 2018 +0000
Parent:
11:aff6dd8238ef
Child:
13:fb7f40c2e446
Commit message:
Removed dependence on Mbed OS

Changed in this revision

log.c Show annotated file Show diff for this revision Revisions of this file
log.cpp Show diff for this revision Revisions of this file
log.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/log.c	Thu Jan 11 17:39:18 2018 +0000
@@ -0,0 +1,196 @@
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <time.h>
+#include <stdbool.h>
+#include "uart.h"
+
+#define BUFFER_LENGTH 0x4000
+
+__attribute__((section("AHBSRAM0"))) static char buffer[BUFFER_LENGTH]; //Pop the buffer into the USB area
+static char* pPush; //Initialised in init
+static char* pPull; //Initialised in init
+static char *pUart; //Initialised in init
+
+static bool enable = true;
+
+bool LogUart = true;
+
+static char* incrementPushPullPointer(char* p, char* buffer, int bufferLength)
+{
+    p++; //increment the pointer by one
+    if (p == buffer + bufferLength) p = buffer; //if the pointer is now beyond the end then point it back to the start
+    return p;
+}
+static void push(char c)
+{
+    //Move the pull position if about to run into it
+    char* pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
+    if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH);
+    
+    //Add the character at the push position
+    *pPush = c;
+    pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
+}
+void LogPush(char c)
+{
+    //Only add if allowed
+    if (!enable) return;
+    
+    //Work out if the character needs to be delimited
+    bool delimited = false;
+    if (c < ' ' && c != '\r' && c != '\n') delimited = true;
+    if (c > 126) delimited = false;
+    
+    //Push the delimiter or the character
+    if (delimited) push('^');
+    else           push(c);
+    
+    //Stop if its not delimited
+    if (!delimited) return;
+    
+    //Push the first digit
+    char h = c >> 4;
+    if (h < 10) h += '0';
+    else        h += 'A' - 10;
+    push(h);
+        
+    //Push the second digit
+    h = c & 0x0F;
+    if (h < 10) h += '0';
+    else        h += 'A' - 10;
+    push(h);
+    
+}
+static char *pEnumerate;
+void LogEnumerateStart()
+{
+    pEnumerate = pPull;
+}
+int LogEnumerate()
+{
+    if (pEnumerate == pPush) return -1;
+    char c = *pEnumerate;
+    pEnumerate = incrementPushPullPointer(pEnumerate, buffer, BUFFER_LENGTH); 
+    return c;
+}
+void LogEnable(bool value)
+{
+    enable = value;
+}
+void LogInit()
+{
+    Uart0Init();
+    pPush = buffer;
+    pPull = buffer;
+    pUart = buffer;
+}
+void LogMain()
+{
+    if (!LogUart)       return;    //Do nothing if uart is not enabled
+    if (pUart == pPush) return;    //Do nothing if all characters have been sent
+    int result = Uart0PutC(*pUart); //Attempt to write the character
+    if (result == 0) pUart = incrementPushPullPointer(pUart, buffer, BUFFER_LENGTH); //If the character was written to the uart then move to the next
+}
+int Log(char* snd)
+{
+    char* ptr = snd;
+    while (*ptr) LogPush(*ptr++); //Send the string to the log buffer
+    return ptr - snd;
+}
+int LogV(char *fmt, va_list argptr)
+{
+    int size  = vsnprintf(NULL, 0, fmt, argptr);  //Find the size required
+    char snd[size + 1];                           //Allocate enough memory for the size required with an extra byte for the terminating null
+    vsprintf(snd, fmt, argptr);                   //Fill the new buffer
+    return Log(snd);                              //Send the string to the log buffer
+}
+int LogF(char *fmt, ...)
+{
+    va_list argptr;
+    va_start(argptr, fmt);
+    int size = LogV(fmt, argptr);
+    va_end(argptr);
+    return size;
+}
+static void pushuint4(int value)
+{    
+    if      (value > 9999) { LogPush('+'); LogPush('+'); LogPush('+'); LogPush('+'); }
+    else if (value <    0) { LogPush('-'); LogPush('-'); LogPush('-'); LogPush('-'); }
+    else
+    {
+        div_t divres;
+        int k, c, t, u;
+        divres = div(value      , 10); u = divres.rem;
+        divres = div(divres.quot, 10); t = divres.rem;
+        divres = div(divres.quot, 10); c = divres.rem;
+                                       k = divres.quot;                           
+        LogPush(k + '0'); LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
+    }
+}
+static void pushuint3(int value)
+{
+    if      (value > 999) { LogPush('+'); LogPush('+'); LogPush('+'); }
+    else if (value <   0) { LogPush('-'); LogPush('-'); LogPush('-'); }
+    else
+    {
+        div_t divres;
+        int c, t, u;
+        divres = div(value      , 10); u = divres.rem;
+        divres = div(divres.quot, 10); t = divres.rem;
+                                       c = divres.quot;
+        LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
+    }
+}
+static void pushuint2(int value)
+{
+    if      (value > 99) { LogPush('+'); LogPush('+'); }
+    else if (value <  0) { LogPush('-'); LogPush('-'); }
+    else
+    {
+        div_t divres;
+        int t, u;
+        divres = div(value      , 10); u = divres.rem;
+                                       t = divres.quot;
+        LogPush(t + '0'); LogPush(u + '0');
+    }
+}
+void (*LogTmFunction)(struct tm* ptm);
+
+static int logTimeOnly()
+{
+    if (!LogTmFunction) return 0;
+    
+    struct tm tm;
+    LogTmFunction(&tm);
+    
+    pushuint4(tm.tm_year + 1900);
+    LogPush('-');
+    pushuint3(tm.tm_yday + 1);
+    LogPush(' ');
+    pushuint2(tm.tm_hour);
+    LogPush(':');
+    pushuint2(tm.tm_min);
+    LogPush(':');
+    pushuint2(tm.tm_sec);
+    return 17;
+}
+int LogTime(char *snd)
+{
+    int size = 0;
+    size += logTimeOnly();
+    size++; LogPush(' ');
+    size += Log(snd);
+    return size;
+}
+int LogTimeF(char *fmt, ...)
+{
+    int size = 0;
+    va_list argptr;
+    va_start(argptr, fmt);
+    size == logTimeOnly();
+    size++; LogPush(' ');
+    size += LogV(fmt, argptr);
+    va_end(argptr);
+    return size;
+}
--- a/log.cpp	Sat Dec 16 14:41:46 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,181 +0,0 @@
-#include "mbed.h"
-#include "clock.h"
-
-#define BUFFER_LENGTH 4096
-static char buffer[BUFFER_LENGTH];
-static char* pPush; //Initialised in init
-static char* pPull; //Initialised in init
-static int enable = true;
-
-static Serial uart(USBTX, USBRX);
-bool LogUart = false;
-
-static char* incrementPushPullPointer(char* p, char* buffer, int bufferLength)
-{
-    p++; //increment the pointer by one
-    if (p == buffer + bufferLength) p = buffer; //if the pointer is now beyond the end then point it back to the start
-    return p;
-}
-static void push(char c)
-{
-    //Move the pull position if about to run into it
-    char* pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
-    if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH);
-    
-    //Add the character at the push position
-    *pPush = c;
-    pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
-    
-    if (LogUart) uart.putc(c);
-}
-void LogPush(char c)
-{
-    //Only add if allowed
-    if (!enable) return;
-    
-    //Work out if the character needs to be delimited
-    bool delimited = false;
-    if (c < ' ' && c != '\r' && c != '\n') delimited = true;
-    if (c > 126) delimited = true;
-    
-    //Push the delimiter or the character
-    if (delimited) push('^');
-    else           push(c);
-    
-    //Stop if its not delimited
-    if (!delimited) return;
-    
-    //Push the first digit
-    char h = c >> 4;
-    if (h < 10) h += '0';
-    else        h += 'A' - 10;
-    push(h);
-        
-    //Push the second digit
-    h = c & 0x0F;
-    if (h < 10) h += '0';
-    else        h += 'A' - 10;
-    push(h);
-    
-}
-static char *pEnumerate;
-void LogEnumerateStart()
-{
-    pEnumerate = pPull;
-}
-int LogEnumerate()
-{
-    if (pEnumerate == pPush) return EOF;
-    char c = *pEnumerate;
-    pEnumerate = incrementPushPullPointer(pEnumerate, buffer, BUFFER_LENGTH); 
-    return c;
-}
-void LogEnable(int on)
-{
-    enable = on;
-}
-int LogInit()
-{
-    uart.baud(115200);
-    pPush = buffer;
-    pPull = buffer;
-    return 0;
-}
-int Log(char* snd)
-{
-    char* ptr = snd;
-    while (*ptr) LogPush(*ptr++); //Send the string to the log buffer
-    return ptr - snd;
-}
-int LogV(char *fmt, va_list argptr)
-{
-    int size  = vsnprintf(NULL, 0, fmt, argptr);  //Find the size required
-    char snd[size + 1];                           //Allocate enough memory for the size required with an extra byte for the terminating null
-    vsprintf(snd, fmt, argptr);                   //Fill the new buffer
-    return Log(snd);                              //Send the string to the log buffer
-}
-int LogF(char *fmt, ...)
-{
-    va_list argptr;
-    va_start(argptr, fmt);
-    int size = LogV(fmt, argptr);
-    va_end(argptr);
-    return size;
-}
-static void pushuint4(int value)
-{    
-    if      (value > 9999) { LogPush('+'); LogPush('+'); LogPush('+'); LogPush('+'); }
-    else if (value <    0) { LogPush('-'); LogPush('-'); LogPush('-'); LogPush('-'); }
-    else
-    {
-        div_t divres;
-        int k, c, t, u;
-        divres = div(value      , 10); u = divres.rem;
-        divres = div(divres.quot, 10); t = divres.rem;
-        divres = div(divres.quot, 10); c = divres.rem;
-                                       k = divres.quot;                           
-        LogPush(k + '0'); LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
-    }
-}
-static void pushuint3(int value)
-{
-    if      (value > 999) { LogPush('+'); LogPush('+'); LogPush('+'); }
-    else if (value <   0) { LogPush('-'); LogPush('-'); LogPush('-'); }
-    else
-    {
-        div_t divres;
-        int c, t, u;
-        divres = div(value      , 10); u = divres.rem;
-        divres = div(divres.quot, 10); t = divres.rem;
-                                       c = divres.quot;
-        LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
-    }
-}
-static void pushuint2(int value)
-{
-    if      (value > 99) { LogPush('+'); LogPush('+'); }
-    else if (value <  0) { LogPush('-'); LogPush('-'); }
-    else
-    {
-        div_t divres;
-        int t, u;
-        divres = div(value      , 10); u = divres.rem;
-                                       t = divres.quot;
-        LogPush(t + '0'); LogPush(u + '0');
-    }
-}
-static int logTimeOnly()
-{
-    struct tm tm;
-    ClockTmUtc(&tm);
-    
-    pushuint4(tm.tm_year + 1900);
-    LogPush('-');
-    pushuint3(tm.tm_yday + 1);
-    LogPush(' ');
-    pushuint2(tm.tm_hour);
-    LogPush(':');
-    pushuint2(tm.tm_min);
-    LogPush(':');
-    pushuint2(tm.tm_sec);
-    return 17;
-}
-int LogTime(char *snd)
-{
-    int size = 0;
-    size += logTimeOnly();
-    size++; LogPush(' ');
-    size += Log(snd);
-    return size;
-}
-int LogTimeF(char *fmt, ...)
-{
-    int size = 0;
-    va_list argptr;
-    va_start(argptr, fmt);
-    size == logTimeOnly();
-    size++; LogPush(' ');
-    size += LogV(fmt, argptr);
-    va_end(argptr);
-    return size;
-}
--- a/log.h	Sat Dec 16 14:41:46 2017 +0000
+++ b/log.h	Thu Jan 11 17:39:18 2018 +0000
@@ -1,6 +1,10 @@
 #include <stdarg.h>
+#include <time.h>
+#include <stdbool.h>
 
-extern int  LogInit();
+extern void (*LogTmFunction)(struct tm* ptm);
+extern void LogInit(void);
+extern void LogMain(void);
 extern bool LogUart;
 
 extern void LogPush(char c);
@@ -12,4 +16,4 @@
 
 extern void LogEnumerateStart(void);
 extern int  LogEnumerate(void);
-extern void LogEnable(int on);
+extern void LogEnable(bool value);