Changeset 229

Show
Ignore:
Timestamp:
08/12/08 18:10:57 (5 years ago)
Author:
behrens
Message:

add working version

Location:
branches/behrens/libusb_win_example
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/behrens/libusb_win_example/USB_Test_modified.m

    r228 r229  
    1212   %packet = hex2dec(['01';'02';'03';'04']); 
    1313   DevHandle = USBTest_OpenHandle(); 
    14    [ok] = USBTest_SendPacket(ton, DevHandle); 
     14    
     15   % play tones 
     16   for i=200:50:200 
     17       ton(1) = 128; % Direct command no reply 
     18       ton(2) = 3;  %PLAYTONE 
     19       ton(3:4)=typecast(int16(i), 'uint8');   %frequency [Hz] Uword 200-14000 Hz 
     20       ton(5:6)=typecast(int16(200), 'uint8');  %duration [ms] Uword 
     21 
     22       [ok] = USBTest_SendPacket(ton, DevHandle); 
     23%        if ok > 0 
     24%            [len data] = USBTest_RecievePacket(DevHandle); 
     25%            fprintf('\nAntwortlaenge (PlayTon): %d\n', len); 
     26%        end 
     27       pause(0.3); 
     28   end 
     29    
     30   % get battery level 
     31   packet = [0;11]; % Direct command no reply / GetBatteryLevel 
     32   [ok] = USBTest_SendPacket(packet, DevHandle); 
    1533   if ok > 0 
    16       [len data] = USBTest_RecievePacket(DevHandle); 
    17       fprintf('\nAntwortlaenge (PlayTon): %d\n', len); 
    18       if len == 3 
    19          [ok] = USBTest_SendPacket(packet, DevHandle); 
    20          if ok > 0 
    21             [len data] = USBTest_RecievePacket(DevHandle); 
    22             fprintf('\nAntwortlaenge (GetSpannung): %d\n', len); 
    23             if len == 5 
    24                fprintf('Restspannung in mV: %d\n', wordbytes2dec(data(4:5),2)); 
    25             end 
    26          end 
    27       end 
     34       [len data] = USBTest_RecievePacket(DevHandle); 
     35       fprintf('\nAntwortlaenge (GetSpannung): %d\n', len); 
     36       if len == 5 
     37           fprintf('Restspannung in mV: %d\n', wordbytes2dec(data(4:5),2)); 
     38       end 
    2839   end 
     40 
     41% % get inputstate 
     42%    packet = [0;7;0]; 
     43%    [ok] = USBTest_SendPacket(packet, DevHandle); 
     44%    if ok > 0 
     45%        [len data] = USBTest_RecievePacket(DevHandle); 
     46%        fprintf('\nAntwortlaenge (GetSpannung): %d\n', len); 
     47%        BlockTachoCount = wordbytes2dec(data(19:22), 4, 'signed') %signed 
     48% %        if len == 5 
     49% %            fprintf('Restspannung in mV: %d\n', wordbytes2dec(data(4:5),2)); 
     50% %        end 
     51%    end 
     52    
     53%    % get outputstate 
     54%    packet = [0;6;0]; 
     55%    [ok] = USBTest_SendPacket(packet, DevHandle); 
     56%    if ok > 0 
     57%        [len data] = USBTest_RecievePacket(DevHandle); 
     58%        fprintf('\nAntwortlaenge (GetSpannung): %d\n', len); 
     59%        BlockTachoCount = wordbytes2dec(data(19:22), 4, 'signed') %signed 
     60% %        if len == 5 
     61% %            fprintf('Restspannung in mV: %d\n', wordbytes2dec(data(4:5),2)); 
     62% %        end 
     63%    end 
     64%    [ok] = USBTest_SendPacket(ton, DevHandle); 
     65%    if ok > 0 
     66%       [len data] = USBTest_RecievePacket(DevHandle); 
     67%       fprintf('\nAntwortlaenge (PlayTon): %d\n', len); 
     68%       if len == 3 
     69%          [ok] = USBTest_SendPacket(packet, DevHandle); 
     70%          if ok > 0 
     71%             [len data] = USBTest_RecievePacket(DevHandle); 
     72%             fprintf('\nAntwortlaenge (GetSpannung): %d\n', len); 
     73%             if len == 5 
     74%                fprintf('Restspannung in mV: %d\n', wordbytes2dec(data(4:5),2)); 
     75%             end 
     76%          end 
     77%       end 
     78%    end 
    2979   USBTest_Close(DevHandle); 
    3080 
     
    3686 
    3787%   int ret = usb_bulk_write(nxt->hdl, 0x1, buf, len, 10000); 
    38     hp = char(uint8(packet)); % setstr depriciated 
     88    hp = uint8(packet); % setstr depriciated 
    3989    fprintf('\nSending USB packet (%s), total len = %d bytes... \n', dec2hex(hp), length(hp)); 
    40     ret = calllib(USBLIB, 'usb_bulk_write', DevHandle, 1, hp, length(packet), 10000); 
     90    ret = calllib(USBLIB, 'usb_bulk_write', DevHandle, 1, hp, length(packet), 100); 
    4191    if ret < 0 
    4292       msg = ['Libusb error ' num2str(ret) ' while sending data: ' getLibusbErrorString(ret)]; 
     
    53103    data = []; 
    54104%   int ret = usb_bulk_read(nxt->hdl, 0x82, buf, len, 500); 
    55     buffer = char(uint8(blanks(64))); 
     105%    buffer = char(uint8(blanks(64))); 
     106    buffer = uint8(zeros(64,1)); 
     107    buffer = uint8(zeros(1,64));     
    56108%    vp = libpointer('voidPtr',buffer); 
    57     ret = calllib(USBLIB, 'usb_bulk_read', DevHandle, uint8(130), buffer, 64, 500); 
     109    [ret something reply] = calllib(USBLIB, 'usb_bulk_read', DevHandle, uint8(130), buffer, 64, 500); 
    58110    if ret < 0 
    59111       msg = ['Libusb error ' num2str(ret) ' while recieving data: ' getLibusbErrorString(ret)]; 
     
    63115       recBytes = ret; 
    64116       data = buffer(1:ret); 
     117       data = reply; 
    65118    end 
    66119end 
     
    83136if ~libisloaded(USBLIB) 
    84137  % use "our" wrapper file... 
    85   loadlibrary(USBLIB, @usblib_proto); 
     138  loadlibrary(USBLIB, @libusb_win_proto); 
    86139end 
    87140 
     
    259312disp('    . discarding any data') 
    260313buffer = char(uint8(blanks(64))); 
     314buffer = uint8(zeros(64,1)); 
    261315ret = 1; 
    262316while ret > 0 
     
    271325%   } 
    272326disp('    . open stream modus') 
     327buffer = char(uint8(blanks(64))); 
    273328ret = calllib(USBLIB, 'usb_control_msg', DevHandle, 65, 3, 0, 0, buffer, 0, 1000); 
    274329if ret < 0 
     
    301356%   usb_bulk_write(nxt->hdl, 0x1, buf, 0, 1000); 
    302357   buffer = blanks(64); 
     358   buffer = uint8(zeros(64,1)); 
    303359   calllib(USBLIB, 'usb_bulk_read', DevHandle, 1, buffer, 0, 1000); 
    304360%   // Turn off stream mode 
    305361%   usb_control_msg(nxt->hdl, 0x41, 0x1, 0, 0, NULL, 0, 5000); 
     362   buffer = blanks(64); 
    306363   calllib(USBLIB, 'usb_control_msg', DevHandle, 65, 1, 0, 0, buffer, 0, 5000); 
    307364%   // Discard any data that is left in the buffer 
     
    310367   ret = 1; 
    311368   while ret > 0 
     369      buffer = uint8(zeros(64,1));        
    312370      ret = calllib(USBLIB, 'usb_bulk_read', DevHandle, 130, buffer, 64, 1); 
    313371   end 
  • branches/behrens/libusb_win_example/libusb_win_proto.m

    r228 r229  
    2020fcns.name{fcnNum}='usb_get_descriptor'; fcns.calltype{fcnNum}='cdecl'; fcns.LHS{fcnNum}='int32'; fcns.RHS{fcnNum}={'usb_dev_handlePtr', 'uint8', 'uint8', 'voidPtr', 'int32'};fcnNum=fcnNum+1; 
    2121% int usb_bulk_write ( usb_dev_handle * dev , int ep , char * bytes , int size , int timeout );  
    22 fcns.name{fcnNum}='usb_bulk_write'; fcns.calltype{fcnNum}='cdecl'; fcns.LHS{fcnNum}='int32'; fcns.RHS{fcnNum}={'usb_dev_handlePtr', 'int32', 'cstring', 'int32', 'int32'};fcnNum=fcnNum+1; 
     22fcns.name{fcnNum}='usb_bulk_write'; fcns.calltype{fcnNum}='cdecl'; fcns.LHS{fcnNum}='int32'; fcns.RHS{fcnNum}={'usb_dev_handlePtr', 'int32', 'voidPtr', 'int32', 'int32'};fcnNum=fcnNum+1; 
    2323% int usb_bulk_read ( usb_dev_handle * dev , int ep , char * bytes , int size , int timeout );  
    24 fcns.name{fcnNum}='usb_bulk_read'; fcns.calltype{fcnNum}='cdecl'; fcns.LHS{fcnNum}='int32'; fcns.RHS{fcnNum}={'usb_dev_handlePtr', 'int32', 'cstring', 'int32', 'int32'};fcnNum=fcnNum+1; 
     24fcns.name{fcnNum}='usb_bulk_read'; fcns.calltype{fcnNum}='cdecl'; fcns.LHS{fcnNum}='int32'; fcns.RHS{fcnNum}={'usb_dev_handlePtr', 'int32', 'voidPtr', 'int32', 'int32'};fcnNum=fcnNum+1; 
    2525% int usb_interrupt_write ( usb_dev_handle * dev , int ep , char * bytes , int size , int timeout );  
    2626fcns.name{fcnNum}='usb_interrupt_write'; fcns.calltype{fcnNum}='cdecl'; fcns.LHS{fcnNum}='int32'; fcns.RHS{fcnNum}={'usb_dev_handlePtr', 'int32', 'cstring', 'int32', 'int32'};fcnNum=fcnNum+1;