No Description

ds1307.cxx 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Author: Jon Trulson <jtrulson@ics.com>
  3. * Copyright (c) 2014 Intel Corporation.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <unistd.h>
  25. #include <iostream>
  26. #include "ds1307.h"
  27. using namespace std;
  28. void printTime(upm::DS1307 *rtc)
  29. {
  30. cout << "The time is: " <<
  31. rtc->month << "/" << rtc->dayOfMonth << "/" << rtc->year << " "
  32. << rtc->hours << ":" << rtc->minutes << ":" << rtc->seconds;
  33. if (rtc->amPmMode)
  34. cout << (rtc->pm) ? " PM " : " AM ";
  35. cout << endl;
  36. cout << "Clock is in " << ((rtc->amPmMode) ? "AM/PM mode" : "24hr mode")
  37. << endl;
  38. }
  39. int
  40. main(int argc, char **argv)
  41. {
  42. //! [Interesting]
  43. // Instantiate a DS1037 on I2C bus 0
  44. upm::DS1307 *rtc = new upm::DS1307(0);
  45. // always do this first
  46. cout << "Loading the current time... " << endl;
  47. if (!rtc->loadTime())
  48. {
  49. cerr << "rtc->loadTime() failed." << endl;
  50. return 0;
  51. }
  52. printTime(rtc);
  53. // set the year as an example
  54. cout << "setting the year to 50" << endl;
  55. rtc->year = 50;
  56. rtc->setTime();
  57. // reload the time and print it
  58. rtc->loadTime();
  59. printTime(rtc);
  60. //! [Interesting]
  61. delete rtc;
  62. return 0;
  63. }