led_strip.py
3.8 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
config and init 24-LEDs-stripe
"""
import os
from matplotlib.pyplot import cla
LED_COUNT = 1000 # 要控制LED的数量.
# LED_PIN = 18 # GPIO接口 (PWM编码).
LED_BRIGHTNESS = 100 # 设置LED亮度 (0-255)
#以下LED配置无需修改
LED_FREQ_HZ = 800000 # LED信号频率(以赫兹为单位)(通常为800khz)
LED_DMA = 10 # 用于生成信号的DMA通道(尝试5)
LED_INVERT = False # 反转信号(使用NPN晶体管电平移位时)
LED_CHANNEL = 0
stop_flag = None
stripStore={}
if os.name != 'nt':
from neopixel import *
from rpi_ws281x import Adafruit_NeoPixel, Color as ColorA,PixelStrip
#Color=ColorA
def Color(red,green, blue, white=0):
return ColorA(green,red,blue)
def get_strip(pin=12):
LED_PIN = pin
if stripStore.get(LED_PIN) is None:
stripStore[LED_PIN] = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS,LED_CHANNEL)
stripStore[LED_PIN].begin()
# Initialize the library (must be called once before other functions).
# log.debug("Initialize: " + str(strip))
return stripStore[LED_PIN]
def color_wipe_full(strip, color, wait_ms=50):
"""Wipe color across display a pixel at a time."""
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
# time.sleep(wait_ms/1000.0)
def reset_strip(strip):
strip.setBrightness(LED_BRIGHTNESS)
color_wipe_full(strip, Color(0,0,0), 20)
# log.debug('LED stripe cleared.')
return
# noinspection PyProtectedMember
def _cleanup_strip(strip: Adafruit_NeoPixel):
strip._cleanup()
else:
class strip:
def numPixels():
return 5
def setPixelColor(i,c):
return
def show():
return
def Color(red,green, blue, white=0):
return (white << 24) | (red << 16) | (green << 8) | blue
def get_strip(pin=12):
if stripStore.get(pin) is None:
stripStore[pin] = strip
print('new_strip:'+str(pin))
print('get_strip:'+str(pin))
return stripStore[pin]
def color_wipe_full(strip, color, wait_ms=50):
print(color)
# time.sleep(wait_ms/1000.0)
def reset_strip(strip):
print('reset_strip:\/')
print(strip)
# log.debug('LED stripe cleared.')
return
# noinspection PyProtectedMember
def _cleanup_strip(strip):
print(strip)
def setcolor(color):
color = color.lower()
if color.startswith('#'):
rgb = tuple(int(color[i:i+2], 16) for i in (1, 3, 5))
return Color(rgb[0],rgb[1],rgb[2])
elif color == 'red':
return Color(255,0,0)
elif color == 'green':
return Color(0,255,0)
elif color == 'yellow':
return Color(255,255,0)
elif color == 'off':
return Color(0,0,0)
elif color == 'blue':
return Color(0,0,255)
elif color == 'orange':
return Color(255,97,0)
elif color == 'cyan':
return Color(0,255,255)
elif color == 'firebrick':
return Color(178,34,34)
elif color == 'purple':
return Color(128,0,128)
elif color == 'forestgreen':
return Color(34,139,34)
elif color == 'lightblue':
return Color(173,216,230)
elif color == 'indianred':
return Color(205,92,92)
elif color == 'skyblue':
return Color(135,206,235)
elif color == 'pink':
return Color(255,192,203)
elif color == 'darkgreen':
return Color(0,100,0)
elif color == 'white':
return Color(255,255,255)
elif color == 'magenta':
return Color(255,0,255)
else:
return Color(255,255,255)
#setcolor('#eeFeee')