2020-03-22 16:40:18 +01:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2020-05-14 22:42:16 +02:00
|
|
|
"""Generic PWM capable vibration motor driver
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
"""
|
2020-01-31 20:24:33 +01:00
|
|
|
|
|
|
|
import time
|
|
|
|
from machine import PWM
|
|
|
|
|
|
|
|
class Vibrator(object):
|
2020-05-14 22:42:16 +02:00
|
|
|
"""Vibration motor driver.
|
|
|
|
|
|
|
|
.. automethod:: __init__
|
|
|
|
"""
|
2020-01-31 20:24:33 +01:00
|
|
|
def __init__(self, pin, active_low=False):
|
2020-05-14 22:42:16 +02:00
|
|
|
"""Specify the pin and configuration used to operate the motor.
|
|
|
|
|
|
|
|
:param machine.Pin pin: The PWM-capable pin used to driver the
|
|
|
|
vibration motor.
|
|
|
|
:param bool active_low: Invert the resting state of the motor.
|
|
|
|
"""
|
2020-01-31 20:24:33 +01:00
|
|
|
pin.value(active_low)
|
|
|
|
self.pin = pin
|
|
|
|
self.freq = PWM.FREQ_16MHZ
|
|
|
|
self.period = 16000
|
|
|
|
self.active_low = active_low
|
|
|
|
|
2020-04-05 10:36:05 +02:00
|
|
|
def pulse(self, duty=25, ms=40):
|
2020-05-14 22:42:16 +02:00
|
|
|
"""Briefly pulse the motor.
|
|
|
|
|
|
|
|
:param int duty: Duty cycle, in percent.
|
|
|
|
:param int ms: Duration, in milliseconds.
|
|
|
|
"""
|
2020-01-31 20:24:33 +01:00
|
|
|
pwm = PWM(0, self.pin, freq=self.freq, duty=duty, period=self.period)
|
|
|
|
pwm.init()
|
|
|
|
time.sleep_ms(ms)
|
|
|
|
pwm.deinit()
|
|
|
|
self.pin.value(self.active_low)
|