예제 #1
0
 def run(self):
     while True:
         try:
             httpd = socketserver.TCPServer(('', self.PORT),
                                            self.handler,
                                            bind_and_activate=False)
             httpd.allow_reuse_address = True
             httpd.server_bind()
             httpd.server_activate()
             success("HTTP server started at port", self.PORT)
             try:
                 self.address.put("http://127.0.0.1:" + str(self.PORT) +
                                  "/static/face")
                 self.address.put("http://127.0.0.1:" + str(self.PORT) +
                                  "/static/controller")
                 httpd.serve_forever()
             except KeyboardInterrupt:
                 httpd.server_close()
                 normal("HTTP server shut down.")
         except OSError as exc:
             if exc.args[0] != 48:
                 raise
             error('Port', self.PORT, 'already in use')
             self.PORT += 1
         else:
             break
예제 #2
0
async def controller_handler(websocket, path):
    normal("Outgoing connection established.")
    while True:
        cmd = wsControllerCommands.get(True)
        try:
            await websocket.send(cmd)
        except websockets.ConnectionClosed:
            warning("Outgoing connection closed.")
            break
예제 #3
0
async def input_handler(websocket, path):
    normal("Incoming connection established.")
    while True:
        try:
            evt = await websocket.recv()
        except websockets.ConnectionClosed:
            warning("Incoming connection closed.")
            break
        wsEvents.put(evt)
예제 #4
0
    def loop(self):

        if self.newSequence:
            normal("NEW_SEQ")
            self.newSequence = False
            self.currentPhrase = self.generatePhrase(self.notes,
                                                     self.noteCount)
            self.noteCount += 1
            for note in self.currentPhrase:
                self.delay(1)
                self.oniMakeNote(note)
            self.delay(1)
예제 #5
0
    def run(self):
        self.start_server = websockets.serve(face_handler, '0.0.0.0', self.port)

        asyncio.get_event_loop().run_until_complete(self.start_server)
        normal("Outgoing websocket server started at", self.port)
        loop = asyncio.get_event_loop()
        try:
            self.ready.set()
            loop.run_forever()
        except KeyboardInterrupt:
            loop.stop()
            loop.close()
            normal("Outgoing websocket server shut down.")
예제 #6
0
 def _mainloop(self):
     if not self.port:
         return
     while True:
         try:
             ch = self.commands.get()
             self.port.write(ch)
         except KeyboardInterrupt:
             if self.port:
                 normal("Close serial port.")
                 self.port.close()
                 self.incoming.terminate.set()
                 self.incoming.join()
             break
예제 #7
0
 def onWebsocket(self, msg):
     if msg['cmd'] == 'animation':
         self.send("face", {"cmd": "face-change", "id": msg["id"]})
     elif msg['cmd'] == 'face-finished':
         normal(msg['id'])
     elif msg['cmd'] == 'interaction':
         if msg['id'] == 0:
             self.interactionQueue.clear()
             self.interactionQueue.add(Sleepy)
         elif msg['id'] == 1:
             self.interactionQueue.clear()
             self.interactionQueue.add(Duet)
         elif msg['id'] == 2:
             self.interactionQueue.clear()
             self.interactionQueue.add(MemoryGame)
