34
34
from adafruit_bus_device import i2c_device
35
35
from micropython import const
36
36
37
+ try :
38
+ from typing import Optional , Tuple , Type
39
+ from types import TracebackType
40
+ from busio import I2C
41
+ except ImportError :
42
+ pass
43
+
37
44
__version__ = "0.0.0+auto.0"
38
45
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VL53L0X.git"
39
46
100
107
_VCSEL_PERIOD_FINAL_RANGE = const (1 )
101
108
102
109
103
- def _decode_timeout (val ) :
110
+ def _decode_timeout (val : int ) -> float :
104
111
# format: "(LSByte * 2^MSByte) + 1"
105
112
return float (val & 0xFF ) * math .pow (2.0 , ((val & 0xFF00 ) >> 8 )) + 1
106
113
107
114
108
- def _encode_timeout (timeout_mclks ) :
115
+ def _encode_timeout (timeout_mclks : float ) -> int :
109
116
# format: "(LSByte * 2^MSByte) + 1"
110
117
timeout_mclks = int (timeout_mclks ) & 0xFFFF
111
118
ls_byte = 0
@@ -119,12 +126,16 @@ def _encode_timeout(timeout_mclks):
119
126
return 0
120
127
121
128
122
- def _timeout_mclks_to_microseconds (timeout_period_mclks , vcsel_period_pclks ):
129
+ def _timeout_mclks_to_microseconds (
130
+ timeout_period_mclks : int , vcsel_period_pclks : int
131
+ ) -> int :
123
132
macro_period_ns = ((2304 * (vcsel_period_pclks ) * 1655 ) + 500 ) // 1000
124
133
return ((timeout_period_mclks * macro_period_ns ) + (macro_period_ns // 2 )) // 1000
125
134
126
135
127
- def _timeout_microseconds_to_mclks (timeout_period_us , vcsel_period_pclks ):
136
+ def _timeout_microseconds_to_mclks (
137
+ timeout_period_us : int , vcsel_period_pclks : int
138
+ ) -> int :
128
139
macro_period_ns = ((2304 * (vcsel_period_pclks ) * 1655 ) + 500 ) // 1000
129
140
return ((timeout_period_us * 1000 ) + (macro_period_ns // 2 )) // macro_period_ns
130
141
@@ -140,7 +151,7 @@ class VL53L0X:
140
151
# Is VL53L0X is currently continuous mode? (Needed by `range` property)
141
152
_continuous_mode = False
142
153
143
- def __init__ (self , i2c , address = 41 , io_timeout_s = 0 ) :
154
+ def __init__ (self , i2c : I2C , address : int = 41 , io_timeout_s : float = 0 ) -> None :
144
155
# pylint: disable=too-many-statements
145
156
self ._i2c = i2c
146
157
self ._device = i2c_device .I2CDevice (i2c , address )
@@ -304,38 +315,38 @@ def __init__(self, i2c, address=41, io_timeout_s=0):
304
315
# "restore the previous Sequence Config"
305
316
self ._write_u8 (_SYSTEM_SEQUENCE_CONFIG , 0xE8 )
306
317
307
- def _read_u8 (self , address ) :
318
+ def _read_u8 (self , address : int ) -> int :
308
319
# Read an 8-bit unsigned value from the specified 8-bit address.
309
320
with self ._device :
310
321
self ._BUFFER [0 ] = address & 0xFF
311
322
self ._device .write (self ._BUFFER , end = 1 )
312
323
self ._device .readinto (self ._BUFFER , end = 1 )
313
324
return self ._BUFFER [0 ]
314
325
315
- def _read_u16 (self , address ) :
326
+ def _read_u16 (self , address : int ) -> int :
316
327
# Read a 16-bit BE unsigned value from the specified 8-bit address.
317
328
with self ._device :
318
329
self ._BUFFER [0 ] = address & 0xFF
319
330
self ._device .write (self ._BUFFER , end = 1 )
320
331
self ._device .readinto (self ._BUFFER )
321
332
return (self ._BUFFER [0 ] << 8 ) | self ._BUFFER [1 ]
322
333
323
- def _write_u8 (self , address , val ) :
334
+ def _write_u8 (self , address : int , val : int ) -> None :
324
335
# Write an 8-bit unsigned value to the specified 8-bit address.
325
336
with self ._device :
326
337
self ._BUFFER [0 ] = address & 0xFF
327
338
self ._BUFFER [1 ] = val & 0xFF
328
339
self ._device .write (self ._BUFFER , end = 2 )
329
340
330
- def _write_u16 (self , address , val ) :
341
+ def _write_u16 (self , address : int , val : int ) -> None :
331
342
# Write a 16-bit BE unsigned value to the specified 8-bit address.
332
343
with self ._device :
333
344
self ._BUFFER [0 ] = address & 0xFF
334
345
self ._BUFFER [1 ] = (val >> 8 ) & 0xFF
335
346
self ._BUFFER [2 ] = val & 0xFF
336
347
self ._device .write (self ._BUFFER )
337
348
338
- def _get_spad_info (self ):
349
+ def _get_spad_info (self ) -> Tuple [ int , bool ] :
339
350
# Get reference SPAD count and type, returned as a 2-tuple of
340
351
# count and boolean is_aperture. Based on code from:
341
352
# https://github.com/pololu/vl53l0x-arduino/blob/master/VL53L0X.cpp
@@ -368,7 +379,7 @@ def _get_spad_info(self):
368
379
self ._write_u8 (pair [0 ], pair [1 ])
369
380
return (count , is_aperture )
370
381
371
- def _perform_single_ref_calibration (self , vhv_init_byte ) :
382
+ def _perform_single_ref_calibration (self , vhv_init_byte : int ) -> None :
372
383
# based on VL53L0X_perform_single_ref_calibration() from ST API.
373
384
self ._write_u8 (_SYSRANGE_START , 0x01 | vhv_init_byte & 0xFF )
374
385
start = time .monotonic ()
@@ -381,7 +392,7 @@ def _perform_single_ref_calibration(self, vhv_init_byte):
381
392
self ._write_u8 (_SYSTEM_INTERRUPT_CLEAR , 0x01 )
382
393
self ._write_u8 (_SYSRANGE_START , 0x00 )
383
394
384
- def _get_vcsel_pulse_period (self , vcsel_period_type ) :
395
+ def _get_vcsel_pulse_period (self , vcsel_period_type : int ) -> int :
385
396
# pylint: disable=no-else-return
386
397
# Disable should be removed when refactor can be tested
387
398
if vcsel_period_type == _VCSEL_PERIOD_PRE_RANGE :
@@ -392,7 +403,7 @@ def _get_vcsel_pulse_period(self, vcsel_period_type):
392
403
return (((val ) + 1 ) & 0xFF ) << 1
393
404
return 255
394
405
395
- def _get_sequence_step_enables (self ):
406
+ def _get_sequence_step_enables (self ) -> Tuple [ bool , bool , bool , bool , bool ] :
396
407
# based on VL53L0X_GetSequenceStepEnables() from ST API
397
408
sequence_config = self ._read_u8 (_SYSTEM_SEQUENCE_CONFIG )
398
409
tcc = (sequence_config >> 4 ) & 0x1 > 0
@@ -402,7 +413,9 @@ def _get_sequence_step_enables(self):
402
413
final_range = (sequence_config >> 7 ) & 0x1 > 0
403
414
return (tcc , dss , msrc , pre_range , final_range )
404
415
405
- def _get_sequence_step_timeouts (self , pre_range ):
416
+ def _get_sequence_step_timeouts (
417
+ self , pre_range : int
418
+ ) -> Tuple [int , int , int , int , float ]:
406
419
# based on get_sequence_step_timeout() from ST API but modified by
407
420
# pololu here:
408
421
# https://github.com/pololu/vl53l0x-arduino/blob/master/VL53L0X.cpp
@@ -439,21 +452,21 @@ def _get_sequence_step_timeouts(self, pre_range):
439
452
)
440
453
441
454
@property
442
- def signal_rate_limit (self ):
455
+ def signal_rate_limit (self ) -> float :
443
456
"""The signal rate limit in mega counts per second."""
444
457
val = self ._read_u16 (_FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT )
445
458
# Return value converted from 16-bit 9.7 fixed point to float.
446
459
return val / (1 << 7 )
447
460
448
461
@signal_rate_limit .setter
449
- def signal_rate_limit (self , val ) :
462
+ def signal_rate_limit (self , val : float ) -> None :
450
463
assert 0.0 <= val <= 511.99
451
464
# Convert to 16-bit 9.7 fixed point value from a float.
452
465
val = int (val * (1 << 7 ))
453
466
self ._write_u16 (_FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT , val )
454
467
455
468
@property
456
- def measurement_timing_budget (self ):
469
+ def measurement_timing_budget (self ) -> int :
457
470
"""The measurement timing budget in microseconds."""
458
471
budget_us = 1910 + 960 # Start overhead + end overhead.
459
472
tcc , dss , msrc , pre_range , final_range = self ._get_sequence_step_enables ()
@@ -473,7 +486,7 @@ def measurement_timing_budget(self):
473
486
return budget_us
474
487
475
488
@measurement_timing_budget .setter
476
- def measurement_timing_budget (self , budget_us ) :
489
+ def measurement_timing_budget (self , budget_us : int ) -> None :
477
490
# pylint: disable=too-many-locals
478
491
assert budget_us >= 20000
479
492
used_budget_us = 1320 + 960 # Start (diff from get) + end overhead
@@ -494,7 +507,7 @@ def measurement_timing_budget(self, budget_us):
494
507
# "Note that the final range timeout is determined by the timing
495
508
# budget and the sum of all other timeouts within the sequence.
496
509
# If there is no room for the final range timeout, then an error
497
- # will be set. Otherwise the remaining time will be applied to
510
+ # will be set. Otherwise, the remaining time will be applied to
498
511
# the final range."
499
512
if used_budget_us > budget_us :
500
513
raise ValueError ("Requested timeout too big." )
@@ -511,14 +524,14 @@ def measurement_timing_budget(self, budget_us):
511
524
self ._measurement_timing_budget_us = budget_us
512
525
513
526
@property
514
- def distance (self ):
527
+ def distance (self ) -> float :
515
528
"""Perform a single reading of the range for an object in front of
516
529
the sensor and return the distance in centimeters.
517
530
"""
518
531
return self .range / 10
519
532
520
533
@property
521
- def range (self ):
534
+ def range (self ) -> int :
522
535
"""Perform a single (or continuous if `start_continuous` called)
523
536
reading of the range for an object in front of the sensor and
524
537
return the distance in millimeters.
@@ -530,15 +543,15 @@ def range(self):
530
543
return self .read_range ()
531
544
532
545
@property
533
- def data_ready (self ):
546
+ def data_ready (self ) -> bool :
534
547
"""Check if data is available from the sensor. If true a call to .range
535
548
will return quickly. If false, calls to .range will wait for the sensor's
536
549
next reading to be available."""
537
550
if not self ._data_ready :
538
551
self ._data_ready = self ._read_u8 (_RESULT_INTERRUPT_STATUS ) & 0x07 != 0
539
552
return self ._data_ready
540
553
541
- def do_range_measurement (self ):
554
+ def do_range_measurement (self ) -> None :
542
555
"""Perform a single reading of the range for an object in front of the
543
556
sensor, but without return the distance.
544
557
"""
@@ -563,7 +576,7 @@ def do_range_measurement(self):
563
576
):
564
577
raise RuntimeError ("Timeout waiting for VL53L0X!" )
565
578
566
- def read_range (self ):
579
+ def read_range (self ) -> int :
567
580
"""Return a range reading in millimeters.
568
581
Note: Avoid calling this directly. If you do single mode, you need
569
582
to call `do_range_measurement` first. Or your program will stuck or
@@ -586,24 +599,29 @@ def read_range(self):
586
599
return range_mm
587
600
588
601
@property
589
- def is_continuous_mode (self ):
602
+ def is_continuous_mode (self ) -> bool :
590
603
"""Is the sensor currently in continuous mode?"""
591
604
return self ._continuous_mode
592
605
593
- def continuous_mode (self ):
606
+ def continuous_mode (self ) -> "VL53L0X" :
594
607
"""Activate the continuous mode manager"""
595
608
return self
596
609
597
- def __enter__ (self ):
610
+ def __enter__ (self ) -> "VL53L0X" :
598
611
"""For continuous mode manager, called when used on `with` keyword"""
599
612
self .start_continuous ()
600
613
return self
601
614
602
- def __exit__ (self , exc_type , exc_value , traceback ):
615
+ def __exit__ (
616
+ self ,
617
+ exc_type : Optional [Type [BaseException ]],
618
+ exc_value : Optional [BaseException ],
619
+ traceback : Optional [TracebackType ],
620
+ ) -> None :
603
621
"""For continuous mode manager, called at the end of `with` scope"""
604
622
self .stop_continuous ()
605
623
606
- def start_continuous (self ):
624
+ def start_continuous (self ) -> None :
607
625
"""Perform a continuous reading of the range for an object in front of
608
626
the sensor.
609
627
"""
@@ -629,7 +647,7 @@ def start_continuous(self):
629
647
raise RuntimeError ("Timeout waiting for VL53L0X!" )
630
648
self ._continuous_mode = True
631
649
632
- def stop_continuous (self ):
650
+ def stop_continuous (self ) -> None :
633
651
"""Stop continuous readings."""
634
652
# Adapted from stopContinuous in pololu code at:
635
653
# https://github.com/pololu/vl53l0x-arduino/blob/master/VL53L0X.cpp
@@ -647,7 +665,7 @@ def stop_continuous(self):
647
665
# restore the sensor to single ranging mode
648
666
self .do_range_measurement ()
649
667
650
- def set_address (self , new_address ) :
668
+ def set_address (self , new_address : int ) -> None :
651
669
"""Set a new I2C address to the instantaited object. This is only called when using
652
670
multiple VL53L0X sensors on the same I2C bus (SDA & SCL pins). See also the
653
671
`example <examples.html#multiple-vl53l0x-on-same-i2c-bus>`_ for proper usage.
0 commit comments