Skip to content

Commit a037723

Browse files
Merge pull request #2 from karol-szuster/master
Ported to kulka
2 parents 7b7be41 + c19beac commit a037723

35 files changed

Lines changed: 738 additions & 567 deletions

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.o
2+
*.so
3+
*.pyc
4+
*.pyo
5+
.cache/
6+
__pycache__/
7+
build/
8+
*.egg-info/
9+
dist/

Examples/spheroBlink.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
import thread
2-
import time
3-
from sphero import core
1+
from kulka import Kulka
42
import random
3+
import time
4+
55

6-
blink_rate = 1
6+
ADDR = 'XX:XX:XX:XX:XX:XX'
77

8-
sphero = core.Sphero("/dev/tty.Sphero-OOB-AMP-SPP") #create Sphero controller
9-
print "Connecting to Sphero..."
10-
sphero.connect() #initialize connection
118

12-
def start_blinking(sphero,delay):
13-
count = 0
14-
while count < 5:
15-
global blink_rate
16-
blink_rate-=0.05
17-
blink_rate=abs(blink_rate)
18-
sphero.set_rgb(random.randint(0,255),random.randint(0,255),random.randint(0,255))
19-
time.sleep(blink_rate)
20-
sphero.set_rgb(0,0,0)
21-
time.sleep(blink_rate)
9+
def main():
10+
with Kulka(ADDR) as kulka:
11+
kulka.set_inactivity_timeout(3600)
12+
blink_rate = 1
2213

23-
try:
24-
thread.start_new_thread(start_blinking, (sphero, 2) )
25-
except:
26-
print "Error: unable to start thread"
14+
for _ in range(5):
15+
blink_rate = abs(blink_rate - 0.05)
16+
kulka.set_rgb(random.randint(0, 255), random.randint(0, 255),
17+
random.randint(0, 255))
18+
time.sleep(blink_rate)
19+
kulka.set_rgb(0, 0, 0)
20+
time.sleep(blink_rate)
2721

28-
while 1:
29-
pass
22+
main()

Examples/spheroCircle.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1-
from sphero import core
1+
from kulka import Kulka
22
import time
33

4-
s = core.Sphero("/dev/tty.Sphero-OOB-AMP-SPP")
5-
print "Connecting to Sphero..."
6-
s.connect()
74

8-
speed = 0x88
9-
sleep_time = 0.1
5+
ADDR = 'XX:XX:XX:XX:XX:XX'
106

11-
def make_a_step(current_angle):
12-
s.roll(speed,current_angle)
13-
time.sleep(sleep_time)
147

15-
def make_a_circle(steps):
16-
rotate_by = int(360/steps)
17-
current_angle = 1
18-
i = 0
19-
while i < steps:
20-
make_a_step(current_angle%360)
21-
current_angle += rotate_by
22-
i += 1
8+
STEPS = 10
9+
SPEED = 0x30
10+
SLEEP_TIME = 0.3
2311

24-
steps=10
25-
make_a_circle(steps)
12+
13+
def make_a_step(kulka, current_angle):
14+
kulka.roll(SPEED, current_angle)
15+
time.sleep(SLEEP_TIME)
16+
kulka.roll(0, current_angle)
17+
18+
19+
def make_a_circle(kulka, steps):
20+
rotate_by = 360 // steps
21+
current_angle = 1
22+
23+
for _ in range(steps):
24+
make_a_step(kulka, current_angle % 360)
25+
current_angle += rotate_by
26+
27+
28+
def main():
29+
with Kulka(ADDR) as kulka:
30+
make_a_circle(kulka, STEPS)
31+
32+
33+
main()

Examples/spheroColors.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
from sphero import core
2-
import time
1+
from __future__ import print_function
2+
from kulka import Kulka
3+
import time
34

