Fix #114 - ultrasonic sensors

Implements support for the HC-SR04 ultrasonic sensor as an input device
class named DistanceSensor
This commit is contained in:
Dave Jones
2016-02-09 14:08:27 +00:00
parent 810bb67a6d
commit 83fb6ae8b4
8 changed files with 287 additions and 4 deletions

View File

@@ -1,3 +1,5 @@
# vim: set fileencoding=utf-8:
from __future__ import (
unicode_literals,
absolute_import,
@@ -25,3 +27,27 @@ def isclose(a, b, rel_tol=1e-9, abs_tol=0.0):
(diff <= abs(rel_tol * a)) or
(diff <= abs_tol)
)
# Backported from py3.4
def mean(data):
if iter(data) is data:
data = list(data)
n = len(data)
if not n:
raise ValueError('cannot calculate mean of empty data')
return sum(data) / n
# Backported from py3.4
def median(data):
data = sorted(data)
n = len(data)
if not n:
raise ValueError('cannot calculate median of empty data')
elif n % 2:
return data[n // 2]
else:
i = n // 2
return (data[n - 1] + data[n]) / 2