changeset 1:ed8d51066e5f

Docs and license
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Mon, 16 May 2016 21:16:32 +0200
parents 0f52e472b121
children f562a6abeaf0
files LICENSE README.rst servo.py
diffstat 3 files changed, 51 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LICENSE	Mon May 16 21:16:32 2016 +0200
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2016 Radomir Dopieralski
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
--- a/README.rst	Mon May 16 12:46:31 2016 +0200
+++ b/README.rst	Mon May 16 21:16:32 2016 +0200
@@ -1,2 +1,13 @@
 A Micropython Library for Hobby Servo Control for ESP8266
 *********************************************************
+
+This is a simple class for controlling hobby servos with Micropython on the
+ESP8266 boards. It's in fact a simple convenience wrapper over the
+``machine.PWM`` functionality.
+
+Currently, the supported pins are 0, 2, 4, 5, 12, 13, 14, 15 -- because only
+those pins are supported by ``machine.PWM``. Pins 1 and 3 may become supported
+in the future.
+
+To use this library, simply copy it to the ESP8266's filesystem, or include
+it in the ``scripts`` directory while compiling the Micropython firmware.
--- a/servo.py	Mon May 16 12:46:31 2016 +0200
+++ b/servo.py	Mon May 16 21:16:32 2016 +0200
@@ -3,26 +3,39 @@
 
 
 class Servo:
-    def __init__(self, pin, freq=50, min_pos=600, max_pos=2400, angle=180):
-        self.min_pos = min_pos
-        self.max_pos = max_pos
+    """
+    A simple class for controlling hobby servos.
+
+    Args:
+        pin (machine.Pin): The pin where servo is connected. Must support PWM.
+        freq (int): The frequency of the signal, in hertz.
+        min_us (int): The minimum signal length supported by the servo.
+        max_us (int): The maximum signal length supported by the servo.
+        angle (int): The angle between the minimum and maximum positions.
+
+    """
+    def __init__(self, pin, freq=50, min_us=600, max_us=2400, angle=180):
+        self.min_us = min_us
+        self.max_us = max_us
         self.us = 0
         self.freq = freq
         self.angle = angle
         self.pwm = PWM(pin, freq=freq, duty=0)
 
     def write_us(self, us):
+        """Set the signal to be ``us`` microseconds long. Zero disables it."""
         if us == 0:
             self.pwm.duty(0)
             return
-        us = min(self.max_pos, max(self.min_pos, us))
+        us = min(self.max_us, max(self.min_us, us))
         duty = us * 1024 * self.freq // 1000000
         self.pwm.duty(duty)
 
     def write_angle(self, degrees=None, radians=None):
+        """Move to the specified angle in ``degrees`` or ``radians``."""
         if degrees is None:
             degrees = math.degrees(radians)
         degrees = degrees % 360
-        total_range = self.max_pos - self.min_pos
-        us = self.min_pos + total_range * degrees // self.angle
+        total_range = self.max_us - self.min_us
+        us = self.min_us + total_range * degrees // self.angle
         self.write_us(us)