def newClient(tcpCliSock,addr): file = open("safeMove.txt", "r") state = file.readline() if state == "True": try: data = '' data = tcpCliSock.recv(BUFSIZE).decode() if data == ctrCmd[0]: servo.left() print('left') if data == ctrCmd[1]: servo.right() print('right') if data == ctrCmd[2]: dc.stepsFor() print('forr') if data == ctrCmd[3]: dc.stepsBack() print('bkk') if data == ctrCmd[4]: servo.cent() print('centre') except KeyboardInterrupt: tcpSerSock.close() time.sleep(0.5) elif state == "False": led.ledOn(37) tcpCliSock.close() file.close()
def winningAnimation(winningLine): # convert line to pins winningPins = winningLine if Turn == GREEN: for i in range(0, 3): winningPins[i] = winningLine[i] + 9 wiringpi.delay(LONG_PAUSE) led.ledsOff(winningPins, MED_PAUSE) # flash all three for i in range(0, 2): led.ledsOnOff(winningPins, MED_PAUSE) led.ledsOn(winningPins, LONG_PAUSE) # flash one at a time for i in range(0, 2): led.ledsOff(winningPins, SHORT_PAUSE) for j in range(0, 3): led.ledOn(winningPins[j], MED_PAUSE) wiringpi.delay(SHORT_PAUSE) led.ledsOff(winningPins, SHORT_PAUSE) # flash all three again for i in range(0, 2): led.ledsOnOff(winningPins, SHORT_PAUSE) # turn back on for final board led.ledsOn(winningPins)
def handleCommand(command): command = command.lower() if command == "/ledon": led.ledOn() elif command == "/ledoff": led.ledOff() elif command == "/ledtoggle": led.ledToggle() else: print("Invalid Command")
def drawBoard(invert=False): # handle board inversion redOffset = 9 if invert else 0 greenOffset = 0 if invert else 9 led.allOff() for i in range(0, 9): if Board[i] == RED: led.ledOn(i + redOffset) elif Board[i] == GREEN: led.ledOn(i + greenOffset)
def handleCommand(command): command = command.lower() if command == "/ledon": led.ledOn() elif command == "/ledoff": led.ledOff() elif command == "/ledtoggle": led.ledToggle() elif command == "/options": options = "Options" #\\n/ledon\\n" #options += "ledon\n" #options += "/ledoff\n" #options += "/ledtoggle\n" telegram.sendMessage(idUser, options) else: print("Invalid Command")
def playGame(): # set initial conditions global Turn, Board Board = [EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY] moveButtonState = selectButtonState = NOT_PRESSED nextTurn() GameState = READY curSquare = flashCount = readyCount = 0 curPin = squareToPin(curSquare) led.allOff() # the game loop while GameState != GAME_OVER: flashCount += 1 # increment the flashing led "timer" if GameState == ANIMATING: # every READY_ANIMATION_SPEED flashCounts, draw one frame of READY_ANIMATION if flashCount % ANIMATION_SPEED == 0: curLeds = ANIMATION[(flashCount / ANIMATION_SPEED) - 1] if curLeds[0] == END: # last frame of ANIMATION: reset and back to READY flashCount = 0 led.allOff() GameState = READY wiringpi.delay(MED_PAUSE) else: if curLeds[0] == OFF: # turn these leds off led.ledsOff(curLeds[1:]) else: # turn these leds on led.ledsOn(curLeds) else: # flash current square if flashCount == 1: led.ledOn(curPin) elif flashCount == FLASH_ON: led.ledOff(curPin) elif flashCount > FLASH_ON + FLASH_OFF: flashCount = 0 if GameState == READY: # each flash brings us closer to ANIMATION readyCount += 1 # get button inputs moveButton = wiringpi.digitalRead(LEFT_BUTTON_PIN) selectButton = wiringpi.digitalRead(RIGHT_BUTTON_PIN) # --- handle select button --- if selectButton == DOWN: if selectButtonState == NOT_PRESSED: # act right away on button press selectButtonState = PRESSED flashCount = readyCount = 0 if GameState == ANIMATING: # stop the animation GameState = READY led.allOff() else: GameState = PLAYING # flash selected square and turn it on for i in range(0, 3): led.flashOne(curPin, SHORT_PAUSE) led.ledOn(curPin) # update Board and check for game over Board[curSquare] = Turn winningLine = checkForWin() if winningLine: # we have a winner! GameState = GAME_OVER # flash winning line winningAnimation(winningLine) else: # no winner, continue on nextTurn() curSquare, curPin = nextEmpty(8) if curSquare < 0: # no more empty squares? it's a tie! GameState = GAME_OVER print " + It's a tie!" gameTiedAnimation() nextTurn(False) # flip first player on ties # clear the other button's state moveButton = UP moveButtonState = NOT_PRESSED else: if selectButtonState == PRESSED: selectButtonState = NOT_PRESSED # pause to avoid button flooding wiringpi.delay(SHORT_PAUSE) # --- handle move button --- if moveButton == DOWN: if moveButtonState == NOT_PRESSED: # act right away on button press moveButtonState = PRESSED flashCount = readyCount = 0 if GameState == ANIMATING: # stop the animation GameState = READY led.allOff() else: # advance the flashing led to the next empty square led.ledOff(squareToPin(curSquare)) curSquare, curPin = nextEmpty(curSquare) # clear the other button's state selectButtonState = NOT_PRESSED else: if moveButtonState == PRESSED: moveButtonState = NOT_PRESSED # pause to avoid button flooding wiringpi.delay(SHORT_PAUSE) if GameState == READY and readyCount > READY_FLASHES: # too long in READY state? play the ANIMATION for attention! GameState = ANIMATING readyCount = 0 # GAME_OVER led.wiringpi.delay(LONG_PAUSE * 8) resetAnimation()