updateFrequencies: function(container) { var frequencies = []; var capped = []; // For each key currently being touched for (var content = container.first; content; content = content.next){ if (content.state){ // Assigns frequency to the active frequency array. frequencies.push(content.behavior.frequency ); // Caps complex modes to one frequency capped[0] = frequencies[0]; } } // Uses synthoption global variable to determine synthesizer mode if(synthoption == "violin"){ Pins.invoke("/audio/setFrequenciesViolin", capped); } else if(synthoption == "bell"){ Pins.invoke("/audio/setFrequenciesBell", capped); } else if(synthoption == "laser"){ Pins.invoke("/audio/setFrequenciesLaser", capped); } else { // Restrict total # of keys pressed to 5 if(frequencies.length > 5){ var filtered = frequencies.slice(0,5); Pins.invoke("/audio/setFrequencies", filtered); } else Pins.invoke("/audio/setFrequencies", frequencies); } },
onUndisplayed(column) { Pins.invoke("/light/LEDOff"); if (this.repeater) { this.repeater.close(); this.repeater = undefined; } }
onDisplayed: function(container) { var peripheral = this.peripheral; // Connect to peripheral if not already connected if (null === peripheral.connection) { // The connection interval values are in units of 1.25 ms. A shorter connection interval speeds up discovery. // Connection interval values must also be divisible by (connection count * 2.5 ms) // The link supervision timeout, expressed in units of 10 ms, governs how long the link layer waits before disconnecting a device. var connection_interval = 16; // 20 ms var timeout = 50; // 1/2 second var params = { address: this.peripheral.address, addressType: this.peripheral.addressType, intervals: {min:connection_interval, max:connection_interval}, timeout: timeout, }; Pins.invoke("/ble/gapConnect", params); return; } // Stop scanning if we're already connected to the peripheral if (null != peripheral.connection) { this.stopScanning(); } // Discover services if not already discovered if (!peripheral.discoveredServices) { this.discoverServices(); } },
OFF(container) { Pins.invoke( "/light/turnOff" ); for ( let i = 0; i < clients.length; i++ ) { clients[ i ].send( "lightIsOff" ); } lightState = "lightIsOff"; }
discoverServices: function() { var container = this.container; var peripheral = this.peripheral; // Discover all primary services peripheral.services = {}; Pins.invoke("/ble/gattDiscoverAllPrimaryServices", { connection:peripheral.connection }); },
onPeripheralConnected: function(container, peripheral) { this.connection_handle = peripheral.connection; // Discover primary services - we'll be looking for heart rate service Pins.invoke("/ble/gattDiscoverAllPrimaryServices", { connection:this.connection_handle }); application.distribute("onConnected"); this.changeState(container, "services"); },
onCreate: function(container, data) { this.container = container; this.advertisementInterval = 3000; container.interval = this.advertisementInterval; Pins.invoke("/ble/gapStartScanning"); container.start(); container.distribute("onScanStart"); },
onGattRequestCompleted: function(container, result) { if ("services" == this.state) { if (null != this.end_handle) { // Discover heart rate service characteristics this.changeState(container, "characteristics"); var params = { connection: this.connection_handle, start: this.start_handle, end: this.end_handle }; Pins.invoke("/ble/gattDiscoverAllCharacteristics", params); } } else if ("characteristics" == this.state) { if (null != this.measurement_handle) { // Discover heart rate service characteristic descriptors this.changeState(container, "descriptors"); var params = { connection: this.connection_handle, start: this.measurement_handle + 1, end: this.end_handle }; Pins.invoke("/ble/gattDiscoverAllCharacteristicDescriptors", params); } } else if ("descriptors" == this.state) { if (null != this.ccc_handle) { // Enable heart rate measurement notifications // 16-bit little endian Client Characteristic Configuration - setting bit 1 enables notification var buffer = new ArrayBuffer(2); var ccc_data = new Uint8Array(buffer); ccc_data[0] = 0x01; ccc_data[1] = 0x00; var params = { connection: this.connection_handle, characteristic: this.ccc_handle, value: buffer }; Pins.invoke("/ble/gattWriteCharacteristicValue", params); this.changeState(container, "reading"); } } },
onDisplayed: function(container) { if (!this.service.discoveredCharacteristics) { var params = { connection: this.peripheral.connection, start: this.service.start_handle, end: this.service.end_handle }; Pins.invoke("/ble/gattDiscoverAllCharacteristics", params); } else application.distribute("onServiceCharacteristicsAdded", this.service); },
onPeripheralConnected: function(container, peripheral) { this.connection = peripheral.connection; let buffer = new ArrayBuffer(2); let ccc_data = new Uint8Array(buffer); ccc_data[0] = 0x01; ccc_data[1] = 0x00; var params = { connection: this.connection, characteristic: CCC_HANDLE, value: buffer } Pins.invoke("/ble/gattWriteCharacteristicValue", params); this.changeState(container, "connected"); application.distribute("onConnected"); },
onDisplaying(column) { model.previousSensorColor = Object.create(CLIENT.color); Pins.invoke("/light/LEDOn"); this.repeater = Pins.repeat("/light/getColor", 200, result => { var prev = model.previousSensorColor; var factor = 0.3 var red = Math.floor(prev.red * factor + result.r * (1 - factor)); var green = Math.floor(prev.green * factor + result.g * (1 - factor)); var blue = Math.floor(prev.blue * factor + result.b * (1 - factor)); if (red != prev.red || green != prev.green || blue != prev.blue) { prev.red = red; prev.green = green; prev.blue = blue; CLIENT.setColor(prev); CLIENT.sendColor(); updateColorTip(); } }); }
onStopRecording(container) { container.stop(); Pins.invoke("/recorder/stopRecording", recording => this.onRecording(container, recording)); }
readRSSI: function(container) { var peripheral = this.peripheral; var params = {connection: peripheral.connection}; Pins.invoke("/ble/gapReadRSSI", params); },
onStartRecording(container) { Pins.invoke("/recorder/startRecording"); container.time = 0; container.start(); }
stopScanning: function() { Pins.invoke("/ble/gapStopScanning"); }
sendACLData(acl) { /* Do not use TypedArray directly */ acl.buffer = (acl.data != null) ? acl.data.buffer : null; acl.data = null; Pins.invoke("/" + this._bll + "/sendACLData", acl); }
}, analogSensor: { require: "Analog", pins: { analog: { pin: 54 } } } }, success => this.onPinsConfigured(application, success)); }, onPinsConfigured(application, success) { if (success) { /* Use the initialized analogSensor object and repeatedly call its read method with a given interval(20ms). */ Pins.repeat("/analogSensor/read", 20, result => application.distribute( "onAnalogValueChanged", result )); // Initialized keyboard, data passed are notes C,D,E,F,G,A,B,C scale, plus all half steps added in order at the end. application.add(new Keyboard([523, 587, 660, 698, 783, 880, 988, 1046, 554, 622, 740, 831, 932, 1109])); // Initialized buttons to the variants defined in theme.xml application.add(new ButtonTray([0,1,2,3])); // Starts audio output hardware Pins.invoke("/audio/start"); Pins.share("ws", {zeroconf: true, name: "simplesynth"}); } else { application.skin = new Skin({ fill:"#f78e0f" }); application.add(new Label({ left:0, right:0, style:errorStyle, string:"Error" })); } } });
sendAttributePDU(data) { Pins.invoke("/" + this._bll + "/attSendAttributePDU", { connection: this._id, buffer: data.buffer }); }
onDisplayed: function(container) { var peripheral = this.peripheral; var params = {connection: peripheral.connection, characteristic: this.data.handle}; Pins.invoke("/ble/gattReadCharacteristicValue", params); },
onAnalogValueChanged: function(container, result) { // Function defines how analog value(0-1) changes audio amplitude(loudness) var test = (2*result+.5) *aval; // Change Amplitude with analog input values Pins.invoke("/audio/setAmplitude", test); },
const TYPE_PWM = "PWM"; let handlers = { pinExplorerStart(helper, query) { Pins.invoke("getPinMux", pinmux => { trace(JSON.stringify(pinmux, null, " ") + "\n"); let explorer = {}; let leftPins = pinmux.leftPins; for (let pin = 0, c = leftPins.length; pin < c; pin++) { helper.pinExplorerAddPin(explorer, 51 + pin, leftPins[pin], pinmux.leftVoltage); } let rightPins = pinmux.rightPins; for (let pin = 0, c = rightPins.length; pin <= c; pin++) { helper.pinExplorerAddPin(explorer, 59 + pin, rightPins[pin], pinmux.rightVoltage); } trace(JSON.stringify(explorer, null, " ") + "\n"); Pins.configure(explorer, success => { let url = helper.pinsStartSharing(query.ip); if (url) helper.wsResponse(url) else helper.wsErrorResponse(500, "Internal Server Error") }, error => { helper.pinsStopSharing(); helper.wsErrorResponse(500, "Internal Server Error"); }); }); }, pinExplorerStop(helper, query) { helper.pinsStopSharing(); helper.wsResponse()
startScanning(container) { Pins.invoke("/ble/gapStartScanning"); }
sendCommand(command) { /* Do not use TypedArray directly */ command.buffer = (command.data != null) ? command.data.buffer : null; command.data = null; Pins.invoke("/" + this._bll + "/sendCommand", command); }
startScanning: function(container) { Pins.invoke("/ble/gapStartScanning", {interval: 0x500}); this.changeState(container, "discovery"); },
connect: function(container, peripheral) { Pins.invoke("/ble/gapConnect", { address:peripheral.address, addressType:peripheral.addressType }); this.changeState(container, "connect"); },
/* Miscellaneous functions */ function updateColorTip() { var color = SERVER.color; Pins.invoke("/led/write", color); colorTip.skin = new Skin("rgb(" + color.red + "," + color.green + "," + color.blue + ")"); }
start(args) { Pins.invoke("configuration", configuration => this.onConfiguration(configuration, args)); }