lejos.nxt.addon
Class RealTimeClock

java.lang.Object
  extended by lejos.nxt.I2CSensor
      extended by lejos.nxt.addon.RealTimeClock
All Implemented Interfaces:
SensorConstants, Clock

public class RealTimeClock
extends I2CSensor
implements Clock

Mindsensors Real-Time Clock www.mindsensors.com

Author:
Robert W. Kramer

Field Summary
static int DEFAULT_RTC_ADDRESS
           
 
Fields inherited from class lejos.nxt.I2CSensor
address, DEFAULT_I2C_ADDRESS, port, REG_PRODUCT_ID, REG_VENDOR_ID, REG_VERSION
 
Fields inherited from interface lejos.nxt.SensorConstants
BLACK, BLANK_INDEX, BLUE, BLUE_INDEX, GREEN, GREEN_INDEX, MAX_AD_RAW, MAX_TYPE, MIN_TYPE, MODE_ANGLESTEP, MODE_BOOLEAN, MODE_CELSIUS, MODE_FARENHEIT, MODE_PCTFULLSCALE, MODE_PERIODCOUNTER, MODE_RAW, MODE_TRANSITIONCNT, RED, RED_INDEX, TYPE_ANGLE, TYPE_COLORBLUE, TYPE_COLORFULL, TYPE_COLORGREEN, TYPE_COLORNONE, TYPE_COLORRED, TYPE_CUSTOM, TYPE_HISPEED, TYPE_LIGHT_ACTIVE, TYPE_LIGHT_INACTIVE, TYPE_LOWSPEED, TYPE_LOWSPEED_9V, TYPE_NO_SENSOR, TYPE_REFLECTION, TYPE_SOUND_DB, TYPE_SOUND_DBA, TYPE_SWITCH, TYPE_TEMPERATURE, WHITE, YELLOW
 
Constructor Summary
RealTimeClock(I2CPort p)
          Constructor
RealTimeClock(I2CPort p, int address)
          Constructor
 
Method Summary
 String getAMPM()
          Get RTC AM/PM indication as String.
 byte getByte(int loc)
          Retrieves RTC RAM byte; can retrieve clock bytes (0-7)
 int getCanonicalHour()
          Get RTC hour as int.
 Date getDate()
          Get RTC date and time as a Date object
 String getDateString()
          Retrieve RTC date as String in mm/dd/yyyy format
 String getDateString(boolean usePadding, boolean dayFirst)
          Retrieve RTC date as String
 int getDay()
          Get RTC day of month as int
 int getDayOfWeek()
          Get RTC day of week as int
 int getHour()
          Get RTC hour as int.
 int getMinute()
          Get RTC minute as int
 int getMonth()
          Get RTC month as int
 int getSecond()
          Get RTC second as int
 String getTimeString()
          Get RTC time as String
 int getYear()
          Get RTC year as int
 boolean isPM()
          Get RTC AM/PM indication as boolean flag.
 void setByte(int loc, byte b)
          Sets RTC RAM byte to new value
 void setDate(int m, int d, int y)
          Set RTC date to m/d/y.
 void setDay(int d)
          Set RTC day of month.
 void setDayOfWeek(int d)
          Set RTC day of week.
 void setHour(int h)
          Set RTC hour.
 void setHour(int h, boolean isPM)
          Set RTC hour using AM/PM format.
 void setHourMode(boolean use24h)
          Reset RTC hour mode.
 void setMinute(int m)
          Set RTC minute
 void setMonth(int m)
          Set RTC month.
 void setSecond(int s)
          Set RTC seconds
 void setTime(int h, int m, int s)
          Set RTC time.
 void setYear(int y)
          Set RTC year.
 
Methods inherited from class lejos.nxt.I2CSensor
fetchString, getAddress, getData, getData, getPort, getProductID, getVendorID, getVersion, sendData, sendData, sendData, setAddress
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_RTC_ADDRESS

