Upload test

Dependencies:   mbed SDFileSystem RTCtester

Files at this revision

API Documentation at this revision

Comitter:
mc2075
Date:
Tue Jan 08 12:29:18 2019 +0000
Parent:
6:2641d53a460a
Commit message:
Ad

Changed in this revision

RTCtester.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
ds3231.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTCtester.lib	Tue Jan 08 12:29:18 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mc2075/code/RTCtester/#0c31daebd967
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Tue Jan 08 12:29:18 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mc2075/code/SDFileSystem/#36320448d978
--- a/ds3231.lib	Thu Mar 19 21:59:08 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/teams/Maxim-Integrated/code/ds3231/#11630748e2f2
--- a/main.cpp	Thu Mar 19 21:59:08 2015 +0000
+++ b/main.cpp	Tue Jan 08 12:29:18 2019 +0000
@@ -1,199 +1,248 @@
-/**********************************************************************
-* 
-* Demo DS3231 Library
-*
-***********************************************************************
-* Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
-*
-* Permission is hereby granted, free of charge, to any person obtaining a
-* copy of this software and associated documentation files (the "Software"),
-* to deal in the Software without restriction, including without limitation
-* the rights to use, copy, modify, merge, publish, distribute, sublicense,
-* and/or sell copies of the Software, and to permit persons to whom the
-* Software is furnished to do so, subject to the following conditions:
-*
-* The above copyright notice and this permission notice shall be included
-* in all copies or substantial portions of the Software.
-*
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
-* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-* OTHER DEALINGS IN THE SOFTWARE.
-*
-* Except as contained in this notice, the name of Maxim Integrated
-* Products, Inc. shall not be used except as stated in the Maxim Integrated
-* Products, Inc. Branding Policy.
-*
-* The mere transfer of this software does not imply any licenses
-* of trade secrets, proprietary technology, copyrights, patents,
-* trademarks, maskwork rights, or any other form of intellectual
-* property whatsoever. Maxim Integrated Products, Inc. retains all
-* ownership rights.
-**********************************************************************/
+/******************************************************** 
+ * Real Time Clock
+ * By Mollie Coleman - University of Bath, 2018
+ *  
+ * This code uses a pre-defined library created
+ * by Justin Jordan which has specified functions
+ * that can be used to set the time and access it
+ *
+ * Hardware:
+ *     1. mbed LPC1768
+ *     2. DS3231MPMB1 Peripheral Module
+ *     3. CR1025 Battery
+ *     4. 4x Jumper Cables
+ *
+ * Connectivity: (RTC --> mbed)
+ *     J1-3 SCL --> P27
+ *     J1-4 SDA --> P28
+ *     J1-5 GND --> GND
+ *     J1-6 VCC --> VOUT
+ *
+ * References: 
+ *     1. https://os.mbed.com/components/DS3231/
+ *     2. https://os.mbed.com/users/techstep/code
+ *               /DS3231/file/1607610a4ee9/DS3231.cpp/
+ *     3. https://os.mbed.com/handbook/Timer
+ ********************************************************/
+
+/*********************************** 
+ * Importing of the required libs  *
+ ***********************************/
+#include "ds3231.h"
+#include "iostream"
+#include "SDFileSystem.h"
+#include "string"
+#include "sstream"
+#include "inttypes.h"
+#include "stdio.h"
+#define ESC 0x1B
+#define PRIx16 "hx"
+
+/************************************ 
+ * Defining the ports that the RTC  *
+ * and SD reader is attatched to.   *
+ ************************************/
+Ds3231 rtc(p28, p27);
+SDFileSystem sd (p5, p6, p7, p8, "sd");
+
+/************************************
+ * Creating a timer variable to be  * 
+ * used to deal with the time taken *
+ * to save the data                 *
+ ************************************/
+Timer t;
 
