led_strip.py 3.5 KB
#!/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 1
        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 == '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 == '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)