Andrew Boyson / log

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

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

Changed in this revision

log.cpp Show annotated file Show diff for this revision Revisions of this file
log.h Show annotated file Show diff for this revision Revisions of this file
--- a/log.cpp	Tue May 09 15:22:21 2017 +0000
+++ b/log.cpp	Thu Jun 29 19:41:08 2017 +0000
@@ -6,56 +6,55 @@
 static char* pPush; //Initialised in init
 static char* pPull; //Initialised in init
 static int enable = true;
+
+Serial LogUart(USBTX, USBRX);
+
 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) LogUart.putc(c);
+}
 void LogPush(char c)
 {
     //Only add if allowed
     if (!enable) return;
     
-    char* pNext;
-    
-    //Work out what has to be sent
+    //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;
     
-    //Move the pull position if about to run into it
-    pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
-    if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH);
+    //Push the delimiter or the character
+    if (delimited) push('^');
+    else           push(c);
     
-    //Add the character at the push position
-    if (delimited) *pPush = '^';
-    else           *pPush = c;
-    pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
-    
+    //Stop if its not delimited
     if (!delimited) return;
     
-    //Move the pull position if about to run into it
-    pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
-    if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH);
-    
-    //Add the character at the push position
+    //Push the first digit
     char h = c >> 4;
     if (h < 10) h += '0';
     else        h += 'A' - 10;
-    *pPush = h;
-    pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
-    
-    //Move the pull position if about to run into it
-    pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
-    if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH);
-    
-    //Add the character at the push position
+    push(h);
+        
+    //Push the second digit
     h = c & 0x0F;
     if (h < 10) h += '0';
     else        h += 'A' - 10;
-    *pPush = h;
-    pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
+    push(h);
     
 }
 static char *pEnumerate;
@@ -76,6 +75,7 @@
 }
 int LogInit()
 {
+    LogUart.baud(115200);
     pPush = buffer;
     pPull = buffer;
     return 0;
--- a/log.h	Tue May 09 15:22:21 2017 +0000
+++ b/log.h	Thu Jun 29 19:41:08 2017 +0000
@@ -1,4 +1,4 @@
-extern int  LogInit(void);
+extern int  LogInit();
 extern void LogV(char *fmt, va_list argptr);
 extern void LogF(char *fmt, ...);
 extern void LogCrLf(char *);