Skip to content

Instantly share code, notes, and snippets.

@isoteemu
Created October 25, 2020 00:43
Show Gist options
  • Save isoteemu/2f8cbed1e50bb775a84dfe1d4c57c026 to your computer and use it in GitHub Desktop.
Save isoteemu/2f8cbed1e50bb775a84dfe1d4c57c026 to your computer and use it in GitHub Desktop.
RGB diskovalo GPIO liitäntään.
import RPi.GPIO as GPIO
import time
from math import (sin, pi)
from psutil import cpu_percent
import logging
logger = logging.getLogger(__name__)
GPIO.setwarnings(False)
red_gpio = 26
green_gpio = 19
blue_gpio = 13
# Kirkkaus 0-1.
#brightness = lambda: cpu_percent() / 100
brightness = 0.1
fps = 1/30
def main(red_gpio, green_gpio, blue_gpio, freq=100):
GPIO.setmode(GPIO.BCM)
GPIO.setup(red_gpio, GPIO.OUT)
GPIO.setup(green_gpio, GPIO.OUT)
GPIO.setup(blue_gpio, GPIO.OUT)
logger.debug("PINS (RGB):", red_gpio, green_gpio, blue_gpio)
RED = GPIO.PWM(red_gpio, freq)
GREEN = GPIO.PWM(green_gpio, freq)
BLUE = GPIO.PWM(blue_gpio, freq)
offset_step = pi*2/3
def get_intensity(offset):
_brightness = brightness() if callable(brightness) else brightness
intensity = sin(seed+(offset_step*offset))
if 0 < intensity < 1:
return min (100, max(0, (100 * intensity) * _brightness))
else:
return 0
try:
RED.start(0)
GREEN.start(0)
BLUE.start(0)
while True:
seed = time.time()
RED.ChangeDutyCycle(get_intensity(1))
GREEN.ChangeDutyCycle(get_intensity(2))
BLUE.ChangeDutyCycle(get_intensity(3))
time.sleep(fps)
except Exception as e:
RED.stop()
GREEN.stop()
BLUE.stop()
GPIO.cleanup()
raise e
if __name__ == "__main__":
main(red_gpio=red_gpio, green_gpio=green_gpio, blue_gpio=blue_gpio)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment