grovepi/Software/Python/grove_chainable_rgb_led/individual_led_control.py
2025-03-21 16:04:17 +01:00

130 lines
4.1 KiB
Python

#!/usr/bin/env python
#
# GrovePi Example for using the Grove Chainable RGB LED (http://www.seeedstudio.com/wiki/Grove_-_Chainable_RGB_LED)
#
# The GrovePi connects the Raspberry Pi and Grove sensors. You can learn more about GrovePi here: http://www.dexterindustries.com/GrovePi
#
# Have a question about this example? Ask on the forums here: http://www.dexterindustries.com/forum/?forum=grovepi
#
'''
## License
The MIT License (MIT)
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
Copyright (C) 2017 Dexter Industries
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
import time
import grovepi
# Connect first LED in Chainable RGB LED chain to digital port D7
# In: CI,DI,VCC,GND
# Out: CO,DO,VCC,GND
pin = 7
# I have 10 LEDs connected in series with the first connected to the GrovePi and the last not connected
# First LED input socket connected to GrovePi, output socket connected to second LED input and so on
numleds = 4 #If you only plug 1 LED, change 10 to 1
grovepi.pinMode(pin,"OUTPUT")
time.sleep(1)
# Chainable RGB LED methods
# grovepi.storeColor(red, green, blue)
# grovepi.chainableRgbLed_init(pin, numLeds)
# grovepi.chainableRgbLed_test(pin, numLeds, testColor)
# grovepi.chainableRgbLed_pattern(pin, pattern, whichLed)
# grovepi.chainableRgbLed_modulo(pin, offset, divisor)
# grovepi.chainableRgbLed_setLevel(pin, level, reverse)
# test colors used in grovepi.chainableRgbLed_test()
testColorBlack = 0 # 0b000 #000000
testColorBlue = 1 # 0b001 #0000FF
testColorGreen = 2 # 0b010 #00FF00
testColorCyan = 3 # 0b011 #00FFFF
testColorRed = 4 # 0b100 #FF0000
testColorMagenta = 5 # 0b101 #FF00FF
testColorYellow = 6 # 0b110 #FFFF00
testColorWhite = 7 # 0b111 #FFFFFF
# patterns used in grovepi.chainableRgbLed_pattern()
thisLedOnly = 0
allLedsExceptThis = 1
thisLedAndInwards = 2
thisLedAndOutwards = 3
sleep_time=0.5
#Turns off the led specified by led_num
def turn_off_led(led_num):
grovepi.storeColor(0,0,0)
time.sleep(.1)
grovepi.chainableRgbLed_pattern(pin, thisLedOnly, led_num)
time.sleep(.1)
#Turns on the led specified by led_num to color set by r,g,b
def turn_on_led(led_num,r,g,b):
grovepi.storeColor(r,g,b)
time.sleep(.1)
grovepi.chainableRgbLed_pattern(pin, thisLedOnly, led_num)
time.sleep(.1)
try:
print("Initialise")
# init chain of leds
grovepi.chainableRgbLed_init(pin, numleds)
time.sleep(.5)
print("1")
# turn on led at 0
turn_on_led(0,0,255,0)
time.sleep(sleep_time)
# turn on led at 1
print("2")
turn_off_led(0)
turn_on_led(1,0,255,0)
time.sleep(sleep_time)
# turn on led at 2
print("3")
turn_off_led(1)
turn_on_led(2,0,255,0)
time.sleep(sleep_time)
# turn on led at 3
print("4")
turn_off_led(2)
turn_on_led(3,0,255,0)
time.sleep(sleep_time)
# turn off all
print("Off")
grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
time.sleep(sleep_time)
except KeyboardInterrupt:
# reset (all off)
grovepi.chainableRgbLed_test(pin, numleds, testColorBlack)
except IOError:
print ("Error")