From: Gerd v. Egidy Date: Sun, 13 Dec 2009 16:53:29 +0000 (+0100) Subject: small improvements in C++ wrapper: X-Git-Tag: v0.17~3 X-Git-Url: http://developer.intra2net.com/git/?p=libftdi;a=commitdiff_plain;h=58cce2d45cf6d7dae4650dadf96dce18b3ba151f small improvements in C++ wrapper: - make ftdi_usb_open_string() available - reduce code dupliation in open() functions - overloaded set_bitmask using the correct enum ftdi_mpsse_mode --- diff --git a/ftdipp/ftdi.cpp b/ftdipp/ftdi.cpp index 7c2dd78..2bc6c6e 100644 --- a/ftdipp/ftdi.cpp +++ b/ftdipp/ftdi.cpp @@ -77,27 +77,44 @@ bool Context::is_open() return d->open; } -int Context::open(int vendor, int product, const std::string& description, const std::string& serial) +int Context::open(int vendor, int product) { - int ret = 0; - // Open device - if (description.empty() && serial.empty()) - ret = ftdi_usb_open(d->ftdi, vendor, product); - else - ret = ftdi_usb_open_desc(d->ftdi, vendor, product, description.c_str(), serial.c_str()); + int ret = ftdi_usb_open(d->ftdi, vendor, product); if (ret < 0) return ret; - // Get device strings (closes device) - get_strings(); + return get_strings_and_reopen(); +} - // Reattach device - ret = ftdi_usb_open_dev(d->ftdi, d->dev); - d->open = (ret >= 0); +int Context::open(int vendor, int product, const std::string& description, const std::string& serial, unsigned int index) +{ + // translate empty strings to NULL + // -> do not use them to find the device (vs. require an empty string to be set in the EEPROM) + const char* c_description=NULL; + const char* c_serial=NULL; + if (!description.empty()) + c_description=description.c_str(); + if (!serial.empty()) + c_serial=serial.c_str(); - return ret; + int ret = ftdi_usb_open_desc_index(d->ftdi, vendor, product, c_description, c_serial, index); + + if (ret < 0) + return ret; + + return get_strings_and_reopen(); +} + +int Context::open(const std::string& description) +{ + int ret = ftdi_usb_open_string(d->ftdi, description.c_str()); + + if (ret < 0) + return ret; + + return get_strings_and_reopen(); } int Context::open(struct usb_device *dev) @@ -108,14 +125,7 @@ int Context::open(struct usb_device *dev) if (d->dev == 0) return -1; - // Get device strings (closes device) - get_strings(); - - // Reattach device - int ret = ftdi_usb_open_dev(d->ftdi, d->dev); - d->open = (ret >= 0); - - return ret; + return get_strings_and_reopen(); } int Context::close() @@ -263,7 +273,7 @@ int Context::set_error_char(unsigned char errorch, unsigned char enable) int Context::bitbang_enable(unsigned char bitmask) { - return ftdi_enable_bitbang(d->ftdi, bitmask); + return ftdi_set_bitmode(d->ftdi, bitmask, BITMODE_BITBANG); } int Context::bitbang_disable() @@ -273,6 +283,11 @@ int Context::bitbang_disable() int Context::set_bitmode(unsigned char bitmask, unsigned char mode) { + return set_bitmode(bitmask, mode); +} + +int Context::set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode) +{ return ftdi_set_bitmode(d->ftdi, bitmask, mode); } @@ -303,6 +318,23 @@ int Context::get_strings() return 1; } +int Context::get_strings_and_reopen() +{ + // Get device strings (closes device) + int ret=get_strings(); + if (ret < 0) + { + d->open = 0; + return ret; + } + + // Reattach device + ret = ftdi_usb_open_dev(d->ftdi, d->dev); + d->open = (ret >= 0); + + return ret; +} + /*! \brief Device strings properties. */ const std::string& Context::vendor() diff --git a/ftdipp/ftdi.hpp b/ftdipp/ftdi.hpp index 76868c3..9c345c3 100644 --- a/ftdipp/ftdi.hpp +++ b/ftdipp/ftdi.hpp @@ -80,7 +80,9 @@ public: /* Device manipulators */ bool is_open(); int open(struct usb_device *dev = 0); - int open(int vendor, int product, const std::string& description = std::string(), const std::string& serial = std::string()); + int open(int vendor, int product); + int open(int vendor, int product, const std::string& description, const std::string& serial = std::string(), unsigned int index=0); + int open(const std::string& description); int close(); int reset(); int flush(int mask = Input|Output); @@ -120,6 +122,7 @@ public: /* BitBang mode */ int set_bitmode(unsigned char bitmask, unsigned char mode); + int set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode); int DEPRECATED(bitbang_enable(unsigned char bitmask)); int bitbang_disable(); int read_pins(unsigned char *pins); @@ -129,6 +132,7 @@ public: protected: int get_strings(); + int get_strings_and_reopen(); /* Properties */ struct ftdi_context* context();