-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleds.py
131 lines (108 loc) · 4.26 KB
/
leds.py
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
import qi
import argparse
import sys
import pepper_cmd
import threading
# does the rotateEyes potentially infinitely, performing one turn at a time,
# until it is requested to stop, then it will terminate at the end of the current cycle.
class _ThinkingStoppable(threading.Thread):
def __init__(self, leds_service, rotation_period):
threading.Thread.__init__(self)
self.leds_service = leds_service
self.rotation_period = rotation_period
self.stop_flag = False
def run(self):
print "THINK LED START"
while not self.stop_flag:
self.leds_service.rotateEyes(0x00FF0000, self.rotation_period, self.rotation_period)
print "THINK LED END"
def request_stop(self):
self.stop_flag = True
def await_stop(self):
self.request_stop()
self.join()
# does a reimplementation of fadeListRGB potentially infinitely, performing one color at a time,
# until it is requested to stop, then it will terminate at the end of the current fade.
# Colrs must be either names or hex code (0x00RRGGBB). Mixed sequences might be allowed.
# Unlike fadeListRGB, all fades must have the same duration, for simplicity.
# Not that we wanted to do otherwise.
class _WaitingStoppable(threading.Thread):
def __init__(self, leds_service, leds_name, colors, fade_time):
threading.Thread.__init__(self)
self.leds_service = leds_service
self.leds_name = leds_name
self.colors = colors
self.fade_time = fade_time
self.i = 0
self.stop_flag = False
def run(self):
print "WAIT LED START"
while not self.stop_flag:
self.leds_service.fadeRGB(
self.leds_name,
self.colors[self.i],
self.fade_time
)
self.i = (self.i + 1) % len(self.colors)
print "WAIT LED END"
def request_stop(self):
self.stop_flag = True
def await_stop(self):
self.request_stop()
self.join()
class Leds:
def __init__(self):
print "init leds..."
self.session=pepper_cmd.robot.session
'''
#alternative connection
pip = "127.0.0.1"
pport = "41763"
self.session = qi.Session()
try:
self.session.connect("tcp://" + pip + ":" + str(pport))
except RuntimeError:
print "Can't connect to Naoqi at ip \"" + pip + "\" on port " + str(pport) +".\n"
sys.exit(1)
'''
self.group_name = "FaceLeds"
self.leds_service = self.session.service("ALLeds")
print "Led started"
def setGroup(self,name):
self.group_name=name
def winning(self):
name = self.group_name
self.leds_service.fadeRGB(name,"green",1.0)
def losing(self):
name = self.group_name
self.leds_service.fadeRGB(name,"red",1.0)
def waiting(self, duration=5, colors=['white', 'red', 'green', 'blue', 'yellow', 'magenta', 'cyan']):
print "LED START"
name = self.group_name
duration = int(duration)
colors.append(colors[0])
self.leds_service.fadeListRGB(name, colors, [duration * (i*1.0)/len(colors) for i in xrange(0, len(colors))])
print "LED END"
def waiting_parloop(self, fade_time=1, colors=['white', 'red', 'green', 'blue', 'yellow', 'magenta', 'cyan']):
# "parallel loop"
leds_thread = _WaitingStoppable(self.leds_service, self.group_name, colors, fade_time)
leds_thread.start()
return leds_thread
def thinking(self, total_duration=2, rotation_speed=1):
n_turns=int(total_duration/rotation_speed)
#rgb code: 0x00RRGGBB
self.leds_service.rotateEyes(0x00FF0000, rotation_speed, total_duration)
def thinking_parloop(self, rotation_period=1):
# "parallel loop"
leds_thread = _ThinkingStoppable(self.leds_service, rotation_period)
leds_thread.start()
return leds_thread
def leds_off(self):
name=self.group_name
self.leds_service.off(name)
def default(self):
name=self.group_name
self.leds_service.fadeRGB(name,"white",0.5)
def neutral(self):
name=self.group_name
self.leds_service.fadeRGB(name,"yellow",0.5)