mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-12-08 20:39:01 +00:00
Fix #114 - ultrasonic sensors
Implements support for the HC-SR04 ultrasonic sensor as an input device class named DistanceSensor
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user