Changeset 231

Show
Ignore:
Timestamp:
08/12/08 21:56:00 (5 years ago)
Author:
behrens
Message:

add libusb_win_proto.m, modified COM_OpenNXTEx.m

Location:
branches/behrens/mfiles
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • branches/behrens/mfiles/COM_OpenNXTEx.m

    r173 r231  
    182182    if ispc 
    183183        h.OSName    = 'Windows'; 
    184         h.OSValue   = 1; 
     184        h.OSValue   = 2; 
    185185    else % no support for Mac yet... 
    186186        h.OSName    = 'Linux'; 
     
    621621     
    622622%% Load libusb library (only if necessary) 
    623     if ~libisloaded('libusb') 
    624         textOut(sprintf('  - Loading library "libusb"... ')); 
     623    if ~libisloaded('libusb0') 
     624        textOut(sprintf('  - Loading library "libusb0"... ')); 
    625625        try 
    626626            % use "our" wrapper file... 
    627             loadlibrary('libusb', @libusb_proto) 
     627            loadlibrary('libusb0', @libusb_win_proto) 
    628628            textOut(sprintf('done.\n')); 
    629629        catch  
     
    636636        end%try 
    637637    else 
    638         textOut(sprintf('  - Library "libusb" already loaded.\n')); 
     638        textOut(sprintf('  - Library "libusb0" already loaded.\n')); 
    639639    end%if 
    640640 
     
    643643 
    644644%% Init libusb 
    645     textOut(sprintf('    . initializing libusb.\n')) 
    646     calllib('libusb', 'usb_init'); 
     645    textOut(sprintf('    . initializing libusb0.\n')) 
     646    calllib('libusb0', 'usb_init'); 
    647647     
    648648    % > - insert usb_set_debug(255) in pyusb.c right behind usb_init() 
     
    657657    % devices since last call, don't neet it... 
    658658    textOut(sprintf('    . finding busses... ')) 
    659     ret = calllib('libusb', 'usb_find_busses'); 
     659    ret = calllib('libusb0', 'usb_find_busses'); 
    660660    displayLibusbStatus(ret); 
    661661     
    662662    textOut(sprintf('    . finding devices... ')) 
    663     ret = calllib('libusb', 'usb_find_devices'); 
     663    ret = calllib('libusb0', 'usb_find_devices'); 
    664664    displayLibusbStatus(ret); 
    665665 
     
    667667     
    668668    textOut(sprintf('    . getting root bus object... ')) 
    669     p = calllib('libusb', 'usb_get_busses'); 
     669    p = calllib('libusb0', 'usb_get_busses'); 
    670670    bus = libstruct('usb_bus', p); 
    671671    displayLibusbStatus(p); 
     
    697697            % care what USB devices the user has. remove comment for advanced debug 
    698698            % mode 
    699             %textOut(sprintf('    . current device: %s\n', strtrim(char(dev.filename)))) 
     699            textOut(sprintf('    . current device: %s\n', strtrim(char(dev.filename)))) 
    700700 
    701701 
     
    710710            % It's debug-info only anyway 
    711711 
    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 
    739739 
    740740 
     
    746746                textOut(sprintf('    . found NXT device\n')); 
    747747 
    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 
    775775                 
    776776            end%if 
     
    822822 
    823823 
     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 
    824898%% NXT found, open connection 
    825899 
    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 
     902disp('*** Opening USB connection') 
     903 
     904disp('    . getting device handle') 
     905DevHandle = calllib('libusb0', 'usb_open', dev); 
     906if isnumeric(DevHandle) 
     907    ret = DevHandle; 
     908    if ret < 0 
     909        disp(getLibusbErrorString(ret)) 
     910    end%if 
     911end%if 
     912 
     913disp('    . reading serial number:') 
     914% int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, size_t buflen); 
     915buffer = blanks(255); 
     916[bytesRead newHandleOrWhat SerialNo] = calllib('libusb0', 'usb_get_string_simple', DevHandle, dev.descriptor.iSerialNumber,  buffer, length(buffer)); 
     917disp(sprintf('      %s', SerialNo)) 
     918 
     919disp('    . setting active configuration') 
     920%calllib(USBLIB, 'usb_set_configuration', DevHandle, dev.config.bConfigurationValue); 
     921ret = calllib('libusb0', 'usb_set_configuration', DevHandle, 1); 
     922if ret < 0 
     923    disp(getLibusbErrorString(ret)) 
     924end%if 
     925 
     926disp('    . claiming interface') 
     927%calllib(USBLIB, 'usb_claim_interface', DevHandle, dev.config.interface.altsetting.bInterfaceNumber); 
     928ret = calllib('libusb0', 'usb_claim_interface', DevHandle, 0); 
     929if ret < 0 
     930    disp(getLibusbErrorString(ret)) 
     931end%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%     ; 
     949disp('    . discarding any data') 
     950buffer = char(uint8(blanks(64))); 
     951buffer = uint8(zeros(64,1)); 
     952ret = 1; 
     953while ret > 0 
     954   ret = calllib('libusb0', 'usb_bulk_read', DevHandle, uint8(130), buffer, 5, 1); 
     955end 
     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%   } 
     963disp('    . open stream modus') 
     964buffer = char(uint8(blanks(64))); 
     965ret = calllib('libusb0', 'usb_control_msg', DevHandle, 65, 3, 0, 0, buffer, 0, 1000); 
     966if ret < 0 
     967  msg = ['Libusb error ' num2str(ret) ' while open stream mode: ' getLibusbErrorString(ret)]; 
     968  warning('MATLAB:RWTHMindstormsNXT:USB:Linux:libusbErrorWhileOpenStreamMode', msg); 
     969end%if 
     970 
     971hNXT = DevHandle; 
     972 
     973% finally, we're good to go! 
    886974    hOut.Handle = DevHandle; 
    887975    hOut.NXTMAC = SerialNo; 
     
    889977    % and, important: 
    890978    hOut.Connected(true); 
    891      
    892      
     979 
     980 
    893981%% clean up 
    894982 
    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 
    898  
     983% needed? or will matlab destroy this private vars anyway after finishing 
     984% this function? probably... 
     985clear p v bus dev newHandleOrWhat  
     986 
     987if ~isnumeric(hNXT) 
     988    disp('Successfully established USB connection to NXT') 
     989else 
     990    disp('Something went wrong, handle doesn''t seem valid') 
     991end%if 
    899992 
    900993end%function