0

我正在用 python 编程 ada 果环 LED 灯,我已经为 midi 控制器设置了一个本地网络,以将数据读入我的 Raspberry Pi 3 并通过网络套接字发送到 3 Pi Zero W。我遇到了一个问题,即我在线程上运行的某些功能将彼此不同步。换句话说,其中一个 LED 灯将以稍快或稍慢的速度运行,最终看起来不同步。我很好奇是否有人遇到过这样的问题,或者我是否只是以不正确的方式使用线程和事件。

这是我的一些服务器端代码:

import socket
from socket import *
from threading import Thread
import threading
import board
import neopixel
import sys
import time
import random
import os
from adafruit_led_animation.animation.comet import Comet

def handle_client(client):
     p = None
     connected = True
     lights_on_default()
     while True:
          global thread_running
          data=client.recv(1024).decode()
          if not data:
               connected = False
               thread_running = False
               break
          if 'Pad' in data:
               thread_running = False
               thread = threading.Event()
               if data == 'Pad 1':
                    thread_running = False
                    if p:
                         p.join()
                    p = Thread(target=slow_circles, args=(thread,))
                    p.daemon = True
                    p.start()

def slow_circles(event):
     global thread_running
     thread_running = True
     while not event.isSet():
          if not thread_running:
               event.set()
               break
          for i in range(num_of_pixels):
               // goes through the lighting of pixels
               if not thread_running:
                    event.set()
                    break
               pixels.show()

这是我的一些客户端代码:

import rtmidi.midiutil as midiutil
import time
import socket

buttons = {
     36: 'Pad 1',
}

try: 
     s1 = socket.socket()
     s2 = socket.socket()
except:
     print('Error making socket')
port1 = 12346
port2 = 12347

serverOne = '192.168.1.18' // 1st Pi Zero W
serverTwo = '192.168.1.17' // 2nd Pi Zero W

def start():
     global s1
     global s2
     notConnected = True
     while True:
          while notConnected:
               connected = False
               try:
                    s1.connect((serverOne, port1))
                    s2.connect((serverTwo, port2))
                    connected = True
                    break
               except:
                    s1.close()
                    s2.close()
                    s1.socket.socket()
                    s2.socket.socket()
          if connected:
               midiin, port = midiutil.open_midiinput(1)
               midiin.set_callback(midiCallback) // gets pad from midi controller
               while True:
                    retry = False
                    try:
                         s1.sendall('red connected'.encode('utf8'))
                         s2.sendall('yellow connected'.encode('utf8'))
                    except:
                         retry = True
                    if retry:
                         s1.close()
                         s2.close()
                         midiin.delete()
                         break
                    else:
                         time.sleep(15)

def midiCallback(message, data):
     control = message[0][1]
     try:
          value = message[0][2]
          if (control in button.keys()):
               name = buttons[control]
               if (value > 0):
                    return buttonDown(name)
     except IndexError:
          pass

def buttonDown(button):
     s1.sendall(f'{button}'.encode('utf8'))
     s2.sendall(f'{button}'.encode('utf8'))

start()

当我敲击键盘并执行该功能时,我将数据发送到两个 PI,它们同时启动,但随着时间的推移会不同步。感谢所有帮助。

4

0 回答 0