Source: org/terraswarm/accessor/accessors/web/mutable/TestGain2.js

  1. // Test accessor that multiplies its input by a scale factor.
  2. //
  3. // Copyright (c) 2016-2017 The Regents of the University of California.
  4. // All rights reserved.
  5. //
  6. // Permission is hereby granted, without written agreement and without
  7. // license or royalty fees, to use, copy, modify, and distribute this
  8. // software and its documentation for any purpose, provided that the above
  9. // copyright notice and the following two paragraphs appear in all copies
  10. // of this software.
  11. //
  12. // IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  13. // FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  15. // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  16. // SUCH DAMAGE.
  17. //
  18. // THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  19. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
  21. // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
  22. // CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
  23. // ENHANCEMENTS, OR MODIFICATIONS.
  24. //
  25. /** Test accessor that multiplies its input by a scale factor.
  26. * This is a modified copy of TestGain.js
  27. *
  28. * First, the parameter is initialized to a different value.
  29. *
  30. * Second, there is an additional output, that sends the opposite of the
  31. * obtained value. This second output is echoed using console.log.
  32. *
  33. * Third, there are 2 realized features, witch are 'gain' and 'opposite'.
  34. *
  35. * @accessor mutable/TestGain2
  36. * @param gain The gain, a number with default 4.
  37. * @param input The input, a number with default 0.
  38. * @param scaled The output, the result of input * gain.
  39. * @author Edward A. Lee
  40. * @version $$Id$$
  41. */
  42. // Stop extra messages from jslint. Note that there should be no
  43. // space between the / and the * and global.
  44. /*globals console, error, exports, require */
  45. /*jshint globalstrict: true*/
  46. "use strict";
  47. exports.setup = function () {
  48. this.input('input', {
  49. 'type': 'number',
  50. 'value': 0
  51. });
  52. this.output('scaled', {
  53. 'type': 'number'
  54. });
  55. this.output('opScaled', {
  56. 'type': 'number'
  57. });
  58. this.parameter('gain', {
  59. 'type': 'number',
  60. 'value': 4
  61. });
  62. };
  63. exports.initialize = function () {
  64. this.addInputHandler('input', function () {
  65. // console.log("TestGain2: inputHandler: input: " + this.get('input') + " gain: " + this.getParameter('gain'));
  66. // console.log('TestGain2: scaled: ' + this.get('input') * this.getParameter('gain') + ' opScaled: ' + -(this.get('input') * this.getParameter('gain')));
  67. this.send('scaled', this.get('input') * this.getParameter('gain'));
  68. this.send('opScaled', -(this.get('input') * this.getParameter('gain')));
  69. });
  70. };