rpi-pico-candle/light.py

71 lines
1.8 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# SPDX-License-Identifier: MIT
#
# Little christmas candle
#
# Author: Nils Freydank <nils.freydank@datenschutz-ist-voll-doof.de>
# Year: 2022
# Copyright: MIT
import machine
import time
pin_led1 = machine.Pin(22)
pin_led2 = machine.Pin(10)
pin_led3 = machine.Pin(8)
def _get_new_duty(
factor: int, signed_step_width: int, limit_lower: int, limit_upper: int
) -> Tuple[int, int]:
duty_fac: int = factor
duty_fac += signed_step_width
if duty_fac > limit_upper:
duty_fac = limit_upper
signed_step_width = (-1) * signed_step_width
elif duty_fac < limit_lower:
duty_fac = limit_lower
signed_step_width = (-1) * signed_step_width
return duty_fac, signed_step_width
def flicker() -> None:
pwm_led1 = machine.PWM(pin_led1)
pwm_led2 = machine.PWM(pin_led2)
pwm_led3 = machine.PWM(pin_led3)
pwm_led1.freq(1_000)
pwm_led2.freq(1_000)
pwm_led3.freq(1_000)
duty_fac_led1: int = 0
signed_step_width_led1: int = 1
duty_fac_led2: int = 0
signed_step_width_led2: int = 1
duty_fac_led3: int = 0
signed_step_width_led3: int = 1
delay_s: float = 0.01
while True:
duty_fac_led1, signed_step_width_led1 = _get_new_duty(
duty_fac_led1, signed_step_width_led1, 240, 250
)
pwm_led1.duty_u16(duty_fac_led1**2)
duty_fac_led2, signed_step_width_led2 = _get_new_duty(
duty_fac_led2, signed_step_width_led2, 0, 200
)
pwm_led2.duty_u16(duty_fac_led2**2)
duty_fac_led3, signed_step_width_led3 = _get_new_duty(
duty_fac_led3, signed_step_width_led3, 30, 100
)
pwm_led2.duty_u16(duty_fac_led3**2)
time.sleep(delay_s)
# vim:fileencoding=utf-8:ts=4:syntax=python:expandtab