Skip to content

Commit b20da2d

Browse files
author
Aaron Tang
committed
updated src for Linux AMD64 v1.5
1 parent baddeb3 commit b20da2d

File tree

3 files changed

+370
-190
lines changed

3 files changed

+370
-190
lines changed

src/python/linux_amd64/rehamove.py

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,76 @@
11
import rehamovelib
22

33
class Rehamove:
4+
5+
current_version = "v1.5"
6+
7+
channel0 = ['r', 'red']
8+
channel1 = ['b', 'blue']
9+
channel2 = ['g1', 'gray1', 'grey1', 'black']
10+
channel3 = ['g2', 'gray2', 'grey2', 'white']
11+
412
def __init__(self, port_name):
513
self.rehamove = rehamovelib.open_port(port_name)
614

15+
def version(self):
16+
c_version = rehamovelib.get_version()
17+
print("Rehamove Version: Python-side " + str(Rehamove.current_version) + ", C-side " + str(c_version))
18+
return Rehamove.current_version
19+
20+
def get_channel(self, channel):
21+
chosen_channel = channel
22+
if isinstance(channel, str):
23+
channel = channel.lower()
24+
if channel in Rehamove.channel0:
25+
chosen_channel = 0
26+
elif channel in Rehamove.channel1:
27+
chosen_channel = 1
28+
elif channel in Rehamove.channel2:
29+
chosen_channel = 2
30+
elif channel in Rehamove.channel3:
31+
chosen_channel = 3
32+
else:
33+
chosen_channel = 0 # Default
34+
elif isinstance(channel, int):
35+
if channel < 0 and channel > 3:
36+
chosen_channel = 0 # Default
37+
else:
38+
chosen_channel = 0
39+
return chosen_channel
40+
741
def pulse(self, channel, current, pulse_width):
8-
rehamovelib.pulse(self.rehamove, channel, current, pulse_width)
42+
if self.rehamove == None:
43+
print("python Rehamove pulse() ERROR! Rehamove object does not exist.")
44+
return -1
45+
chosen_channel = self.get_channel(channel)
46+
result = rehamovelib.pulse(self.rehamove, chosen_channel, current, pulse_width)
47+
if result != 0:
48+
print("python Rehamove pulse() ERROR!")
49+
return -1
50+
else:
51+
print("python Rehamove pulse() sent.")
52+
return 0
953

1054
def custom_pulse(self, channel, points_array):
55+
if self.rehamove == None:
56+
print("python Rehamove custom_pulse() ERROR! Rehamove object does not exist.")
57+
return -1
58+
chosen_channel = self.get_channel(channel)
1159
original_length = len(points_array)
1260
num_points = len(points_array)
1361
# Error handling (warning) if too many points.
1462
if num_points > 16:
15-
print("custom_pulse(): WARNING! Maximum of 16 points allowed, truncating points array.")
63+
print("python Rehamove custom_pulse() WARNING! Maximum of 16 points allowed, truncating points array.")
1664
num_points = 16
1765

1866
# Error handling (exception) if malformed points.
1967
try:
2068
for i in range(0, num_points):
2169
current = points_array[i][0]
2270
pulse_width = points_array[i][1]
23-
#print("Point {}: {} mA {} us".format(i, current, pulse_width))
2471
except:
25-
print("custom_pulse: ERROR! Malformed points array, should be: [ (current0, pulse_width0), (current1, pulse_width1), ... ]")
26-
return
72+
print("python Rehamove custom_pulse() ERROR! Malformed points array, should be: [ (current0, pulse_width0), (current1, pulse_width1), ... ]")
73+
return -1
2774

2875
# Handle if the user supplies less than 16 points: fill up empty points in the array.
2976
remaining_points = 16 - num_points
@@ -48,12 +95,31 @@ def custom_pulse(self, channel, points_array):
4895
c14, w14 = points_array[14][0], points_array[14][1]
4996
c15, w15 = points_array[15][0], points_array[15][1]
5097

51-
rehamovelib.custom_pulse(self.rehamove, channel, original_length, c0, w0, c1, w1, c2, w2, c3, w3, c4, w4, c5, w5, c6, w6, c7, w7, c8, w8, c9, w9, c10, w10, c11, w11, c12, w12, c13, w13, c14, w14, c15, w15)
98+
result = rehamovelib.custom_pulse(self.rehamove, chosen_channel, original_length, c0, w0, c1, w1, c2, w2, c3, w3, c4, w4, c5, w5, c6, w6, c7, w7, c8, w8, c9, w9, c10, w10, c11, w11, c12, w12, c13, w13, c14, w14, c15, w15)
99+
if result != 0:
100+
print("python Rehamove custom_pulse() ERROR!")
101+
return -1
102+
else:
103+
print("python Rehamove custom_pulse() sent.")
104+
return 0
52105

53-
#print("PYTHON custom_pulse {} {}, {} {} {} {} {} {} {} {} / {} {} {} {} {} {} {} {} / {} {} {} {} {} {} {} {} / {} {} {} {} {} {} {} {}".format(self.rehamove, channel, c0, w0, c1, w1, c2, w2, c3, w3, c4, w4, c5, w5, c6, w6, c7, w7, c8, w8, c9, w9, c10, w10, c11, w11, c12, w12, c13, w13, c14, w14, c15, w15))
54-
55106
def battery(self):
56-
rehamovelib.battery(self.rehamove)
107+
if self.rehamove == None:
108+
print("python Rehamove ERROR! Rehamove object does not exist.")
109+
return -1
110+
result = rehamovelib.battery_request(self.rehamove)
111+
if result != 0:
112+
print("python Rehamove battery() ERROR!")
113+
return -1
114+
else:
115+
battery_level = rehamovelib.get_battery(self.rehamove)
116+
print("python Rehamove battery(): " + str(battery_level) + "%")
117+
return battery_level
57118

58119
def __del__(self):
59-
rehamovelib.close_port(self.rehamove)
120+
# Only close the port if we have a Rehamove object to close
121+
if self.rehamove != None:
122+
result = rehamovelib.close_port(self.rehamove)
123+
if result != 0:
124+
print("python Rehamove close_port() ERROR!")
125+

0 commit comments

Comments
 (0)