| 712 | | % % open device to get a handle |
| 713 | | % DevHandle = calllib('libusb', 'usb_open', dev); |
| 714 | | % |
| 715 | | % % now the string stuff |
| 716 | | % buffer = blanks(255); |
| 717 | | % % we don't need a real buffer or pointer, matlab seems to do this |
| 718 | | % % for us, so we pass that buffer variable, without really needing |
| 719 | | % % it. it seems like matlab "knows" how strings get written by |
| 720 | | % % reference and returns the new value from the function. if you |
| 721 | | % % compare the matlab-returnvalues of usb_get_string_simple using |
| 722 | | % % libfunctionsview libusb, you'll find that they don't match whats |
| 723 | | % % written inside usb.h. very nice and handy, thank you matlab :-) |
| 724 | | % %pBuffer = libpointer('cstring', buffer); not needed, see above |
| 725 | | % |
| 726 | | % % now the actual call: |
| 727 | | % [bytesRead newHandleOrWhat ManufacturerName] = calllib('libusb', 'usb_get_string_simple', ... |
| 728 | | % DevHandle, dev.descriptor.iManufacturer, buffer, length(buffer)); |
| 729 | | % % again: |
| 730 | | % [bytesRead newHandleOrWhat ProductName] = calllib('libusb', 'usb_get_string_simple', ... |
| 731 | | % DevHandle, dev.descriptor.iProduct, buffer, length(buffer)); |
| 732 | | % |
| 733 | | % disp(sprintf(' Manufacturer: %s', ManufacturerName)) |
| 734 | | % disp(sprintf(' Product: %s', ProductName)) |
| 735 | | % |
| 736 | | % % close device again |
| 737 | | % ret = calllib('libusb', 'usb_close', DevHandle); |
| 738 | | % clear DevHandle %better doing it now than forgetting it later |
| | 712 | % open device to get a handle |
| | 713 | DevHandle = calllib('libusb0', 'usb_open', dev); |
| | 714 | |
| | 715 | % now the string stuff |
| | 716 | buffer = blanks(255); |
| | 717 | % we don't need a real buffer or pointer, matlab seems to do this |
| | 718 | % for us, so we pass that buffer variable, without really needing |
| | 719 | % it. it seems like matlab "knows" how strings get written by |
| | 720 | % reference and returns the new value from the function. if you |
| | 721 | % compare the matlab-returnvalues of usb_get_string_simple using |
| | 722 | % libfunctionsview libusb, you'll find that they don't match whats |
| | 723 | % written inside usb.h. very nice and handy, thank you matlab :-) |
| | 724 | %pBuffer = libpointer('cstring', buffer); not needed, see above |
| | 725 | |
| | 726 | % now the actual call: |
| | 727 | [bytesRead newHandleOrWhat ManufacturerName] = calllib('libusb0', 'usb_get_string_simple', ... |
| | 728 | DevHandle, dev.descriptor.iManufacturer, buffer, length(buffer)); |
| | 729 | % again: |
| | 730 | [bytesRead newHandleOrWhat ProductName] = calllib('libusb0', 'usb_get_string_simple', ... |
| | 731 | DevHandle, dev.descriptor.iProduct, buffer, length(buffer)); |
| | 732 | |
| | 733 | disp(sprintf(' Manufacturer: %s', ManufacturerName)) |
| | 734 | disp(sprintf(' Product: %s', ProductName)) |
| | 735 | |
| | 736 | % close device again |
| | 737 | ret = calllib('libusb0', 'usb_close', DevHandle); |
| | 738 | clear DevHandle %better doing it now than forgetting it later |
| 748 | | textOut(sprintf(' . opening device... ')); |
| 749 | | DevHandle = calllib('libusb', 'usb_open', dev); |
| 750 | | displayLibusbStatus(DevHandle); |
| 751 | | |
| 752 | | textOut(sprintf(' . reading serial number... ')); |
| 753 | | % c-syntax from documentation so we know whats going on: |
| 754 | | % int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, size_t buflen); |
| 755 | | buffer = blanks(255); |
| 756 | | [bytesRead newHandleOrWhat SerialNo] = calllib('libusb', 'usb_get_string_simple', DevHandle, dev.descriptor.iSerialNumber, buffer, length(buffer)); |
| 757 | | displayLibusbStatus(bytesRead); |
| 758 | | |
| 759 | | SerialNo = strtrim(SerialNo); % just to be safe |
| 760 | | textOut(sprintf(' . MAC = %s ', SerialNo)) |
| 761 | | |
| 762 | | % is it the right one? |
| 763 | | if isempty(hIn.NXTMAC) || strcmpi(hIn.NXTMAC, SerialNo) |
| 764 | | foundNXT = true; |
| 765 | | textOut(sprintf('(MAC matches, this is our desired NXT)\n')); |
| 766 | | break % search came to an end |
| 767 | | else |
| 768 | | foundNXT = false; |
| 769 | | textOut(sprintf('(no match, not using this NXT)\n')); |
| 770 | | % don't forget to close the opened NXT that didnt match... |
| 771 | | textOut(sprintf(' . closing device... ')); |
| 772 | | status = calllib('libusb', 'usb_close', DevHandle); |
| 773 | | displayLibusbStatus(status); |
| 774 | | end%if |
| | 748 | % textOut(sprintf(' . opening device... ')); |
| | 749 | % DevHandle = calllib('libusb', 'usb_open', dev); |
| | 750 | % displayLibusbStatus(DevHandle); |
| | 751 | % |
| | 752 | % textOut(sprintf(' . reading serial number... ')); |
| | 753 | % % c-syntax from documentation so we know whats going on: |
| | 754 | % % int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, size_t buflen); |
| | 755 | % buffer = blanks(255); |
| | 756 | % [bytesRead newHandleOrWhat SerialNo] = calllib('libusb', 'usb_get_string_simple', DevHandle, dev.descriptor.iSerialNumber, buffer, length(buffer)); |
| | 757 | % displayLibusbStatus(bytesRead); |
| | 758 | % |
| | 759 | % SerialNo = strtrim(SerialNo); % just to be safe |
| | 760 | % textOut(sprintf(' . MAC = %s ', SerialNo)) |
| | 761 | % |
| | 762 | % % is it the right one? |
| | 763 | % if isempty(hIn.NXTMAC) || strcmpi(hIn.NXTMAC, SerialNo) |
| | 764 | % foundNXT = true; |
| | 765 | % textOut(sprintf('(MAC matches, this is our desired NXT)\n')); |
| | 766 | % break % search came to an end |
| | 767 | % else |
| | 768 | % foundNXT = false; |
| | 769 | % textOut(sprintf('(no match, not using this NXT)\n')); |
| | 770 | % % don't forget to close the opened NXT that didnt match... |
| | 771 | % textOut(sprintf(' . closing device... ')); |
| | 772 | % status = calllib('libusb', 'usb_close', DevHandle); |
| | 773 | % displayLibusbStatus(status); |
| | 774 | % end%if |
| | 824 | % %% NXT found, open connection |
| | 825 | % |
| | 826 | % % DevHandle is now the NXT we want |
| | 827 | % |
| | 828 | % % the following commands are standard procedure for USB devices, similar usage |
| | 829 | % % can be found in the open source packages Python_NXT and LEGO::NXT (Perl) |
| | 830 | % |
| | 831 | % % flag to remember |
| | 832 | % ErrorWhileOpening = false; |
| | 833 | % |
| | 834 | % % if the following fails with error -16, the NXT is probably already in |
| | 835 | % % use (opened in another handle!) |
| | 836 | % textOut(sprintf(' . setting active configuration... ')); |
| | 837 | % % somehow this doesn't work, so we use the hardcoded configuration 1! |
| | 838 | % %calllib('libusb', 'usb_set_configuration', DevHandle, dev.config.bConfigurationValue); |
| | 839 | % ret = calllib('libusb', 'usb_set_configuration', DevHandle, LIBUSB_Configuration); |
| | 840 | % displayLibusbStatus(ret); |
| | 841 | % if (ret < 0) |
| | 842 | % ErrorWhileOpening = true; |
| | 843 | % end%if |
| | 844 | % |
| | 845 | % textOut(sprintf(' . claiming interface... ')); |
| | 846 | % % again, interface is hardcoded (compare implementations in python and |
| | 847 | % % perl, they do it the same way) |
| | 848 | % %calllib('libusb', 'usb_claim_interface', DevHandle, dev.config.interface.altsetting.bInterfaceNumber); |
| | 849 | % ret = calllib('libusb', 'usb_claim_interface', DevHandle, LIBUSB_Interface); |
| | 850 | % displayLibusbStatus(ret); |
| | 851 | % if (ret < 0) |
| | 852 | % ErrorWhileOpening = true; |
| | 853 | % end%if |
| | 854 | % |
| | 855 | % |
| | 856 | % % we don't need an alternative interface, whatever that is. |
| | 857 | % % but from debugging experience (also with Windows), you never know |
| | 858 | % % when you might need this, so we keep it in here! |
| | 859 | % |
| | 860 | % % disp(' . set altinterface') |
| | 861 | % % % %[int32, usb_dev_handlePtr] usb_set_altinterface(usb_dev_handlePtr, int32) |
| | 862 | % % ret = calllib('libusb', 'usb_set_altinterface', DevHandle, dev.config.interface.altsetting.bInterfaceNumber); |
| | 863 | % % disp(sprintf('usb_strerror: %s', calllib('libusb', 'usb_strerror'))) |
| | 864 | % |
| | 865 | % % is this necessary? but found it in perl and python versions... |
| | 866 | % % the main point: never touch a running system |
| | 867 | % textOut(sprintf(' . resetting device... ')); |
| | 868 | % ret = calllib('libusb', 'usb_reset', DevHandle); |
| | 869 | % displayLibusbStatus(ret); |
| | 870 | % if (ret < 0) |
| | 871 | % ErrorWhileOpening = true; |
| | 872 | % end%if |
| | 873 | % |
| | 874 | % % now it's time to decide: |
| | 875 | % if ErrorWhileOpening || isnumeric(DevHandle) |
| | 876 | % if ~SuppressErrors |
| | 877 | % errordlg('Something went wrong while opening the NXT device via USB (is it already open in another handle?). Please try to reboot the NXT or call COM_CloseNXT(''all'')!') |
| | 878 | % error('MATLAB:RWTHMindstormsNXT:USB:Linux:couldNotOpenNXT', 'Something went wrong while opening the NXT device via USB (is it already open in another handle?). Please try to reboot the NXT or call COM_CloseNXT(''all'')!') |
| | 879 | % else |
| | 880 | % % again, exit silently when no success |
| | 881 | % return |
| | 882 | % end%if |
| | 883 | % end%if |
| | 884 | % |
| | 885 | % % finally, we're good to go! |
| | 886 | % hOut.Handle = DevHandle; |
| | 887 | % hOut.NXTMAC = SerialNo; |
| | 888 | % |
| | 889 | % % and, important: |
| | 890 | % hOut.Connected(true); |
| | 891 | % |
| | 892 | % |
| | 893 | % %% clean up |
| | 894 | % |
| | 895 | % % is this needed? or will matlab destroy this private vars anyway after |
| | 896 | % % finishing this function? just to be sure with pointers... |
| | 897 | % clear p v bus dev newHandleOrWhat DevHandle |
| 826 | | % DevHandle is now the NXT we want |
| 827 | | |
| 828 | | % the following commands are standard procedure for USB devices, similar usage |
| 829 | | % can be found in the open source packages Python_NXT and LEGO::NXT (Perl) |
| 830 | | |
| 831 | | % flag to remember |
| 832 | | ErrorWhileOpening = false; |
| 833 | | |
| 834 | | % if the following fails with error -16, the NXT is probably already in |
| 835 | | % use (opened in another handle!) |
| 836 | | textOut(sprintf(' . setting active configuration... ')); |
| 837 | | % somehow this doesn't work, so we use the hardcoded configuration 1! |
| 838 | | %calllib('libusb', 'usb_set_configuration', DevHandle, dev.config.bConfigurationValue); |
| 839 | | ret = calllib('libusb', 'usb_set_configuration', DevHandle, LIBUSB_Configuration); |
| 840 | | displayLibusbStatus(ret); |
| 841 | | if (ret < 0) |
| 842 | | ErrorWhileOpening = true; |
| 843 | | end%if |
| 844 | | |
| 845 | | textOut(sprintf(' . claiming interface... ')); |
| 846 | | % again, interface is hardcoded (compare implementations in python and |
| 847 | | % perl, they do it the same way) |
| 848 | | %calllib('libusb', 'usb_claim_interface', DevHandle, dev.config.interface.altsetting.bInterfaceNumber); |
| 849 | | ret = calllib('libusb', 'usb_claim_interface', DevHandle, LIBUSB_Interface); |
| 850 | | displayLibusbStatus(ret); |
| 851 | | if (ret < 0) |
| 852 | | ErrorWhileOpening = true; |
| 853 | | end%if |
| 854 | | |
| 855 | | |
| 856 | | % we don't need an alternative interface, whatever that is. |
| 857 | | % but from debugging experience (also with Windows), you never know |
| 858 | | % when you might need this, so we keep it in here! |
| 859 | | |
| 860 | | % disp(' . set altinterface') |
| 861 | | % % %[int32, usb_dev_handlePtr] usb_set_altinterface(usb_dev_handlePtr, int32) |
| 862 | | % ret = calllib('libusb', 'usb_set_altinterface', DevHandle, dev.config.interface.altsetting.bInterfaceNumber); |
| 863 | | % disp(sprintf('usb_strerror: %s', calllib('libusb', 'usb_strerror'))) |
| 864 | | |
| 865 | | % is this necessary? but found it in perl and python versions... |
| 866 | | % the main point: never touch a running system |
| 867 | | textOut(sprintf(' . resetting device... ')); |
| 868 | | ret = calllib('libusb', 'usb_reset', DevHandle); |
| 869 | | displayLibusbStatus(ret); |
| 870 | | if (ret < 0) |
| 871 | | ErrorWhileOpening = true; |
| 872 | | end%if |
| 873 | | |
| 874 | | % now it's time to decide: |
| 875 | | if ErrorWhileOpening || isnumeric(DevHandle) |
| 876 | | if ~SuppressErrors |
| 877 | | errordlg('Something went wrong while opening the NXT device via USB (is it already open in another handle?). Please try to reboot the NXT or call COM_CloseNXT(''all'')!') |
| 878 | | error('MATLAB:RWTHMindstormsNXT:USB:Linux:couldNotOpenNXT', 'Something went wrong while opening the NXT device via USB (is it already open in another handle?). Please try to reboot the NXT or call COM_CloseNXT(''all'')!') |
| 879 | | else |
| 880 | | % again, exit silently when no success |
| 881 | | return |
| 882 | | end%if |
| 883 | | end%if |
| 884 | | |
| 885 | | % finally, we're good to go! |
| | 900 | % dev should now be our NXT |
| | 901 | |
| | 902 | disp('*** Opening USB connection') |
| | 903 | |
| | 904 | disp(' . getting device handle') |
| | 905 | DevHandle = calllib('libusb0', 'usb_open', dev); |
| | 906 | if isnumeric(DevHandle) |
| | 907 | ret = DevHandle; |
| | 908 | if ret < 0 |
| | 909 | disp(getLibusbErrorString(ret)) |
| | 910 | end%if |
| | 911 | end%if |
| | 912 | |
| | 913 | disp(' . reading serial number:') |
| | 914 | % int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, size_t buflen); |
| | 915 | buffer = blanks(255); |
| | 916 | [bytesRead newHandleOrWhat SerialNo] = calllib('libusb0', 'usb_get_string_simple', DevHandle, dev.descriptor.iSerialNumber, buffer, length(buffer)); |
| | 917 | disp(sprintf(' %s', SerialNo)) |
| | 918 | |
| | 919 | disp(' . setting active configuration') |
| | 920 | %calllib(USBLIB, 'usb_set_configuration', DevHandle, dev.config.bConfigurationValue); |
| | 921 | ret = calllib('libusb0', 'usb_set_configuration', DevHandle, 1); |
| | 922 | if ret < 0 |
| | 923 | disp(getLibusbErrorString(ret)) |
| | 924 | end%if |
| | 925 | |
| | 926 | disp(' . claiming interface') |
| | 927 | %calllib(USBLIB, 'usb_claim_interface', DevHandle, dev.config.interface.altsetting.bInterfaceNumber); |
| | 928 | ret = calllib('libusb0', 'usb_claim_interface', DevHandle, 0); |
| | 929 | if ret < 0 |
| | 930 | disp(getLibusbErrorString(ret)) |
| | 931 | end%if |
| | 932 | |
| | 933 | |
| | 934 | % disp(' . set altinterface') |
| | 935 | % % %[int32, usb_dev_handlePtr] usb_set_altinterface(usb_dev_handlePtr, int32) |
| | 936 | % ret = calllib('libusb0', 'usb_set_altinterface', DevHandle, dev.config.interface.altsetting.bInterfaceNumber); |
| | 937 | % disp(sprintf('usb_strerror: %s', calllib('libusb0', 'usb_strerror'))) |
| | 938 | % |
| | 939 | |
| | 940 | % disp(' . resetting device') |
| | 941 | % ret = calllib(USBLIB, 'usb_reset', DevHandle); |
| | 942 | % if ret < 0 |
| | 943 | % disp(getLibusbErrorString(ret)) |
| | 944 | % end%if |
| | 945 | |
| | 946 | % // Discard any data that is left in the buffer |
| | 947 | % while (usb_bulk_read(nxt->hdl, 0x82, buf, sizeof(buf), 1) > 0) |
| | 948 | % ; |
| | 949 | disp(' . discarding any data') |
| | 950 | buffer = char(uint8(blanks(64))); |
| | 951 | buffer = uint8(zeros(64,1)); |
| | 952 | ret = 1; |
| | 953 | while ret > 0 |
| | 954 | ret = calllib('libusb0', 'usb_bulk_read', DevHandle, uint8(130), buffer, 5, 1); |
| | 955 | end |
| | 956 | |
| | 957 | % // try to set the stream I/O feature |
| | 958 | % ret = usb_control_msg(nxt->hdl, 0x41, 0x3, 0, 0, NULL, 0, 1000); |
| | 959 | % if (ret >= 0) |
| | 960 | % { |
| | 961 | % nxt->stream_mode = 1; |
| | 962 | % } |
| | 963 | disp(' . open stream modus') |
| | 964 | buffer = char(uint8(blanks(64))); |
| | 965 | ret = calllib('libusb0', 'usb_control_msg', DevHandle, 65, 3, 0, 0, buffer, 0, 1000); |
| | 966 | if ret < 0 |
| | 967 | msg = ['Libusb error ' num2str(ret) ' while open stream mode: ' getLibusbErrorString(ret)]; |
| | 968 | warning('MATLAB:RWTHMindstormsNXT:USB:Linux:libusbErrorWhileOpenStreamMode', msg); |
| | 969 | end%if |
| | 970 | |
| | 971 | hNXT = DevHandle; |
| | 972 | |
| | 973 | % finally, we're good to go! |