Fork of Demo program for ard2pmod library. Alarm features of RTC have not been tested, please try them out.

Dependencies:   Terminal ard2pmod mbed

Fork of ard2pmod_demo by Maxim Integrated

main.cpp

Committer:
j3
Date:
2016-03-29
Revision:
14:08a83dc0e58e
Parent:
13:067920a73c78

File content as of revision 14:08a83dc0e58e:

/**********************************************************************
* 
* Demo Ard2Pmod Library
*
* Configures mux for desired pmod type and then displays current time
* and date in endless loop after asking the user for input to configure 
* the RTC.
*
* Requires TeraTerm, or your favorite terminal emulator, configured at
* 8N1 9600bps
*
***********************************************************************
* 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.
**********************************************************************/


#include "Terminal.h"
#include "ard2pmod.h"
#include "ds3231.h"



int main(void)
{
    /* 
    available PMOD types
       PMOD_TYPE_I2C_A,
       PMOD_TYPE_I2C_B,
       PMOD_TYPE_I2C_AB,
       PMOD_TYPE_1_GPIO,
       PMOD_TYPE_2_SPI,
       PMOD_TYPE_3_UART,
       PMOD_TYPE_4_UART,
       PMOD_TYPE_5_HBRIDGE,
       PMOD_TYPE_6_HBRIDGE
    */
    
    Terminal term(USBTX, USBRX);
    
    Ard2Pmod board(Ard2Pmod::PMOD_TYPE_1_GPIO);
    
    Ds3231 rtc(D14, D15);
    
    //DS3231 rtc variables
    //default for control and status registers, 
    //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);
    
    uint32_t user_input;
    
    user_input = term.get_int32("\nDo you want to set the RTC time and calendar (1 for yes, 0 for no): \n", 0, 1);
    
    if(user_input)
    {
        //get day from user
        rtc_calendar.day = term.get_int32("\nPlease enter day of week, 1 for Sunday (1-7): ", 1, 7);
    
        //get day of month from user
        rtc_calendar.date = term.get_int32("\nPlease enter day of month (1-31): ", 1, 31);
    
        //get month from user
        rtc_calendar.month = term.get_int32("\nPlease enter the month, 1 for January (1-12): ", 1, 12);
    
        //get year from user
        rtc_calendar.year = term.get_int32("\nPlease enter the year (0-99): ", 0, 99);
          
        //Get time mode
        rtc_time.mode = term.get_int32("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1);
        
        if(rtc_time.mode)
        {
            //Get AM/PM status
            rtc_time.am_pm = term.get_int32("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1);
            
            //Get hour from user
            rtc_time.hours = term.get_int32("\nPlease enter the hour (1-12): ", 1, 12);
        }
        else
        {
            //Get hour from user
            rtc_time.hours = term.get_int32("\nPlease enter the hour (0-23): ", 0, 23);
        }
         
        //Get minutes from user
        rtc_time.minutes = term.get_int32("\nPlease enter the minute (0-59): ", 0, 59);
        
        
        //Get seconds from user
        rtc_time.seconds = term.get_int32("\nPlease enter the second (0-59): ", 0, 59);
                       
        //Set the time, uses inverted logic for return value
        if(rtc.set_time(rtc_time))
        {
            term.printf("\nrtc.set_time failed!!\n");
            while(1);
        }
        
        //Set the calendar, uses inverted logic for return value
        if(rtc.set_calendar(rtc_calendar))
        {
            term.printf("\nrtc.set_calendar failed!!\n");
            while(1);
        }
    }
    
    time_t epoch_time;
    char buffer[32];
    
    for(;;)
    {   
        term.cls();
        term.locate(0,0); //move home
        
        //new epoch time fx
        epoch_time = rtc.get_epoch();
        
        term.printf("\nTime as seconds since January 1, 1970 = %d\n", epoch_time);
        
        term.printf("\nTime as a basic string = %s", ctime(&epoch_time));
 
        strftime(buffer, 32, "%I:%M %p", localtime(&epoch_time));
        term.printf("\nTime as a custom formatted string = %s", buffer);
        
        wait_ms(1000);
    } 
}


/**********************************************************************
* 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
    {
        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");
        }
    }
    while((*(member) < min) || (*(member) > max));
}


void get_user_input(char* message, uint8_t min, uint8_t max, bool* member)
{
    uint32_t temp;

    do
    {
        printf("\n%s", message);
        scanf("%d", &temp);
        
        if(temp == 1)
        {
            *member = true;
        }
        else if(temp == 0)
        {
            *member = false;
        }
        else
        {
            printf("\nERROR-RTI");
        }
    }
    while((temp != 1) && (temp != 0));
}