| 1 | function [protocol_version firmware_version] = NXT_Get_Firmware_Version(varargin) |
|---|
| 2 | % Returns the protocol and firmware version of the NXT |
|---|
| 3 | % |
|---|
| 4 | % Syntax |
|---|
| 5 | % |[protocol_version firmware_version] = NXT_Get_Firmware_Version()| |
|---|
| 6 | % |
|---|
| 7 | % |[protocol_version firmware_version] = NXT_Get_Firmware_Version(handle)| |
|---|
| 8 | % |
|---|
| 9 | % Description |
|---|
| 10 | % |[protocol_version firmware_version] = NXT_Get_Firmware_Version()| returns the protocol and |
|---|
| 11 | % firmware version of the NXT as strings. |
|---|
| 12 | % |
|---|
| 13 | % |[protocol_version firmware_version] = NXT_Get_Firmware_Version(handle)| uses the given |
|---|
| 14 | % Bluetooth connection |handle|. This should be a serial handle on a PC system and a file handle on a Linux system. |
|---|
| 15 | % |
|---|
| 16 | % If no Bluetooth |handle| is specified the default one (|COM_GetDefaultHandle|) is used. |
|---|
| 17 | % |
|---|
| 18 | % Examples |
|---|
| 19 | %+ [protocol_version firmware_version] = NXT_Get_Firmware_Version(); |
|---|
| 20 | % |
|---|
| 21 | %+ bt_handle = BT_OpenHandle('bluetooth.ini','check'); |
|---|
| 22 | %+ [protocol_version firmware_version] = NXT_Get_Firmware_Version(bt_handle); |
|---|
| 23 | % |
|---|
| 24 | % See also: COM_GetDefaultHandle |
|---|
| 25 | % |
|---|
| 26 | % Signature |
|---|
| 27 | % Author: Alexander Behrens (see AUTHORS) |
|---|
| 28 | % Date: 2008/05/22 |
|---|
| 29 | % Copyright: 2008, RWTH Aachen University |
|---|
| 30 | % |
|---|
| 31 | ; |
|---|
| 32 | % |
|---|
| 33 | % *********************************************************************************************** |
|---|
| 34 | % * This file is part of the RWTH - Mindstorms NXT Toolbox. * |
|---|
| 35 | % * * |
|---|
| 36 | % * The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify * |
|---|
| 37 | % * it under the terms of the GNU General Public License as published by the Free Software * |
|---|
| 38 | % * Foundation, either version 3 of the License, or (at your option) any later version. * |
|---|
| 39 | % * * |
|---|
| 40 | % * The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful, * |
|---|
| 41 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * |
|---|
| 42 | % * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
|---|
| 43 | % * * |
|---|
| 44 | % * You should have received a copy of the GNU General Public License along with the * |
|---|
| 45 | % * RWTH - Mindstorms NXT Toolbox. If not, see <http: |
|---|
| 46 | % *********************************************************************************************** |
|---|
| 47 | |
|---|
| 48 | %% Parameter check |
|---|
| 49 | % check if bluetooth handle is given; if not use default one |
|---|
| 50 | if nargin > 0 |
|---|
| 51 | handle = varargin{1}; |
|---|
| 52 | else |
|---|
| 53 | handle = COM_GetDefaultHandle; |
|---|
| 54 | end%if |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | %% Use wrapper functions |
|---|
| 58 | NXT_RequestFirmwareVersion(handle); |
|---|
| 59 | [protocol_version firmware_version] = NXT_CollectFirmwareVersion(handle); |
|---|
| 60 | |
|---|
| 61 | end % end function |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | %% ### Function: Request Firmware Version ### |
|---|
| 66 | function NXT_RequestFirmwareVersion(varargin) |
|---|
| 67 | % Sends the "GetFirmwareVersion" packet: Requests the protocol and firmware version of the NXT |
|---|
| 68 | % |
|---|
| 69 | % Usage: NXT_RequestFirmwareVersion(varargin) |
|---|
| 70 | % varargin : bluetooth handle (optional) |
|---|
| 71 | % |
|---|
| 72 | |
|---|
| 73 | %% Parameter check |
|---|
| 74 | % check if bluetooth handle is given; if not use default one |
|---|
| 75 | if nargin > 0 |
|---|
| 76 | handle = varargin{1}; |
|---|
| 77 | else |
|---|
| 78 | handle = COM_GetDefaultHandle; |
|---|
| 79 | end%if |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | %% Build bluetooth command |
|---|
| 83 | [type cmd] = name2commandbytes('GET_FIRMWARE_VERSION'); |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | %% Pack bluetooth packet |
|---|
| 87 | packet = COM_CreatePacket(type, cmd, 'reply', []); |
|---|
| 88 | textOut(sprintf('+ Requesting firmware version...\n')); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | %% Send bluetooth packet |
|---|
| 92 | COM_SendPacket(packet, handle); |
|---|
| 93 | |
|---|
| 94 | end % end function |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | %% ### Function: Collect Protocol and Firmware Version ### |
|---|
| 99 | function [protocol_version firmware_version] = NXT_CollectFirmwareVersion(varargin) |
|---|
| 100 | % Retrieves the previously requested protocol and firmware version of the NXT |
|---|
| 101 | % |
|---|
| 102 | % Returns: protocl_version : protcol version of the NXT |
|---|
| 103 | % firmware_version : firmware version of the NXT |
|---|
| 104 | % |
|---|
| 105 | |
|---|
| 106 | %% Parameter check |
|---|
| 107 | % check if bluetooth handle is given; if not use default one |
|---|
| 108 | if nargin > 0 |
|---|
| 109 | handle = varargin{1}; |
|---|
| 110 | else |
|---|
| 111 | handle = COM_GetDefaultHandle; |
|---|
| 112 | end%if |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | %% Get reference |
|---|
| 116 | [dontcare ExpectedCmd] = name2commandbytes('GET_FIRMWARE_VERSION'); |
|---|
| 117 | |
|---|
| 118 | %% Collect bluetooth packet |
|---|
| 119 | [type cmd status content] = COM_CollectPacket(handle); |
|---|
| 120 | |
|---|
| 121 | %% Check if packet is the right one |
|---|
| 122 | if cmd ~= ExpectedCmd || status ~= 0 |
|---|
| 123 | warning('MATLAB:RWTHMindstormsNXT:Bluetooth:discardingUnexpectedPacket', 'Received packed not expected. Discarding and trying to continue...'); |
|---|
| 124 | protocol_version = NaN; |
|---|
| 125 | firmware_version = NaN; |
|---|
| 126 | return; |
|---|
| 127 | end%if |
|---|
| 128 | |
|---|
| 129 | %% Interpret packet content |
|---|
| 130 | protocol_version = [num2str(wordbytes2dec(content(2),1)) '.' sprintf('%02d',wordbytes2dec(content(1),1))]; |
|---|
| 131 | firmware_version = [num2str(wordbytes2dec(content(4),1)) '.' sprintf('%02d',wordbytes2dec(content(3),1))]; |
|---|
| 132 | |
|---|
| 133 | end % end function |
|---|