-#include "ds3231.h"
-
-#define ESC 0x1B
+/************************************
+ * This can be updated for more     *
+ * variables, or change the type to *
+ * suit the needs. These are the    *
+ * variables to be stored in on the *
+ * SD card in a .txt file           * 
+ ************************************/
+float temperature = 0.0;
+float uvData = 0.0;
+float LUXData = 0.0;
+char buffer[12];
+    
+/********************************************************
+ * settingCalTimeVal Funtion
+ *
+ * Parameters:
+ *     1. ds3231_time_t rtc_time
+ *     2. ds3231_calendar_t rtc_calendar
+ *
+ * This funtion is only used when the time 
+ * needs to be set for the first time. Once
+ * done, this is no longer required as the 
+ * time and date is stored and the RTC keeps 
+ * it up to date. 
+ *
+ * The data is stored in a tempoary variable,
+ * temp. A pointer is used and assigned the 
+ * reference point for that specific piece 
+ * of data.The vale of temp is then stored 
+ * at that location in memory. 
+ *
+ * Once all the assigning and storing of data 
+ * is complete, the time and data are set and 
+ * stored indefinitely.
+ *
+ * rtc.set_time() <- Function defined in ds3231.cpp
+ * rtc.set_calendar() <- Function defined in ds3231.cpp
+ *     If the time/date cannot be set, the function will
+ *     return 1, where the code is then terminated.
+ *
+ * (If the time needs to be updated, the numbers in the 
+ *  below can be altered accordingly)
+ ********************************************************/
+void settingCalTimeVal(ds3231_time_t rtc_time, ds3231_calendar_t rtc_calendar)
+{
+    uint32_t temp;
+    uint32_t* mem;
+    bool* mem2;
+    
+    temp = 17;
+    mem = &rtc_time.hours;
+    *mem = temp;
+    
+    temp = 30;
+    mem = &rtc_time.minutes;
+    *mem = temp;
+    
+    temp = 30;
+    mem = &rtc_time.seconds;
+    *mem = temp;
+    
+    temp = 0;
+    mem2 = &rtc_time.mode;
+    *mem2 = temp;
+    
+    temp = 2;
+    mem = &rtc_calendar.day;
+    *mem = temp;
+    
+    temp = 06;
+    mem = &rtc_calendar.date;
+    *mem = temp;
+    
+    temp = 11;
+    mem = &rtc_calendar.month;
+    *mem = temp;
+    
+    temp = 18;
+    mem = &rtc_calendar.year;
+    *mem = temp;
+    
+    //Setting the time/date and storing it       
+    if(rtc.set_time(rtc_time) || rtc.set_calendar(rtc_calendar))
+    {
+        printf("uh oh");
+        exit(0); //terminates the program
+    }
+}
 
-void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member);
-void get_user_input(char* message, uint8_t min, uint8_t max, bool* member);
+/*
+ * Converts the input into a type String
+ */
+template <typename T>
+std::string to_string(T value)
+{
+    std::ostringstream os;
+    os << value ;
+    return os.str() ;
+}
 
+/***********************************************************
+ * Main Function
+ * 
+ * Calls the function that sets the time it it is needed, 
+ * else it is commented out and the time is retrieved. 
+ *
+ * rtc.get_time() <- Function defined in ds3231.cpp
+ * rtc.get_calendar() <- Function defined in ds3231.cpp
+ *     These are called if the time has already been
+ *     set and we do not want to over-write what has
+ *     been stored in the memory.
+ *
+ * When the mbed is disconencted, it will continue to  
+ * run and update the time. This means, when re-attatched
+ * the time is what it should be, not the last time it was
+ * when it was switched off.  
+ ************************************************************/
 int main(void)
 {
-    //rtc object
-    Ds3231 rtc(D14, D15); 
-    
     time_t epoch_time;
-    
-    //DS3231 rtc variables
-    
-    //default, use bit masks in ds3231.h for desired operation
+
+    /* default, use bit masks in ds3231.h for desired operation */
     ds3231_cntl_stat_t rtc_control_status = {0,0}; 
     ds3231_time_t rtc_time;
     ds3231_calendar_t rtc_calendar;
-    
     rtc.set_cntl_stat_reg(rtc_control_status);
     
-    //get day from user
-    get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 1,
-                    7, &rtc_calendar.day);
-
-    //get day of month from user
-    get_user_input("\nPlease enter day of month (1-31): ", 1, 31, 
-                    &rtc_calendar.date);
-
-    //get month from user
-    get_user_input("\nPlease enter the month, 1 for January (1-12): ", 1, 
-                    12, &rtc_calendar.month);
-
-    //get year from user
-    get_user_input("\nPlease enter the year (0-99): ",0, 99, 
-                    &rtc_calendar.year);
-      
-    //Get time mode
-    get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1, 
-                   &rtc_time.mode);  
+    /* Uncomment if you want to set the time */
+    //settingCalTimeVal(rtc_time, rtc_calendar);
+    
+    rtc.get_time(&rtc_time);
+    rtc.get_calendar(&rtc_calendar);    
+    //std::string CalendarResult = to_string((uint16_t)dates);
+    //printf("Cal: %lu", (unsigned long)dates);
     
