From 30fc2729a9301e8c0450d03dde706cc426264aca Mon Sep 17 00:00:00 2001 From: Soltan Masood Malekghassemi Date: Sat, 10 Aug 2013 22:13:41 -0400 Subject: [PATCH 1/3] Added a non-blocking read to the kit. Removed extraneous cmake file. Added flag for position independent code so that compiles would work for linking libraries on Linux. --- .gitignore | 3 +- CMakeLists.txt | 3 +- cmake_modules/Findlibusb-1.0.cmake | 72 ------------------------------ include/emokit/emokit.h | 9 ++++ src/emokit.c | 15 ++++++- 5 files changed, 27 insertions(+), 75 deletions(-) delete mode 100644 cmake_modules/Findlibusb-1.0.cmake diff --git a/.gitignore b/.gitignore index 745ac35..957a6f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *build* -*.pyc \ No newline at end of file +*.pyc +.*.swp diff --git a/CMakeLists.txt b/CMakeLists.txt index f320e74..018b882 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,8 @@ -cmake_minimum_required (VERSION 2.6) +cmake_minimum_required (VERSION 2.8.9) PROJECT(emokit) SET(BUILD_SHARED_LIBS false) +SET_TARGET_PROPERTIES(emokit PROPERTIES POSITION_INDEPENDENT_CODE TRUE) LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake_modules) SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) diff --git a/cmake_modules/Findlibusb-1.0.cmake b/cmake_modules/Findlibusb-1.0.cmake deleted file mode 100644 index 82a7c57..0000000 --- a/cmake_modules/Findlibusb-1.0.cmake +++ /dev/null @@ -1,72 +0,0 @@ -# - Try to find libusb-1.0 -# Once done this will define -# -# LIBUSB_1_FOUND - system has libusb -# LIBUSB_1_INCLUDE_DIRS - the libusb include directory -# LIBUSB_1_LIBRARIES - Link these to use libusb -# LIBUSB_1_DEFINITIONS - Compiler switches required for using libusb -# -# Adapted from cmake-modules Google Code project -# -# Copyright (c) 2006 Andreas Schneider -# -# (Changes for libusb) Copyright (c) 2008 Kyle Machulis -# -# Redistribution and use is allowed according to the terms of the New BSD license. -# For details see the accompanying COPYING-CMAKE-SCRIPTS file. -# - - -if (LIBUSB_1_LIBRARIES AND LIBUSB_1_INCLUDE_DIRS) - # in cache already - set(LIBUSB_FOUND TRUE) -else (LIBUSB_1_LIBRARIES AND LIBUSB_1_INCLUDE_DIRS) - find_path(LIBUSB_1_INCLUDE_DIR - NAMES - libusb.h - PATHS - /usr/include - /usr/local/include - /opt/local/include - /sw/include - PATH_SUFFIXES - libusb-1.0 - ) - - find_library(LIBUSB_1_LIBRARY - NAMES - usb-1.0 - PATHS - /usr/lib - /usr/local/lib - /opt/local/lib - /sw/lib - ) - - set(LIBUSB_1_INCLUDE_DIRS - ${LIBUSB_1_INCLUDE_DIR} - ) - set(LIBUSB_1_LIBRARIES - ${LIBUSB_1_LIBRARY} -) - - if (LIBUSB_1_INCLUDE_DIRS AND LIBUSB_1_LIBRARIES) - set(LIBUSB_1_FOUND TRUE) - endif (LIBUSB_1_INCLUDE_DIRS AND LIBUSB_1_LIBRARIES) - - if (LIBUSB_1_FOUND) - if (NOT libusb_1_FIND_QUIETLY) - message(STATUS "Found libusb-1.0:") - message(STATUS " - Includes: ${LIBUSB_1_INCLUDE_DIRS}") - message(STATUS " - Libraries: ${LIBUSB_1_LIBRARIES}") - endif (NOT libusb_1_FIND_QUIETLY) - else (LIBUSB_1_FOUND) - if (libusb_1_FIND_REQUIRED) - message(FATAL_ERROR "Could not find libusb") - endif (libusb_1_FIND_REQUIRED) - endif (LIBUSB_1_FOUND) - - # show the LIBUSB_1_INCLUDE_DIRS and LIBUSB_1_LIBRARIES variables only in the advanced view - mark_as_advanced(LIBUSB_1_INCLUDE_DIRS LIBUSB_1_LIBRARIES) - -endif (LIBUSB_1_LIBRARIES AND LIBUSB_1_INCLUDE_DIRS) \ No newline at end of file diff --git a/include/emokit/emokit.h b/include/emokit/emokit.h index f166879..783459d 100644 --- a/include/emokit/emokit.h +++ b/include/emokit/emokit.h @@ -130,6 +130,15 @@ extern "C" * @return 0 if successful, < 0 for error */ EMOKIT_DECLSPEC int emokit_read_data(struct emokit_device* dev); + + /** + * Poll the device. Like emokit_read_data, but non-blocking. + * + * @param dev Opened device structure + * + * @return 0 if nothing read, 1 if report was read, < 0 for error + */ + EMOKIT_DECLSPEC int emokit_poll_data(struct emokit_device* dev); EMOKIT_DECLSPEC struct emokit_frame emokit_get_next_frame(struct emokit_device* dev); diff --git a/src/emokit.c b/src/emokit.c index 5bc3cde..9a64e4b 100644 --- a/src/emokit.c +++ b/src/emokit.c @@ -178,7 +178,20 @@ int emokit_close(struct emokit_device* s) int emokit_read_data(struct emokit_device* s) { - return hid_read(s->_dev, s->raw_frame, 32); + int res = hid_set_nonblocking(s->_dev, 0); + if(res < 0) + return res; + else + return hid_read(s->_dev, s->raw_frame, 32); +} + +int emokit_poll_data(struct emokit_device* s) +{ + int res = hid_set_nonblocking(s->_dev, 1); + if(res < 0) + return res; + else + return hid_read(s->_dev, s->raw_frame, 32); } EMOKIT_DECLSPEC From 967e12b56ae422b8515aac3c8772575b732a4f0b Mon Sep 17 00:00:00 2001 From: Soltan Masood Malekghassemi Date: Sun, 11 Aug 2013 21:05:29 -0400 Subject: [PATCH 2/3] Fixed doc comment on emokit_poll_data --- include/emokit/emokit.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/emokit/emokit.h b/include/emokit/emokit.h index 783459d..c569a64 100644 --- a/include/emokit/emokit.h +++ b/include/emokit/emokit.h @@ -136,7 +136,7 @@ extern "C" * * @param dev Opened device structure * - * @return 0 if nothing read, 1 if report was read, < 0 for error + * @return 0 if nothing read, >=1 if report was read, < 0 for error */ EMOKIT_DECLSPEC int emokit_poll_data(struct emokit_device* dev); From 8047c2af1cf9514154ffb9f9b708ee12d0ec93a5 Mon Sep 17 00:00:00 2001 From: Soltan Masood Malekghassemi Date: Fri, 13 Dec 2013 22:28:06 -0500 Subject: [PATCH 3/3] added some extra functionality (nonblocking dequeue) --- python/emokit/emotiv.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/python/emokit/emotiv.py b/python/emokit/emotiv.py index bd7a053..a2ad7c9 100644 --- a/python/emokit/emotiv.py +++ b/python/emokit/emotiv.py @@ -359,9 +359,9 @@ def updateStdout(self): os.system('cls') else: os.system('clear') - print "Packets Received: %s Packets Processed: %s" % (self.packetsReceived, self.packetsProcessed) + print("Packets Received: %s Packets Processed: %s" % (self.packetsReceived, self.packetsProcessed)) print('\n'.join("%s Reading: %s Strength: %s" % (k[1], self.sensors[k[1]]['value'],self.sensors[k[1]]['quality']) for k in enumerate(self.sensors))) - print "Battery: %i" % g_battery + print("Battery: %i" % g_battery) gevent.sleep(1) def getLinuxSetup(self): @@ -388,7 +388,7 @@ def getLinuxSetup(self): with open(input[0] + "/serial", 'r') as f: serial = f.readline().strip() f.close() - print "Serial: " + serial + " Device: " + input[1] + print("Serial: " + serial + " Device: " + input[1]) # Great we found it. But we need to use the second one... hidraw = input[1] id_hidraw = int(hidraw[-1]) @@ -398,7 +398,7 @@ def getLinuxSetup(self): print "Serial: " + serial + " Device: " + hidraw + " (Active)" return [serial, hidraw, ] except IOError as e: - print "Couldn't open file: %s" % e + print("Couldn't open file: %s" % e) def setupWin(self): devices = [] @@ -508,7 +508,7 @@ def setupCrypto(self, sn): key = ''.join(k) iv = Random.new().read(AES.block_size) cipher = AES.new(key, AES.MODE_ECB, iv) - for i in k: print "0x%.02x " % (ord(i)) + for i in k: print("0x%.02x " % (ord(i))) while self._goOn: while not tasks.empty(): task = tasks.get() @@ -522,8 +522,14 @@ def setupCrypto(self, sn): def dequeue(self): try: return self.packets.get() - except Exception, e: - print e + except Exception as e: + print(e) + + def dequeue_nowait(self): + try: + return self.packets.get_nowait() + except Exception as e: + return None def close(self): if windows: