Dallas DS1307 real-time clock minimalistic driver

Dependents:   testing_RTC_OneWire EMIRv2

Revision:
0:f29e45a25cba
Child:
1:60383faa35a9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DS1307.h	Fri Apr 25 18:15:47 2014 +0000
@@ -0,0 +1,58 @@
+/* DS1307.h */
+/*
+Copyright (c) 2014 Ales Povalac
+Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
+
+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.
+*/
+#ifndef __DS1307_H__
+#define __DS1307_H__
+
+
+class DS1307
+{
+protected:
+    I2C* m_rtc;
+    static const char *m_weekDays[];
+
+public:
+    DS1307(PinName sda, PinName scl);
+    ~DS1307();
+
+    const char* weekdayToString(int wday) { 
+        return m_weekDays[wday % 7]; 
+    }
+
+    time_t getNow();
+    bool setNow(time_t time);
+
+private:
+    bool read(int address, char* buffer, int len);
+    bool write(int address, char* buffer, int len);
+
+    static int bcdToDecimal(int bcd) {
+        return ((bcd&0xF0)>>4)*10 + (bcd&0x0F);
+    }
+
+    static int decimalToBcd(int dec) {
+        return (dec%10) + ((dec/10)<<4);
+    }
+};
+
+#endif // __DS1307_H__