%% Communicating with the Lego(R) Mindstorms(R) NXT brick over Bluetooth(R)
%
% This example illustrates the use of Bluetooth Serial Port Profile to communicate with a Lego Mindstorms NXT brick 
% using text commands.

% Copyright 2011-2012 The MathWorks, Inc.

%% Get information about available Bluetooth devices
%
% Before setting up a connection in MATLAB, the NXT brick  has to be paired
% with your computer. Once this is done, the device will be visible in MATLAB.
%
% To get a list of all the available Bluetooth devices use the |instrhwinfo|
% command. Get the "friendly" names of the Bluetooth devices available
% using the |RemoteNames| field from the output of |instrhwinfo|.

bluetoothDevices = instrhwinfo('Bluetooth')

remoteNames = bluetoothDevices.RemoteNames

%% Obtain information to connect to the NXT brick
% Get the details of the NXT brick.
% This example uses an Abe Bluetooth dongle and the Lego Mindstorms NXT using
% software version 2.0.
deviceInfo = instrhwinfo('Bluetooth','NXT')

%% Set up a connection to the NXT brick
% The device's |RemoteName| is |NXT| and its |Channel| is |1|.
% Construct a bluetooth object called |bt| as shown by the |ObjectConstructorName|
% in the previous step.
%
bt = Bluetooth('NXT', 1);

%%
%
% Now connect to the device.
fopen(bt);

%% Get information about the NXT brick
%
% Use the Lego Mindstorms NXT Communication Protocol to communicate with NXT brick. 
% It specifies that the commands written to NXT should have following format:
% 
% * Bytes 1:2 - Length of command
% * Byte 3   - Type of command 
% * Byte 4   - Additional information to execute the command
%
% To get the device information, raw data packet is formed as follows:
%
% * Bytes 1:2 - Length of command = 2
% * Byte 3   - Type of command = 1 (since it is a system command with reply from device)
% * Byte 4   - 0x9B (This value is defined by the Lego Mindstorms NXT Communication Protocol)
%
% This translates the raw data packet to be: |[2 0 1 155]|. Write this
% data to the brick.

fwrite(bt,[2 0 1 155]);

%%
% The device then gives a response containing device information. The
% response has following format:
%
% * Bytes 1:2   - Length of response
% * Byte 3     - Type of command issued
% * Byte 4     - Data sent to device
% * Byte 5     - Command status
% * Bytes 6:20  - Name of device (14 characters + null terminator) 
% * Bytes 21:27 - Bluetooth address
% * Byte 28    - LSB of Bluetooth signal strength
% * Byte 29    - Not defined
% * Byte 30    - Not defined
% * Byte 31    - MSB of Bluetooth signal strength
% * Byte 32    - LSB of user flash
% * Byte 33    - Not defined
% * Byte 34    - Not defined
% * Byte 35    - MSB of user flash
%
% Read the response from the brick to get its information
%
btResponse = fread(bt,32)';

%%
% In this example, the response from the NXT brick is:
%
% * Bytes 1:2   - Length of response = 33
% * Byte 3     - Type of command issued = 2
% * Byte 4     - Data sent to device = 0x9B
% * Byte 5     - Command status = 0 (command was successful)
% * Bytes 6:20  - Name of device (14 characters + null terminator) 
% * Bytes 21:27 - Bluetooth address
% * Byte 28    - LSB of Bluetooth signal strength = 0
% * Byte 29    - Not defined = 0
% * Byte 30    - Not defined = 0
% * Byte 31    - MSB of Bluetooth signal strength = 0
% * Byte 32    - LSB of user flash = 0
% * Byte 33    - Not defined = 0
% * Byte 34    - Not defined = 0
% * Byte 35    - MSB of user flash = 132

%%
% To retrieve only the name of the brick from its response, read bytes 6 to
% 20, which correspond to the brick's name.
%
btName = char(btResponse(6:20))

%%
% Clear the buffer of the Bluetooth object.
% Discard the remaining bytes from the input buffer by flushing the data using
% |flushinput| command.
%
flushinput(bt);

%% Play a tone on the NXT brick
% 
% The raw data packet to play a tone is defined by the Lego Mindstorms NXT 
% Direct commands as follows:
%
% * Bytes 1:2 - Length of command
% * Byte 3   - Type of command 
% * Byte 4   - Message id 
% * Bytes 5:6 - Frequency of the tone in Hz
% * Bytes 7:8 - Duration of the tone in ms
%
% Hence the raw data packet used to play a tone at frequency 1500 Hz for 1000 ms translates to |[6 0 0 3 220 5 232 3]|.

fwrite(bt,[6 0 0 3 220 5 232 3]);

%% Close the connection to the NXT brick
% Now that we are done with our tasks, let us close the connection to the
% device and clear the variable |bt|.

fclose(bt);
delete(bt);
clear('bt');

%%
% This example shows you how to create low level commands and control the 
% Lego Mindstorms NXT. There are higher level libraries for programming the 
% NXT. You can get more information on this
% <http://www.mathworks.com/academia/lego-mindstorms-nxt-software/ here>.
