We will again use the existing qnx.volume extension as an example. You can use this existing extension as a framework for creating your own.
PPS activities of an extension are typically dealt with in the extension_name.js file (volume.js in our example). You can name your file anything you want, but we recommend following the extension_name.js convention. The code for volume.js is shown below, with explanations of its various components from top to bottom.
/** * The abstraction layer for volume functionality * * @author myName * $Id$ */ var _pps = require('../../lib/pps/ppsUtils'), _statusPPS, _controlPPS, _trigger;
/** * Exports are the publicly accessible functions */ module.exports = { /** * Initializes the extension */ init: function() { //statusPPS _statusPPS = _pps.createObject(); _statusPPS.init(); _statusPPS.onChange = function(event) { if (_trigger && event && event.data && !isNaN(event.data["output.speaker.volume"])) { _trigger({ volume: event.data["output.speaker.volume"] }); } }; _statusPPS.open('/pps/services/audio/status', JNEXT.PPS_RDONLY);
//controlPPS _controlPPS = _pps.createObject(); _controlPPS.init(); _controlPPS.open("/pps/services/audio/control", JNEXT.PPS_WRONLY); },
/** * Sets the trigger function to call when a volume event is fired * @param trigger {Function} The trigger function to call when the event is fired */ setTrigger: function(trigger) { _trigger = trigger; },
/** * Returns the current volume * @returns {Number} The current audio parameters */ get: function() { return _statusPPS.ppsObj["output.speaker.volume"]; },
/** * Sets the volume * @param volume {Number} The new volume to set */ set: function(volume) { //write volume to pps if (!isNaN(volume) && volume >= 0 && volume <= 100) { _controlPPS.write({ id: 4, msg: "set_output_level", dat: { ctxt: 0, output_id: 0, level: volume } }); } }, };
Remember to set the onChange method to set up your callback handler for PPS events.
The format and content of your PPS objects will depend on the extension you're writing. Use PPS object definitions appropriate to your extension.