Skip to content

Commit

Permalink
updated src for Linux AMD64 v1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Tang committed Aug 26, 2019
1 parent baddeb3 commit b20da2d
Show file tree
Hide file tree
Showing 3 changed files with 370 additions and 190 deletions.
86 changes: 76 additions & 10 deletions src/python/linux_amd64/rehamove.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,76 @@
import rehamovelib

class Rehamove:

current_version = "v1.5"

channel0 = ['r', 'red']
channel1 = ['b', 'blue']
channel2 = ['g1', 'gray1', 'grey1', 'black']
channel3 = ['g2', 'gray2', 'grey2', 'white']

def __init__(self, port_name):
self.rehamove = rehamovelib.open_port(port_name)

def version(self):
c_version = rehamovelib.get_version()
print("Rehamove Version: Python-side " + str(Rehamove.current_version) + ", C-side " + str(c_version))
return Rehamove.current_version

def get_channel(self, channel):
chosen_channel = channel
if isinstance(channel, str):
channel = channel.lower()
if channel in Rehamove.channel0:
chosen_channel = 0
elif channel in Rehamove.channel1:
chosen_channel = 1
elif channel in Rehamove.channel2:
chosen_channel = 2
elif channel in Rehamove.channel3:
chosen_channel = 3
else:
chosen_channel = 0 # Default
elif isinstance(channel, int):
if channel < 0 and channel > 3:
chosen_channel = 0 # Default
else:
chosen_channel = 0
return chosen_channel

def pulse(self, channel, current, pulse_width):
rehamovelib.pulse(self.rehamove, channel, current, pulse_width)
if self.rehamove == None:
print("python Rehamove pulse() ERROR! Rehamove object does not exist.")
return -1
chosen_channel = self.get_channel(channel)
result = rehamovelib.pulse(self.rehamove, chosen_channel, current, pulse_width)
if result != 0:
print("python Rehamove pulse() ERROR!")
return -1
else:
print("python Rehamove pulse() sent.")
return 0

def custom_pulse(self, channel, points_array):
if self.rehamove == None:
print("python Rehamove custom_pulse() ERROR! Rehamove object does not exist.")
return -1
chosen_channel = self.get_channel(channel)
original_length = len(points_array)
num_points = len(points_array)
# Error handling (warning) if too many points.
if num_points > 16:
print("custom_pulse(): WARNING! Maximum of 16 points allowed, truncating points array.")
print("python Rehamove custom_pulse() WARNING! Maximum of 16 points allowed, truncating points array.")
num_points = 16

# Error handling (exception) if malformed points.
try:
for i in range(0, num_points):
current = points_array[i][0]
pulse_width = points_array[i][1]
#print("Point {}: {} mA {} us".format(i, current, pulse_width))
except:
print("custom_pulse: ERROR! Malformed points array, should be: [ (current0, pulse_width0), (current1, pulse_width1), ... ]")
return
print("python Rehamove custom_pulse() ERROR! Malformed points array, should be: [ (current0, pulse_width0), (current1, pulse_width1), ... ]")
return -1

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

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)
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)
if result != 0:
print("python Rehamove custom_pulse() ERROR!")
return -1
else:
print("python Rehamove custom_pulse() sent.")
return 0

#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))

def battery(self):
rehamovelib.battery(self.rehamove)
if self.rehamove == None:
print("python Rehamove ERROR! Rehamove object does not exist.")
return -1
result = rehamovelib.battery_request(self.rehamove)
if result != 0:
print("python Rehamove battery() ERROR!")
return -1
else:
battery_level = rehamovelib.get_battery(self.rehamove)
print("python Rehamove battery(): " + str(battery_level) + "%")
return battery_level

def __del__(self):
rehamovelib.close_port(self.rehamove)
# Only close the port if we have a Rehamove object to close
if self.rehamove != None:
result = rehamovelib.close_port(self.rehamove)
if result != 0:
print("python Rehamove close_port() ERROR!")

Loading

0 comments on commit b20da2d

Please sign in to comment.