public static final int DEFAULT_RTC_ADDRESS
See Also:
Constant Field Values
Constructor Detail

RealTimeClock

public RealTimeClock(I2CPort p,
                     int address)
Constructor

Parameters:
p - Port used by RTC
address - I2C address of the RTC

RealTimeClock

public RealTimeClock(I2CPort p)
Constructor

Parameters:
p - Port used by RTC
Method Detail

getYear

public int getYear()
            throws IOException
Get RTC year as int

Specified by:
getYear in interface Clock
Returns:
Current year (2000-2099)
Throws:
IOException - if communication to RTC fails

getMonth

public int getMonth()
             throws IOException
Get RTC month as int

Specified by:
getMonth in interface Clock
Returns:
Current month (1-12)
Throws:
IOException - if communication to RTC fails

getDay

public int getDay()
           throws IOException
Get RTC day of month as int

Specified by:
getDay in interface Clock
Returns:
Current day (1-31)
Throws:
IOException - if communication to RTC fails

getDayOfWeek

public int getDayOfWeek()
                 throws IOException
Get RTC day of week as int

Specified by:
getDayOfWeek in interface Clock
Returns:
Current day of week (1-7)
Throws:
IOException - if communication to RTC fails

getHour

public int getHour()
            throws IOException
Get RTC hour as int. Uses current RTC format (12- or 24-hour)

Specified by:
getHour in interface Clock
Returns:
Current hour (1-12 or 0-23)
Throws:
IOException - if communication to RTC fails

getCanonicalHour

public int getCanonicalHour()
                     throws IOException
Get RTC hour as int. Uses 0-23 format.

Returns:
Current hour (0-23)
Throws:
IOException - if communication to RTC fails

isPM

public boolean isPM()
             throws IOException
Get RTC AM/PM indication as boolean flag. Works in 12- and 24-hour formats.

Returns:
True = AM; false = PM
Throws:
IOException - if communication to RTC fails

getMinute

public int getMinute()
              throws IOException
Get RTC minute as int

Specified by:
getMinute in interface Clock
Returns:
Current minute (0-59)
Throws:
IOException - if communication to RTC fails

getSecond

public int getSecond()
              throws IOException
Get RTC second as int

Specified by:
getSecond in interface Clock
Returns:
Current second (0-59)
Throws:
IOException - if communication to RTC fails

getDate

public Date getDate()
             throws IOException
Get RTC date and time as a Date object

Returns:
Current date and time
Throws:
IOException - if communication to RTC fails

getDateString

public String getDateString(boolean usePadding,
                            boolean dayFirst)
                     throws IOException
Retrieve RTC date as String

Parameters:
usePadding - True = always use two digits for day and month
dayFirst - True = dd/mm/yyyy, false = mm/dd/yyyy
Returns:
Current date as String
Throws:
IOException - if communication to RTC fails

getDateString

public String getDateString()
                     throws IOException
Retrieve RTC date as String in mm/dd/yyyy format

Specified by:
getDateString in interface Clock
Returns:
Current date as String
Throws:
IOException - if communication to RTC fails

getTimeString

public String getTimeString()
                     throws IOException
Get RTC time as String

Specified by:
getTimeString in interface Clock
Returns:
Current time in hh:mm:ss format
Throws:
IOException - if communication to RTC fails

getAMPM

public String getAMPM()
               throws IOException
Get RTC AM/PM indication as String. Works in both 12- and 24-hour formats.

Specified by:
getAMPM in interface Clock
Returns:
"AM" or "PM"
Throws:
IOException - if communication to RTC fails

setHour

public void setHour(int h)
             throws IllegalArgumentException,
                    IOException
Set RTC hour. Uses canonical input, does not change 12/24 hour mode.

Parameters:
h - New hour count
Throws:
IllegalArgumentException - if h < 0 or h > 23
IOException - if communication to RTC fails

setHour

