File Coverage

blib/lib/RPi/RTC/DS3231.pm
Criterion Covered Total %
statement 12 123 9.7
branch 0 54 0.0
condition 0 15 0.0
subroutine 4 23 17.3
pod 15 15 100.0
total 31 230 13.4


line stmt bran cond sub pod time code
1             package RPi::RTC::DS3231;
2              
3 15     15   69386 use strict;
  15         26  
  15         431  
4 15     15   124 use warnings;
  15         20  
  15         1094  
5              
6             our $VERSION = '0.03';
7              
8             require XSLoader;
9             XSLoader::load('RPi::RTC::DS3231', $VERSION);
10              
11 15     15   85 use Carp qw(croak);
  15         20  
  15         1056  
12              
13 15     15   70 use constant DS3231_ADDR => 0x68;
  15         43  
  15         22672  
14              
15             sub new {
16 0     0 1   my ($class, $rtc_addr) = @_;
17              
18 0 0         $rtc_addr = DS3231_ADDR if ! defined $rtc_addr;
19              
20 0           my $self = bless {}, $class;
21 0           $self->_fd($rtc_addr);
22 0           return $self;
23             }
24              
25             # misc methods
26              
27             sub temp {
28 0     0 1   my ($self, $output) = @_;
29 0           my $celcius = getTemp($self->_fd);
30 0 0 0       my $temp = defined $output && $output eq 'f' ? $celcius * 9/5 + 32 : $celcius;
31              
32             # Normalize to two decimal places so the return value is consistent
33             # regardless of scale (Fahrenheit conversion can yield a single decimal).
34 0           return sprintf "%.2f", $temp;
35             }
36              
37             # time/date methods
38              
39             sub year {
40 0     0 1   my ($self, $year) = @_;
41 0 0         if (defined $year){
42 0           setYear($self->_fd, $year);
43             }
44 0           return getYear($self->_fd);
45             }
46             sub month {
47 0     0 1   my ($self, $month) = @_;
48 0 0         if (defined $month){
49 0           setMonth($self->_fd, $month);
50             }
51 0           return getMonth($self->_fd);
52             }
53             sub mday {
54 0     0 1   my ($self, $mday) = @_;
55 0 0         if (defined $mday){
56 0           setDayOfMonth($self->_fd, $mday);
57             }
58 0           return getDayOfMonth($self->_fd);
59             }
60             sub day {
61 0     0 1   my ($self, $wday) = @_;
62 0 0         if (defined $wday){
63 0           setDayOfWeek($self->_fd, $wday);
64             }
65 0           return getDayOfWeek($self->_fd);
66             }
67             sub hour {
68 0     0 1   my ($self, $hour) = @_;
69 0 0         if (defined $hour){
70 0           setHour($self->_fd, $hour);
71             }
72              
73 0           return getHour($self->_fd);
74             }
75             sub min {
76 0     0 1   my ($self, $min) = @_;
77 0 0         if (defined $min){
78 0           setMinutes($self->_fd, $min);
79             }
80 0           return getMinutes($self->_fd);
81             }
82             sub sec {
83 0     0 1   my ($self, $sec) = @_;
84 0 0         if (defined $sec){
85 0           setSeconds($self->_fd, $sec);
86             }
87 0           return getSeconds($self->_fd);
88             }
89              
90             # auxillary time/date methods
91              
92             sub am_pm {
93 0     0 1   my ($self, $meridien) = @_;
94              
95 0 0         if (defined $meridien) {
96 0 0 0       if ($meridien ne 'AM' && $meridien ne 'PM'){
97 0           croak("am_pm() requires either 'AM' or 'PM' as a param\n");
98             }
99 0 0         if ($meridien eq 'AM') {
100 0           $meridien = 0;
101             }
102             else {
103 0           $meridien = 1;
104             }
105 0           setMeridien($self->_fd, $meridien);
106             }
107 0 0         return getMeridien($self->_fd) ? 'PM' : 'AM';
108             }
109             sub clock_hours {
110 0     0 1   my ($self, $value) = @_;
111 0 0         if (defined $value){
112 0 0 0       if ($value !~ /\d+/ || ($value != 12 && $value != 24)){
      0        
113 0           croak "clock_hours() requires either 12 or 24 as a parameter\n";
114             }
115 0 0         $value = $value == 12 ? 1 : 0;
116 0           setMilitary($self->_fd, $value);
117             }
118 0 0         return getMilitary($self->_fd) ? 12 : 24;
119             }
120             sub hms {
121 0     0 1   my ($self) = @_;
122              
123 0           my $h = _stringify(getHour($self->_fd));
124 0           my $m = _stringify(getMinutes($self->_fd));
125 0           my $s = _stringify(getSeconds($self->_fd));
126              
127 0           my $hms = "$h:$m:$s";
128              
129 0 0         $hms = "$hms " . $self->am_pm if $self->clock_hours == 12;
130              
131 0           return $hms;
132             }
133             sub date_time {
134 0     0 1   my ($self, $datetime) = @_;
135              
136 0 0         if (defined $datetime){
137 0           my @dt;
138              
139 0 0         if (@dt =
140             $datetime =~ /(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})/)
141             {
142 0           my $ch = $self->clock_hours;
143              
144 0 0         $self->clock_hours(24) if $ch == 12;
145              
146 0           $self->year($dt[0]);
147 0           $self->month($dt[1]);
148 0           $self->mday($dt[2]);
149              
150 0           $self->hour($dt[3]);
151 0           $self->min($dt[4]);
152 0           $self->sec($dt[5]);
153              
154 0 0         $self->clock_hours(12) if $ch == 12;
155             }
156             else {
157 0           croak(
158             "datetime parameter must be in the format " .
159             "'yyyy-mm-dd hh:mm:ss'. You supplied '$datetime'\n"
160             );
161             }
162             }
163 0           my $y = getYear($self->_fd);
164 0           my $mon = _stringify(getMonth($self->_fd));
165 0           my $day = _stringify(getDayOfMonth($self->_fd));
166              
167 0           my $h;
168              
169 0 0         if ($self->clock_hours == 12){
170 0           $self->clock_hours(24);
171 0           $h = _stringify(getHour($self->_fd));
172 0           $self->clock_hours(12);
173             }
174             else {
175 0           $h = _stringify(getHour($self->_fd));
176             }
177              
178 0           my $m = _stringify(getMinutes($self->_fd));
179 0           my $s = _stringify(getSeconds($self->_fd));
180              
181 0           return "$y-$mon-$day $h:$m:$s";
182             }
183             sub dt_hash {
184 0     0 1   my ($self) = @_;
185              
186 0           my %dt;
187              
188 0           $dt{year} = getYear($self->_fd);
189 0           $dt{month} = _stringify(getMonth($self->_fd));
190 0           $dt{day} = _stringify(getDayOfMonth($self->_fd));
191              
192 0 0         if ($self->clock_hours == 12){
193 0           $self->clock_hours(24);
194 0           $dt{hour} = _stringify(getHour($self->_fd));
195 0           $self->clock_hours(12);
196             }
197             else {
198 0           $dt{hour} = _stringify(getHour($self->_fd));
199             }
200              
201 0           $dt{minute} = _stringify(getMinutes($self->_fd));
202 0           $dt{second} = _stringify(getSeconds($self->_fd));
203              
204 0           return %dt;
205             }
206              
207             # operation methods
208              
209             sub close {
210 0     0 1   my ($self) = @_;
211 0           _close($self->_fd);
212             }
213              
214             # internal methods
215              
216             sub _get_register {
217             # retrieve the contents of an entire 8-bit register
218 0     0     my ($self, $reg) = @_;
219 0           return getRegister($self->_fd, $reg);
220             }
221             sub _fd {
222             # initializes the I2C communications
223 0     0     my ($self, $rtc_addr) = @_;
224              
225 0 0         if (! exists $self->{fd}){
226 0           $self->{fd} = getFh($rtc_addr);
227             }
228 0           return $self->{fd};
229             }
230             sub _stringify {
231             # left-pads with a zero any integer with only a single digit
232 0     0     my ($int) = @_;
233              
234 0 0 0       if (! defined $int || $int !~ /\d+/){
235 0           croak "as_string() requires an integer to check/convert to str\n";
236             }
237              
238 0 0         return length($int) < 2 ? "0$int" : $int;
239             }
240              
241       0     sub __vim {};
242              
243             1;
244             __END__