RTC 

Example

: Interactive Configuration Shell

This example shows how to interact with the RTC using a smart shell


File:
"WaspRTC_6_interactiveConfigurationShell.pde"


Code



/*
 *  ------Waspmote RTC Interactive Configuration Shell Example-------
 *
 *  Explanation: This example shows how to interact with the RTC using a 
 *  smart shell
 *
 *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version:                0.1
 *  Design:                 David Gascón
 *  Implementation:         Alberto Bielsa
 */

#define TIME    1
#define DATE    2

#define ABOUT    "\nWASP I2C RTC test program, meant to\ncheck the correct functionality of the on-board RTC\n(c) 2009 M. Yarza & D. Cuartielles for Libelium.com\ndavid@blushingboy.org\nm.yarza@libelium.com\n"
#define INFOMENU "\n0 - about\n1 - set time\n2 - set date\n3 - set weekday\n4 - send data (time and date) to RTC\n5 - read RTC status\n6 - echo a RTC's register\n7 - set alarm 1\n8 - set alarm 2\n9 - read temperature\nA - reset the alarm flags\nB - Go to total save power mode"
#define INFOONELINEMENU "\n0 about | 1 time | 2 date | 3 weekday | 4 send to RTC | 5 RTC status | 6 registers | 7 watchdog test | 8 configuration"
#define INFOCONFIG "\n0 - toggle COMPLEX VIEW mode\n1 - toggle to one-line menu"
#define INFOTIME "Enter the values for hour, minutes, and seconds separated by colons (hh:mm:ss)"
#define INFODATE "Enter the values for year, month, and day separated by colons (yy:mm:dd)"
#define INFODAY  "Enter the day of the week, encoded as 1 for Sunday to 7 for Saturday"
#define INFOREGISTER  "Enter the number (0..C - Hex) for the register inside the RTC to print out"
#define ERRORMSG "* ERROR * "
#define ERRORHOUR "hours cannot be bigger than 23, negative numbers, or characters"
#define ERRORMINUTE "minutes cannot be bigger than 59, negative numbers, or characters"
#define ERRORSECOND "seconds cannot be bigger than 59, negative numbers, or characters"
#define ERRORDATE "there is no month with more than 31 days, nor negative days"
#define ERRORMONTH "there is no year with more than 12 months, nor negative ones"

#define INFOALARM1 "Enter the values for Alarm 1 hour, minute and seconds separated by colons (hh:mm:ss)"
#define INFOALARM2 "Enter the values for Alarm 2 date / day, hour and minutes separated by colons (dd:hh:mm)"
#define INFODAYALARM1 "Enter the value for date / day Alarm1 (dd)"
#define INFOALARM1MODE "Enter the alarm 1 mode\n0 - alarm when day, hours, minutes and seconds match\n1 - alarm when date, hours, minutes, and seconds match\n2 - alarm when hours, minutes and seconds match\n3 - alarm when minuts and seconds match\n4 - alarm when seconds match\n5 - alarm once per second\n6 - alarm 1 OFF"
#define INFOALARM2MODE "Enter the alarm 2 mode\n0 - alarm when day, hours and minutes\n1 - alarm when date, hours and minutes match\n2 - alarm when hours and minutes match\n3 - alarm when minuts match\n4 - alarm once per minute (00 seconds of every minute)\n5 - alarm 2 OFF"

#define INFOPOWERSAVEMODE "Enter the alarm you want to use to awake Wasp\n1 - alarm 1\n2 - alarm 2"



byte newRTCData = 0;
byte COMPLEX_VIEW = 0;
byte ONE_LINE = 0;

void printMSG(char* theMessage) {
  USB.println(theMessage);
}



