| 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, 2019-2023 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use v5.26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Object::Pad 0.800; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | package Device::Chip::BNO055 0.04; | ||||||||||||||||||||||||||||
| 11 | class Device::Chip::BNO055 | ||||||||||||||||||||||||||||
| 12 | 1 | ▶ | :isa(Device::Chip); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use Data::Bitfield 0.03 qw( bitfield enumfield ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use constant PROTOCOL => "I2C"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 NAME | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | C<Device::Chip::BNO055> - chip driver for F<BNO055> | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | use Device::Chip::BNO055; | ||||||||||||||||||||||||||||
| 34 | use Future::AsyncAwait; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | my $chip = Device::Chip::BNO055->new; | ||||||||||||||||||||||||||||
| 37 | await $chip->mount( Device::Chip::Adapter::...->new ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | This L<Device::Chip> subclass provides specific communications to a | ||||||||||||||||||||||||||||
| 42 | F<Bosch> F<BNO055> orientation sensor chip. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | The reader is presumed to be familiar with the general operation of this chip; | ||||||||||||||||||||||||||||
| 45 | the documentation here will not attempt to explain or define chip-specific | ||||||||||||||||||||||||||||
| 46 | concepts or features, only the use of this module to access them. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub I2C_options | ||||||||||||||||||||||||||||
| 51 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
I2C_options: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | return ( | ||||||||||||||||||||||||||||
| 53 | 3 | addr => 0x29, | |||||||||||||||||||||||||||
| 54 | max_bitrate => 400E3, | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 METHODS | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | The following methods documented in an C<await> expression return L<Future> | ||||||||||||||||||||||||||||
| 61 | instances. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Actual chip hardware uses "paged" registers. We'll fake this for the cache | ||||||||||||||||||||||||||||
| 66 | # by using the 8th bit to store the page number | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | ▶ | use constant REGPAGE_1 => 0x80; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 4 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 4 | REG_CHIP_ID => 0x00, | |||||||||||||||||||||||||||
| 72 | REG_PAGE_ID => 0x07, | ||||||||||||||||||||||||||||
| 73 | REG_ACC_DATA_X_LSB => 0x08, | ||||||||||||||||||||||||||||
| 74 | REG_MAG_DATA_X_LSB => 0x0E, | ||||||||||||||||||||||||||||
| 75 | REG_GYR_DATA_X_LSB => 0x14, | ||||||||||||||||||||||||||||
| 76 | REG_EUL_Heading_LSB => 0x1A, | ||||||||||||||||||||||||||||
| 77 | REG_QUA_Data_w_LSB => 0x20, | ||||||||||||||||||||||||||||
| 78 | REG_LIA_Data_X_LSB => 0x28, | ||||||||||||||||||||||||||||
| 79 | REG_GRV_Data_X_LSB => 0x2E, | ||||||||||||||||||||||||||||
| 80 | REG_UNIT_SEL => 0x3B, | ||||||||||||||||||||||||||||
| 81 | REG_OPR_MODE => 0x3D, | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | REG_ACC_Config => REGPAGE_1 | 0x08, | ||||||||||||||||||||||||||||
| 84 | 4 | }; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 4 | my @OPR_MODES = qw( | |||||||||||||||||||||||||||
| 87 | CONFIGMODE ACCONLY MAGONLY GYROONLY ACCMAG ACCGYRO MAGGYRO AMG | ||||||||||||||||||||||||||||
| 88 | IMU COMPASS M4G NDOF_FMC_OFF NDOF | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4 | bitfield { format => "bytes-LE" }, OPR_MODE => | |||||||||||||||||||||||||||
| 92 | OPR_MODE => enumfield(0, @OPR_MODES); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | bitfield { format => "bytes-LE" }, CONFIG => | |||||||||||||||||||||||||||
| 95 | # Page 0 | ||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||
| 97 | # REG_UNIT_SEL | ||||||||||||||||||||||||||||
| 98 | ACC_Unit => enumfield( 0, qw( m/s² mg )), | ||||||||||||||||||||||||||||
| 99 | GYR_Unit => enumfield( 1, qw( dps rps )), | ||||||||||||||||||||||||||||
| 100 | EUL_Unit => enumfield( 2, qw( degrees radians )), | ||||||||||||||||||||||||||||
| 101 | TEMP_Unit => enumfield( 4, qw( Celsius Farenheit )), | ||||||||||||||||||||||||||||
| 102 | ORI_Android_Windows => enumfield( 7, qw( Windows Android )), | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # REG_OPR_MODE | ||||||||||||||||||||||||||||
| 105 | OPR_MODE => enumfield(2*8+0, @OPR_MODES), | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # REG_PWR_MODE | ||||||||||||||||||||||||||||
| 108 | PWR_MODE => enumfield(3*8+0, qw( normal low-power suspend . )), | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # REG_TEMP_SOURCE | ||||||||||||||||||||||||||||
| 111 | TEMP_Source => enumfield(5*8+0, qw( accelerometer gyroscope . . )), | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # REG_AXIS_MAP_CONFIG | ||||||||||||||||||||||||||||
| 114 | X_AXIS_MAP => enumfield(6*8+0, qw( X Y Z . )), | ||||||||||||||||||||||||||||
| 115 | Y_AXIS_MAP => enumfield(6*8+2, qw( X Y Z . )), | ||||||||||||||||||||||||||||
| 116 | Z_AXIS_MAP => enumfield(6*8+4, qw( X Y Z . )), | ||||||||||||||||||||||||||||
| 117 | # REG_AXIS_MAP_SIGN | ||||||||||||||||||||||||||||
| 118 | X_AXIS_SIGN => enumfield(7*8+0, qw( positive negative )), | ||||||||||||||||||||||||||||
| 119 | Y_AXIS_SIGN => enumfield(7*8+1, qw( positive negative )), | ||||||||||||||||||||||||||||
| 120 | Z_AXIS_SIGN => enumfield(7*8+2, qw( positive negative )), | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Page 1 | ||||||||||||||||||||||||||||
| 123 | # | ||||||||||||||||||||||||||||
| 124 | # REG_ACC_Config | ||||||||||||||||||||||||||||
| 125 | ACC_Range => enumfield(8*8+0, qw( 2G 4G 8G 16G )), | ||||||||||||||||||||||||||||
| 126 | ACC_BW => enumfield(8*8+2, qw( 7.81Hz 15.63Hz 31.25Hz 62.5Hz 125Hz 250Hz 500Hz 1000Hz )), | ||||||||||||||||||||||||||||
| 127 | ACC_PWR_Mode => enumfield(8*8+5, qw( normal suspend low-power-1 standby low-power-2 deep-suspend . . )), | ||||||||||||||||||||||||||||
| 128 | # REG_MAG_Config | ||||||||||||||||||||||||||||
| 129 | MAG_Data_output_rate => enumfield(9*8+0, qw( 2Hz 6Hz 8Hz 10Hz 15Hz 20Hz 25Hz 30Hz )), | ||||||||||||||||||||||||||||
| 130 | MAG_OPR_Mode => enumfield(9*8+3, qw( low-power regular enhanced-regular high-accuracy )), | ||||||||||||||||||||||||||||
| 131 | MAG_Power_mode => enumfield(9*8+6, qw( normal sleep suspend force-mode )), | ||||||||||||||||||||||||||||
| 132 | # GYR_Config_0 | ||||||||||||||||||||||||||||
| 133 | GYR_Range => enumfield(10*8+0, qw( 2000dps 1000dps 500dps 250dps 125dps . . . )), | ||||||||||||||||||||||||||||
| 134 | GYR_Bandwidth => enumfield(10*8+3, qw( 523Hz 230Hz 116Hz 47Hz 23Hz 12Hz 64Hz 32Hz )), | ||||||||||||||||||||||||||||
| 135 | # GYR_Config_1 | ||||||||||||||||||||||||||||
| 136 | GYR_Power_Mode => enumfield(11*8+0, qw( normal fast-powerup deep-suspend suspend advanced-powersave . . . )), | ||||||||||||||||||||||||||||
| 137 | ; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | field $_reg_page = 0; | ||||||||||||||||||||||||||||
| 140 | 4 | field $_regcache = ""; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 4 | field $_ACC_Unit; | |||||||||||||||||||||||||||
| 143 | 4 | field $_GYR_Unit; | |||||||||||||||||||||||||||
| 144 | 4 | field $_EUL_Unit; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 12 | ▶ | async method read_reg ( $reg, $len ) | ||||||||||||||||||||||||||
|
Subroutine
read_reg: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 147 | 12 | { | |||||||||||||||||||||||||||
| 148 | 12 | my $protocol = $self->protocol; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 12 | my $page = 0 + ( $reg >= REGPAGE_1 ); $reg &= ~REGPAGE_1; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 12 | ▶ | if( $_reg_page != $page ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 3 | await $protocol->write( pack "C C", REG_PAGE_ID, $page ); | |||||||||||||||||||||||||||
| 154 | 3 | $_reg_page = $page; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 12 | return await $protocol->write_then_read( pack( "C", $reg ), $len ); | |||||||||||||||||||||||||||
| 158 | 4 | } | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 14 | ▶ | async method cached_read_reg ( $reg, $len ) | ||||||||||||||||||||||||||
|
Subroutine
cached_read_reg: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 161 | 14 | { | |||||||||||||||||||||||||||
| 162 | 4 | ▶ | no warnings 'numeric'; # quiet the warning about negative repeat count | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 14 | ▶ | if( length $_regcache >= $reg + $len ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 10 | return substr( $_regcache, $reg, $len ); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 4 | my $bytes = await $self->read_reg( $reg, $len ); | |||||||||||||||||||||||||||
| 169 | 4 | $_regcache .= "\0" x ( $reg + $len - length $_regcache ); | |||||||||||||||||||||||||||
| 170 | 4 | substr( $_regcache, $reg, $len ) = $bytes; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 4 | return $bytes; | |||||||||||||||||||||||||||
| 173 | 4 | } | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | ▶ | async method write_reg ( $reg, $bytes ) | ||||||||||||||||||||||||||
|
Subroutine
write_reg: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | 2 | { | |||||||||||||||||||||||||||
| 177 | 2 | my $protocol = $self->protocol; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 2 | my $page = 0 + ( $reg >= REGPAGE_1 ); $reg &= ~REGPAGE_1; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 2 | ▶ | if( $_reg_page != $page ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 1 | await $protocol->write( pack "C C", REG_PAGE_ID, $page ); | |||||||||||||||||||||||||||
| 183 | 1 | $_reg_page = $page; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 2 | return await $protocol->write( pack "C a*", $reg, $bytes ); | |||||||||||||||||||||||||||
| 187 | 4 | } | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 3 | ▶ | async method cached_write_reg ( $reg, $bytes ) | ||||||||||||||||||||||||||
|
Subroutine
cached_write_reg: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 190 | 3 | { | |||||||||||||||||||||||||||
| 191 | 4 | ▶ | no warnings 'numeric'; # quiet the warning about negative repeat count | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # Trim common prefix/suffix | ||||||||||||||||||||||||||||
| 194 | 3 | ▶ | while( length $bytes and substr( $bytes, 0, 1 ) eq substr( $_regcache, $reg, 1 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $bytes and substr($bytes, 0, 1) eq substr($_regcache, $reg, 1)
MC/DC100%2/2
length $bytes and substr($bytes, 0, 1) eq substr($_regcache, $reg, 1)
length $bytes
substr($bytes, 0, 1) eq substr($_regcache, $reg, 1)
Truth table100%3/3
A and B
A= length $bytesB= substr($bytes, 0, 1) eq substr($_regcache, $reg, 1)
| |||||||||||||||||||||||||||||
| 195 | 11 | $bytes =~ s/^.//s; | |||||||||||||||||||||||||||
| 196 | 11 | $reg++; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 3 | my $len = length $bytes; | |||||||||||||||||||||||||||
| 200 | 3 | ▶ | while( $len and substr( $bytes, $len - 1, 1 ) eq substr( $_regcache, $reg + $len - 1, 1 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$len and substr($bytes, $len - 1, 1) eq substr($_regcache, $reg + $len - 1, 1)
MC/DC0%0/2
$len and substr($bytes, $len - 1, 1) eq substr($_regcache, $reg + $len - 1, 1)
$len
substr($bytes, $len - 1, 1) eq substr($_regcache, $reg + $len - 1, 1)
Truth table66%2/3
A and B
A= $lenB= substr($bytes, $len - 1, 1) eq substr($_regcache, $reg + $len - 1, 1)
| |||||||||||||||||||||||||||||
| 201 | 0 | $bytes =~ s/.$//s; | |||||||||||||||||||||||||||
| 202 | 0 | $len--; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 3 | ▶ | return unless $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 2 | await $self->write_reg( $reg, $bytes ); | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 2 | $_regcache .= "\0" x ( $reg + $len - length $_regcache ); | |||||||||||||||||||||||||||
| 210 | 2 | substr( $_regcache, $reg, $len ) = $bytes; | |||||||||||||||||||||||||||
| 211 | 4 | } | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head2 read_ids | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | $ids = await $chip->read_ids; | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Returns an 8-character string composed of the four ID registers. For a | ||||||||||||||||||||||||||||
| 218 | C<BNO055> chip this should be the string | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | "A0FB320F" | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =cut | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 1 | ▶ | async method read_ids () | ||||||||||||||||||||||||||
|
Subroutine
read_ids: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 225 | 1 | { | |||||||||||||||||||||||||||
| 226 | 1 | return uc unpack "H*", await $self->read_reg( REG_CHIP_ID, 4 ); | |||||||||||||||||||||||||||
| 227 | 4 | } | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head2 read_config | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | $config = await $chip->read_config; | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Returns the current chip configuration. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =cut | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 7 | ▶ | async method read_config () | ||||||||||||||||||||||||||
|
Subroutine
read_config: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 238 | 7 | { | |||||||||||||||||||||||||||
| 239 | return { | ||||||||||||||||||||||||||||
| 240 | 7 | unpack_CONFIG( join "", await Future->needs_all( | |||||||||||||||||||||||||||
| 241 | $self->cached_read_reg( REG_UNIT_SEL, 8 ), | ||||||||||||||||||||||||||||
| 242 | $self->cached_read_reg( REG_ACC_Config, 4 ), | ||||||||||||||||||||||||||||
| 243 | ) ), | ||||||||||||||||||||||||||||
| 244 | }; | ||||||||||||||||||||||||||||
| 245 | 4 | } | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head2 change_config | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | await $chip->change_config( %changes ); | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Changes the configuration. Any field names not mentioned will be preserved at | ||||||||||||||||||||||||||||
| 252 | their existing values. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | This method can only be used while the chip is in config mode, and cannot | ||||||||||||||||||||||||||||
| 255 | itself be used to set C<OPR_MODE>. For that, use L</set_opr_mode>. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =cut | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 1 | ▶ | async method change_config ( %changes ) | ||||||||||||||||||||||||||
|
Subroutine
change_config: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 260 | 1 | { | |||||||||||||||||||||||||||
| 261 | $changes{OPR_MODE} and | ||||||||||||||||||||||||||||
| 262 | 1 | ▶ | croak "->change_config cannot change OPR_MODE; use ->set_opr_mode\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 1 | my $config = await $self->read_config; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 1 | ▶ | $config->{OPR_MODE} eq "CONFIGMODE" or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | croak "Can only ->change_config when in CONFIGMODE"; | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 1 | $config->{$_} = $changes{$_} for keys %changes; | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 1 | my ( $page0, $page1 ) = unpack "a8 a4", pack_CONFIG( %$config ); | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 1 | await Future->needs_all( | |||||||||||||||||||||||||||
| 274 | $self->cached_write_reg( REG_UNIT_SEL, $page0 ), | ||||||||||||||||||||||||||||
| 275 | $self->cached_write_reg( REG_ACC_Config, $page1 ), | ||||||||||||||||||||||||||||
| 276 | ); | ||||||||||||||||||||||||||||
| 277 | 4 | } | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =head2 set_opr_mode | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | await $chip->set_opr_mode( $mode ); | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | Sets the C<OPR_MODE> register. | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | =cut | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 1 | ▶ | async method set_opr_mode ( $mode ) | ||||||||||||||||||||||||||
|
Subroutine
set_opr_mode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 288 | 1 | { | |||||||||||||||||||||||||||
| 289 | # TODO: Only allow state transitions when one of old or new mode is CONFIGMODE | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 1 | await $self->cached_write_reg( REG_OPR_MODE, pack_OPR_MODE( OPR_MODE => $mode ) ); | |||||||||||||||||||||||||||
| 292 | 4 | } | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =head2 read_accelerometer_raw | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | ( $x, $y, $z ) = await $chip->read_accelerometer_raw; | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | Returns the most recent accelerometer readings in raw 16bit signed integers | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =cut | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 1 | ▶ | async method read_accelerometer_raw () | ||||||||||||||||||||||||||
|
Subroutine
read_accelerometer_raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 303 | 1 | { | |||||||||||||||||||||||||||
| 304 | 1 | return unpack "s< s< s<", await $self->read_reg( REG_ACC_DATA_X_LSB, 6 ); | |||||||||||||||||||||||||||
| 305 | 4 | } | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =head2 read_accelerometer | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | ( $x, $y, $z ) = await $chip->read_accelerometer; | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Returns the most recent accelerometer readings in converted units, either | ||||||||||||||||||||||||||||
| 312 | C<m/s²> or C<G> depending on the chip's C<ACC_Unit> configuration. | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =cut | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 1 | ▶ | async method read_accelerometer () | ||||||||||||||||||||||||||
|
Subroutine
read_accelerometer: called CC=4 SCAR=15.5
Pod
documented
| |||||||||||||||||||||||||||||
| 317 | 1 | { | |||||||||||||||||||||||||||
| 318 | 1 | ▶ | my $units = $_ACC_Unit //= ( await $self->read_config )->{ACC_Unit}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$_ACC_Unit //= <custom op await>->{"ACC_Unit"}
MC/DC0%0/2
$_ACC_Unit //= <custom op await>->{"ACC_Unit"}
$_ACC_Unit
<custom op await>->{"ACC_Unit"}
Truth table33%1/3
A //= B
A= $_ACC_UnitB= <custom op await>->{"ACC_Unit"}
| |||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 1 | ▶ | if( $units eq "mg" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 321 | 0 | return map { $_ / 1000 } await $self->read_accelerometer_raw; | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | elsif( $units eq "m/s²" ) { | ||||||||||||||||||||||||||||
| 324 | 1 | return map { $_ / 100 } await $self->read_accelerometer_raw; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | 4 | } | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head2 read_magnetometer_raw | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | ( $x, $y, $z ) = await $chip->read_magnetometer_raw; | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | Returns the most recent magnetometer readings in raw 16bit signed integers | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =cut | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 1 | ▶ | async method read_magnetometer_raw () | ||||||||||||||||||||||||||
|
Subroutine
read_magnetometer_raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 337 | 1 | { | |||||||||||||||||||||||||||
| 338 | 1 | return unpack "s< s< s<", await $self->read_reg( REG_MAG_DATA_X_LSB, 6 ); | |||||||||||||||||||||||||||
| 339 | 4 | } | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head2 read_magnetometer | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | ( $x, $y, $z ) = await $chip->read_magnetometer; | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | Returns the most recent magnetometer readings in converted units of C<µT>. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =cut | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 1 | ▶ | async method read_magnetometer () | ||||||||||||||||||||||||||
|
Subroutine
read_magnetometer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 350 | 1 | { | |||||||||||||||||||||||||||
| 351 | 1 | return map { $_ / 16 } await $self->read_magnetometer_raw; | |||||||||||||||||||||||||||
| 352 | 4 | } | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =head2 read_gyroscope_raw | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | ( $x, $y, $z ) = await $chip->read_gyroscope_raw; | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | Returns the most recent gyroscope readings in raw 16bit signed integers | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =cut | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 1 | ▶ | async method read_gyroscope_raw () | ||||||||||||||||||||||||||
|
Subroutine
read_gyroscope_raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 363 | 1 | { | |||||||||||||||||||||||||||
| 364 | 1 | return unpack "s< s< s<", await $self->read_reg( REG_GYR_DATA_X_LSB, 6 ); | |||||||||||||||||||||||||||
| 365 | 4 | } | |||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =head2 read_gyroscope | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | ( $x, $y, $z ) = await $chip->read_gyroscope; | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | Returns the most recent gyroscope readings in converted units, either | ||||||||||||||||||||||||||||
| 372 | C<dps> or C<rps> depending on the chip's C<GYR_Unit> configuration. | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =cut | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 1 | ▶ | async method read_gyroscope () | ||||||||||||||||||||||||||
|
Subroutine
read_gyroscope: called CC=4 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 377 | 1 | { | |||||||||||||||||||||||||||
| 378 | 1 | ▶ | my $units = $_GYR_Unit //= ( await $self->read_config )->{GYR_Unit}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$_GYR_Unit //= <custom op await>->{"GYR_Unit"}
MC/DC0%0/2
$_GYR_Unit //= <custom op await>->{"GYR_Unit"}
$_GYR_Unit
<custom op await>->{"GYR_Unit"}
Truth table33%1/3
A //= B
A= $_GYR_UnitB= <custom op await>->{"GYR_Unit"}
| |||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 1 | ▶ | if( $units eq "dps" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 381 | 1 | return map { $_ / 16 } await $self->read_gyroscope_raw; | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | elsif( $units eq "rps" ) { | ||||||||||||||||||||||||||||
| 384 | 0 | return map { $_ / 900 } await $self->read_gyroscope_raw; | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | 4 | } | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head2 read_euler_angles | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | ( $heading, $roll, $pitch ) = await $chip->read_euler_angles; | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | Returns the most recent Euler angle fusion readings in converted units, either | ||||||||||||||||||||||||||||
| 393 | degrees or radians depending on the chip's C<EUL_units> configuration. | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =cut | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 1 | ▶ | async method read_euler_angles () | ||||||||||||||||||||||||||
|
Subroutine
read_euler_angles: called CC=4 SCAR=16.0
Pod
documented
| |||||||||||||||||||||||||||||
| 398 | 1 | { | |||||||||||||||||||||||||||
| 399 | 1 | ▶ | my $units = $_EUL_Unit //= ( await $self->read_config )->{EUL_Unit}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$_EUL_Unit //= <custom op await>->{"EUL_Unit"}
MC/DC0%0/2
$_EUL_Unit //= <custom op await>->{"EUL_Unit"}
$_EUL_Unit
<custom op await>->{"EUL_Unit"}
Truth table33%1/3
A //= B
A= $_EUL_UnitB= <custom op await>->{"EUL_Unit"}
| |||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | 1 | my ( $heading, $roll, $pitch ) = unpack "s< s< s<", await $self->read_reg( REG_EUL_Heading_LSB, 6 ); | |||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 1 | ▶ | if( $units eq "degrees" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 404 | 1 | return map { $_ / 16 } ( $heading, $roll, $pitch ); | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | elsif( $units eq "radians" ) { | ||||||||||||||||||||||||||||
| 407 | 0 | return map { $_ / 900 } ( $heading, $roll, $pitch ); | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | 4 | } | |||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =head2 read_quarternion | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | ( $w, $x, $y, $z ) = await $chip->read_quarternion; | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | Returns the most recent quarternion fusion readings in converted units as | ||||||||||||||||||||||||||||
| 416 | scaled numbers between -1 and 1. | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =cut | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 1 | ▶ | async method read_quarternion () | ||||||||||||||||||||||||||
|
Subroutine
read_quarternion: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 421 | 1 | { | |||||||||||||||||||||||||||
| 422 | 1 | my ( $w, $x, $y, $z ) = unpack "s< s< s< s<", await $self->read_reg( REG_QUA_Data_w_LSB, 8 ); | |||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 1 | return map { $_ / 2**14 } ( $w, $x, $y, $z ); | |||||||||||||||||||||||||||
| 425 | 4 | } | |||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | =head2 read_linear_acceleration | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | ( $x, $y, $z ) = await $chip->read_linear_acceleration; | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | Returns the most recent linear acceleration fusion readings in converted | ||||||||||||||||||||||||||||
| 432 | units, either C<m/s²> or C<G> depending on the chip's C<ACC_units> | ||||||||||||||||||||||||||||
| 433 | configuration. | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | =cut | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 1 | ▶ | async method read_linear_acceleration () | ||||||||||||||||||||||||||
|
Subroutine
read_linear_acceleration: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 438 | 1 | { | |||||||||||||||||||||||||||
| 439 | 1 | ▶ | my $units = $_ACC_Unit //= ( await $self->read_config )->{ACC_Unit}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$_ACC_Unit //= <custom op await>->{"ACC_Unit"}
MC/DC0%0/2
$_ACC_Unit //= <custom op await>->{"ACC_Unit"}
$_ACC_Unit
<custom op await>->{"ACC_Unit"}
Truth table33%1/3
A //= B
A= $_ACC_UnitB= <custom op await>->{"ACC_Unit"}
| |||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | 1 | my ( $x, $y, $z ) = unpack "s< s< s<", await $self->read_reg( REG_LIA_Data_X_LSB, 6 ); | |||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 1 | ▶ | if( $units eq "mg" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 444 | 0 | return map { $_ / 1000 } ( $x, $y, $z ); | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | elsif( $units eq "m/s²" ) { | ||||||||||||||||||||||||||||
| 447 | 1 | return map { $_ / 100 } ( $x, $y, $z ); | |||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||
| 449 | 4 | } | |||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =head2 read_linear_acceleration | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | ( $x, $y, $z ) = await $chip->read_linear_acceleration; | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | Returns the most recent gravity fusion readings in converted units, either | ||||||||||||||||||||||||||||
| 456 | C<m/s²> or C<G> depending on the chip's C<ACC_units> configuration. | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =cut | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | 1 | ▶ | async method read_gravity () | ||||||||||||||||||||||||||
|
Subroutine
read_gravity: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 461 | 1 | { | |||||||||||||||||||||||||||
| 462 | 1 | ▶ | my $units = $_ACC_Unit //= ( await $self->read_config )->{ACC_Unit}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$_ACC_Unit //= <custom op await>->{"ACC_Unit"}
MC/DC0%0/2
$_ACC_Unit //= <custom op await>->{"ACC_Unit"}
$_ACC_Unit
<custom op await>->{"ACC_Unit"}
Truth table33%1/3
A //= B
A= $_ACC_UnitB= <custom op await>->{"ACC_Unit"}
| |||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | 1 | my ( $x, $y, $z ) = unpack "s< s< s<", await $self->read_reg( REG_GRV_Data_X_LSB, 6 ); | |||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 1 | ▶ | if( $units eq "mg" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 467 | 0 | return map { $_ / 1000 } ( $x, $y, $z ); | |||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | elsif( $units eq "m/s²" ) { | ||||||||||||||||||||||||||||
| 470 | 1 | return map { $_ / 100 } ( $x, $y, $z ); | |||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | 4 | } | |||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | =cut | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 4 | 0x55AA; | |||||||||||||||||||||||||||