SunFounder pisloth
(continued from previous page)
(manual)
while
True
:
key
=
readchar()
# print(key)
if
key
==
"w"
:
sloth
.
do_action(
'forward'
,
1
,
90
)
elif
key
==
"a"
:
sloth
.
do_action(
'turn left'
,
1
,
90
)
elif
key
==
"s"
:
sloth
.
do_action(
'backward'
,
1
,
90
)
elif
key
==
"d"
:
sloth
.
do_action(
'turn right'
,
1
,
90
)
elif
key
==
"1"
:
music
.
sound_effect_play(
'./sounds/talk1.wav'
)
elif
key
==
"2"
:
music
.
sound_effect_play(
'./sounds/talk2.wav'
)
elif
key
==
"3"
:
music
.
sound_effect_play(
'./sounds/talk3.wav'
)
elif
key
==
"4"
:
music
.
sound_effect_play(
'./sounds/depress.wav'
)
elif
key
==
"q"
:
tts
.
say(
"Oh hello there"
)
elif
key
==
"e"
:
tts
.
say(
"bye"
)
elif
key
==
chr
(
27
):
# 27 for ESC
break
time
.
sleep(
0.05
)
(
"
\n
Quit"
)
if
__name__
==
"__main__"
:
main()
How it works?
This function refers to the standard input stream and returns the first character of the data stream read.
•
tty.setraw(sys.stdin.fileno)
is to change the standard input stream to raw mode, that is, all char-
acters will not be escaped during transmission, including special characters. Before changing the mode, back
up the original mode, and restore it after the change.
•
old_settings = termios.tcgetattr(fd)
and
termios.tcsetattr(fd, termios.
TCSADRAIN, old_settings)
plays the role of backup and restore.
def
readchar
():
fd
=
sys
.
stdin
.
fileno()
old_settings
=
termios
.
tcgetattr(fd)
try
:
tty
.
setraw(sys
.
stdin
.
fileno())
ch
=
sys
.
stdin
.
read(
1
)
finally
:
termios
.
tcsetattr(fd, termios
.
TCSADRAIN, old_settings)
return
ch
Finally, according to the read keyboard characters, let PiSloth do the actions we set, call the
tts.say()
function to
speak or play the sound effects prepared in advance.
4.8. Remote Control
75
Summary of Contents for PiSloth
Page 1: ...SunFounder pisloth www sunfounder com Oct 19 2021 ...
Page 2: ......
Page 4: ...ii ...
Page 6: ...SunFounder pisloth 2 CONTENTS ...
Page 8: ...SunFounder pisloth 4 Chapter 1 Component List and Assembly Instructions ...
Page 18: ...SunFounder pisloth EXAMPLE 14 Chapter 3 Play with Ezblock ...
Page 20: ...SunFounder pisloth 16 Chapter 3 Play with Ezblock ...
Page 22: ...SunFounder pisloth Confuse Happy 18 Chapter 3 Play with Ezblock ...
Page 23: ...SunFounder pisloth Fear Sad 3 5 Emotional PiSloth 19 ...
Page 25: ...SunFounder pisloth 3 6 Dance Now PiSltoh will show you his newly learned dance 3 6 Dance 21 ...
Page 28: ...SunFounder pisloth 24 Chapter 3 Play with Ezblock ...
Page 32: ...SunFounder pisloth 28 Chapter 3 Play with Ezblock ...
Page 35: ...SunFounder pisloth 3 8 Remote Control 31 ...
Page 39: ...SunFounder pisloth 3 9 Custom Step 35 ...
Page 41: ...SunFounder pisloth The complete code is as follows 3 9 Custom Step 37 ...
Page 50: ...SunFounder pisloth Step 7 Click the WRITE button 46 Chapter 4 Play with Python ...
Page 86: ...SunFounder pisloth 82 Chapter 4 Play with Python ...
Page 90: ...SunFounder pisloth Yes then Ok Finish 86 Chapter 5 Appendix ...
Page 94: ...SunFounder pisloth Step 4 Double click the connection just created 90 Chapter 5 Appendix ...
Page 102: ...SunFounder pisloth 98 Chapter 5 Appendix ...