DS18B20 uSD data logger with MAX32630FTHR

Dependencies:   OneWire SDFileSystem max32630fthr mbed

Fork of MAX32630FTHR_iButton_uSD_Logger by Justin Jordan

Revision:
2:9e45f00fd2de
Parent:
0:0cdbc206e85f
--- a/main.cpp	Fri Feb 17 19:21:02 2017 +0000
+++ b/main.cpp	Fri Feb 17 22:23:17 2017 +0000
@@ -27,14 +27,14 @@
 #include "OneWire.h"
 #include "max32630fthr.h"
 #include "SDFileSystem.h"
-#include "Sharp_LS012B7DD01.h"
+
+#define TERM_HOME "\033[H"
+#define TERM_CLEAR_FROM_CURSOR "\033[0J"
+#define TERM_CLEAR_EOL "\033[K"
 
 using namespace OneWire;
 using namespace RomCommands;
 
-uint8_t printMessage(SharpLS012B7DD01 &lcd, const char * msg, uint8_t ln);
-uint8_t printRomId(SharpLS012B7DD01 &lcd, RomId &romId, uint8_t ln);
-
 
 Ticker oneSecondTicker;
 volatile uint32_t secondCounter = 0;
@@ -59,70 +59,58 @@
     MAX32630FTHR pegasus;
     pegasus.init(MAX32630FTHR::VIO_3V3);
     
-    
     //Turn RGB LED off
     DigitalOut rLED(LED1, LED_ON);
     DigitalOut gLED(LED2, LED_OFF);
     DigitalOut bLED(LED3, LED_OFF);
     
-    
-    //Get LCD instance
-    SPI spiBus(P5_1, P5_2, P5_0);
-    SharpLS012B7DD01 lcd(P5_3, P5_4, P5_5, spiBus);
-    printMessage(lcd, "Starting...", 0);
-    wait(2.0);
-    lcd.clear_display();
-    
+    //Clear terminal screen
+    printf(TERM_HOME);
+    printf(TERM_CLEAR_FROM_CURSOR);
     
     //Get 1-Wire Master (owm) instance
-    #if defined(TARGET_MAX32630FTHR)
-        MCU_OWM owm(true, true);
-    #else //some other FTHR on mbed?
-        //Replace with appropriate pin names for your FTHR
-        I2C i2cBus(P3_4, P3_5); 
-        DS2484 owm(i2cBus);
-    #endif
+    //         (extWeakPup, extStrongPup)
+    MCU_OWM owm(false, true);
     
     //Make sure owm is initialized
     OneWireMaster::CmdResult result = owm.OWInitMaster();
     while(result != OneWireMaster::Success)
     {
-        printMessage(lcd, "Failed to init OWM...", 0);
+        printf("Failed to init OWM...\r\n\r\n");
         result = owm.OWInitMaster();
         wait(0.5);
     }
-    lcd.clear_display();
-    printMessage(lcd, "OWM Initialized...", 0);
+    printf("OWM Initialized...\r\n");
     wait(2.0);
-    lcd.clear_display();
+    printf(TERM_HOME);
+    printf(TERM_CLEAR_FROM_CURSOR);
     
     
-    //Check for DS1920 iButton
+    //Check for DS18B20
     SearchState search_state;
-    search_state.findFamily(0x10);
+    search_state.findFamily(0x28);
     do
     {
         result = OWNext(owm, search_state);
-        if(search_state.romId.familyCode() != 0x10)
+        if(search_state.romId.familyCode() != 0x28)
         {
-            printMessage(lcd, "Failed to find DS1920...", 0);
-            printMessage(lcd, "Please connect DS1920...", 1);
+            printf("Failed to find DS18B20...\r\n");
+            printf("Please connect DS18B20...\r\n\r\n");
             wait(0.5);
         }
         else
         {
-            lcd.clear_display();
-            printMessage(lcd, "Found DS1920...", 0);
-            printRomId(lcd, search_state.romId, 1);
-            wait(2.0);
-            lcd.clear_display();
+            printf("Found DS18B20...\r\n");
         }
     }
-    while(search_state.romId.familyCode() != 0x10);
+    while(search_state.romId.familyCode() != 0x28);
+    wait(2.0);
+    printf(TERM_HOME);
+    printf(TERM_CLEAR_FROM_CURSOR);
     
-    //Get instance of DS1920 object
+    //Get instance of DS18B20 object
     MultidropRomIterator selector(owm);
