File: playTone.m

package info (click to toggle)
octave-arduino 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,616 kB
  • sloc: cpp: 3,221; python: 438; makefile: 152; xml: 22; sh: 1
file content (97 lines) | stat: -rw-r--r-- 2,921 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
## Copyright (C) 2018-2021 John Donoghue <john.donoghue@ieee.org>
## 
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## -*- texinfo -*- 
## @deftypefn {} {} playTone (@var{ar}, @var{pin}, @var{freq}, @var{duration})
## Play a tone of a given frequency on a specified pin.
##
## @subsubheading Inputs
## @var{ar} - connected arduino object
##
## @var{pin} - digital pin to play tone on
##
## @var{freq} - frequency in  hertz to play between 0 and 32767Hz.
##
## @var{duration} duration in seconds to play tone between 0 and 30 seconds
##
## If duration is 0 or not specified, tone will continue to play until next tone is commanded.
## If frequency is 0, tone will stop playing
##
## @strong{NOTE:} use of playTone can interfere with PWM output.
## @end deftypefn

function playTone (obj, pin, freq, duration)
  ARDUINO_PLAYTONE = 6;

  if nargin < 3
    error ("@arduino.playTone: expected pin name and frequency");
  endif
  if nargin < 4
    duration = 0;
  endif
  if !ischar(pin) && !isnumeric(pin)
    error ("@arduino.playTone: expected pin name as string");
  endif
  if (!isnumeric(freq) || freq < 0 || freq > 32767)
    error ("@arduino.playTone: expected freq between 0 .. 32767");
  endif  
  if (!isnumeric(duration) || duration < 0 || duration > 30)
    error ("@arduino.playTone: expected duration between 0 .. 30");
  endif  
  
  pininfo = obj.get_pin(pin);

  # first use ?
  if strcmp(pininfo.mode, "unset")
    configurePin(obj, pin, "digitaloutput")
  else
    [pinstate, pinmode] = pinStateMode(pininfo.mode);
    if !strcmp(pinmode, "digital")
      error ("@arduino.playTone: pin is in an incompatable mode");
    endif
  endif

  freq = uint16(freq);
  freqh = bitshift (freq, -8);
  freql = bitand(freq, hex2dec('FF'));

  duration = uint16(duration*10);
  durh = bitshift (duration, -8);
  durl = bitand(duration, hex2dec('FF'));

  datain = uint8([pininfo.id freqh freql durh durl]);
  
  [dataout, status] = __sendCommand__ (obj, 0, ARDUINO_PLAYTONE, datain);
  
  if status != 0
    error ("@arduino.playTone: failed to set tone err=%d - %s", status, char(dataout));
  endif 
 
endfunction

%!shared ar
%! ar = arduino();

%!test
%! if !strcmp(ar.Board, "due")
%! playTone(ar,"d2", 0, 0);
%! playTone(ar,"d2", 220, 1);
%! playTone(ar, "d2", 0);
%! else
%! # due doesnt have playTone
%! endif

%!error <undefined> playTone()

%!error <expected pin name and frequency> playTone(ar)

%!error <unknown pin nopin> playTone(ar, "nopin", 220)