-
Notifications
You must be signed in to change notification settings - Fork 0
/
grove_led.py
148 lines (126 loc) · 4.87 KB
/
grove_led.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# grove_chainable_rgb_led.py
# Beaglebone Green Python library
# Copyright (c) 2015 seeed technology inc.
# Website : www.seeed.cc
# Author : Wuruibin
# Created Time: August 2015
# Modified Time:
# The MIT License (MIT)
# 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.
# !/usr/bin/python
# -*- coding: utf-8 -*-
import time
import Adafruit_BBIO.GPIO as GPIO
CLK_PIN = "P9_14"
DATA_PIN = "P9_16"
NUMBER_OF_LEDS = 1
class ChainableLED():
def __init__(self, clk_pin, data_pin, number_of_leds):
self.__clk_pin = clk_pin
self.__data_pin = data_pin
self.__number_of_leds = number_of_leds
GPIO.setup(self.__clk_pin, GPIO.OUT)
GPIO.setup(self.__data_pin, GPIO.OUT)
for i in range(self.__number_of_leds):
self.setColorRGB(i, 0, 0, 0)
def clk(self):
GPIO.output(self.__clk_pin, GPIO.LOW)
time.sleep(0.00002)
GPIO.output(self.__clk_pin, GPIO.HIGH)
time.sleep(0.00002)
def sendByte(self, b):
"Send one bit at a time, starting with the MSB"
for i in range(8):
# If MSB is 1, write one and clock it, else write 0 and clock
if (b & 0x80) != 0:
GPIO.output(self.__data_pin, GPIO.HIGH)
else:
GPIO.output(self.__data_pin, GPIO.LOW)
self.clk()
# Advance to the next bit to send
b = b << 1
def sendColor(self, red, green, blue):
"Start by sending a byte with the format '1 1 /B7 /B6 /G7 /G6 /R7 /R6' "
# prefix = B11000000
prefix = 0xC0
if (blue & 0x80) == 0:
# prefix |= B00100000
prefix |= 0x20
if (blue & 0x40) == 0:
# prefix |= B00010000
prefix |= 0x10
if (green & 0x80) == 0:
# prefix |= B00001000
prefix |= 0x08
if (green & 0x40) == 0:
# prefix |= B00000100
prefix |= 0x04
if (red & 0x80) == 0:
# prefix |= B00000010
prefix |= 0x02
if (red & 0x40) == 0:
# prefix |= B00000001
prefix |= 0x01
self.sendByte(prefix)
# Now must send the 3 colors
self.sendByte(blue)
self.sendByte(green)
self.sendByte(red)
def setColorRGB(self, led, red, green, blue):
# Send data frame prefix (32x '0')
self.sendByte(0x00)
self.sendByte(0x00)
self.sendByte(0x00)
self.sendByte(0x00)
# Send color data for each one of the leds
for i in range(self.__number_of_leds):
'''
if i == led:
_led_state[i*3 + _CL_RED] = red;
_led_state[i*3 + _CL_GREEN] = green;
_led_state[i*3 + _CL_BLUE] = blue;
sendColor(_led_state[i*3 + _CL_RED],
_led_state[i*3 + _CL_GREEN],
_led_state[i*3 + _CL_BLUE]);
'''
self.sendColor(red, green, blue)
# Terminate data frame (32x "0")
self.sendByte(0x00)
self.sendByte(0x00)
self.sendByte(0x00)
self.sendByte(0x00)
# Note: Use P9_22(UART2_RXD) and P9_21(UART2_TXD) as GPIO.
# Connect the Grove - Chainable RGB LED to UART Grove port of Beaglebone Green.
if __name__ == "__main__":
rgb_led = ChainableLED(CLK_PIN, DATA_PIN, NUMBER_OF_LEDS)
print('Cycling RGB LED...')
while True:
# The first parameter: NUMBER_OF_LEDS - 1; Other parameters: the RGB values.
rgb_led.setColorRGB(0, 255, 0, 0)
time.sleep(2)
rgb_led.setColorRGB(0, 0, 255, 0)
time.sleep(2)
rgb_led.setColorRGB(0, 0, 0, 255)
time.sleep(2)
rgb_led.setColorRGB(0, 0, 255, 255)
time.sleep(2)
rgb_led.setColorRGB(0, 255, 0, 255)
time.sleep(2)
rgb_led.setColorRGB(0, 255, 255, 0)
time.sleep(2)
rgb_led.setColorRGB(0, 255, 255, 255)
time.sleep(2)