/
ProcessConnectionThread.py
103 lines (93 loc) · 3.12 KB
/
ProcessConnectionThread.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
""" ProcessConnectionThread.py
Purpose: Final demo
Author: Tiago Pimentel
t.pimentelms@gmail.com
Joao Felipe
joaofelipenp@gmail.com
Matheus Camargo
matheusfc09@gmail.com
CSE 467 -- Embedded Computing Systems
WUSTL, Spring 2013
Date: Apr., 29, 2013
Description:
Process the bluetooth connection
Version log:
30/04/2013 Joao Felipe
Sending light status through bluetooth
"""
from java.lang import Runnable
from java.nio import ByteBuffer
EXIT_CMD = -1
START = 2
END = 3
SET_LIGHT_STATUS = 76
REQUEST_LIGHT_STATUS = 82
SET_ROUGH = 109
SET_REALLY_ROUGH = 77
class ProcessConnectionThread(Runnable):
def __init__(self, connection, arduino):
self.connection = connection
self.arduino = arduino
self.buffer = []
self.count = 0;
self.first = False
def run(self):
try:
input_stream = self.connection.openInputStream()
self.output_stream = self.connection.openOutputStream()
print "waiting for input"
while True:
command = input_stream.read()
if command == EXIT_CMD:
print "finish process"
break
self.process_command(command)
except Exception:
print "Bluetooth Device is not available"
def process_command(self, command):
if command == START and self.count <= 0:
self.buffer = []
self.first = True
elif command == END and self.count <= 0:
print self.buffer
buffer = ByteBuffer.allocate(len(self.buffer))
for b in self.buffer:
buffer.put(b)
buffer.flip()
cmd = buffer.get()
if cmd == REQUEST_LIGHT_STATUS:
self.send_light_status()
pass
elif cmd == SET_LIGHT_STATUS:
time = buffer.getLong()
status = buffer.get()
print status
self.arduino.switch_arduino_lights(status, time)
elif cmd == SET_ROUGH:
time = buffer.getLong()
self.arduino.send_rough_data(time)
elif cmd == SET_REALLY_ROUGH:
time = buffer.getLong()
self.arduino.send_really_rough_data(time)
else:
self.buffer.append(command)
if self.first:
if command == SET_LIGHT_STATUS:
self.count = 10
elif command == REQUEST_LIGHT_STATUS:
self.count = 1
elif command == SET_ROUGH:
self.count = 9
elif command == SET_REALLY_ROUGH:
self.count = 9
self.first = False
self.count -= 1
def send_light_status(self):
buffer = ByteBuffer.allocate(12)
buffer.put(START)
buffer.put(SET_LIGHT_STATUS)
buffer.putLong(self.arduino.light_time)
buffer.put(1 if self.arduino.light_status else 0)
buffer.put(END)
buf = buffer.array();
self.output_stream.write(buf)