-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_websocket02.py
More file actions
143 lines (123 loc) · 4.01 KB
/
Copy pathled_websocket02.py
File metadata and controls
143 lines (123 loc) · 4.01 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
137
138
139
140
141
142
143
import time
import board
import neopixel
import websocket
import json
# URL du serveur websocket sur la Raspberry Pi
server_url = "ws://172.20.10.6:8000/ws/swipes"
# Choix d'un pin ouvert connecté à l'entrée de données de la bande NeoPixel, par exemple board.D18
pixel_pin = board.D18
# Nombre de NeoPixels
num_pixels = 115
intensity = 55
switch = False
# Configuration des NeoPixels
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.2, auto_write=False)
def up_left():
for i in range(0, 5):
pixels[i] = (255, 255, 255)
for i in range(110, 115):
pixels[i] = (255, 255, 255)
pixels.show()
def right():
for i in range(13, 23):
pixels[i] = (255, 255, 255)
pixels.show()
def down_left():
for i in range(31, 41):
pixels[i] = (255, 255, 255)
pixels.show()
def down():
for i in range(41, 51):
pixels[i] = (255, 255, 255)
pixels.show()
def down_right():
for i in range(52, 62):
pixels[i] = (255, 255, 255)
pixels.show()
def left():
for i in range(70, 80):
pixels[i] = (255, 255, 255)
pixels.show()
def up_right():
for i in range(89, 99):
pixels[i] = (255, 255, 255)
pixels.show()
def up():
for i in range(99, 109):
pixels[i] = (255, 255, 255)
pixels.show()
def shutdown():
pixels.fill((0, 0, 0))
pixels.show()
def hover_select(data):
if data['action'] == 'hover_down-right':
up_left()
if data['action'] == 'hover_right':
left()
if data['action'] == 'hover_up-right':
down_left()
if data['action'] == 'hover_down':
up()
if data['action'] == 'hover_down-left':
up_right()
if data['action'] == 'hover_left':
right()
if data['action'] == 'hover_up-left':
down_right()
if data['action'] == 'hover_up':
down()
# Fonction appelée lorsque la connexion websocket est ouverte
def on_open(ws):
print("Connexion établie")
# Fonction appelée lors de la réception de données via websocket
def on_message(ws, message):
global intensity
global switch
try:
# Analyser les données JSON reçues
data = json.loads(message)
print("Données reçues:", data)
if data['hand'] is None:
if switch == True:
switch = False
if intensity > 55:
intensity = intensity - 50
pixels.fill((intensity, intensity, intensity))
pixels.show()
else:
if intensity < 255 and switch == False:
intensity = intensity + 50
if intensity == 255:
switch = True
if switch == True and intensity > 105:
intensity = intensity - 50
pixels.fill((intensity, intensity, intensity))
pixels.show()
if data['action'] == 'click':
pixels.fill((0, 255, 0))
pixels.show()
time.sleep(0.5)
pixels.fill((intensity, intensity, intensity))
pixels.show()
if data['action'] == 'up' or data['action'] == 'down' or data['action'] == 'left' or data['action'] == 'right' or data['action'] == 'up-left' or data['action'] == 'up-right' or data['action'] == 'down-left' or data['action'] == 'down-right':
pixels.fill((0, 0, 255))
pixels.show()
time.sleep(0.5)
pixels.fill((intensity, intensity, intensity))
pixels.show()
hover_select(data)
except Exception as e:
print("Erreur lors du traitement des données:", e)
# Fonction pour se connecter au serveur websocket
def connect_to_websocket():
print("Tentative de connexion au serveur websocket...")
ws = websocket.WebSocketApp(server_url, on_open=on_open, on_message=on_message)
ws.run_forever()
# Boucle pour essayer de se reconnecter toutes les 5 secondes
while True:
try:
connect_to_websocket()
except Exception as e:
print("Erreur lors de la connexion au serveur websocket:", e)
time.sleep(5)