예제 #8
0
    def onMidi(self, msg):
        # normal(msg)
        if msg['cmd'] == 'note-on':
            if self.lastTime == msg['time']:
                self.lastTime = msg['time']
                return
            self.lastTime = msg['time']
            notice(self.state)
            if self.state == 'config-1':
                normal("CONF_1")
                if msg['note'] in self.notes:
                    self.makeFace("nono.json")
                else:
                    self.oniSetLED(0, 2)
                    self.oniSetLED(1, 1)
                    self.makeFace("hint.json")
                    self.state = 'config-2'
                    self.notes[0] = msg['note']
                return

            elif self.state == 'config-2':
                normal("CONF_2")
                if msg['note'] in self.notes:
                    self.makeFace("serious-1.json")
                else:
                    self.oniSetLED(1, 2)
                    self.oniSetLED(2, 1)
                    self.makeFace("hint.json")
                    self.state = 'config-3'
                    self.notes[1] = msg['note']
                return

            elif self.state == 'config-3':
                normal("CONF_3")
                if msg['note'] in self.notes:
                    self.makeFace("nono.json")
                else:
                    self.oniSetLED(2, 2)
                    self.makeFace("hint.json")
                    self.delay(0.3)
                    self.notes[2] = msg['note']
                    self.makeFace('happy_open.json')
                    self.delay(1)
                    self.makeFace('serious-2.json')
                    self.setAlarm(5, self.hint)
                    self.state = 'playing'

                    self.started = True

                    self.newSequence = True
                return

            elif self.state == 'playing' and self.started:
                normal("NOTE_INPUT")
                normal(self.inputCounter)
                normal(self.currentPhrase[self.inputCounter])
                if msg['note'] == self.currentPhrase[self.inputCounter]:
                    # player correctly inputs a note
                    self.inputCounter += 1
                    if self.noteCount == self.inputCounter + 1:
                        # player gets a point
                        normal("PLAYER SCORES")
                        self.inputCounter = 0
                        self.playerScore += 1
                        if self.playerScore == 2:
                            # player wins!
                            self.win('player')
                            return
                        self.oniShowPoints(self.playerScore, self.chirpScore)
                        # chirp is not happy about this
                        self.delay(0.3)
                        self.makeFace('pityful.json')
                        self.delay(0.5)
                        self.makeFace('serious-1.json')
                        # chirp's turn
                        self.delay(1)

                        mistake = False
                        for note in self.currentPhrase:
                            normal("Chirp plays")
                            if random.random() < self.errorRate:
                                # chirp "decides" to make a mistake
                                self.makeFace('laugh.json')
                                n = random.choice(self.notes)
                                while n == note:
                                    n = random.choice(self.notes)
                                self.noteDelay(n, 0.6)
                                self.makeFace('serious-1.json')
                                self.delay(0.4)
                                # The mistake is made
                                mistake = True
                                break
                            else:
                                self.makeFace('laugh.json')
                                self.noteDelay(note, 0.6)
                                self.makeFace('serious-1.json')
                                self.delay(0.4)

                        if mistake:
                            # a mistake was made!
                            self.oniFail()
                            self.makeFace('pityful.json')
                            self.delay(0.5)
                            self.makeFace('nono.json')
                            self.delay(1.5)
                            self.makeFace('serious-1.json')
                        else:
                            self.oniSuccess()
                            self.makeFace('laugh.json')
                            self.delay(1.5)
                            self.chirpScore += 1
                            if self.chirpScore == 2:
                                # chirp wins!
                                self.win('chirp')
                                return
                        self.newSequence = True
                    self.oniShowPoints(self.playerScore, self.chirpScore)

                else:
                    # player fails
                    self.oniFail()
                    self.makeFace('laugh.json')
                    self.delay(1.5)

                    # chirp's turn
                    self.delay(1)

                    mistake = False
                    for note in self.currentPhrase:
                        if random.random() < self.errorRate:
                            # chirp "decides" to make a mistake
                            self.makeFace('laugh.json')
                            n = random.choice(self.notes)
                            while n == note:
                                n = random.choice(self.notes)
                            self.noteDelay(n, 0.6)
                            self.makeFace('serious-1.json')
                            self.delay(0.4)
                            # The mistake is made
                            mistake = True
                            break
                        else:
                            self.makeFace('laugh.json')
                            self.noteDelay(note, 0.6)
                            self.makeFace('serious-1.json')
                            self.delay(0.4)

                    if mistake:
                        # a mistake was made!
                        self.oniFail()
                        self.makeFace('pityful.json')
                        self.delay(0.5)
                        self.makeFace('nono.json')
                        self.delay(1.5)
                        self.makeFace('serious-1.json')
                    else:
                        self.oniSuccess()
                        self.makeFace('laugh.json')
                        self.delay(1.5)
                        self.chirpScore += 1
                        if self.chirpScore == 2:
                            # chirp wins!
                            self.win('chirp')
                            return
                    self.newSequence = True
                self.oniShowPoints(self.playerScore, self.chirpScore)
                return
예제 #9
0
 def terminate(self):
     normal("InteractionQueue shutting down.")
     self.clear()
     self.commands.close()
예제 #10
0
 def __init__(self):
     normal("InteractionQueue running.")
     self.running.clear()