Last active
July 27, 2018 18:42
-
-
Save ilovetogetspamed/e2d0bd1e3258ea8fe0b3a2912b286e85 to your computer and use it in GitHub Desktop.
Class for controlling LEDs on MicroPython PyBoard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyb, micropython | |
micropython.alloc_emergency_exception_buf(100) | |
from pyb import Pin | |
class LED_Control(object): | |
def __init__(self, timer, ledPin): | |
self.led = pyb.Pin(ledPin, pyb.Pin.OUT_PP) | |
self.led.value(True) | |
self.timer = timer | |
self.timer.callback(self.cb) | |
def cb(self, tim): | |
# toggle pin | |
self.led.value(not self.led.value()) | |
def on(self): | |
self.led.value(True) | |
if self.blinking: | |
self.timer.callback(self.cb) | |
else: | |
self.timer.callback(None) | |
def off(self): | |
self.led.value(False) | |
self.timer.callback(None) | |
def on_blinking(self): | |
self.blinking = True | |
self.on() | |
def set_timer_freq(self, new_freq): | |
# todo: test for bad new_freq | |
self.timer.init(freq=new_freq) | |
""" Usage: | |
>>> pinY1 = LED_Control(pyb.Timer(4, freq=1), pyb.Pin.board.Y1) | |
>>> pinY2 = LED_Control(pyb.Timer(5, freq=2), pyb.Pin.board.Y2) | |
>>> pinY3 = LED_Control(pyb.Timer(6, freq=3), pyb.Pin.board.Y3) | |
>>> pinY1.off() | |
>>> pinY1.on_blinking() | |
>>> pinY2.off() | |
>>> pinY2.on() | |
>>> pinY3.set_timer_freq(10) | |
>>> pinY3.set_timer_freq(0.10) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment