Line data Source code
1 : /* 2 : generic object to allow a script to use a serial driver stream from both 3 : driver and device perspectives 4 : */ 5 : 6 : #include "AP_Scripting_config.h" 7 : #include "AP_Scripting.h" 8 : #include "AP_Scripting_SerialAccess.h" 9 : 10 : #if AP_SCRIPTING_ENABLED 11 : 12 : #if AP_SCRIPTING_SERIALDEVICE_ENABLED 13 : #define check_is_device_port() (is_device_port) 14 : #define ON_DEVICE_PORT(func, ...) (((AP_Scripting_SerialDevice::Port*)stream)->device_##func (__VA_ARGS__)) 15 : #else 16 : #define check_is_device_port() (false) 17 : #define ON_DEVICE_PORT(...) (0) // not executed 18 : #endif 19 : 20 1 : void AP_Scripting_SerialAccess::begin(uint32_t baud) 21 : { 22 1 : if (!check_is_device_port()) { 23 1 : stream->begin(baud); 24 : } 25 1 : } 26 : 27 0 : void AP_Scripting_SerialAccess::begin() 28 : { 29 0 : if (!check_is_device_port()) { 30 0 : stream->begin(stream->get_baud_rate()); 31 : } 32 0 : } 33 : 34 0 : void AP_Scripting_SerialAccess::configure_parity(uint8_t parity) { 35 0 : if (!check_is_device_port()) { 36 0 : stream->configure_parity(parity); 37 : } 38 0 : } 39 : 40 0 : void AP_Scripting_SerialAccess::set_stop_bits(uint8_t stop_bits) { 41 0 : if (!check_is_device_port()) { 42 0 : stream->set_stop_bits(stop_bits); 43 : } 44 0 : } 45 : 46 0 : void AP_Scripting_SerialAccess::set_unbuffered_writes(bool on) { 47 0 : if (!check_is_device_port()) { 48 0 : stream->set_unbuffered_writes(on); 49 : } 50 0 : } 51 : 52 12 : size_t AP_Scripting_SerialAccess::write(uint8_t c) 53 : { 54 12 : return write(&c, 1); 55 : } 56 : 57 13 : size_t AP_Scripting_SerialAccess::write(const uint8_t *buffer, size_t size) 58 : { 59 13 : if (!check_is_device_port()) { 60 12 : return stream->write(buffer, size); 61 : } 62 1 : return ON_DEVICE_PORT(write, buffer, size); 63 : } 64 : 65 0 : int16_t AP_Scripting_SerialAccess::read(void) 66 : { 67 : uint8_t c; 68 0 : if (read(&c, 1) != 1) { 69 0 : return -1; 70 : } 71 0 : return c; 72 : } 73 : 74 2 : ssize_t AP_Scripting_SerialAccess::read(uint8_t* buffer, uint16_t count) 75 : { 76 2 : if (!check_is_device_port()) { 77 1 : return stream->read(buffer, count); 78 : } 79 1 : return ON_DEVICE_PORT(read, buffer, count); 80 : } 81 : 82 0 : uint32_t AP_Scripting_SerialAccess::available(void) 83 : { 84 0 : if (!check_is_device_port()) { 85 0 : return stream->available(); 86 : } 87 0 : return ON_DEVICE_PORT(available); 88 : } 89 : 90 0 : void AP_Scripting_SerialAccess::set_flow_control(enum AP_HAL::UARTDriver::flow_control fcs) 91 : { 92 0 : if (!check_is_device_port()) { 93 0 : stream->set_flow_control(fcs); 94 : } 95 0 : } 96 : 97 : #endif // AP_SCRIPTING_ENABLED