COSA
An Object-Oriented Platform for Arduino Programming
DS1307.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_DS1307_HH
22 #define COSA_DS1307_HH
23 
24 #include "Cosa/TWI.hh"
25 #include "Cosa/IOStream.hh"
26 #include "Cosa/Time.hh"
27 
50 class DS1307 : private TWI::Driver {
51 public:
55  union control_t {
56  uint8_t as_uint8;
57  struct {
58  uint8_t rs:2;
59  uint8_t reserved1:2;
60  uint8_t sqwe:1;
61  uint8_t reserved2:2;
62  uint8_t out:1;
63  };
64 
69  operator uint8_t()
70  {
71  return (as_uint8);
72  }
73 
78  {
79  as_uint8 = 0;
80  }
81  };
82 
86  enum Rate {
87  RS_1_HZ = 0,
91  } __attribute__((packed));
92 
96  struct timekeeper_t {
99  };
100 
102  const static uint8_t RAM_START = sizeof(timekeeper_t);
103 
105  const static uint8_t RAM_END = 0x3f;
106 
108  const static uint8_t RAM_MAX = RAM_END - RAM_START + 1;
109 
113  DS1307() : TWI::Driver(0x68) {}
114 
123  int read(void* ram, uint8_t size = sizeof(time_t), uint8_t pos = 0);
124 
133  int write(void* ram, uint8_t size = sizeof(time_t), uint8_t pos = 0);
134 
141  bool get_time(time_t& now)
142  __attribute__((always_inline))
143  {
144  return (read(&now) == sizeof(now));
145  }
146 
153  bool set_time(time_t& now)
154  __attribute__((always_inline))
155  {
156  return (write(&now) == sizeof(now));
157  }
158 
165  bool enable(Rate rs = RS_1_HZ);
166 
172  bool disable();
173 };
174 
175 #endif
Definition: TWI.hh:51
uint8_t rs
< Bitfield access.
Definition: DS1307.hh:58
Rate
Definition: DS1307.hh:86
uint8_t out
Output Control.
Definition: DS1307.hh:62
uint8_t reserved1
Reserved/1.
Definition: DS1307.hh:59
time_t clock
Time/Date in BCD.
Definition: DS1307.hh:97
bool disable()
Definition: DS1307.cpp:57
bool get_time(time_t &now)
Definition: DS1307.hh:141
static const uint8_t RAM_START
Definition: DS1307.hh:102
static const uint8_t RAM_END
Definition: DS1307.hh:105
bool enable(Rate rs=RS_1_HZ)
Definition: DS1307.cpp:45
Definition: DS1307.hh:50
static const uint8_t RAM_MAX
Definition: DS1307.hh:108
uint8_t as_uint8
Unsigned byte access.
Definition: DS1307.hh:56
int read(void *ram, uint8_t size=sizeof(time_t), uint8_t pos=0)
Definition: DS1307.cpp:24
Definition: Time.hh:102
DS1307()
Definition: DS1307.hh:113
Driver(uint8_t addr)
Definition: TWI.hh:70
uint8_t sqwe
Square-Ware Enable.
Definition: DS1307.hh:60
uint8_t reserved2
Reserved/2.
Definition: DS1307.hh:61
bool set_time(time_t &now)
Definition: DS1307.hh:153
int write(void *ram, uint8_t size=sizeof(time_t), uint8_t pos=0)
Definition: DS1307.cpp:34
control_t control
Timekeeper control register.
Definition: DS1307.hh:98