Created
March 18, 2017 22:31
-
-
Save banditkroot/4d7fd377f7c99cdddd1af1404504367c to your computer and use it in GitHub Desktop.
Pocket Chip bluetooth RC car
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
import bluetooth | |
import pygame | |
import sys | |
#Préparation de l'écran et de pygame | |
size = width, height = 480, 272 | |
screen = pygame.display.set_mode(size) | |
pygame.display.set_caption("Fiat 500") | |
pygame.init() | |
font = pygame.font.SysFont("Monospace", 15) | |
screen.blit(back, (0, 0)) | |
#Préparation du bluetooth | |
uuid = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #uuid du service SPP | |
addr = "xx:xx:xx:xx:xx:xx" #mac de la voiture | |
service_matches = bluetooth.find_service(uuid = uuid, address = addr) | |
first_match = service_matches[0] | |
if len(service_matches) == 0: | |
label_err = font.render("Voiture non détectée !",1 , (180, 20, 0)) | |
screen.blit(label_err, (50,50)) | |
sys.exit(0) | |
port = first_match["port"] | |
name = first_match["name"] | |
host = first_match["host"] | |
label_conn = font.render("Connexion à la voiture...",1 , (180, 20, 0)) | |
screen.blit(label_conn, (50,50)) | |
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) | |
sock.connect((host, port)) | |
label_conn = font.render("Connecté !",1 , (180, 20, 0)) | |
screen.blit(label_conn, (50,66)) | |
avar = "" | |
while True: | |
for event in pygame.event.get(): | |
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: | |
pygame.display.quit() | |
sock.close() | |
sys.exit(0) | |
elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP: | |
avar = "\x01" | |
elif event.type == pygame.KEYUP and event.key == pygame.K_UP: | |
sock.send("\x00") | |
avar = "" | |
elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN: | |
avar = "\x03" | |
elif event.type == pygame.KEYUP and event.key == pygame.K_DOWN: | |
sock.send("\x02") | |
avar = "" | |
elif event.type == pygame.KEYDOWN and event.key == pygame.K_0: | |
sock.send("\x05") | |
elif event.type == pygame.KEYUP and event.key == pygame.K_0: | |
sock.send("\x04") | |
elif event.type == pygame.KEYDOWN and event.key == pygame.K_EQUALS: | |
sock.send("\x07") | |
elif event.type == pygame.KEYUP and event.key == pygame.K_EQUALS: | |
sock.send("\x06") | |
if len(avar) > 0: | |
sock.send(avar) | |
pygame.display.update() | |
pygame.time.delay(200) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment