InetrfaceProducts NXP / Mbed 2 deprecated PCF2127_Hello

Dependencies:   PCF2127 mbed

main.cpp

Committer:
nxp_ip
Date:
2014-12-09
Revision:
0:658f351580f0
Child:
1:86dc8dfe6a20

File content as of revision 0:658f351580f0:

/*
 *  A sample code for PCF2127 library
 *
 *  @author     Akifumi (Tedd) OKANO, NXP Semiconductors
 *  @version    1.6
 *  @date       09-Dec-2014
 *
 *  PCF2127 is a "real time clock (RTC)" module which is including a Xtal and TCXO
 *  http://www.nxp.com/products/interface_and_connectivity/real_time_clocks/rtcs_with_temp_compensation/series/PCF2127.html
 *
 *  RTC initializing part is ported from..
 *    http://mbed.org/users/roen/notebook/real-time/
 *
 *  This code is refined version of..
 *    http://developer.mbed.org/users/okano/code/NXP_PCF2127A/
 */
 */

#include "mbed.h"
#include "PCF2127.h"

PCF2127 rtc( p28, p27 );

void    show_time( void );
void    set_time( void );

int main()
{
    printf( "PCF2127 demo started.\r\n" );

    if ( rtc.is_init_required() ) {
        set_time();
    }

    while ( 1 ) {
        show_time();
        wait( 0.25 );
    }
}

void show_time( void )
{
    struct tm   dt, *dtp;
    time_t      t;
    char        s[ 30 ];
    
    dtp     = &dt;

    rtc.clear_intr();

    //  get time information from PCF2127
    if ( PCF2127::TIME_FUNC_ERROR == (t   = rtc.time( NULL )) )
    {
        printf( "error at reading time\r\n" );
        return;
    }
    
    dtp     = localtime( &t );

    //  print time and date on terminal
    strftime( s, 30, "%H:%M:%S, %Y/%b/%d %a", dtp );
    printf( "%s\r\n", s );
}

void set_time( void )
{
#define MAX_CHAR_LENGTH 21
    char    s[ MAX_CHAR_LENGTH ];

    printf( "Enter current date and time:\r\n" );
    printf( "YYYY MM DD HH MM SS[enter]\r\n" );

    fgets( s, MAX_CHAR_LENGTH, stdin );
    printf( "user input: \"%s\"\r\n", s );

    rtc.set_time( s );
}