Skip to content

Commit

Permalink
try to fix latency for cyton on macos (#551)
Browse files Browse the repository at this point in the history
* try to fix latency for cyton on macos

Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
  • Loading branch information
Andrey1994 committed Dec 4, 2022
1 parent cfd95e7 commit fc8c289
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cppcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
env:
DEBIAN_FRONTEND: noninteractive
- name: Run Cppcheck
run: cppcheck --std=c++11 -ithird_party/spdlog -ithird_party/libsvm -ithird_party/SimpleBLE -ithird_party/fmt -ithird_party/SimpleDBus -ithird_party/SimpleBluez --error-exitcode=1 --xml --xml-version=2 --force src cpp_package third_party 2>cppcheck_res.xml
run: cppcheck --std=c++11 -ithird_party/spdlog -ithird_party/libsvm -ithird_party/SimpleBLE -ithird_party/fmt -ithird_party/SimpleDBus -ithird_party/SimpleBluez -isrc/utils/os_serial_ioctl.cpp --error-exitcode=1 --xml --xml-version=2 --force src cpp_package third_party 2>cppcheck_res.xml
- name: Generate Report
if: ${{ failure() }}
run: cppcheck-htmlreport --title=BrainFlow --file=cppcheck_res.xml --report-dir=report
Expand Down
4 changes: 4 additions & 0 deletions src/board_controller/openbci/openbci_serial_board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ int OpenBCISerialBoard::set_port_settings ()
return (int)BrainFlowExitCodes::SET_PORT_ERROR;
}
safe_logger (spdlog::level::trace, "set port settings");
#ifdef __APPLE__
int set_latency_res = serial->set_custom_latency (1);
safe_logger (spdlog::level::info, "set_latency_res is: {}", set_latency_res);
#endif
return send_to_board ("v");
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/inc/libftdi_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class LibFTDISerial : public Serial
bool is_port_open ();
int set_serial_port_settings (int ms_timeout = 1000, bool timeout_only = false);
int set_custom_baudrate (int baudrate);
int set_custom_latency (int latency = 1);
int flush_buffer ();
int read_from_serial_port (void *bytes_to_read, int size);
int send_to_serial_port (const void *message, int length);
Expand Down
1 change: 1 addition & 0 deletions src/utils/inc/os_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class OSSerial : public Serial
bool is_port_open ();
int set_serial_port_settings (int ms_timeout = 1000, bool timeout_only = false);
int set_custom_baudrate (int baudrate);
int set_custom_latency (int latency = 1);
int flush_buffer ();
int read_from_serial_port (void *bytes_to_read, int size);
int send_to_serial_port (const void *message, int length);
Expand Down
1 change: 1 addition & 0 deletions src/utils/inc/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Serial
// timeout_only indicates to configure only the timeout, not the baud and line properties
virtual int set_serial_port_settings (int ms_timeout = 1000, bool timeout_only = false) = 0;
virtual int set_custom_baudrate (int baudrate) = 0;
virtual int set_custom_latency (int latency = 1) = 0;
virtual int flush_buffer () = 0;
virtual int read_from_serial_port (void *bytes_to_read, int size) = 0;
virtual int send_to_serial_port (const void *message, int length) = 0;
Expand Down
11 changes: 11 additions & 0 deletions src/utils/libftdi_serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ int LibFTDISerial::set_custom_baudrate (int baudrate)
}
}

int LibFTDISerial::set_custom_latency (int latency)
{
log_error ("libftdi", "set_custom_latency is not supported for libftdi serial");
return (int)SerialExitCodes::SET_PORT_STATE_ERROR;
}

int LibFTDISerial::flush_buffer ()
{
#if FTDI_MAJOR_VERSION >= 2 || (FTDI_MAJOR_VERSION == 1 && FTDI_MINOR_VERSIOM >= 5)
Expand Down Expand Up @@ -317,6 +323,11 @@ int LibFTDISerial::set_custom_baudrate (int baudrate)
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
}

int LibFTDISerial::set_custom_latency (int latency)
{
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
}

int LibFTDISerial::flush_buffer ()
{
return (int)SerialExitCodes::NO_LIBFTDI_ERROR;
Expand Down
42 changes: 40 additions & 2 deletions src/utils/os_serial_ioctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
#include <termios.h>
#include <unistd.h>

#if defined __has_include
#if __has_include(<sys/ioctl.h>) && __has_include(<IOKit/serial/ioss.h>)
#include <sys/ioctl.h>

#include <IOKit/serial/ioss.h>
#else
#define NO_IOCTL_HEADERS
#endif
#else
#define NO_IOCTL_HEADERS
#endif

#endif


Expand All @@ -51,7 +63,20 @@ int OSSerial::set_custom_baudrate (int baudrate)
return SerialExitCodes::OK;
}

int OSSerial::set_custom_latency (int latency)
{
// there is a method to set latency in registry but it restarts the device, keep it separate and
// dont add here
return SerialExitCodes::SET_PORT_STATE_ERROR;
}

#elif defined(__linux__) && !defined(__ANDROID__)

int OSSerial::set_custom_latency (int latency)
{
return SerialExitCodes::SET_PORT_STATE_ERROR;
}

#ifdef NO_IOCTL_HEADERS
int OSSerial::set_custom_baudrate (int baudrate)
{
Expand Down Expand Up @@ -80,8 +105,17 @@ int OSSerial::set_custom_baudrate (int baudrate)
}
}
#endif

#elif defined(__APPLE__)
int OSSerial::set_custom_latency (int latency)
{
#ifndef NO_IOCTL_HEADERS
unsigned long mics = latency;
ioctl (this->port_descriptor, IOSSDATALAT, &mics);
return SerialExitCodes::OK;
#else
return SerialExitCodes::NO_SYSTEM_HEADERS_FOUND_ERROR;
#endif
}
int OSSerial::set_custom_baudrate (int baudrate)
{
struct termios port_settings;
Expand All @@ -96,10 +130,14 @@ int OSSerial::set_custom_baudrate (int baudrate)
tcflush (this->port_descriptor, TCIOFLUSH);
return SerialExitCodes::OK;
}

#else
int OSSerial::set_custom_baudrate (int baudrate)
{
return SerialExitCodes::SET_PORT_STATE_ERROR;
}

int OSSerial::set_custom_latency (int latency)
{
return SerialExitCodes::SET_PORT_STATE_ERROR;
}
#endif

0 comments on commit fc8c289

Please sign in to comment.