-    DS1920 tempSensor(selector);
+    DS18B20 tempSensor(selector);
     tempSensor.setRomId(search_state.romId);
     
     
@@ -138,10 +126,9 @@
     //Check for card
     if(uSDdetect)
     {
-        printMessage(lcd, "Please insert uSD card", 0);
+        printf("Please insert uSD card\r\n");
         while(uSDdetect);
     }
-    lcd.clear_display();
     
     rLED = LED_ON;
     gLED = LED_ON;
@@ -150,26 +137,25 @@
         fp = fopen(FILE_NAME, "w");
         if(fp != NULL)
         {
-            lcd.clear_display();
-            printMessage(lcd, "Card detected...", 0);
-            printMessage(lcd, "Preparing log file...", 1);
+            printf("Card detected...\r\n");
+            printf("Preparing log file...\r\n");
             fprintf(fp, "Time-Stamp, Battery Voltage, Temperature\n");
             fclose(fp);
         }
         else
         {
-            lcd.clear_display();
-            printMessage(lcd, "Failed to open file...", 0);
-            printMessage(lcd, "Press Reset Button...", 1);
-            wait(2.0);
+            printf("Failed to open file...\r\n");
+            printf("Press Reset Button...\r\n\r\n");
+            wait(0.5);
         }
     }
     while(fp == NULL);
     rLED = LED_OFF;
     gLED = LED_ON;
+    
     wait(2.0);
-    lcd.clear_display();
-    
+    printf(TERM_HOME);
+    printf(TERM_CLEAR_FROM_CURSOR);
     
     enum UiStates
     {
@@ -227,13 +213,19 @@
             if(!firstSampleTaken)
             {
                 firstSampleTaken = true;
-                lcd.clear_display();
+                printf(TERM_HOME);
+                printf(TERM_CLEAR_FROM_CURSOR);
                 oldState = currentState;
-                
-                printMessage(lcd, "Logging Data...", 0);
+                    
                 pegasus.getBatteryVoltage(&batteryVoltage);
                 tempSensor.convertTemperature(temperature);
                 
+                printf("Logging Data...\r\n\r\n");
+                printf("Timestamp: %d (seconds)\r\n\r\n", sampleTime);
+                printf("Battery Voltage = %s%3.2f\r\n\r\n", TERM_CLEAR_EOL, batteryVoltage);
+                printf("Temperature = %s%5.2f\r\n\r\n", TERM_CLEAR_EOL, temperature);
+                printf(TERM_HOME);
+                
                 rLED = LED_ON;
                 fp = fopen(FILE_NAME, "a");
                 if(fp != NULL)
@@ -243,10 +235,11 @@
                 }
                 else
                 {
-                    lcd.clear_display();
-                    printMessage(lcd, "Failed to open file", 0);
-                    printMessage(lcd, "Press Reset Button...", 1);
-                    printMessage(lcd, "MBED DIE!!...", 2);
+                    printf(TERM_HOME);
+                    printf(TERM_CLEAR_FROM_CURSOR);
+                    printf("Failed to open file\r\n");
+                    printf("Press Reset Button...\r\n");
+                    printf("MBED DIE!!...\r\n");
                     mbed_die();
                 }
                 rLED = LED_OFF;
@@ -256,7 +249,8 @@
         //If state has changed, clear dispaly
         if(oldState != currentState)
         {
-            lcd.clear_display();
+            printf(TERM_HOME);
+            printf(TERM_CLEAR_FROM_CURSOR);
             oldState = currentState;
         }
         
@@ -264,17 +258,19 @@
         switch(currentState)
         {
             case NoLog_NoCardDetected:
-                printMessage(lcd, "Please insert uSD card...", 0);
+                printf("Please insert uSD card...\r\n");
+                printf(TERM_HOME);
             break;
             
             case NoLog_CardDetected:
-                printMessage(lcd, "Press SW2 to start logging", 0);
-                printMessage(lcd, "data...", 1);
+                printf("Press SW2 to start logging data...\r\n");
+                printf(TERM_HOME);
             break;
             
             case Log_NoCardDetected:
                 startLog = false;
-                printMessage(lcd, "Please insert uSD card", 0);
+                printf("Please insert uSD card\r\n");
+                printf(TERM_HOME);
             break;
             
             case Log_CardDetected:
@@ -284,10 +280,15 @@
                     secondCounter = 0;
                     sampleTime += SAMPLE_INTERVAL;
                     
-                    printMessage(lcd, "Logging Data...", 0);
                     pegasus.getBatteryVoltage(&batteryVoltage);
                     tempSensor.convertTemperature(temperature);
                     
+                    printf("Logging Data...\r\n\r\n");
+                    printf("Timestamp: %d (seconds)\r\n\r\n", sampleTime);
+                    printf("Battery Voltage = %s%3.2f\r\n\r\n", TERM_CLEAR_EOL, batteryVoltage);
+                    printf("Temperature = %s%5.2f\r\n\r\n", TERM_CLEAR_EOL, temperature);
+                    printf(TERM_HOME);
+                    
                     rLED = LED_ON;
                     fp = fopen(FILE_NAME, "a");
                     if(fp != NULL)
@@ -297,10 +298,11 @@
                     }
                     else
                     {
-                        lcd.clear_display();
-                        printMessage(lcd, "Failed to open file", 0);
-                        printMessage(lcd, "Press Reset Button...", 1);
-                        printMessage(lcd, "MBED DIE!!...", 2);
+                        printf(TERM_HOME);
+                        printf(TERM_CLEAR_FROM_CURSOR);
+                        printf("Failed to open file\r\n");
+                        printf("Press Reset Button...\r\n");
+                        printf("MBED DIE!!...\r\n");
                         mbed_die();
                     }
                     rLED = LED_OFF;
@@ -309,99 +311,13 @@
             break;
             
             default:
-                lcd.clear_display();
-                printMessage(lcd, "Bad State...", 0);
-                printMessage(lcd, "Press Reset Button...", 1);
-                printMessage(lcd, "MBED DIE!!...", 2);
+                printf(TERM_HOME);
+                printf(TERM_CLEAR_FROM_CURSOR);
+                printf("Bad State\r\n");
+                printf("Press Reset Button...\r\n");
+                printf("MBED DIE!!...\r\n");
                 mbed_die();
             break;
         }
     }    
 }
