Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
python-ev3dev2 (2.0.0~beta4) UNRELEASED; urgency=medium

[Daniel Walton]
* micropython support for LED animations
* StopWatch class
* Avoid race condition due to poll(None)
* MoveDifferential odometry support, tracks robot's (x,y) position
Expand Down
55 changes: 17 additions & 38 deletions ev3dev2/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
if sys.version_info < (3,4):
raise SystemError('Must be using Python 3.4 or higher')

import datetime as dt
import os
import stat
import time
import _thread
from collections import OrderedDict
from ev3dev2 import get_current_platform, Device
from ev3dev2.stopwatch import StopWatch
from time import sleep

# Import the LED settings, this is platform specific
Expand Down Expand Up @@ -62,35 +62,6 @@
raise Exception("Unsupported platform '%s'" % platform)


def datetime_delta_to_ms(delta):
"""
Given a datetime.timedelta object, return the delta in milliseconds
"""
delta_ms = delta.days * 24 * 60 * 60 * 1000
delta_ms += delta.seconds * 1000
delta_ms += delta.microseconds / 1000
delta_ms = int(delta_ms)
return delta_ms


def datetime_delta_to_seconds(delta):
return int(datetime_delta_to_ms(delta) / 1000)


def duration_expired(start_time, duration_seconds):
"""
Return True if ``duration_seconds`` have expired since ``start_time``
"""

if duration_seconds is not None:
delta_seconds = datetime_delta_to_seconds(dt.datetime.now() - start_time)

if delta_seconds >= duration_seconds:
return True

return False


class Led(Device):
"""
Any device controlled by the generic LED driver.
Expand Down Expand Up @@ -430,7 +401,9 @@ def animate_police_lights(self, color1, color2, group1='LEFT', group2='RIGHT', s
def _animate_police_lights():
self.all_off()
even = True
start_time = dt.datetime.now()
duration_ms = duration * 1000
stopwatch = StopWatch()
stopwatch.start()

while True:
if even:
Expand All @@ -440,7 +413,7 @@ def _animate_police_lights():
self.set_color(group1, color2)
self.set_color(group2, color1)

if self.animate_thread_stop or duration_expired(start_time, duration):
if self.animate_thread_stop or stopwatch.value_ms >= duration_ms:
break

even = not even
Expand Down Expand Up @@ -473,7 +446,9 @@ def animate_flash(self, color, groups=('LEFT', 'RIGHT'), sleeptime=0.5, duration

def _animate_flash():
even = True
start_time = dt.datetime.now()
duration_ms = duration * 1000
stopwatch = StopWatch()
stopwatch.start()

while True:
if even:
Expand All @@ -482,7 +457,7 @@ def _animate_flash():
else:
self.all_off()

if self.animate_thread_stop or duration_expired(start_time, duration):
if self.animate_thread_stop or stopwatch.value_ms >= duration_ms:
break

even = not even
Expand Down Expand Up @@ -516,7 +491,9 @@ def animate_cycle(self, colors, groups=('LEFT', 'RIGHT'), sleeptime=0.5, duratio
def _animate_cycle():
index = 0
max_index = len(colors)
start_time = dt.datetime.now()
duration_ms = duration * 1000
stopwatch = StopWatch()
stopwatch.start()

while True:
for group in groups:
Expand All @@ -527,7 +504,7 @@ def _animate_cycle():
if index == max_index:
index = 0

if self.animate_thread_stop or duration_expired(start_time, duration):
if self.animate_thread_stop or stopwatch.value_ms >= duration_ms:
break

sleep(sleeptime)
Expand Down Expand Up @@ -568,7 +545,9 @@ def _animate_rainbow():
MIN_VALUE = 0
MAX_VALUE = 1
self.all_off()
start_time = dt.datetime.now()
duration_ms = duration * 1000
stopwatch = StopWatch()
stopwatch.start()

while True:

Expand Down Expand Up @@ -601,7 +580,7 @@ def _animate_rainbow():
elif state == 3 and right_value == MIN_VALUE:
state = 0

if self.animate_thread_stop or duration_expired(start_time, duration):
if self.animate_thread_stop or stopwatch.value_ms >= duration_ms:
break

sleep(sleeptime)
Expand Down
12 changes: 6 additions & 6 deletions ev3dev2/stopwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
A StopWatch class for tracking the amount of time between events
"""

try:
import datetime as dt
micropython = False
except ImportError:
Comment thread
dwalton76 marked this conversation as resolved.
Resolved
from ev3dev2 import is_micropython

if is_micropython():
import utime
micropython = True
else:
import datetime as dt


def get_ticks_ms():
if micropython:
if is_micropython():
return utime.ticks_ms()
else:
return int(dt.datetime.timestamp(dt.datetime.now()) * 1000)
Expand Down