1
1
import rehamovelib
2
2
3
3
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
+
4
12
def __init__ (self , port_name ):
5
13
self .rehamove = rehamovelib .open_port (port_name )
6
14
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
+
7
41
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
9
53
10
54
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 )
11
59
original_length = len (points_array )
12
60
num_points = len (points_array )
13
61
# Error handling (warning) if too many points.
14
62
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." )
16
64
num_points = 16
17
65
18
66
# Error handling (exception) if malformed points.
19
67
try :
20
68
for i in range (0 , num_points ):
21
69
current = points_array [i ][0 ]
22
70
pulse_width = points_array [i ][1 ]
23
- #print("Point {}: {} mA {} us".format(i, current, pulse_width))
24
71
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
27
74
28
75
# Handle if the user supplies less than 16 points: fill up empty points in the array.
29
76
remaining_points = 16 - num_points
@@ -48,12 +95,31 @@ def custom_pulse(self, channel, points_array):
48
95
c14 , w14 = points_array [14 ][0 ], points_array [14 ][1 ]
49
96
c15 , w15 = points_array [15 ][0 ], points_array [15 ][1 ]
50
97
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
52
105
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
-
55
106
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
57
118
58
119
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