-
-
-//*********************************************************************
-uint8_t printMessage(SharpLS012B7DD01 &lcd, const char * msg, uint8_t ln)
-{
-    char temp_buff[31];
-    uint8_t buf_idx = 0;
-    temp_buff[buf_idx++] = '>';
-    temp_buff[buf_idx++] = '>';
-    for(; buf_idx < 30; buf_idx++)
-    {
-        temp_buff[buf_idx] = ' ';
-    }
-    buf_idx = 2;
-    
-    if(strlen(msg) <= 28)
-    {
-        memcpy((temp_buff + buf_idx), msg, strlen(msg));
-        temp_buff[buf_idx + strlen(msg)] = NULL;
-    
-        if(ln > 3)
-        {
-            lcd.clear_display();
-            ln = 0;
-        }
-        lcd.print_str(ln++, 0, temp_buff);
-    }
-    return ln;
-}
-
-
-//*********************************************************************
-uint8_t printRomId(SharpLS012B7DD01 &lcd, RomId &romId, uint8_t ln)
-{
-    int8_t idx = 7;
-    uint8_t temp_hi, temp_lo;
-    
-    char temp_buff[31];
-    uint8_t buf_idx = 0;
-    temp_buff[buf_idx++] = '>';
-    temp_buff[buf_idx++] = '>';
-    for(; buf_idx < 30; buf_idx++)
-    {
-        temp_buff[buf_idx] = ' ';
-    }
-    buf_idx = 2;
-    
-    do
-    {
-        
-        temp_hi = ((romId.buffer[idx] & 0xF0) >> 4);
-        temp_lo = (romId.buffer[idx--] & 0x0F);
-        
-        //get high nib in ascii
-        if(temp_hi < 10)
-        {
-            temp_buff[buf_idx++] = (temp_hi + 0x30);
-        }
-        else
-        {
-            temp_buff[buf_idx++] = ((temp_hi - 10) + 0x41);
-        }
-        
-        //get low nib in ascii
-        if(temp_lo < 10)
-        {
-            temp_buff[buf_idx++] = (temp_lo + 0x30);
-        }
-        else
-        {
-            temp_buff[buf_idx++] = ((temp_lo - 10) + 0x41);
-        }
-        
-        temp_buff[buf_idx++] = ' ';
-    }
-    while(idx >= 0);
-    temp_buff[buf_idx++] = NULL;
-    
-    if(ln > 3)
-    {
-        lcd.clear_display();
-        ln = 0;
-    }
-    lcd.print_str(ln++, 0, temp_buff);
-    
-    return ln;
-}