2024-06-16 16:33:23 -03:00
|
|
|
/*
|
2024-06-02 01:02:31 -03:00
|
|
|
generic object to allow a script to use a serial driver stream from both
|
|
|
|
driver and device perspectives
|
2024-06-16 16:33:23 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AP_Scripting_config.h"
|
2024-06-02 01:02:31 -03:00
|
|
|
#include "AP_Scripting.h"
|
2024-06-16 16:33:23 -03:00
|
|
|
#include "AP_Scripting_SerialAccess.h"
|
|
|
|
|
|
|
|
#if AP_SCRIPTING_ENABLED
|
|
|
|
|
2024-06-02 01:02:31 -03:00
|
|
|
#if AP_SCRIPTING_SERIALDEVICE_ENABLED
|
|
|
|
#define check_is_device_port() (is_device_port)
|
|
|
|
#define ON_DEVICE_PORT(func, ...) (((AP_Scripting_SerialDevice::Port*)stream)->device_##func (__VA_ARGS__))
|
|
|
|
#else
|
|
|
|
#define check_is_device_port() (false)
|
|
|
|
#define ON_DEVICE_PORT(...) (0) // not executed
|
|
|
|
#endif
|
|
|
|
|
2024-06-16 16:33:23 -03:00
|
|
|
void AP_Scripting_SerialAccess::begin(uint32_t baud)
|
|
|
|
{
|
2024-06-02 01:02:31 -03:00
|
|
|
if (!check_is_device_port()) {
|
|
|
|
stream->begin(baud);
|
|
|
|
}
|
2024-06-16 16:33:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t AP_Scripting_SerialAccess::write(uint8_t c)
|
|
|
|
{
|
|
|
|
return write(&c, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t AP_Scripting_SerialAccess::write(const uint8_t *buffer, size_t size)
|
|
|
|
{
|
2024-06-02 01:02:31 -03:00
|
|
|
if (!check_is_device_port()) {
|
|
|
|
return stream->write(buffer, size);
|
|
|
|
}
|
|
|
|
return ON_DEVICE_PORT(write, buffer, size);
|
2024-06-16 16:33:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int16_t AP_Scripting_SerialAccess::read(void)
|
|
|
|
{
|
|
|
|
uint8_t c;
|
|
|
|
if (read(&c, 1) != 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t AP_Scripting_SerialAccess::read(uint8_t* buffer, uint16_t count)
|
|
|
|
{
|
2024-06-02 01:02:31 -03:00
|
|
|
if (!check_is_device_port()) {
|
|
|
|
return stream->read(buffer, count);
|
|
|
|
}
|
|
|
|
return ON_DEVICE_PORT(read, buffer, count);
|
2024-06-16 16:33:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AP_Scripting_SerialAccess::available(void)
|
|
|
|
{
|
2024-06-02 01:02:31 -03:00
|
|
|
if (!check_is_device_port()) {
|
|
|
|
return stream->available();
|
|
|
|
}
|
|
|
|
return ON_DEVICE_PORT(available);
|
2024-06-16 16:33:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_Scripting_SerialAccess::set_flow_control(enum AP_HAL::UARTDriver::flow_control fcs)
|
|
|
|
{
|
2024-06-02 01:02:31 -03:00
|
|
|
if (!check_is_device_port()) {
|
|
|
|
stream->set_flow_control(fcs);
|
|
|
|
}
|
2024-06-16 16:33:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // AP_SCRIPTING_ENABLED
|