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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
|
| 3 |
+
##########################################################################
|
| 4 |
+
# MIT License
|
| 5 |
+
#
|
| 6 |
+
# Copyright (c) 2013-2017 Sensel, Inc.
|
| 7 |
+
#
|
| 8 |
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
| 9 |
+
# software and associated documentation files (the "Software"), to deal in the Software
|
| 10 |
+
# without restriction, including without limitation the rights to use, copy, modify, merge,
|
| 11 |
+
# publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
| 12 |
+
# to whom the Software is furnished to do so, subject to the following conditions:
|
| 13 |
+
#
|
| 14 |
+
# The above copyright notice and this permission notice shall be included in all copies or
|
| 15 |
+
# substantial portions of the Software.
|
| 16 |
+
#
|
| 17 |
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
| 18 |
+
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
| 19 |
+
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
| 20 |
+
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
| 21 |
+
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
| 22 |
+
# DEALINGS IN THE SOFTWARE.
|
| 23 |
+
##########################################################################
|
| 24 |
+
|
| 25 |
+
import sys
|
| 26 |
+
|
| 27 |
+
from ctypes import *
|
| 28 |
+
from .sensel_register_map import *
|
| 29 |
+
import platform
|
| 30 |
+
|
| 31 |
+
sensel_lib = None
|
| 32 |
+
sensel_lib_decompress = None
|
| 33 |
+
|
| 34 |
+
SENSEL_MAX_DEVICES = 16
|
| 35 |
+
|
| 36 |
+
FRAME_CONTENT_PRESSURE_MASK = 0x01
|
| 37 |
+
FRAME_CONTENT_LABELS_MASK = 0x02
|
| 38 |
+
FRAME_CONTENT_CONTACTS_MASK = 0x04
|
| 39 |
+
FRAME_CONTENT_ACCEL_MASK = 0x08
|
| 40 |
+
|
| 41 |
+
CONTACT_MASK_ELLIPSE = 0x01
|
| 42 |
+
CONTACT_MASK_DELTAS = 0x02
|
| 43 |
+
CONTACT_MASK_BOUNDING_BOX = 0x04
|
| 44 |
+
CONTACT_MASK_PEAK = 0x08
|
| 45 |
+
|
| 46 |
+
CONTACT_INVALID = 0
|
| 47 |
+
CONTACT_START = 1
|
| 48 |
+
CONTACT_MOVE = 2
|
| 49 |
+
CONTACT_END = 3
|
| 50 |
+
|
| 51 |
+
platform_name = platform.system()
|
| 52 |
+
if platform_name == "Windows":
|
| 53 |
+
python_is_x64 = sys.maxsize > 2**32
|
| 54 |
+
if python_is_x64:
|
| 55 |
+
sensel_lib_decompress = windll.LoadLibrary("C:\\Program Files\\Sensel\\SenselLib\\x64\\LibSenselDecompress.dll")
|
| 56 |
+
sensel_lib = windll.LoadLibrary("C:\\Program Files\\Sensel\\SenselLib\\x64\\LibSensel.dll")
|
| 57 |
+
else:
|
| 58 |
+
sensel_lib_decompress = windll.LoadLibrary("C:\\Program Files\\Sensel\\SenselLib\\x86\\LibSenselDecompress.dll")
|
| 59 |
+
sensel_lib = windll.LoadLibrary("C:\\Program Files\\Sensel\\SenselLib\\x86\\LibSensel.dll")
|
| 60 |
+
elif platform_name == "Darwin":
|
| 61 |
+
sensel_lib = cdll.LoadLibrary("/usr/local/lib/libSensel.dylib")
|
| 62 |
+
else:
|
| 63 |
+
sensel_lib = cdll.LoadLibrary("/usr/lib/libsensel.so")
|
| 64 |
+
|
| 65 |
+
class SenselSensorInfo(Structure):
|
| 66 |
+
_fields_ = [("max_contacts", c_ubyte),
|
| 67 |
+
("num_rows", c_ushort),
|
| 68 |
+
("num_cols", c_ushort),
|
| 69 |
+
("width", c_float),
|
| 70 |
+
("height", c_float)]
|
| 71 |
+
|
| 72 |
+
class SenselContact(Structure):
|
| 73 |
+
_fields_ = [("content_bit_mask", c_ubyte),
|
| 74 |
+
("id", c_ubyte),
|
| 75 |
+
("state", c_int),
|
| 76 |
+
("x_pos", c_float),
|
| 77 |
+
("y_pos", c_float),
|
| 78 |
+
("total_force", c_float),
|
| 79 |
+
("area", c_float),
|
| 80 |
+
("orientation", c_float),
|
| 81 |
+
("major_axis", c_float),
|
| 82 |
+
("minor_axis", c_float),
|
| 83 |
+
("delta_x", c_float),
|
| 84 |
+
("delta_y", c_float),
|
| 85 |
+
("delta_force", c_float),
|
| 86 |
+
("delta_area", c_float),
|
| 87 |
+
("min_x", c_float),
|
| 88 |
+
("min_y", c_float),
|
| 89 |
+
("max_x", c_float),
|
| 90 |
+
("max_y", c_float),
|
| 91 |
+
("peak_x", c_float),
|
| 92 |
+
("peak_y", c_float),
|
| 93 |
+
("peak_force", c_float)]
|
| 94 |
+
|
| 95 |
+
class SenselAccelData(Structure):
|
| 96 |
+
_fields_ = [("x", c_int),
|
| 97 |
+
("y", c_int),
|
| 98 |
+
("z", c_int)]
|
| 99 |
+
|
| 100 |
+
class SenselFrameData(Structure):
|
| 101 |
+
_fields_ = [("content_bit_mask", c_ubyte),
|
| 102 |
+
("lost_frame_count", c_int),
|
| 103 |
+
("n_contacts", c_ubyte),
|
| 104 |
+
("contacts", POINTER(SenselContact)),
|
| 105 |
+
("force_array", POINTER(c_float)),
|
| 106 |
+
("labels_array", POINTER(c_ubyte)),
|
| 107 |
+
("accel_data", POINTER(SenselAccelData))]
|
| 108 |
+
|
| 109 |
+
class SenselDeviceID(Structure):
|
| 110 |
+
_fields_ = [("idx", c_ubyte),
|
| 111 |
+
("serial_num", c_ubyte*64),
|
| 112 |
+
("com_port", c_ubyte*64)]
|
| 113 |
+
|
| 114 |
+
class SenselDeviceList(Structure):
|
| 115 |
+
_fields_ = [("num_devices", c_ubyte),
|
| 116 |
+
("devices", SenselDeviceID*SENSEL_MAX_DEVICES)]
|
| 117 |
+
|
| 118 |
+
def open():
|
| 119 |
+
handle = c_void_p(0)
|
| 120 |
+
error = sensel_lib.senselOpen(POINTER(handle))
|
| 121 |
+
return (error, handle)
|
| 122 |
+
|
| 123 |
+
def getDeviceList():
|
| 124 |
+
device_list = SenselDeviceList(0)
|
| 125 |
+
for i in range(SENSEL_MAX_DEVICES):
|
| 126 |
+
device_list.devices[i] = SenselDeviceID(0)
|
| 127 |
+
error = sensel_lib.senselGetDeviceList(byref(device_list))
|
| 128 |
+
return (error, device_list)
|
| 129 |
+
|
| 130 |
+
def openDeviceByID(idx):
|
| 131 |
+
c_idx = c_ubyte(idx)
|
| 132 |
+
handle = c_void_p(0)
|
| 133 |
+
error = sensel_lib.senselOpenDeviceByID(byref(handle), c_idx)
|
| 134 |
+
return (error, handle)
|
| 135 |
+
|
| 136 |
+
def close(handle):
|
| 137 |
+
error = sensel_lib.senselClose(handle)
|
| 138 |
+
return error
|
| 139 |
+
|
| 140 |
+
def softReset(handle):
|
| 141 |
+
error = sensel_lib.senselSoftReset(handle)
|
| 142 |
+
return error
|
| 143 |
+
|
| 144 |
+
def getSensorInfo(handle):
|
| 145 |
+
info = SenselSensorInfo(0,0,0,0,0)
|
| 146 |
+
error = sensel_lib.senselGetSensorInfo(handle, byref(info))
|
| 147 |
+
return (error, info)
|
| 148 |
+
|
| 149 |
+
def allocateFrameData(handle):
|
| 150 |
+
frame_pointer = POINTER(SenselFrameData)()
|
| 151 |
+
error = sensel_lib.senselAllocateFrameData(handle, byref(frame_pointer))
|
| 152 |
+
return (error, frame_pointer.contents)
|
| 153 |
+
|
| 154 |
+
def freeFrameData(handle, frame):
|
| 155 |
+
error = sensel_lib.senselFreeFrameData(handle, byref(frame))
|
| 156 |
+
return error
|
| 157 |
+
|
| 158 |
+
def setScanDetail(handle, detail):
|
| 159 |
+
c_detail = c_int(detail)
|
| 160 |
+
error = sensel_lib.senselSetScanDetail(handle, c_detail)
|
| 161 |
+
return error
|
| 162 |
+
|
| 163 |
+
def getScanDetail(handle):
|
| 164 |
+
detail = c_int(0)
|
| 165 |
+
error = sensel_lib.senselGetScanDetail(handle, byref(detail))
|
| 166 |
+
return (error, detail.value)
|
| 167 |
+
|
| 168 |
+
def setMaxFrameRate(handle, frame_rate):
|
| 169 |
+
c_frame_rate = c_int(frame_rate)
|
| 170 |
+
error = sensel_lib.senselSetMaxFrameRate(handle, c_frame_rate)
|
| 171 |
+
return error
|
| 172 |
+
|
| 173 |
+
def getMaxFrameRate(handle):
|
| 174 |
+
frame_rate = c_int(0)
|
| 175 |
+
error = sensel_lib.senselGetMaxFrameRate(handle, byref(frame_rate))
|
| 176 |
+
return (error, frame_rate.value)
|
| 177 |
+
|
| 178 |
+
def getSupportedFrameContent(handle):
|
| 179 |
+
content = c_ubyte(0)
|
| 180 |
+
error = sensel_lib.senselGetSupportedFrameContent(handle, byref(content))
|
| 181 |
+
return (error, content.value)
|
| 182 |
+
|
| 183 |
+
def setFrameContent(handle, content):
|
| 184 |
+
c_content = c_ubyte(content)
|
| 185 |
+
error = sensel_lib.senselSetFrameContent(handle, c_content)
|
| 186 |
+
return error
|
| 187 |
+
|
| 188 |
+
def getFrameContent(handle):
|
| 189 |
+
content = c_ubyte(0)
|
| 190 |
+
error = sensel_lib.senselGetFrameContent(handle, byref(content))
|
| 191 |
+
return (error, content.value)
|
| 192 |
+
|
| 193 |
+
def startScanning(handle):
|
| 194 |
+
error = sensel_lib.senselStartScanning(handle)
|
| 195 |
+
return error
|
| 196 |
+
|
| 197 |
+
def stopScanning(handle):
|
| 198 |
+
error = sensel_lib.senselStopScanning(handle)
|
| 199 |
+
return error
|
| 200 |
+
|
| 201 |
+
def readSensor(handle):
|
| 202 |
+
error = sensel_lib.senselReadSensor(handle)
|
| 203 |
+
return error
|
| 204 |
+
|
| 205 |
+
def getNumAvailableFrames(handle):
|
| 206 |
+
num_frames = c_int(0)
|
| 207 |
+
error = sensel_lib.senselGetNumAvailableFrames(handle, byref(num_frames))
|
| 208 |
+
return (error, num_frames.value)
|
| 209 |
+
|
| 210 |
+
def getFrame(handle, frame):
|
| 211 |
+
error = sensel_lib.senselGetFrame(handle, byref(frame))
|
| 212 |
+
return error
|
| 213 |
+
|
| 214 |
+
def setLEDBrightness(handle, led_id, brightness):
|
| 215 |
+
c_led_id = c_ubyte(led_id)
|
| 216 |
+
c_brightness = c_ushort(brightness)
|
| 217 |
+
error = sensel_lib.senselSetLEDBrightness(handle, c_led_id, c_brightness)
|
| 218 |
+
return error;
|
| 219 |
+
|
| 220 |
+
def setContactsMask(handle, mask):
|
| 221 |
+
c_mask = c_ubyte(mask)
|
| 222 |
+
error = sensel_lib.senselSetContactsMask(handle, c_mask)
|
| 223 |
+
return error
|
| 224 |
+
|
| 225 |
+
def getFrameContent(handle):
|
| 226 |
+
mask = c_ubyte(0)
|
| 227 |
+
error = sensel_lib.senselGetContactsMask(handle, byref(mask))
|
| 228 |
+
return (error, content.value)
|
| 229 |
+
|
| 230 |
+
def readReg(handle, reg, size):
|
| 231 |
+
buf = (c_byte * size)()
|
| 232 |
+
error = sensel_lib.senselReadReg(handle, c_ubyte(reg), c_ubyte(size), buf)
|
| 233 |
+
return (error, buf)
|
| 234 |
+
|
| 235 |
+
def writeReg(handle, reg, size, data):
|
| 236 |
+
buf = (c_ubyte * size)(*data)
|
| 237 |
+
error = sensel_lib.senselWriteReg(handle, c_ubyte(reg), c_ubyte(size), buf)
|
| 238 |
+
return error
|
| 239 |
+
|
| 240 |
+
def readRegVS(handle, reg, size):
|
| 241 |
+
buf = (c_byte * size)()
|
| 242 |
+
read_size = c_int(0)
|
| 243 |
+
error = sensel_lib.senselReadRegVS(handle, c_ubyte(reg), c_ubyte(size), buf, byref(read_size))
|
| 244 |
+
return (error, buf, read_size)
|
| 245 |
+
|
| 246 |
+
def writeRegVS(handle, reg, size, data):
|
| 247 |
+
buf = (c_byte * size)(*data)
|
| 248 |
+
write_size = c_int(0)
|
| 249 |
+
error = sensel_lib.senselReadRegVS(handle, c_ubyte(reg), c_ubyte(size), buf, byref(write_size))
|
| 250 |
+
return (error, write_size)
|