Created
February 12, 2024 22:26
-
-
Save asafmatan/527e926ae403116b80fd2f3ce36f43c7 to your computer and use it in GitHub Desktop.
PurimIrisBox - Servo and Led
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
from gpiozero import Servo | |
from time import sleep | |
from gpiozero.pins.pigpio import PiGPIOFactory | |
import sys | |
import time | |
import sys | |
from rpi_ws281x import PixelStrip, Color | |
import os | |
import errno | |
import ast | |
# LED strip configuration: | |
LED_COUNT = 1 # Number of LED pixels | |
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM) | |
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest | |
# Create PixelStrip object with appropriate configuration | |
strip = PixelStrip(LED_COUNT, LED_PIN, brightness=LED_BRIGHTNESS) | |
strip.begin() | |
#servo init | |
factory = PiGPIOFactory() | |
servo = Servo(12, min_pulse_width=0.5/1000, max_pulse_width=2.5/1000, pin_factory=factory) | |
def set_led_color(red, green, blue): | |
try: | |
# Color the pixel | |
strip.setPixelColor(0, Color(red, green, blue)) | |
strip.show() | |
except KeyboardInterrupt: | |
# Turn off the pixel on Ctrl+C | |
strip.setPixelColor(0, Color(0, 0, 0)) | |
strip.show() | |
#range is -1 to 1 | |
def set_servo(value): | |
servo.value = value | |
FIFO = 'mypipe' | |
try: | |
os.mkfifo(FIFO) | |
except OSError as oe: | |
if oe.errno != errno.EEXIST: | |
raise | |
#servo.min() | |
servo.value = 0.9 | |
#Examples of write servo commands | |
#echo "{'servo':-1}" > mypipe | |
#echo "{'servo':-1}" > mypipe | |
#echo "{'servo':-1}" > mypipe | |
#led | |
#echo "{'led':(255,0,0)}" > mypipe | |
#echo "{'led':(255,255,0)}" > mypipe | |
#echo "{'led':(255,255,255)}" > mypipe | |
while True: | |
fifo_data = "" | |
print("Opening FIFO...") | |
with open(FIFO) as fifo: | |
print("FIFO opened") | |
while True: | |
buffer = fifo.read() | |
if len(buffer) == 0: | |
print("Writer closed") | |
break | |
else: | |
fifo_data = fifo_data + buffer | |
read_dict = ast.literal_eval(fifo_data) | |
if type(read_dict) == dict: | |
print(read_dict) | |
for key in read_dict: | |
if key == 'servo': | |
if read_dict['servo'] >= -1 and read_dict['servo'] <= 1: | |
set_servo(read_dict['servo']) | |
if key == 'led': | |
red = read_dict['led'][0] | |
green = read_dict['led'][1] | |
blue = read_dict['led'][2] | |
set_led_color(red, green, blue) | |
else: | |
print ("Not a dict") | |
while True: | |
try: | |
# This can be a sleep statement or some other logic | |
sleep(1) | |
except KeyboardInterrupt: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment