Set Alarm

 

#include <stdio.h>

#include <string.h>

#include <stdarg.h>

#include <time.h>

 

#include "m2m_type.h"

#include "m2m_hw_api.h"

#include "m2m_clock_api.h"

 

/* See chapter 19.1.8 PrintToUart */

 

INT32 RTC_example( int days, int hours, int minutes )

{

 

  /* Sets the RTC alarm clock according to defined time interval expressed in dd:hh:mm */

  /* The function returns 0 on success, else returns -1 */

 

  /* set epoch in seconds: 1429163640:

   * Year 2015, Month 4, Date 16, Hours 07, Minutes 54, Seconds 0, Time Zone Local */

 

  const time_t start_sec_prog = 1429163640;

 

 

  INT32 sec_prog = minutes*60 + hours*3600 + days*24*3600;

  INT32 sec_cur;

 

  M2M_T_RTC_DATE       date_mem;

  M2M_T_RTC_TIME       time_mem;

  M2M_T_RTC_RESULT   res_RTC;

 

  struct M2M_T_RTC_TIMEVAL     tv;

  struct M2M_T_RTC_TIMEZONE  tz;

  struct tm         *restart;

 

  restart = localtime(&start_sec_prog);

  PrintToUart("Initial Local time and date: %s", asctime(restart));

 

  tv.tv_sec = 1429163640;

  tv.tv_msec = 0;

  tz.tz_tzone = 0;

  tz.tz_dst = 0;

 

  if (m2m_set_timeofday(&tv, &tz) == -1 )

    {

     PrintToUart("Exit: set_timeofday ERROR\n");             /* See chapter 19.1.8 PrintToUart */

     return -1;

    }

 

  /* get epoch, it is just an example */

 

  if (m2m_get_timeofday(&tv, &tz) == -1 )

    {

     PrintToUart("Exit: get_timeofday ERROR\n");

     return -1;

    }

 

  /* add the time interval. When it is elapsed, the M2M_onWakeup(void) callback is activated. */

 

  sec_cur = tv.tv_sec;

  sec_prog = sec_cur + sec_prog;

  PrintToUart("epoch when the time interval will be over: %d\n", sec_prog);

 

  restart = localtime((const time_t *) &sec_prog);

 

  PrintToUart("Local time and date when the time interval will be over: %s", asctime(restart));

 

  /* set the alarm when the timer interval is over */

 

  time_mem.hour =(CHAR)restart->tm_hour;

  time_mem.minute = (CHAR)restart->tm_min;

  time_mem.second = (CHAR)restart->tm_sec;

 

  date_mem.day = (CHAR)restart->tm_mday;

  date_mem.month = (CHAR)(restart->tm_mon + 1);

  date_mem.year = (CHAR)(restart->tm_year -100);

 

 

  res_RTC = m2m_rtc_set_alarm(date_mem, time_mem);

 

  if (res_RTC != M2M_RTC_SUCCESS)

    {

      PrintToUart("Exit: set_alarm ERROR\n");

      return -1;

    }

 

  return 0;

}

 

void M2M_main ( INT32 argc, CHAR argv[M2M_ARGC_MAX][M2M_ARGV_MAXTOKEN + 1] )

{

  PrintToUart("Start alarm setting");

  PrintToUart("After one minute the M2M_onWakeup(void) callback sends: Alarm is waked up!\n");

  RTC_example( 0, 0, 1);

}

 

 

/* Write the code into  M2M_onWakeup(void) callback contained in the M2M_HwEvents.c file */

 

#include "m2m_type.h"

#include "m2m_hw_api.h"

 

void M2M_onWakeup(void)

{

  M2M_T_HW_UART_HANDLE USIF0_handle;

  INT32 len_sent;

 

  USIF0_handle = m2m_hw_uart_open();

  m2m_hw_uart_write(USIF0_handle, "Alarm is waked up!", 18, &len_sent);

  m2m_hw_uart_close(USIF0_handle);

}