root/tags/version-1.00/RWTHMindstormsNXT/NXT_PlayTone.m @ 3

Revision 3, 3.6 KB (checked in by behrens, 5 years ago)

add tag and trunk toolbox

Line 
1function NXT_PlayTone(frequency, duration, varargin)
2% Plays a tone with the given frequency and duration
3
4% Syntax
5%   NXT_PlayTone(frequency, duration)
6%
7%   NXT_PlayTone(frequency, duration, bt_handle)
8%
9% Description
10%   NXT_PlayTone(frequency, duration) plays a tone of the frequency in Hz (200 - 14000Hz) and the
11%   duration in milli seconds.
12%
13%   NXT_PlayTone(frequency, duration, bt_handle) sends the play tone command over the specific
14%   Bluetooth handle (serial handle (PC) / file handle (Linux)).
15%
16%   If no Bluetooth handle is specified the default one (BT_GetDefaultHandle) is used.
17%
18% For more details see the official LEGO Mindstorms communication protocol.
19%
20% Examples
21%   NXT_PlayTone(440, 100);
22%
23%   bt_handle = BT_OpenHandle('bluetooth.ini','check');
24%   BT_SetDefaultHandle(bt_handle);
25%   NXT_PlayTone(1200, 120);
26%
27% See also: BT_GetDefaultHandle
28%
29% Signature
30%   Author: Linus Atorf (see AUTHORS)
31%   Date: 2007/10/15
32%   Copyright: 2007, RWTH Aachen University
33%
34%
35% ***********************************************************************************************
36% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    *
37% *                                                                                             *
38% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  *
39% *  it under the terms of the GNU General Public License as published by the Free Software     *
40% *  Foundation, either version 3 of the License, or (at your option) any later version.        *
41% *                                                                                             *
42% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       *
43% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *
44% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
45% *                                                                                             *
46% *  You should have received a copy of the GNU General Public License along with the           *
47% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 *
48% ***********************************************************************************************
49
50%% Parameter check
51% check if bluetooth handle is given; if not use default one
52if nargin > 2
53    if (ispc && isa(varargin{1}, 'serial')) || (isunix && isscalar(varargin{1}))
54        handle = varargin{1};
55    else
56        error('MATLAB:RWTHMindstormsNXT:Bluetooth:invalidHandle', 'Optional NXT bluetooth handle specified, but not a valid serial port handle');
57    end%if
58else
59    handle = BT_GetDefaultHandle;
60end%if
61
62% check if frequency is valid
63if (frequency < 200 || frequency > 14000)
64    error('MATLAB:RWTHMindstormsNXT:invalidPlayToneFrequency', 'Frequency %d has to be between 200 and 14000 Hz!', frequency);
65end%if
66
67% check if duration is valid
68if (duration < 0)
69    error('MATLAB:RWTHMindstormsNXT:invalidPlayToneDuration', 'Error: Duration %d has to be > 0 ms', duration);
70end%if
71
72
73%% Build bluetooth command
74[type cmd] = name2commandbytes('PLAYTONE');
75
76content(1:2) = dec2wordbytes(frequency, 2);
77content(3:4) = dec2wordbytes(duration, 2);
78
79
80%% Pack bluetooth packet
81packet = BT_CreatePacket(type, cmd, 'dontreply', content);
82textOut(sprintf('+ Send tone frequency %d Hz and duration %d ms...\n', frequency, duration));
83
84
85%% Send bluetooth packet
86BT_SendPacket(packet, handle);
87
88end%function
Note: See TracBrowser for help on using the browser.