4-
s = core.Sphero("/dev/tty.Sphero-OOB-AMP-SPP")
5-
print "Connecting to Sphero..."
6-
s.connect()
7-
print "My current color is: " , s.get_rgb().body
8-
iterations = 3
9-
while iterations > 0:
10-
print "Setting color to red"
11-
s.set_rgb(255,0,0)
12-
print "Setting color to green"
13-
s.set_rgb(0,255,0)
14-
print "Setting color to blue"
15-
s.set_rgb(0,0,255)
16-
time.sleep(0.1)
17-
iterations -= 1
185

6+
ADDR = 'XX:XX:XX:XX:XX:XX'
197

8+
9+
with Kulka(ADDR) as kulka:
10+
for _ in range(3):
11+
print("Setting color to red")
12+
kulka.set_rgb(255, 0, 0)
13+
time.sleep(0.1)
14+
15+
print("Setting color to green")
16+
kulka.set_rgb(0, 255, 0)
17+
time.sleep(0.1)
18+
19+
print("Setting color to blue")
20+
kulka.set_rgb(0, 0, 255)
21+
time.sleep(0.1)

Examples/spheroRiddle.py

Lines changed: 83 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,90 @@
1+
from __future__ import print_function
2+
from Kulka import Kulka
3+
from textwrap import dedent
14
from random import randint
2-
from sphero import core
3-
import time
4-
import thread
5-
6-
# this program is a simple implementation of a game,
7-
# where the user is supposed to guess the number
8-
# in a range 1 to 100, while the program can give
9-
# him a hint, whether the number provided is greater,
10-
# equal or less to the one provided by the player
11-
12-
# pulse rate means how close is user to guess the number
13-
# the smaller the value is, the closer the user is.
14-
# range: 0-100
15-
16-
s = core.Sphero("/dev/tty.Sphero-OOB-AMP-SPP") #create Sphero controller
17-
print "Connecting to Sphero..."
18-
s.connect() #initialize connection
19-
20-
def show_hint_in_sphero(pulse_rate):
21-
print "pulse rate: %s" % pulse_rate
22-
green_channel=abs(255-int(pulse_rate*2.55))%255
23-
color="10,%s,10" % (green_channel)
24-
s.set_rgb(0,green_channel,0)
25-
print "color: "+color
26-
27-
def show_hint(difference):
28-
if difference > 0:
29-
print "My number is greater!"
30-
else:
31-
print "My number is smaller!"
32-
pulse_rate = abs(difference)
33-
show_hint_in_sphero(pulse_rate)
34-
35-
def game():
36-
print """
37-
Welcome to the SpheroRiddle game! Guess my number!
38-
If you want to have a hint about how close you are,
39-
take a look at the Sphero. The faster it blinks and
40-
the brighter it shines, the closer you are.
41-
--------------------------------------------------
42-
Type in the number!
5+
6+
try:
7+
input_ = raw_input
8+
except NameError:
9+
input_ = input
10+
11+
12+
ADDR = 'XX:XX:XX:XX:XX:XX'
13+
14+
4315
"""
16+
This program is a simple implementation of a game, where the user is
17+
supposed to guess the number in a range 1 to 100, while the program
18+
can give him a hint, whether the number provided is greater, equal
19+
or less to the one provided by the player
20+
21+
pulse rate means how close is user to guess the number the smaller
22+
the value is, the closer the user is. range: 0-100
23+
"""
24+
25+
26+
def show_hint_in_sphero(kulka, pulse_rate):
27+
green_channel = abs(255 - int(pulse_rate * 2.55)) % 255
28+
kulka.set_rgb(0, green_channel, 0)
29+
print("pulse rate: %s" % pulse_rate)
30+
print("color: 10, {}, 10".format(green_channel))
31+
32+
33+
def show_hint(kulka, difference):
34+
if difference > 0:
35+
print("My number is greater!")
36+
else:
37+
print("My number is smaller!")
38+
39+
pulse_rate = abs(difference)
40+
show_hint_in_sphero(kulka, pulse_rate)
41+
42+
43+
def game(kulka):
44+
print(dedent("""\
45+
Welcome to the SpheroRiddle game! Guess my number!
46+
If you want to have a hint about how close you are,
47+
take a look at the Sphero. The faster it blinks and
48+
the brighter it shines, the closer you are.
49+
--------------------------------------------------
50+
Type in the number!
51+
"""))
52+
53+
secret_number = randint(1, 100)
54+
guess = ''
55+
trials = 0
56+
57+
while True:
58+
guess = input_("> ")
59+
60+
if guess == 'q' or guess == 'quit':
61+
break
62+
elif guess.isdigit():
63+
guess = int(guess)
64+
trials += 1
65+
66+
if secret_number is not guess:
67+
show_hint(kulka, secret_number - int(guess))
68+
else:
69+
print("Bravo! You've guessed my number in %s trials!" % trials)
70+
break
71+
else:
72+
print("Please input the number or 'q'/'quit' if you want to end "
73+
"the game.")
4474

45-
secret_number=randint(1,100)
46-
guess=-1
47-
trials=0
48-
end=False
49-
while(not end):
50-
guess=raw_input("> ")
51-
if guess == 'q' or guess == 'quit':
52-
end = True
53-
elif guess.isdigit():
54-
guess=int(guess)
55-
trials = trials + 1
56-
if secret_number is not guess:
57-
show_hint(secret_number-guess)
58-
else:
59-
print "Bravo! You've guessed my number in %s trials!" % trials
60-
end=True
61-
else:
62-
print "Please input the number or 'q'/'quit' if you want to end the game."
6375

6476
def play_next():
65-
next=raw_input("Do you want to play another game? (y/n): ").lower()
66-
return next=="y" or next =="yes"
77+
next_ = input_("Do you want to play another game? (y/n): ")
78+
return next_.lower() in ["y", "yes"]
79+
80+
81+
def main():
82+
with Kulka(ADDR) as kulka:
83+
while True:
84+
game(kulka)
85+
86+
if not play_next():
87+
break
6788

68-
game()
69-
while play_next():
70-
game()
7189

90+
main()

Examples/spheroSleep.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
from sphero import core
2-
s = core.Sphero("/dev/tty.Sphero-OOB-AMP-SPP")
3-
print "Connecting to Sphero..."
4-
s.connect()
5-
s.sleep()
1+
from kulka import Kulka
2+
3+
4+
ADDR = 'XX:XX:XX:XX:XX:XX'
5+
6+
7+
with Kulka(ADDR) as kulka:
8+
kulka.sleep()

Examples/spheroSquare.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
from sphero import core
1+
from __future__ import print_function
2+
from kulka import Kulka
23
import time
34

4-
s = core.Sphero("/dev/tty.Sphero-OOB-AMP-SPP")
5-
print "Connecting to Sphero..."
6-
s.connect()
75

8-
def doTheDance():
9-
speed = 0x88
10-
sleepTime = 1
11-
s.roll(speed,1)
12-
time.sleep(sleepTime)
13-
s.roll(speed,90)
14-
time.sleep(sleepTime)
15-
s.roll(speed,180)
16-
time.sleep(sleepTime)
17-
s.roll(speed,270)
18-
time.sleep(sleepTime)
19-
s.stop()
6+
ADDR = 'XX:XX:XX:XX:XX:XX'
207

21-
i=3
22-
while i>0:
23-
doTheDance()
24-
time.sleep(0.1) #sleep 1 seconds
25-
i-=1
268

27-
print "The end. Sphero goes to bed."
28-
s.sleep()
9+
def do_the_dance(kulka):
10+
speed = 0x88
11+
sleep_time = 1
2912

13+
for angle in [1, 90, 180, 270]:
14+
kulka.roll(speed, angle)
15+
time.sleep(sleep_time)
3016

17+
kulka.roll(0, 0)
3118

19+
20+
def main():
21+
with Kulka(ADDR) as kulka:
22+
for _ in range(3):
23+
do_the_dance(kulka)
24+
time.sleep(0.1)
25+
26+
print("The end. Sphero goes to bed.")
27+
kulka.sleep()

0 commit comments

Comments
 (0)