Giovani Cardona / Mbed 2 deprecated 4_Alarma_DS3231_Encoder_IRDA

Dependencies:   mbed ds3231

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**********************************************************************
00002 * 
00003 * Demo DS3231 Library
00004 *
00005 ***********************************************************************
00006 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
00007 *
00008 * Permission is hereby granted, free of charge, to any person obtaining a
00009 * copy of this software and associated documentation files (the "Software"),
00010 * to deal in the Software without restriction, including without limitation
00011 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00012 * and/or sell copies of the Software, and to permit persons to whom the
00013 * Software is furnished to do so, subject to the following conditions:
00014 *
00015 * The above copyright notice and this permission notice shall be included
00016 * in all copies or substantial portions of the Software.
00017 *
00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00021 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00022 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00023 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00024 * OTHER DEALINGS IN THE SOFTWARE.
00025 *
00026 * Except as contained in this notice, the name of Maxim Integrated
00027 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00028 * Products, Inc. Branding Policy.
00029 *
00030 * The mere transfer of this software does not imply any licenses
00031 * of trade secrets, proprietary technology, copyrights, patents,
00032 * trademarks, maskwork rights, or any other form of intellectual
00033 * property whatsoever. Maxim Integrated Products, Inc. retains all
00034 * ownership rights.
00035 **********************************************************************/
00036 
00037 #include "ds3231.h"
00038 
00039 #define ESC 0x1B
00040 
00041 void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member);
00042 void get_user_input(char* message, uint8_t min, uint8_t max, bool* member);
00043 
00044 int main(void)
00045 {
00046     //rtc object
00047     Ds3231 rtc(D14, D15); 
00048     
00049     time_t epoch_time;
00050     
00051     //DS3231 rtc variables
00052     
00053     //default, use bit masks in ds3231.h for desired operation
00054     ds3231_cntl_stat_t rtc_control_status = {0,0}; 
00055     ds3231_time_t rtc_time;
00056     ds3231_calendar_t rtc_calendar;
00057     
00058     rtc.set_cntl_stat_reg(rtc_control_status);
00059     
00060     //get day from user
00061     get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 1,
00062                     7, &rtc_calendar.day);
00063 
00064     //get day of month from user
00065     get_user_input("\nPlease enter day of month (1-31): ", 1, 31, 
00066                     &rtc_calendar.date);
00067 
00068     //get month from user
00069     get_user_input("\nPlease enter the month, 1 for January (1-12): ", 1, 
00070                     12, &rtc_calendar.month);
00071 
00072     //get year from user
00073     get_user_input("\nPlease enter the year (0-99): ",0, 99, 
00074                     &rtc_calendar.year);
00075       
00076     //Get time mode
00077     get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1, 
00078                    &rtc_time.mode);  
00079     
00080     if(rtc_time.mode)
00081     {
00082         //Get AM/PM status
00083         get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1, 
00084                        &rtc_time.am_pm);  
00085         //Get hour from user
00086         get_user_input("\nPlease enter the hour (1-12): ", 1, 12, 
00087                        &rtc_time.hours);
00088     }
00089     else
00090     {
00091         //Get hour from user
00092         get_user_input("\nPlease enter the hour (0-23): ", 0, 23, 
00093                        &rtc_time.hours);
00094     }
00095      
00096     //Get minutes from user
00097     get_user_input("\nPlease enter the minute (0-59): ", 0, 59, 
00098                    &rtc_time.minutes);
00099     
00100     
00101     //Get seconds from user
00102     get_user_input("\nPlease enter the second (0-59): ", 0, 59, 
00103                    &rtc_time.seconds);
00104     
00105     
00106     
00107     //Set the time, uses inverted logic for return value
00108     if(rtc.set_time(rtc_time))
00109     {
00110         printf("\nrtc.set_time failed!!\n");
00111         exit(0);
00112     }
00113     
00114     //Set the calendar, uses inverted logic for return value
00115     if(rtc.set_calendar(rtc_calendar))
00116     {
00117         printf("\nrtc.set_calendar failed!!\n");
00118         exit(0);
00119     }
00120     
00121     char buffer[32];
00122     
00123     for(;;)
00124     {   
00125         printf("%c[2J", ESC); //clear screen
00126         printf("%c[H", ESC); //move cursor to Home
00127         
00128         //new epoch time fx
00129         epoch_time = rtc.get_epoch();
00130         
00131         printf("\nTime as seconds since January 1, 1970 = %d\n", epoch_time);
00132         
00133         printf("\nTime as a basic string = %s", ctime(&epoch_time));
00134  
00135         strftime(buffer, 32, "%I:%M %p\n", localtime(&epoch_time));
00136         printf("\nTime as a custom formatted string = %s", buffer);
00137         
00138         wait(1.0);
00139     }//loop 
00140 }
00141 
00142 
00143 /**********************************************************************
00144 * Function: get_user_input() 
00145 * Parameters: message - user prompt
00146 *             min - minimum value of input
00147 *             max - maximum value of input
00148 *             member - pointer to struct member              
00149 * Returns: none
00150 *
00151 * Description: get time/date input from user
00152 *
00153 **********************************************************************/
00154 void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member)
00155 {
00156     uint32_t temp;
00157 
00158     do
00159     {
00160         printf("\n%s", message);
00161         
00162         //for some reason mbed doesn't like a pointer to a member in scanf
00163         //term.scanf("%d", member); works with gcc on RPi
00164         scanf("%d", &temp);
00165         
00166         *member = temp;
00167        
00168         if((*(member)< min) || (*(member) > max))
00169         {
00170             printf("\nERROR-RTI");
00171         }
00172     }
00173     while((*(member) < min) || (*(member) > max));
00174 }
00175 
00176 
00177 void get_user_input(char* message, uint8_t min, uint8_t max, bool* member)
00178 {
00179     uint32_t temp;
00180 
00181     do
00182     {
00183         printf("\n%s", message);
00184         
00185         //for some reason mbed doesn't like a pointer to a member in scanf
00186         //term.scanf("%d", member); works with gcc on RPi
00187         scanf("%d", &temp);
00188         
00189         *member = temp;
00190        
00191         if((*(member)< min) || (*(member) > max))
00192         {
00193             printf("\nERROR-RTI");
00194         }
00195     }
00196     while((*(member) < min) || (*(member) > max));
00197 }
00198 
00199