Utility library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependents:   mtsas mtsas thermostat_fan_demo--fan mtsas ... more

NOTE: MTS-Utils has moved to GitHub. This version will not be updated. For updates, go to the GitHub version.

Files at this revision

API Documentation at this revision

Comitter:
Mike Fiore
Date:
Mon Jun 08 15:30:12 2015 -0500
Parent:
12:7818d55b35c6
Child:
14:1d88cf5266c8
Commit message:
update to 25a04c8ca1d5f2d503bf327e58caa949b8f1778a of internal repo

Changed in this revision

MTSCircularBuffer.cpp Show annotated file Show diff for this revision Revisions of this file
MTSLog.cpp Show annotated file Show diff for this revision Revisions of this file
MTSLog.h Show annotated file Show diff for this revision Revisions of this file
MTSText.cpp Show annotated file Show diff for this revision Revisions of this file
MTSText.h Show annotated file Show diff for this revision Revisions of this file
--- a/MTSCircularBuffer.cpp	Mon Mar 23 16:37:56 2015 -0500
+++ b/MTSCircularBuffer.cpp	Mon Jun 08 15:30:12 2015 -0500
@@ -20,8 +20,10 @@
         if (readIndex == bufferSize) {
             readIndex = 0;
         }
+        __disable_irq();
         data[i++] = buffer[readIndex++];
         bytes--;
+        __enable_irq();
         checkThreshold();
     }
     return i;
@@ -35,8 +37,10 @@
     if (readIndex == bufferSize) {
         readIndex = 0;
     }
+    __disable_irq();
     data = buffer[readIndex++];
     bytes--;
+    __enable_irq();
     checkThreshold();
     return 1;
 }
@@ -48,8 +52,10 @@
         if(writeIndex == bufferSize) {
             writeIndex = 0;
         }
+        __disable_irq();
         buffer[writeIndex++] = data[i++];
         bytes++;
+        __enable_irq();
         checkThreshold();
     }
     return i;
@@ -63,8 +69,10 @@
     if(writeIndex == bufferSize) {
         writeIndex = 0;
     }
+    __disable_irq();
     buffer[writeIndex++] = data;
     bytes++;
+    __enable_irq();
     checkThreshold();
     return 1;
 }
--- a/MTSLog.cpp	Mon Mar 23 16:37:56 2015 -0500
+++ b/MTSLog.cpp	Mon Jun 08 15:30:12 2015 -0500
@@ -14,7 +14,7 @@
 const char* MTSLog::DEBUG_LABEL = "DEBUG";
 const char* MTSLog::TRACE_LABEL = "TRACE";
 
-void MTSLog::printMessage(MTSLog::logLevel level, const char* format, ...) {
+void MTSLog::printMessage(int level, const char* format, ...) {
     if (printable(level)) {
         va_list argptr;
         va_start(argptr, format);
@@ -23,11 +23,16 @@
     }
 }
 
-bool MTSLog::printable(MTSLog::logLevel level) {
+bool MTSLog::printable(int level) {
     return level <= currentLevel;
 }
 
-void MTSLog::setLogLevel(MTSLog::logLevel level) {
+void MTSLog::setLogLevel(int level) {
+    if (level < NONE_LEVEL)
+        currentLevel = NONE_LEVEL;
+    else if (level > TRACE_LEVEL)
+        currentLevel = TRACE_LEVEL;
+    else
     currentLevel = level;
 }
 
--- a/MTSLog.h	Mon Mar 23 16:37:56 2015 -0500
+++ b/MTSLog.h	Mon Jun 08 15:30:12 2015 -0500
@@ -49,17 +49,17 @@
 
     /** Print log message.
      */
-    static void printMessage(MTSLog::logLevel level, const char* format, ...);
+    static void printMessage(int level, const char* format, ...);
 
     /** Determine if the given level is currently printable.
      */
-    static bool printable(MTSLog::logLevel level);
+    static bool printable(int level);
 
     /** Set log level
      * Messages with lower priority than the current level will not be printed.
      * If the level is set to NONE, no messages will print.
      */
-    static void setLogLevel(MTSLog::logLevel level);
+    static void setLogLevel(int level);
 
     /** Get the current log level.
      */
--- a/MTSText.cpp	Mon Mar 23 16:37:56 2015 -0500
+++ b/MTSText.cpp	Mon Jun 08 15:30:12 2015 -0500
@@ -1,5 +1,6 @@
 #include "MTSText.h"
 #include "ctype.h"
+#include <math.h>
 
 using namespace mts;
 
@@ -46,8 +47,8 @@
     std::string result = std::string(index, length);
     index += length;
     return result;
-}
-
+}
+
 std::string Text::toUpper(const std::string str)
 {
     std::string ret = str;
@@ -59,3 +60,52 @@
 
     return ret;
 }
+
+std::string Text::float2String(double val, int precision) {
+    char buff[100];
+    sprintf(buff, "%d.%d", (int)val, (int)((val - floor(val)) * (int)pow(10.0, precision)));
+    return std::string(buff);
+}
+
+std::string Text::bin2hexString(const std::vector<uint8_t>& data, const char* delim, bool leadingZeros) {
+    uint8_t data_arr[data.size()];
+
+    for (int i = 0; i < data.size(); i++)
+        data_arr[i] = data[i];
+
+    return bin2hexString(data_arr, data.size(), delim, leadingZeros);
+}
+
+std::string Text::bin2hexString(const uint8_t* data, const uint32_t len, const char* delim, bool leadingZeros) {
+    std::string str;
+    char buf[32];
+    char lead[] = "0x";
+
+    for (uint32_t i = 0; i < len; i++) {
+        if (leadingZeros)
+            str.append(lead);
+        snprintf(buf, sizeof(buf), "%02x", data[i]);
+        str.append(buf, strlen(buf));
+        if (i < len - 1)
+            str.append(delim);
+    }
+
+    return str;
+}
+
+void Text::ltrim(std::string& str, const char* args) {
+    size_t startpos = str.find_first_not_of(args);
+    if (startpos != std::string::npos)
+        str = str.substr(startpos);
+}
+
+void Text::rtrim(std::string& str, const char* args) {
+    size_t endpos = str.find_last_not_of(args);
+    if (endpos != std::string::npos)
+        str = str.substr(0, endpos + 1);
+}
+
+void Text::trim(std::string& str, const char* args) {
+    ltrim(str, args);
+    rtrim(str, args);
+}
--- a/MTSText.h	Mon Mar 23 16:37:56 2015 -0500
+++ b/MTSText.h	Mon Jun 08 15:30:12 2015 -0500
@@ -4,6 +4,9 @@
 #include <string>
 #include <vector>
 #include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
 
 namespace mts
 {
@@ -49,7 +52,19 @@
     static std::string readString(char* index, int length);
 
     static std::string toUpper(const std::string str);
+
+    std::string float2String(double val, int precision);
+
+    static std::string bin2hexString(const std::vector<uint8_t>& data, const char* delim = "", bool leadingZeros = false);
 
+    static std::string bin2hexString(const uint8_t* data, const uint32_t len, const char* delim = "", bool leadingZeros = false);
+
+    static void ltrim(std::string& str, const char* args);
+
+    static void rtrim(std::string& str, const char* args);
+
+    static void trim(std::string& str, const char* args);
+
 private:
     // Safety for class with only static methods
     Text();