-    if(rtc_time.mode)
+    /* 
+     * This if statement checks to see if there is a directory already made on the
+     * sd card and if there is not, then it will create the wanted directory. This
+     * means that any SD card that is plugged in will have this directory created 
+     * automatically. 
+     */
+    DIR* dir = opendir("/sd/data");
+    if(dir)
     {
-        //Get AM/PM status
-        get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1, 
-                       &rtc_time.am_pm);  
-        //Get hour from user
-        get_user_input("\nPlease enter the hour (1-12): ", 1, 12, 
-                       &rtc_time.hours);
+        printf("The directory already exits");  
     }
     else
     {
-        //Get hour from user
-        get_user_input("\nPlease enter the hour (0-23): ", 0, 23, 
-                       &rtc_time.hours);
-    }
-     
-    //Get minutes from user
-    get_user_input("\nPlease enter the minute (0-59): ", 0, 59, 
-                   &rtc_time.minutes);
-    
-    
-    //Get seconds from user
-    get_user_input("\nPlease enter the second (0-59): ", 0, 59, 
-                   &rtc_time.seconds);
-    
-    
-    
-    //Set the time, uses inverted logic for return value
-    if(rtc.set_time(rtc_time))
-    {
-        printf("\nrtc.set_time failed!!\n");
-        exit(0);
-    }
-    
-    //Set the calendar, uses inverted logic for return value
-    if(rtc.set_calendar(rtc_calendar))
-    {
-        printf("\nrtc.set_calendar failed!!\n");
-        exit(0);
+        mkdir("/sd/data", 0777);
+        printf("The directory has been created on the SD card");
     }
     
-    char buffer[32];
+    /*
+     * This section deals with the setting of the file name to be the date. This
+     * is also where the data is stored into the file, in a specific format. 
+     */
+    epoch_time = rtc.get_epoch();
+    strftime(buffer, 32, "%I:%M:%S", localtime(&epoch_time));
     
-    for(;;)
-    {   
-        printf("%c[2J", ESC); //clear screen
-        printf("%c[H", ESC); //move cursor to Home
-        
-        //new epoch time fx
-        epoch_time = rtc.get_epoch();
-        
-        printf("\nTime as seconds since January 1, 1970 = %d\n", epoch_time);
-        
-        printf("\nTime as a basic string = %s", ctime(&epoch_time));
- 
-        strftime(buffer, 32, "%I:%M %p\n", localtime(&epoch_time));
-        printf("\nTime as a custom formatted string = %s", buffer);
-        
-        wait(1.0);
-    }//loop 
-}
-
-
-/**********************************************************************
-* Function: get_user_input() 
-* Parameters: message - user prompt
-*             min - minimum value of input
-*             max - maximum value of input
-*             member - pointer to struct member              
-* Returns: none
-*
-* Description: get time/date input from user
-*
-**********************************************************************/
-void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member)
-{
-    uint32_t temp;
-
-    do
+    struct tm tm = *localtime(&epoch_time);
+    double date  = tm.tm_mday;
+    double month = tm.tm_mon + 1;
+    double year  = tm.tm_year + 1900;
+    std::string dateS = to_string(date);
+    std::string monthS = to_string(month);
+    std::string yearS = to_string(year);
+    
+    //Setting the filename to be the date in the format date-month-year
+    std::string fileNameS = "/sd/data/" + dateS + "-" + monthS + "-" + yearS + ".txt";
+    const char *fileNameC = fileNameS.c_str();
+    FILE *out_file = fopen(fileNameC, "w");
+    
+    if(out_file == NULL)
     {
-        printf("\n%s", message);
-        
-        //for some reason mbed doesn't like a pointer to a member in scanf
-        //term.scanf("%d", member); works with gcc on RPi
-        scanf("%d", &temp);
-        
-        *member = temp;
-       
-        if((*(member)< min) || (*(member) > max))
-        {
-            printf("\nERROR-RTI");
-        }
+        error("Could not open file for write\n");
+    }
+    
+    fseek(out_file, 0, SEEK_END); // goto end of file
+    if(ftell(out_file) == 0)
+    {
+        fprintf(out_file, "-----------------------------------------------\r\n|   Time   | Temperature | UV Data | LUX Data |\r\n-----------------------------------------------\r\n");
     }
-    while((*(member) < min) || (*(member) > max));
-}
-
-
-void get_user_input(char* message, uint8_t min, uint8_t max, bool* member)
-{
-    uint32_t temp;
-
-    do
+    
+    if(temperature != 0 && uvData != 0 && LUXData != 0)
     {
-        printf("\n%s", message);
+        fseek(out_file, 0, SEEK_END);
+        epoch_time = rtc.get_epoch();
+        strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&epoch_time));
         
-        //for some reason mbed doesn't like a pointer to a member in scanf
-        //term.scanf("%d", member); works with gcc on RPi
-        scanf("%d", &temp);
-        
-        *member = temp;
-       
-        if((*(member)< min) || (*(member) > max))
-        {
-            printf("\nERROR-RTI");
-        }
+        fprintf(out_file, "|    %s    |    %f    | UV Data | LUX Data |\r\n-----------------------------------------------\r\n", buffer, temperature);
     }
-    while((*(member) < min) || (*(member) > max));
-}
-
-    
\ No newline at end of file
+    printf("Finished writing to the file");
+    fclose(out_file);
+}
\ No newline at end of file
--- a/mbed.bld	Thu Mar 19 21:59:08 2015 +0000
+++ b/mbed.bld	Tue Jan 08 12:29:18 2019 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/7e07b6fb45cf
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/e95d10626187
\ No newline at end of file