// send a the help menu to the serial monitor
byte inputMenu(char* theMessage) {
  USB.println("MENU: ");
  printMSG(theMessage);
  while(!USB.available()) {
    if(intFlag & RTC_INT)
    {
      intFlag &= ~(RTC_INT);
      Utils.blinkLEDs(1000);
      USB.println("INT CAPTURED");
      RTC.clearAlarmFlag();
      Utils.setLED(LED0,LED_ON);
    }
  };
  int d=USB.read();
  return d;
}

// Read the data for time or date
void inputData(char* theMessage) {
  int timecount = 0;
  int aux = 0;
  XBee.println(theMessage);
  while(timecount < 8)    // slave may send less than requested
  { 
    if (XBee.available()) {
      byte c = XBee.read(); // receive a byte as character
      switch (timecount) {
      case 0:
        aux = c;
        break;
      case 1:
        if (theMessage == INFOTIME) {
          RTC.hour = RTC.BCD2byte(aux - 48, c - 48);
        }
        if (theMessage == INFODATE) {
          RTC.year = RTC.BCD2byte(aux - 48, c - 48);
        }
        if (theMessage == INFOALARM1){
          RTC.hour_alarm1 = RTC.BCD2byte(aux - 48, c - 48);
        }
        if (theMessage == INFOALARM2){
          RTC.day_alarm2 = RTC.BCD2byte(aux - 48, c - 48);
        }
        break;
      case 3:
        aux = c;
        break;
      case 4:
        if (theMessage == INFOTIME) {
          RTC.minute = RTC.BCD2byte(aux - 48, c - 48);
        }
        if (theMessage == INFODATE) {
          RTC.month = RTC.BCD2byte(aux - 48, c - 48);
        }
        if (theMessage == INFOALARM1){
          RTC.minute_alarm1 = RTC.BCD2byte(aux - 48, c - 48);
        }
        if (theMessage == INFOALARM2){
          RTC.hour_alarm2 = RTC.BCD2byte(aux - 48, c - 48);
        }
        break;
      case 6:
        aux = c;
        break;
      case 7:
        if (theMessage == INFOTIME) {
          RTC.second = RTC.BCD2byte(aux - 48, c - 48);
        }
        if (theMessage == INFODATE) {
          RTC.date = RTC.BCD2byte(aux - 48, c - 48);
        }
        if (theMessage == INFOALARM1){
          RTC.second_alarm1 = RTC.BCD2byte(aux - 48, c - 48);
        }
        if (theMessage == INFOALARM2){
          RTC.minute_alarm2 = RTC.BCD2byte(aux - 48, c - 48);
        }
        break;
      }
      timecount++;
    }
    if (theMessage == INFOTIME) {
      newRTCData |= TIME;
    }
    if (theMessage == INFODATE) {
      newRTCData |= DATE;
    }
  }
}

/* printCurrentVariables
 * - will make a printout of the current state 
 *   of the variables over the USB
 * - includes: day of the week, date, and time
 */
void printCurrentVariables() {
  USB.print("\n--------------------------------\n   ");
  USB.println(RTC.getTimestamp());
  USB.println("--------------------------------\n");
}

void inputDayAlarm (char* theMessage){
  int timecount = 0;
  int aux = 0;
  XBee.println(theMessage);
  while(timecount <= 1)    // slave may send less than requested
  { 
    if (XBee.available()) {
      byte c = XBee.read(); // receive a byte as character
      switch (timecount) {
      case 0:
        aux = c;
        break;
      case 1:
        if (theMessage == INFODAYALARM1) {
          RTC.day_alarm1 = RTC.BCD2byte(aux - 48, c - 48);
        }
       
        break;
      }
      timecount++;
    }
    
  }
}

void setup()
{
  // start communication
  RTC.ON();       // init variables, get info from the clock and store in in the register
  USB.println("ready");  // send a ping
  USB.begin();
}