public void setHour(int h,
                    boolean isPM)
             throws IllegalArgumentException,
                    IOException
Set RTC hour using AM/PM format. Does not change RTC 12/24 hour mode.

Parameters:
h - Current hour (1-12)
isPM - True = PM; false = AM
Throws:
IllegalArgumentException - if h < 1 or h > 12
IOException - if communication to RTC fails

setMinute

public void setMinute(int m)
               throws IllegalArgumentException,
                      IOException
Set RTC minute

Parameters:
m - New minute count
Throws:
IllegalArgumentException - if m < 0 or m > 59
IOException - if communication to RTC fails

setSecond

public void setSecond(int s)
               throws IllegalArgumentException,
                      IOException
Set RTC seconds

Parameters:
s - New second count
Throws:
IllegalArgumentException - if s < 0 or s > 59
IOException - if communication to RTC fails

setTime

public void setTime(int h,
                    int m,
                    int s)
             throws IllegalArgumentException,
                    IOException
Set RTC time. Hours are canonical, does not change 12/24 hour format.

Specified by:
setTime in interface Clock
Parameters:
h - New hour
m - New minute
s - New second
Throws:
IllegalArgumentException - if h, m or s are out of bounds
IOException - if communication to RTC fails

setMonth

public void setMonth(int m)
              throws IllegalArgumentException,
                     IOException
Set RTC month. Also sets RTC day of week.

Parameters:
m - New month
Throws:
IllegalArgumentException - if m < 1 or m > 12
IOException - if communication to RTC fails

setDay

public void setDay(int d)
            throws IllegalArgumentException,
                   IOException
Set RTC day of month. Also sets RTC day of week.

Parameters:
d - New day of month
Throws:
IllegalArgumentException - if d is an invalid day of current month
IOException - if communication to RTC fails

setYear

public void setYear(int y)
             throws IllegalArgumentException,
                    IOException
Set RTC year. Also sets RTC day of week.

Parameters:
y - New year to use
Throws:
IllegalArgumentException - if y < 0 or y > 99
IOException - if communication to RTC fails

setDayOfWeek

public void setDayOfWeek(int d)
                  throws IllegalArgumentException,
                         IOException
Set RTC day of week. Does not change current date.

Parameters:
d - New day of week
Throws:
IllegalArgumentException - if d < 1 or d > 7
IOException - if communication to RTC fails

setDate

public void setDate(int m,
                    int d,
                    int y)
             throws IllegalArgumentException,
                    IOException
Set RTC date to m/d/y. Also sets RTC day of week.

Specified by:
setDate in interface Clock
Parameters:
m - New month
d - New day of month
y - New year
Throws:
IllegalArgumentException - if m, d or y is invalid
IOException - if communication to RTC fails

setHourMode

public void setHourMode(boolean use24h)
                 throws IOException
Reset RTC hour mode. Does not change RTC current hour.

Specified by:
setHourMode in interface Clock
Parameters:
use24h - True = use 24-hour format; false = use 12-hour format.
Throws:
IOException - if communication to RTC fails

getByte

public byte getByte(int loc)
             throws IndexOutOfBoundsException,
                    IOException
Retrieves RTC RAM byte; can retrieve clock bytes (0-7)

Specified by:
getByte in interface Clock
Parameters:
loc - Byte to retrieve, 0 <= loc <= 0x3f
Returns:
RTC RAM[loc]
Throws:
IndexOutOfBoundsException - if loc < 0 or loc > 63 (0x3f)
IOException - if communication to RTC fails

setByte

public void setByte(int loc,
                    byte b)
             throws IndexOutOfBoundsException,
                    IOException
Sets RTC RAM byte to new value

Specified by:
setByte in interface Clock
Parameters:
loc - Byte to set, 0x08 <= loc <= 0x3f
b - New value of byte
Throws:
IndexOutOfBoundsException - if loc < 8 or loc > 63 (0x3f)
IOException - if communication to RTC fails