Skip to content

Instantly share code, notes, and snippets.

@tjoen
Created January 31, 2018 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjoen/f19540b802e6d46604156652f73a338c to your computer and use it in GitHub Desktop.
Save tjoen/f19540b802e6d46604156652f73a338c to your computer and use it in GitHub Desktop.
Mycroft test script to use animated character, needs 7 gifs with correct mouthshapes
import websocket
import time
import threading
import json
import pyglet
# Create and open a window
window = pyglet.window.Window(257, 257)
# Load sprites
s0 = pyglet.resource.image('0.gif')
s1 = pyglet.resource.image('1.gif')
s2 = pyglet.resource.image('2.gif')
s3 = pyglet.resource.image('3.gif')
s4 = pyglet.resource.image('4.gif')
s5 = pyglet.resource.image('5.gif')
s6 = pyglet.resource.image('6.gif')
sprites = [s0, s1, s2, s3, s4, s5, s6]
# Animation
#sprite = pyglet.sprite.Sprite(s1) #anim
frame_index = 4
current_frame = sprites[frame_index]
def update(dt):
global current_frame
current_frame = sprites[frame_index]
@window.event
def on_draw():
window.clear()
current_frame.blit(0, 0)
class ThreadingPyg(object):
""" Threading example class
The run() method will be started and it will run in the background
until the application exits.
"""
def __init__(self, interval=0.2):
""" Constructor
:type interval: int
:param interval: Check interval, in seconds
"""
self.interval = interval
thread = threading.Thread(target=self.run, args=())
thread.daemon = True # Daemonize thread
thread.start() # Start the execution
def on_message(self, ws, message):
mes = json.loads( message )
global frame_index
if mes['type'] == 'enclosure.mouth.viseme':
frame_index = int(mes['data']['code'])
#print( mes['data']['code'] )
def on_error(self, ws, error):
print(error)
def on_close(self, ws):
print("### closed ###")
def run(self):
""" Method that runs forever """
while True:
# Do something
print('Starting websocket')
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:8181/core",
on_message = self.on_message,
on_error = self.on_error,
on_close = self.on_close)
ws.run_forever()
if __name__ == '__main__':
sockit = ThreadingPyg()
pyglet.clock.schedule_interval(update, 1/30.0)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment