Skip to content

Commit

Permalink
Fix Arduino Serial Defines
Browse files Browse the repository at this point in the history
  • Loading branch information
kwagyeman committed Sep 4, 2020
1 parent e00a554 commit 209c7e3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 48 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=OpenMV Arduino RPC
version=1.0.0
version=1.0.1
author=OpenMV
maintainer=OpenMV
sentence=OpenMV Arduino RPC Interface Library
Expand Down
62 changes: 33 additions & 29 deletions src/openmvrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
// followed by a UINT32 command, UINT32 payload length and a UINT16 CRC
// The back and forth protocol works like this:
// (all integers are in little-endian order)
//
//
// Send Command:
// Master --> Slave magic HEADER value, cmd, payload len, CRC
// Master <-- Slave magic HEADER ack + CRC (4 bytes total)
// Master --> Slave magic DATA value, n-byte payload, CRC
// Master <-- Slave magic DATA ack + UINT16 CRC (4 bytes total)
//
//
// Get Result:
// Master --> Slave magic RESULT value, CRC
// Master <-- Slave magic RESULT ack, length, CRC
Expand Down Expand Up @@ -307,23 +307,23 @@ bool rpc_master::__get_result(uint8_t **data, size_t *size, unsigned long timeou
}

bool rpc_master::call_no_copy_no_args(const __FlashStringHelper *name,
void **result_data, size_t *result_data_len,
void **result_data, size_t *result_data_len,
unsigned long send_timeout, unsigned long recv_timeout)
{
return __put_command(_hash(name), NULL, 0, send_timeout)
? __get_result((uint8_t **) result_data, result_data_len, recv_timeout) : false;
}

bool rpc_master::call_no_copy_no_args(const String &name,
void **result_data, size_t *result_data_len,
void **result_data, size_t *result_data_len,
unsigned long send_timeout, unsigned long recv_timeout)
{
return __put_command(_hash(name.c_str(), name.length()), NULL, 0, send_timeout)
? __get_result((uint8_t **) result_data, result_data_len, recv_timeout) : false;
}

bool rpc_master::call_no_copy_no_args(const char *name,
void **result_data, size_t *result_data_len,
void **result_data, size_t *result_data_len,
unsigned long send_timeout, unsigned long recv_timeout)
{
return __put_command(_hash(name), NULL, 0, send_timeout)
Expand All @@ -332,7 +332,7 @@ bool rpc_master::call_no_copy_no_args(const char *name,

bool rpc_master::call_no_copy(const __FlashStringHelper *name,
void *command_data, size_t command_data_len,
void **result_data, size_t *result_data_len,
void **result_data, size_t *result_data_len,
unsigned long send_timeout, unsigned long recv_timeout)
{
return __put_command(_hash(name), (uint8_t *) command_data, command_data_len, send_timeout)
Expand All @@ -341,7 +341,7 @@ bool rpc_master::call_no_copy(const __FlashStringHelper *name,

bool rpc_master::call_no_copy(const String &name,
void *command_data, size_t command_data_len,
void **result_data, size_t *result_data_len,
void **result_data, size_t *result_data_len,
unsigned long send_timeout, unsigned long recv_timeout)
{
return __put_command(_hash(name.c_str(), name.length()), (uint8_t *) command_data, command_data_len, send_timeout)
Expand All @@ -350,7 +350,7 @@ bool rpc_master::call_no_copy(const String &name,

bool rpc_master::call_no_copy(const char *name,
void *command_data, size_t command_data_len,
void **result_data, size_t *result_data_len,
void **result_data, size_t *result_data_len,
unsigned long send_timeout, unsigned long recv_timeout)
{
return __put_command(_hash(name), (uint8_t *) command_data, command_data_len, send_timeout)
Expand Down Expand Up @@ -474,7 +474,7 @@ bool rpc_slave::__get_command(uint32_t *command, uint8_t **data, size_t *size, u
_get_short_timeout = min(_get_short_timeout + 1, timeout);
}

return false;
return false;
}

bool rpc_slave::__put_result(uint8_t *data, size_t size, unsigned long timeout)
Expand Down Expand Up @@ -857,84 +857,88 @@ bool rpc_spi_master::put_bytes(uint8_t *data, size_t size, unsigned long timeout
return true;
}

#define RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(name) \
#define RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(name, port) \
void rpc_hardware_serial##name##_uart_master::_flush() \
{ \
for (int i = Serial##name.available(); i > 0; i--) Serial##name.read(); \
for (int i = port.available(); i > 0; i--) port.read(); \
} \
\
bool rpc_hardware_serial##name##_uart_master::get_bytes(uint8_t *buff, size_t size, unsigned long timeout) \
{ \
Serial##name.setTimeout(timeout + 1); \
bool ok = (Serial##name.readBytes((char *) buff, size) == size) && (!_same(buff, size)); \
port.setTimeout(timeout + 1); \
bool ok = (port.readBytes((char *) buff, size) == size) && (!_same(buff, size)); \
if (!ok) delay(_get_short_timeout); \
return ok; \
} \
\
bool rpc_hardware_serial##name##_uart_master::put_bytes(uint8_t *buff, size_t size, unsigned long timeout) \
{ \
(void) timeout; \
return Serial##name.write((char *) buff, size) == size; \
return port.write((char *) buff, size) == size; \
}

#ifdef SERIAL_PORT_HARDWARE
RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION()
RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(,SERIAL_PORT_HARDWARE)
#endif

#ifdef SERIAL_PORT_HARDWARE1
RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(1)
RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(1,SERIAL_PORT_HARDWARE1)
#endif

#ifdef SERIAL_PORT_HARDWARE2
RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(2)
RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(2,SERIAL_PORT_HARDWARE2)
#endif

#ifdef SERIAL_PORT_HARDWARE3
RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(3)
RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(3,SERIAL_PORT_HARDWARE3)
#endif

#ifdef SERIAL_PORT_USBVIRTUAL
RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(USB)
RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION(USB,SERIAL_PORT_USBVIRTUAL)
#endif

#define RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(name) \
#undef RPC_HARDWARE_SERIAL_UART_MASTER_IMPLEMENTATION

#define RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(name, port) \
void rpc_hardware_serial##name##_uart_slave::_flush() \
{ \
for (int i = Serial##name.available(); i > 0; i--) Serial##name.read(); \
for (int i = port.available(); i > 0; i--) port.read(); \
} \
\
bool rpc_hardware_serial##name##_uart_slave::get_bytes(uint8_t *buff, size_t size, unsigned long timeout) \
{ \
Serial##name.setTimeout(timeout + 1); \
return Serial##name.readBytes((char *) buff, size) == size; \
port.setTimeout(timeout + 1); \
return port.readBytes((char *) buff, size) == size; \
} \
\
bool rpc_hardware_serial##name##_uart_slave::put_bytes(uint8_t *buff, size_t size, unsigned long timeout) \
{ \
(void) timeout; \
return Serial##name.write((char *) buff, size) == size; \
return port.write((char *) buff, size) == size; \
}

#ifdef SERIAL_PORT_HARDWARE
RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION()
RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(,SERIAL_PORT_HARDWARE)
#endif

#ifdef SERIAL_PORT_HARDWARE1
RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(1)
RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(1,SERIAL_PORT_HARDWARE1)
#endif

#ifdef SERIAL_PORT_HARDWARE2
RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(2)
RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(2,SERIAL_PORT_HARDWARE2)
#endif

#ifdef SERIAL_PORT_HARDWARE3
RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(3)
RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(3,SERIAL_PORT_HARDWARE3)
#endif

#ifdef SERIAL_PORT_USBVIRTUAL
RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(USB)
RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION(USB,SERIAL_PORT_USBVIRTUAL)
#endif

#undef RPC_HARDWARE_SERIAL_UART_SLAVE_IMPLEMENTATION

#ifdef ARDUINO_ARCH_AVR
void rpc_software_serial_uart_master::_flush()
{
Expand Down
40 changes: 22 additions & 18 deletions src/openmvrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class rpc_spi_master : public rpc_master
rpc_spi_master(const rpc_spi_master &);
};

#define RPC_HARDWARE_SERIAL_UART_MASTER(name) \
#define RPC_HARDWARE_SERIAL_UART_MASTER(name, port) \
class rpc_hardware_serial##name##_uart_master : public rpc_master \
{ \
public: \
Expand All @@ -316,34 +316,36 @@ public: \
virtual void _flush() override; \
virtual bool get_bytes(uint8_t *buff, size_t size, unsigned long timeout) override; \
virtual bool put_bytes(uint8_t *data, size_t size, unsigned long timeout) override; \
virtual void begin() override { Serial##name.begin(__baudrate); } \
virtual void end() override { Serial##name.end(); } \
virtual void begin() override { port.begin(__baudrate); } \
virtual void end() override { port.end(); } \
private: \
unsigned long __baudrate; \
rpc_hardware_serial##name##_uart_master(const rpc_hardware_serial##name##_uart_master &); \
};

#ifdef SERIAL_PORT_HARDWARE
RPC_HARDWARE_SERIAL_UART_MASTER()
RPC_HARDWARE_SERIAL_UART_MASTER(,SERIAL_PORT_HARDWARE)
#endif

#ifdef SERIAL_PORT_HARDWARE1
RPC_HARDWARE_SERIAL_UART_MASTER(1)
RPC_HARDWARE_SERIAL_UART_MASTER(1,SERIAL_PORT_HARDWARE1)
#endif

#ifdef SERIAL_PORT_HARDWARE2
RPC_HARDWARE_SERIAL_UART_MASTER(2)
RPC_HARDWARE_SERIAL_UART_MASTER(2,SERIAL_PORT_HARDWARE2)
#endif

#ifdef SERIAL_PORT_HARDWARE3
RPC_HARDWARE_SERIAL_UART_MASTER(3)
RPC_HARDWARE_SERIAL_UART_MASTER(3,SERIAL_PORT_HARDWARE3)
#endif

#ifdef SERIAL_PORT_USBVIRTUAL
RPC_HARDWARE_SERIAL_UART_MASTER(USB)
RPC_HARDWARE_SERIAL_UART_MASTER(USB,SERIAL_PORT_USBVIRTUAL)
#endif

#define RPC_HARDWARE_SERIAL_UART_SLAVE(name) \
#undef RPC_HARDWARE_SERIAL_UART_MASTER

#define RPC_HARDWARE_SERIAL_UART_SLAVE(name, port) \
class rpc_hardware_serial##name##_uart_slave : public rpc_slave \
{ \
public: \
Expand All @@ -352,33 +354,35 @@ public: \
virtual void _flush() override; \
virtual bool get_bytes(uint8_t *buff, size_t size, unsigned long timeout) override; \
virtual bool put_bytes(uint8_t *data, size_t size, unsigned long timeout) override; \
virtual void begin() override { Serial##name.begin(__baudrate); } \
virtual void end() override { Serial##name.end(); } \
virtual void begin() override { port.begin(__baudrate); } \
virtual void end() override { port.end(); } \
private: \
unsigned long __baudrate; \
rpc_hardware_serial##name##_uart_slave(const rpc_hardware_serial##name##_uart_slave &); \
};

#ifdef SERIAL_PORT_HARDWARE
RPC_HARDWARE_SERIAL_UART_SLAVE()
RPC_HARDWARE_SERIAL_UART_SLAVE(,SERIAL_PORT_HARDWARE)
#endif

#ifdef SERIAL_PORT_HARDWARE1
RPC_HARDWARE_SERIAL_UART_SLAVE(1)
RPC_HARDWARE_SERIAL_UART_SLAVE(1,SERIAL_PORT_HARDWARE1)
#endif

#ifdef SERIAL_PORT_HARDWARE2
RPC_HARDWARE_SERIAL_UART_SLAVE(2)
RPC_HARDWARE_SERIAL_UART_SLAVE(2,SERIAL_PORT_HARDWARE2)
#endif

#ifdef SERIAL_PORT_HARDWARE3
RPC_HARDWARE_SERIAL_UART_SLAVE(3)
RPC_HARDWARE_SERIAL_UART_SLAVE(3,SERIAL_PORT_HARDWARE3)
#endif

#ifdef SERIAL_PORT_USBVIRTUAL
RPC_HARDWARE_SERIAL_UART_SLAVE(USB)
RPC_HARDWARE_SERIAL_UART_SLAVE(USB,SERIAL_PORT_USBVIRTUAL)
#endif

#undef RPC_HARDWARE_SERIAL_UART_SLAVE

#ifdef ARDUINO_ARCH_AVR
class rpc_software_serial_uart_master : public rpc_master
{
Expand All @@ -392,7 +396,7 @@ class rpc_software_serial_uart_master : public rpc_master
private:
long __baudrate;
SoftwareSerial __serial;
rpc_software_serial_uart_master(const rpc_software_serial_uart_master &);
rpc_software_serial_uart_master(const rpc_software_serial_uart_master &);
};

class rpc_software_serial_uart_slave : public rpc_slave
Expand All @@ -407,7 +411,7 @@ class rpc_software_serial_uart_slave : public rpc_slave
private:
long __baudrate;
SoftwareSerial __serial;
rpc_software_serial_uart_slave(const rpc_software_serial_uart_slave &);
rpc_software_serial_uart_slave(const rpc_software_serial_uart_slave &);
};
#endif // ARDUINO_ARCH_AVR

Expand Down

0 comments on commit 209c7e3

Please sign in to comment.