树莓皮Python和Flask网络控制与Adafruit DotStar LED

2024-09-30 22:28:14 发布

您现在位置:Python中文网/ 问答频道 /正文

抱歉,如果这不是正确的地方问,但我做了一些搜索,并没有找到多少指向我的正确方向。我不太确定要找什么。我是python和编程的新手,但通常可以通过google搜索和窃取其他代码片段来运行我的项目。但是我在这里遇到了一个障碍。在

我需要控制一个Adafruit DotStar lightstrip与一个烧瓶网络浏览器应用程序。我已经能够让flask应用程序工作,我已经做了一个简单的概念证明,打开和关闭一个LED等,我可以启动我的lightstrip脚本,但我试图运行的代码,为lightstrip需要不断循环,仍然能够改变“模式”。我有几个不同的图像显示在光带上,我想能够选择哪个(s)正在播放,但目前主要我只想能够启动和停止“shuffle all”模式。如果我在while循环中运行模块,它将永远循环,并且我不能将参数更改为不同的“mode”。我基于Adafruit的DotStar库构建了一个简单的脚本(特别是vision脚本的图像持久性,我只是使用PNG图像作为不同lightstrip“显示”的地图)。在

它目前所有的工作,除了它只运行一次模式显然。我把它放在一个while循环中,它永远循环第一个选择的模式,我无法关闭它或切换模式。我还想也许我应该使用多处理,我想让它发挥作用,但我不知道如何停止一个进程一旦启动。在

下面是光带脚本:

(“关闭”模式只是一个黑色图像。我相信有更干净的方法来做这件事,但我也不确定该怎么做)

import Image
from dotstar import Adafruit_DotStar
import random

def lightstrip(mode):
    loopLength = 120        #loop length in pixels

    fade  = "/home/pi/lightshow/images/fade.png"
    sparkle = "/home/pi/lightshow/images/sparkle.png"
    steeplechase = "/home/pi/lightshow/images/steeplechase.png"
    bump = "/home/pi/lightshow/images/bump.png"
    spaz = "/home/pi/lightshow/images/spaz.png"
    sine = "/home/pi/lightshow/images/sine.png"
    bounce = "/home/pi/lightshow/images/bounce.png"
    off = "/home/pi/lightshow/images/null.png"

    numpixels = 30
    datapin   = 23
    clockpin  = 24
    strip     = Adafruit_DotStar(numpixels, 100000)

    rOffset = 3
    gOffset = 2
    bOffset = 1

    strip.begin()

    if mode == 1:
        options = [fade, sparkle, steeplechase, bump, spaz, sine, bounce]
        print "Shuffling All..."
    if mode == 2:
        options = [bump, spaz, sine, bounce]
        print "Shuffling Dance..."
    if mode == 3:
        options = [fade, sparkle, steeplechase]
        print "Shuffling Chill..."
    if mode == 0:
        choice = off
        print "Lightstrip off..."

    if mode != 0:
        choice = random.choice(options)
    print "Loading..."
    img       = Image.open(choice).convert("RGB")
    pixels    = img.load()
    width     = img.size[0]
    height    = img.size[1]
    print "%dx%d pixels" % img.size

    # Calculate gamma correction table, makes mid-range colors look 'right':
    gamma = bytearray(256)
    for i in range(256):
        gamma[i] = int(pow(float(i) / 255.0, 2.7) * 255.0 + 0.5)

    # Allocate list of bytearrays, one for each column of image.
    # Each pixel REQUIRES 4 bytes (0xFF, B, G, R).
    print "Allocating..."
    column = [0 for x in range(width)]
    for x in range(width):
        column[x] = bytearray(height * 4)

    # Convert entire RGB image into column-wise BGR bytearray list.
    # The image-paint.py example proceeds in R/G/B order because it's counting
    # on the library to do any necessary conversion.  Because we're preparing
    # data directly for the strip, it's necessary to work in its native order.
    print "Converting..."
    for x in range(width):          # For each column of image...
        for y in range(height): # For each pixel in column...
            value             = pixels[x, y]    # Read pixel in image
            y4                = y * 4           # Position in raw buffer
            column[x][y4]     = 0xFF            # Pixel start marker
            column[x][y4 + rOffset] = gamma[value[0]] # Gamma-corrected R
            column[x][y4 + gOffset] = gamma[value[1]] # Gamma-corrected G
            column[x][y4 + bOffset] = gamma[value[2]] # Gamma-corrected B

    print "Displaying..."
    count = loopLength
    while (count > 0):

        for x in range(width):         # For each column of image...
            strip.show(column[x])  # Write raw data to strip
            count = count - 1

以及py主菜单运行web应用程序的脚本:

^{pr2}$

再次,我在这里有点不知所措,这可能是一个简单的解决方案,或者我可能处理它完全错误,但任何和所有的帮助将不胜感激。我对解决这样的问题完全是个初学者。谢谢你


Tags: inimagehomeforpngmode模式pi
1条回答
网友
1楼 · 发布于 2024-09-30 22:28:14

下面是一个示例,演示如何使用multiprocessingpsutil启动和停止进程。在本例中,task_runner在启动新进程之前杀死所有正在运行的进程。在

from flask import Flask
import multiprocessing
import psutil

app = Flask(__name__)

def blink(var):
    while True:
        # do stuff
        print(var)

def task_runner(var):
    processes = psutil.Process().children()
    for p in processes:
        p.kill()
    process = multiprocessing.Process(target=blink, args=(var,))
    process.start()

@app.route("/red")
def red():
    task_runner('red')
    return 'red started'

@app.route("/blue")
def blue():
    task_runner('blue')
    return 'blue started'

if __name__ == "__main__":
    app.run()

对于您的问题,task_runner将类似于:

^{pr2}$

相关问题 更多 >