2828if sys .version_info < (3 ,4 ):
2929 raise SystemError ('Must be using Python 3.4 or higher' )
3030
31- import datetime as dt
3231import os
3332import stat
3433import time
3534import _thread
3635from collections import OrderedDict
3736from ev3dev2 import get_current_platform , Device
37+ from ev3dev2 .stopwatch import StopWatch
3838from time import sleep
3939
4040# Import the LED settings, this is platform specific
6262 raise Exception ("Unsupported platform '%s'" % platform )
6363
6464
65- def datetime_delta_to_ms (delta ):
66- """
67- Given a datetime.timedelta object, return the delta in milliseconds
68- """
69- delta_ms = delta .days * 24 * 60 * 60 * 1000
70- delta_ms += delta .seconds * 1000
71- delta_ms += delta .microseconds / 1000
72- delta_ms = int (delta_ms )
73- return delta_ms
74-
75-
76- def datetime_delta_to_seconds (delta ):
77- return int (datetime_delta_to_ms (delta ) / 1000 )
78-
79-
80- def duration_expired (start_time , duration_seconds ):
81- """
82- Return True if ``duration_seconds`` have expired since ``start_time``
83- """
84-
85- if duration_seconds is not None :
86- delta_seconds = datetime_delta_to_seconds (dt .datetime .now () - start_time )
87-
88- if delta_seconds >= duration_seconds :
89- return True
90-
91- return False
92-
93-
9465class Led (Device ):
9566 """
9667 Any device controlled by the generic LED driver.
@@ -430,7 +401,9 @@ def animate_police_lights(self, color1, color2, group1='LEFT', group2='RIGHT', s
430401 def _animate_police_lights ():
431402 self .all_off ()
432403 even = True
433- start_time = dt .datetime .now ()
404+ duration_ms = duration * 1000
405+ stopwatch = StopWatch ()
406+ stopwatch .start ()
434407
435408 while True :
436409 if even :
@@ -440,7 +413,7 @@ def _animate_police_lights():
440413 self .set_color (group1 , color2 )
441414 self .set_color (group2 , color1 )
442415
443- if self .animate_thread_stop or duration_expired ( start_time , duration ) :
416+ if self .animate_thread_stop or stopwatch . value_ms >= duration_ms :
444417 break
445418
446419 even = not even
@@ -473,7 +446,9 @@ def animate_flash(self, color, groups=('LEFT', 'RIGHT'), sleeptime=0.5, duration
473446
474447 def _animate_flash ():
475448 even = True
476- start_time = dt .datetime .now ()
449+ duration_ms = duration * 1000
450+ stopwatch = StopWatch ()
451+ stopwatch .start ()
477452
478453 while True :
479454 if even :
@@ -482,7 +457,7 @@ def _animate_flash():
482457 else :
483458 self .all_off ()
484459
485- if self .animate_thread_stop or duration_expired ( start_time , duration ) :
460+ if self .animate_thread_stop or stopwatch . value_ms >= duration_ms :
486461 break
487462
488463 even = not even
@@ -516,7 +491,9 @@ def animate_cycle(self, colors, groups=('LEFT', 'RIGHT'), sleeptime=0.5, duratio
516491 def _animate_cycle ():
517492 index = 0
518493 max_index = len (colors )
519- start_time = dt .datetime .now ()
494+ duration_ms = duration * 1000
495+ stopwatch = StopWatch ()
496+ stopwatch .start ()
520497
521498 while True :
522499 for group in groups :
@@ -527,7 +504,7 @@ def _animate_cycle():
527504 if index == max_index :
528505 index = 0
529506
530- if self .animate_thread_stop or duration_expired ( start_time , duration ) :
507+ if self .animate_thread_stop or stopwatch . value_ms >= duration_ms :
531508 break
532509
533510 sleep (sleeptime )
@@ -568,7 +545,9 @@ def _animate_rainbow():
568545 MIN_VALUE = 0
569546 MAX_VALUE = 1
570547 self .all_off ()
571- start_time = dt .datetime .now ()
548+ duration_ms = duration * 1000
549+ stopwatch = StopWatch ()
550+ stopwatch .start ()
572551
573552 while True :
574553
@@ -601,7 +580,7 @@ def _animate_rainbow():
601580 elif state == 3 and right_value == MIN_VALUE :
602581 state = 0
603582
604- if self .animate_thread_stop or duration_expired ( start_time , duration ) :
583+ if self .animate_thread_stop or stopwatch . value_ms >= duration_ms :
605584 break
606585
607586 sleep (sleeptime )
0 commit comments