Skip to content

Commit 56dc7ca

Browse files
authored
micropython support for LED animations (#634)
micropython support for LED animations
1 parent 2be6c87 commit 56dc7ca

File tree

3 files changed

+24
-44
lines changed

3 files changed

+24
-44
lines changed

debian/changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
python-ev3dev2 (2.0.0~beta4) UNRELEASED; urgency=medium
22

33
[Daniel Walton]
4+
* micropython support for LED animations
45
* StopWatch class
56
* Avoid race condition due to poll(None)
67
* MoveDifferential odometry support, tracks robot's (x,y) position

ev3dev2/led.py

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
if sys.version_info < (3,4):
2929
raise SystemError('Must be using Python 3.4 or higher')
3030

31-
import datetime as dt
3231
import os
3332
import stat
3433
import time
3534
import _thread
3635
from collections import OrderedDict
3736
from ev3dev2 import get_current_platform, Device
37+
from ev3dev2.stopwatch import StopWatch
3838
from time import sleep
3939

4040
# Import the LED settings, this is platform specific
@@ -62,35 +62,6 @@
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-
9465
class 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)

ev3dev2/stopwatch.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
A StopWatch class for tracking the amount of time between events
33
"""
44

5-
try:
6-
import datetime as dt
7-
micropython = False
8-
except ImportError:
5+
from ev3dev2 import is_micropython
6+
7+
if is_micropython():
98
import utime
10-
micropython = True
9+
else:
10+
import datetime as dt
1111

1212

1313
def get_ticks_ms():
14-
if micropython:
14+
if is_micropython():
1515
return utime.ticks_ms()
1616
else:
1717
return int(dt.datetime.timestamp(dt.datetime.now()) * 1000)

0 commit comments

Comments
 (0)