void loop()
{
  
  Utils.setLED(LED0,LED_ON);
  int val = 0;
  if (ONE_LINE) val = inputMenu(INFOONELINEMENU);
  else val = inputMenu(INFOMENU);
  int valReg = 0;
  switch (val) {
  case '0':
    printMSG(ABOUT);
    break;
  case '1':
    inputData(INFOTIME);
    break;
  case '2':
    inputData(INFODATE);
    break;
  case '3':
    RTC.day = inputMenu(INFODAY) - 48;
    break;
  case '4':
    if (newRTCData & TIME) {
      USB.println(" new time to write");
    }
    if (newRTCData & DATE) {
      USB.println(" new date to write");
    }
    USB.println(" sending time and date:");
    printCurrentVariables();
    RTC.writeRTC();
    USB.println(" please, verify time and date:");
    printCurrentVariables();
    RTC.readRTC(RTC_ALARM2_ADDRESS);
    newRTCData = 0;
    break;
  case '5':
    RTC.readRTC(RTC_ALARM2_ADDRESS);
    printCurrentVariables();
    break;
  case '6':
    valReg = inputMenu(INFOREGISTER);
    if (valReg > '9') {
      valReg -= 65 - 10;
    } else {
      valReg -= 48;
    }
    RTC.readRTCregister(valReg);
    USB.print("The value inside register number ");
    USB.print(valReg, DEC);
    USB.print(" is: ");
    USB.println(RTC.registersRTC[valReg], BIN);
    USB.print("The watchdog's RST pin is ");
    if (digitalRead(RST_RTC)) 
      USB.println("HIGH");
    else
      USB.println("LOW");
    break;
    
  case '7':
    inputData(INFOALARM1);
    inputDayAlarm(INFODAYALARM1);
    RTC.writeRTCalarm1();
    RTC.alarm1Mode = inputMenu(INFOALARM1MODE) - 48;
    USB.print("Alarm mode: ");
    USB.println(RTC.alarm1Mode,DEC);
    RTC.configureAlarmMode(1,RTC.alarm1Mode);
    break;

  case '8':
    inputData(INFOALARM2);
    RTC.writeRTCalarm2();
    RTC.alarm2Mode = inputMenu(INFOALARM2MODE) - 48;
    XBee.print("Alarm mode: ");
    XBee.println(RTC.alarm2Mode,DEC);
    RTC.configureAlarmMode(2,RTC.alarm2Mode);
    break;
    
 case '9':
    RTC.getTemperature();
    USB.print("Temperature: ");
    if(RTC.tempNegative)  USB.print("- ");
    else  USB.print("+ ");
    USB.println(RTC.temp,DEC);
    
    break;
   case 'A':
     RTC.registersRTC[0x0F] &= B11111100;  // reset the alarm flags
     RTC.writeRTCregister(0x0F);
     break; 
     
   case 'B':
     int val_aux = 0;
     val_aux = inputMenu(INFOPOWERSAVEMODE);
     if (val_aux == '1'){
        inputData(INFOALARM1);
        inputDayAlarm(INFODAYALARM1);
        RTC.writeRTCalarm1();
        RTC.alarm1Mode = inputMenu(INFOALARM1MODE) - 48;
        XBee.print("Alarm mode: ");
        XBee.println(RTC.alarm1Mode,DEC);
        RTC.configureAlarmMode(1,RTC.alarm1Mode);
     }
     if (val_aux =='2'){
       inputData(INFOALARM2);
       RTC.writeRTCalarm2();
       RTC.alarm2Mode = inputMenu(INFOALARM2MODE) - 48;
       XBee.print("Alarm mode: ");
       XBee.println(RTC.alarm2Mode,DEC);
       RTC.configureAlarmMode(2,RTC.alarm2Mode);
     }
     
     Utils.setLED(LED0,LED_OFF);
     USB.close();
     PWR.sleep(ALL_OFF);
     USB.begin();
     break;
  }
}
 

Download code

You can download the code of this example.


© 2009 Libelium Comunicaciones Distribuidas S.L.

| Terms of use