-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialcomm.py
166 lines (145 loc) · 4.48 KB
/
serialcomm.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#from __future__ import print_function
import sys
#import re
from time import sleep, time
from struct import pack
from serial import Serial
#import os
import math
import threading
import datetime
#from calendar import monthrange
command = -1
i = -1
time = datetime.datetime.now()
time_init = datetime.datetime.now()
flag = 0
def usr_input(port):
global command
global time
global flag
global time_init
try:
while(command !=0):
packed_data = ""
command = int(input("Enter command:\n\t0:quit\n\t1:start\n\t2:quick start\n\t3:wake\n\t4:quick wake\n\t5:stop"))
if (command == 0):
break;
elif (command == 1):
NS = input("Enter number of samples: ")
SD = input("Enter sampling delay: ")
PD = input("Enter pulse delay: ")
raw_time = input("Enter time to wake up (XX:XX:XX): ")
flag = 1
user_time = raw_time.split(":")
user_time[0] = int(user_time[0])
user_time[1] = int(user_time[1])
user_time[2] = int(user_time[2])
year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day
time_init = datetime.datetime(year, month, day, user_time[0], user_time[1], user_time[2], 0, None)
threshold_sec = user_time[2] - 30
if (threshold_sec < 0):
threshold_min = user_time[1] - 1
if (threshold_min < 0):
threshold_hour = user_time[0] - 1
if (threshold_hour < 0):
user_time[0] = 23
user_time[1] = 60 + threshold_min
user_time[2] = 60 + threshold_sec
else:
user_time[0] = threshold_hour
user_time[1] = 60 + threshold_min
user_time[2] = 60 + threshold_sec
else:
user_time[1] = threshold_min
user_time[2] = 60 + threshold_sec
else:
user_time[2] = threshold_sec
time = datetime.datetime(year, month, day, user_time[0], user_time[1], user_time[2], 0, None)
packed_data = pack("=BHHH", 1, NS, SD, PD)
elif (command == 2):
raw_time = input("Enter time to wake up (XX:XX:XX): ")
flag = 1
user_time = raw_time.split(":")
user_time[0] = int(user_time[0])
user_time[1] = int(user_time[1])
user_time[2] = int(user_time[2])
year = datetime.datetime.now().year
month = datetime.datetime.now().month
day = datetime.datetime.now().day
time_init = datetime.datetime(year, month, day, user_time[0], user_time[1], user_time[2], 0, None)
threshold_sec = user_time[2] - 30
if (threshold_sec < 0):
threshold_min = user_time[1] - 1
if (threshold_min < 0):
threshold_hour = user_time[0] - 1
if (threshold_hour < 0):
user_time[0] = 23
user_time[1] = 60 + threshold_min
user_time[2] = 60 + threshold_sec
else:
user_time[0] = threshold_hour
user_time[1] = 60 + threshold_min
user_time[2] = 60 + threshold_sec
else:
user_time[1] = threshold_min
user_time[2] = 60 + threshold_sec
else:
user_time[2] = threshold_sec
time = datetime.datetime(year, month, day, user_time[0], user_time[1], user_time[2], 0, None)
packed_data = pack("=B", 4)
elif (command == 3):
brightness = int(input("Enter brightness: "))
ramp_up = int(input("Enter ramp up: "))
packed_data = pack("=BBH", 2, brightness, ramp_up)
elif (command == 4):
packed_data = pack("=B", 5)
elif (command == 5):
packed_data = pack("=B", 3)
else:
print("Unauthorized command")
port.write(packed_data)
except Exception as e:
print("Failed\n" + str(e))
command = 0
def usb_read(port):
global command
global time
global flag
global time_init
packed_data = pack("=BBH", 2, 255, 5000)
while(command != 0):
line = port.read(1)
if (line != "" and command != 0 and len(line)!=0):
print(line, end='')
sleep_mode = ord(line)
if (sleep_mode == 1):
print("USER IS IN LIGHT SLEEP\n")
if (time < datetime.datetime.now()):
port.write(packed_data)
print("Waking user now\n")
flag = 0
if (flag == 1 and time_init <= datetime.datetime.now()):
flag = 0
port.write(packed_data)
print("Waking user now\n")
def connect():
try:
conn = Serial('/dev/ttyUSB0', dsrdtr=0, rtscts=0, timeout=1) #cu.usbmodemFA131
except IOError:
print("Error opening serial port.", file=sys.stderr)
sys.exit(2)
return conn
def main():
print("Connecting...")
conn = connect()
print("Connected!")
print("Running code...")
t1 = threading.Thread(target=usb_read, args=(conn,))
t2 = threading.Thread(target=usr_input, args=(conn,))
t1.start()
t2.start()
if __name__ == "__main__":
main()