EasyManua.ls Logo

SunFounder PiSloth - Page 98

SunFounder PiSloth
113 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder pisloth
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.
key = readchar().lower()
# 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
Note: You can add different sound effects or music to musics or sounds folder via Filezilla Software.
94 Chapter 4. Play with Python

Related product manuals