blib/lib/Device/Chip/DS1307.pm

filter: Statementstmt 37.9% 63 / 166 Branchbran 25.0% 1 / 4 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.4% 15 / 33 Podpod 9.5% 2 / 21 totaltotal 35.8% 82 / 229 CC 4 SCAR 5.9
CC
4
Cov
38%
read_hours
6.0
write_time
3.3
read_reg_u8
2.0
CRAP 1.8
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2015-2023 -- leonerd@leonerd.org.uk
5  
6 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Device::Chip::DS1307 0.08;
11 class Device::Chip::DS1307
12 3 ▶ :isa(Device::Chip::Base::RegisteredI2C);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use constant DEFAULT_ADDR => 0x68;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =encoding UTF-8
23  
24 =head1 NAME
25  
26 C<Device::Chip::DS1307> - chip driver for a F<DS1307>
27  
28 =head1 DESCRIPTION
29  
30 This L<Device::Chip> subclass provides specific communication to a
31 F<Maxim Integrated> F<DS1307> chip attached to a computer via an I²C adapter.
32  
33 =cut
34  
35 field $_address :param = DEFAULT_ADDR;
36  
37 3 method I2C_options
38 2 ▶{
Subroutine I2C_options: called CC=1 SCAR=0.0
Pod undocumented
39 return (
40 2 addr => $_address,
41 max_bitrate => 100E3,
42 );
43 3 }
44  
45 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 REG_SECONDS => 0x00,
47 REG_MINUTES => 0x01,
48 REG_HOURS => 0x02,
49 REG_WDAY => 0x03,
50 REG_MDAY => 0x04,
51 REG_MONTH => 0x05,
52 REG_YEAR => 0x06,
53 REG_CONTROL => 0x07,
54 3 };
55  
56 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
57 # REG_SECONDS
58 3 MASK_CLOCKHALT => 1<<7,
59  
60 # REG_HOURS
61 MASK_12H => 1<<6,
62 MASK_PM => 1<<5,
63  
64 # REG_CONTROL
65 MASK_OUT => 1<<7,
66 MASK_SQWE => 1<<4,
67 MASK_RS => 3<<0,
68 3 };
69  
70 0 ▶async method read_reg_u8 ( $reg )
Subroutine read_reg_u8: not called CC=1 SCAR=6.9
Pod undocumented
71 0 {
72 0 return unpack "C", await $self->read_reg( $reg );
73 3 }
74  
75 0 ▶async method write_reg_u8 ( $reg, $value )
Subroutine write_reg_u8: not called CC=1 SCAR=6.9
Pod undocumented
76 0 {
77 0 await $self->write_reg( $reg, pack "C", $value );
78 3 }
79  
80 6 ▶sub _unpack_bcd { ( $_[0] >> 4 )*10 + ( $_[0] % 16 ) }
Subroutine _unpack_bcd: called CC=1 SCAR=0.0
81 7 ▶sub _pack_bcd { int( $_[0] / 10 ) << 4 | ( $_[0] % 10 ) }
Subroutine _pack_bcd: called CC=1 SCAR=0.0
82  
83 0 ▶async method read_reg_bcd ( $reg )
Subroutine read_reg_bcd: not called CC=1 SCAR=6.9
Pod undocumented
84 0 {
85 0 return _unpack_bcd unpack "C", await $self->read_reg( $reg );
86 3 }
87  
88 0 ▶async method write_reg_bcd ( $reg, $value )
Subroutine write_reg_bcd: not called CC=1 SCAR=6.9
Pod undocumented
89 0 {
90 0 await $self->write_reg( $reg, pack "C", _pack_bcd( $value ) );
91 3 }
92  
93 =head1 METHODS
94  
95 The following methods documented in an C<await> expression return L<Future>
96 instances.
97  
98 =cut
99  
100 =head2 read_FIELD
101  
102 $sec = await $ds->read_seconds;
103 $min = await $ds->read_minutes;
104 $hr = await $ds->read_hours;
105 $wday = await $ds->read_wday;
106 $mday = await $ds->read_mday;
107 $mon = await $ds->read_month;
108 $year = await $ds->read_year;
109  
110 Reads a timekeeping field and returns a decimal integer. The following fields
111 are recognised:
112  
113 The C<hours> field is always returned in 24-hour mode, even if the chip is in
114 12-hour ("AM/PM") mode.
115  
116 =cut
117  
118 # REG_SECONDS also contains the CLOCK HALTED flag
119 0 ▶async method read_seconds ()
Subroutine read_seconds: not called CC=1 SCAR=6.9
Pod undocumented
120 0 {
121 0 my $v = await $self->read_reg_u8( REG_SECONDS );
122 0 $v &= ~MASK_CLOCKHALT;
123 0 return _unpack_bcd $v;
124 3 }
125  
126 0 ▶async method read_minutes () { return await $self->read_reg_bcd( REG_MINUTES ); }
Subroutine read_minutes: not called CC=1 SCAR=4.6
Pod undocumented
127  
128 # REG_HOURS is either in 12 or 24-hour mode.
129 0 ▶async method read_hours ()
Subroutine read_hours: not called CC=2 SCAR=17.9
Pod undocumented
130 0 {
131 0 my $v = await $self->read_reg_u8( REG_HOURS );
132 0 ▶ if( $v & MASK_12H ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v & 64) { } 0 0 0
133 0 my $pm = $v & MASK_PM;
134 0 $v &= ~(MASK_12H|MASK_PM);
135 0 return _unpack_bcd( $v ) + 12*$pm;
136 }
137 else {
138 0 return _unpack_bcd $v;
139 }
140 3 }
141  
142 0 ▶async method read_wday () { return await $self->read_reg_u8 ( REG_WDAY ); }
Subroutine read_wday: not called CC=1 SCAR=4.6
Pod undocumented
143 0 ▶async method read_mday () { return await $self->read_reg_bcd( REG_MDAY ); }
Subroutine read_mday: not called CC=1 SCAR=4.6
Pod undocumented
144 0 ▶async method read_month () { return await $self->read_reg_bcd( REG_MONTH ); }
Subroutine read_month: not called CC=1 SCAR=4.6
Pod undocumented
145 0 ▶async method read_year () { return await $self->read_reg_bcd( REG_YEAR ); }
Subroutine read_year: not called CC=1 SCAR=4.6
Pod undocumented
146  
147 =head2 write_FIELD
148  
149 await $ds->write_seconds( $sec );
150 await $ds->write_minutes( $min );
151 await $ds->write_hours ( $hr );
152 await $ds->write_wday ( $wday );
153 await $ds->write_mday ( $mday );
154 await $ds->write_month ( $mon );
155 await $ds->write_year ( $year );
156  
157 Writes a timekeeping field as a decimal integer. The following fields are
158 recognised:
159  
160 The C<hours> field is always written back in 24-hour mode.
161  
162 =cut
163  
164 0 ▶async method write_seconds () { await $self->write_reg_bcd( REG_SECONDS, $_[1] ); }
Subroutine write_seconds: not called CC=1 SCAR=4.6
Pod undocumented
165 0 ▶async method write_minutes () { await $self->write_reg_bcd( REG_MINUTES, $_[1] ); }
Subroutine write_minutes: not called CC=1 SCAR=4.6
Pod undocumented
166 0 ▶async method write_hours () { await $self->write_reg_bcd( REG_HOURS, $_[1] ); }
Subroutine write_hours: not called CC=1 SCAR=4.6
Pod undocumented
167 0 ▶async method write_wday () { await $self->write_reg_u8 ( REG_WDAY, $_[1] ); }
Subroutine write_wday: not called CC=1 SCAR=4.6
Pod undocumented
168 0 ▶async method write_mday () { await $self->write_reg_bcd( REG_MDAY, $_[1] ); }
Subroutine write_mday: not called CC=1 SCAR=4.6
Pod undocumented
169 0 ▶async method write_month () { await $self->write_reg_bcd( REG_MONTH, $_[1] ); }
Subroutine write_month: not called CC=1 SCAR=4.6
Pod undocumented
170 0 ▶async method write_year () { await $self->write_reg_bcd( REG_YEAR, $_[1] ); }
Subroutine write_year: not called CC=1 SCAR=4.6
Pod undocumented
171  
172 =head2 read_time
173  
174 @tm = await $ds->read_time;
175  
176 Returns a 7-element C<struct tm>-compatible list of values by reading the
177 timekeeping registers, suitable for passing to C<POSIX::mktime>, etc... Note
178 that the returned list does not contain the C<yday> or C<is_dst> fields.
179  
180 Because the F<DS1307> only stores a 2-digit year number, the year is presumed
181 to be in the range C<2000>-C<2099>.
182  
183 This method presumes C<POSIX>-compatible semantics for the C<wday> field
184 stored on the chip; i.e. that 0 is Sunday.
185  
186 This method performs an atomic reading of all the timekeeping registers as a
187 single I²C transaction, so is preferrable to invoking multiple calls to
188 individual read methods.
189  
190 =cut
191  
192 async method read_time
193 1 ▶{
Subroutine read_time: called CC=1 SCAR=0.0
Pod documented
194 1 my ( $bcd_sec, $bcd_min, $bcd_hour, $wday, $bcd_mday, $bcd_mon, $bcd_year ) =
195 unpack "C7", await $self->read_reg( REG_SECONDS, 7 );
196  
197 return (
198 1 _unpack_bcd( $bcd_sec ),
199 _unpack_bcd( $bcd_min ),
200 _unpack_bcd( $bcd_hour ),
201 _unpack_bcd( $bcd_mday ),
202 _unpack_bcd( $bcd_mon ) - 1,
203 _unpack_bcd( $bcd_year ) + 100,
204 $wday,
205 );
206 3 }
207  
208 =head2 write_time
209  
210 await $ds->write_time( @tm );
211  
212 Writes the timekeeping registers from a 7-element C<struct tm>-compatible list
213 of values. This method ignores the C<yday> and C<is_dst> fields, if present.
214  
215 Because the F<DS1307> only stores a 2-digit year number, the year must be in
216 the range C<2000>-C<2099> (i.e. numerical values of C<100> to C<199>).
217  
218 This method performs an atomic writing of all the timekeeping registers as a
219 single I²C transaction, so is preferrable to invoking multiple calls to
220 individual write methods.
221  
222 =cut
223  
224 async method write_time
225 1 ▶{
Subroutine write_time: called CC=3 SCAR=12.0
Pod documented
226 1 my ( $sec, $min, $hour, $mday, $mon, $year, $wday ) = @_;
227  
228 1 ▶ $year >= 100 and $year <= 199 or croak "Invalid year ($year)";
Branch: 1 branch
BranchTrueFalseTotal
$year >= 100 and $year <= 199 or croak("Invalid year ($year)") 1 0 1
Condition33%1/3
$year >= 100 and $year <= 199
!ll&&!rl&&r
001
MC/DC0%0/2
$year >= 100 and $year <= 199
$year >= 100 $year <= 199
Truth table33%1/3
A and B
A= $year >= 100B= $year <= 199
ABresult
0X0
100
111
229  
230 1 await $self->write_reg( REG_SECONDS, pack "C7",
231 _pack_bcd( $sec ),
232 _pack_bcd( $min ),
233 _pack_bcd( $hour ),
234 _pack_bcd( $wday ),
235 _pack_bcd( $mday ),
236 _pack_bcd( $mon + 1 ),
237 _pack_bcd( $year - 100 ),
238 );
239 3 }
240  
241 =head1 AUTHOR
242  
243 Paul Evans <leonerd@leonerd.org.uk>
244  
245 =cut
246  
247 3 0x55AA;
↑ Summary