ds1307: initial implementation of the ds1307 RTC

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Sarah Knepper <sarah.knepper@intel.com>
This commit is contained in:
Jon Trulson
2014-12-04 13:50:13 -07:00
committed by Sarah Knepper
parent 66d39af6c5
commit 02156d11c8
8 changed files with 567 additions and 0 deletions

View File

@ -54,6 +54,7 @@ add_executable (am2315-example am2315.cxx)
add_executable (itg3200-example itg3200.cxx)
add_executable (enc03r-example enc03r.cxx)
add_executable (adc121c021-example adc121c021.cxx)
add_executable (ds1307-example ds1307.cxx)
include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@ -95,6 +96,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/am2315)
include_directories (${PROJECT_SOURCE_DIR}/src/itg3200)
include_directories (${PROJECT_SOURCE_DIR}/src/enc03r)
include_directories (${PROJECT_SOURCE_DIR}/src/adc121c021)
include_directories (${PROJECT_SOURCE_DIR}/src/ds1307)
target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@ -152,3 +154,4 @@ target_link_libraries (am2315-example am2315 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (itg3200-example itg3200 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (enc03r-example enc03r ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (adc121c021-example adc121c021 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (ds1307-example ds1307 ${CMAKE_THREAD_LIBS_INIT})

77
examples/ds1307.cxx Normal file
View File

@ -0,0 +1,77 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2014 Intel Corporation.
*
* 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 THE AUTHORS OR COPYRIGHT HOLDERS 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.
*/
#include <unistd.h>
#include <iostream>
#include "ds1307.h"
using namespace std;
void printTime(upm::DS1307 *rtc)
{
cout << "The time is: " <<
rtc->month << "/" << rtc->dayOfMonth << "/" << rtc->year << " "
<< rtc->hours << ":" << rtc->minutes << ":" << rtc->seconds;
if (rtc->amPmMode)
cout << (rtc->pm) ? " PM " : " AM ";
cout << endl;
cout << "Clock is in " << ((rtc->amPmMode) ? "AM/PM mode" : "24hr mode")
<< endl;
}
int
main(int argc, char **argv)
{
//! [Interesting]
// Instantiate a DS1037 on I2C bus 0
upm::DS1307 *rtc = new upm::DS1307(0);
// always do this first
cout << "Loading the current time... " << endl;
if (!rtc->loadTime())
{
cerr << "rtc->loadTime() failed." << endl;
return 0;
}
printTime(rtc);
// set the year as an example
cout << "setting the year to 50" << endl;
rtc->year = 50;
rtc->setTime();
// reload the time and print it
rtc->loadTime();
printTime(rtc);
//! [Interesting]
delete rtc;
return 0;
}

View File

@ -0,0 +1,68 @@
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
/*global */
/*
* Author: Zion Orent <zorent@ics.com>
* Copyright (c) 2014 Intel Corporation.
*
* 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 THE AUTHORS OR COPYRIGHT HOLDERS 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.
*/
// Load RTC Clock module for Grove - RTC clock
var ds1307 = require('jsupm_ds1307');
// load this on i2c bus 0
var myRTCClockObj = new ds1307.DS1307(0);
// always do this first
console.log("Loading the current time... ");
var result = myRTCClockObj.loadTime();
if (!result)
{
console.log("myRTCClockObj.loadTime() failed.");
process.exit(1);
}
printTime(myRTCClockObj);
// set the year as an example
console.log("setting the year to 50");
myRTCClockObj.year = 50;
myRTCClockObj.setTime();
// reload the time and print it
myRTCClockObj.loadTime();
printTime(myRTCClockObj);
function printTime(RTCObj)
{
var timeStr = "The time is: " +
RTCObj.month + "/" + RTCObj.dayOfMonth + "/" + RTCObj.year + " " +
RTCObj.hours + ":" + RTCObj.minutes + ":" + RTCObj.seconds;
if (RTCObj.amPmMode)
timeStr += (RTCObj.pm ? " PM " : " AM ");
console.log(timeStr);
console.log("Clock is in " +
(RTCObj.amPmMode ? "AM/PM mode" : "24hr mode"));
}