← Back to ratslair.com
aboutsummaryrefslogtreecommitdiff
path: root/control.py
blob: f86d3bfd1a91c83c6b6067e7710bfc1e203acbd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import bluetooth
import time

import termios
import tty
import sys

def get_input() -> str:
  
    filedescriptors = termios.tcgetattr(sys.stdin)
    tty.setcbreak(sys.stdin)
    key = sys.stdin.read(1)[0]
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN,filedescriptors)
  
    return key

pi = "B8:27:EB:D1:A5:A9"

sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)

print("Attempting to connect to %s..." % pi)

sock.connect((pi, 1))

print("Connected to %s." % pi)

input = "0"

while input != "q":
    input = get_input()

    print("Sending: %s" % input)
    if input == "i":
        sock.send("11")
    elif input == "j":
        sock.send("44")
    elif input == "l":
        sock.send("33")
    elif input == "k":
        sock.send("22")
    elif input == "q":
        sock.send("1000")
    else:
        sock.send("0")

    read = sock.recv(1024)
    read = read.decode("utf-8").strip('\n')
    print("Received: '%s'" % read )

    time.sleep(0.1)

print("Sending: %s" % input)
sock.send(input)

read = sock.recv(1024)
read = read.decode("utf-8").strip('\n')
print("Received: '%s'" % read )

print("Shutting down...")

sock.close()