xxxxxxxxxx
135
// Global Constants
const MAX_SOUND_OPERATION_NUM = 59;
const MAX_SOUND_BINARY_NUM = 3 * MAX_SOUND_OPERATION_NUM;
const CUBE_ID_ARRAY = [ 0, 1, 2 ];
const SUPPORT_CUBE_NUM = CUBE_ID_ARRAY.length;
// Global Variables.
const gCubes = [ undefined, undefined, undefined ];
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
}
function draw() {
ellipse(mouseX, mouseY, 20, 20);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
background(100);
}
function mouseClicked() {
console.log( 'test');
let fs = fullscreen();
fullscreen(!fs);
connectNewCube();
}
// Cube Connection
const SERVICE_UUID = '10b20100-5b3b-4571-9508-cf3efcd7bbae';
const LIGHT_CHARCTERISTICS_UUID = '10b20103-5b3b-4571-9508-cf3efcd7bbae';
const SOUND_CHARCTERISTICS_UUID = '10b20104-5b3b-4571-9508-cf3efcd7bbae';
const connectNewCube = () => {
const cube = {
device:undefined,
sever:undefined,
service:undefined,
soundChar:undefined,
lightChar:undefined
};
// Scan only toio Core Cubes
const options = {
filters: [
{ services: [ SERVICE_UUID ] },
],
}
navigator.bluetooth.requestDevice( options ).then( device => {
cube.device = device;
if( cube === gCubes[0] ){
turnOnLightCian( cube );
const cubeID = 1;
}else if( cube === gCubes[1] ){
turnOnLightGreen( cube );
const cubeID = 2;
}
return device.gatt.connect();
}).then( server => {
cube.server = server;
return server.getPrimaryService( SERVICE_UUID );
}).then(service => {
cube.service = service;
return cube.service.getCharacteristic( SOUND_CHARCTERISTICS_UUID );
}).then( characteristic => {
cube.soundChar = characteristic;
return cube.service.getCharacteristic( LIGHT_CHARCTERISTICS_UUID );
}).then( characteristic => {
cube.lightChar = characteristic;
if( cube === gCubes[0] ){
turnOnLightCian( cube );
enablePlaySampleButton();
enablePlaySampleSEButton();
enablePlayNoteButton();
enablePlayPreInSEButton();
enableMIDIButton();
}else if( cube === gCubes[1] ){
turnOnLightGreen( cube );
}else{
turnOnLightRed( cube );
}
});
return cube;
}
// Cube Commands
// -- Light Commands
const turnOffLight = ( cube ) => {
const CMD_TURN_OFF = 0x01;
const buf = new Uint8Array([ CMD_TURN_OFF ]);
if( ( cube !== undefined ) && ( cube.lightChar !== undefined ) ){
cube.lightChar.writeValue( buf );
}
}
const turnOnLightGreen = ( cube ) => {
// Green light
const buf = new Uint8Array([ 0x03, 0x00, 0x01, 0x01, 0x00, 0xFF, 0x00 ]);
if( ( cube !== undefined ) && ( cube.lightChar !== undefined ) ){
cube.lightChar.writeValue( buf );
}
}
const turnOnLightCian = ( cube ) => {
// Cian light
const buf = new Uint8Array([ 0x03, 0x00, 0x01, 0x01, 0x00, 0xFF, 0xFF ]);
if( ( cube !== undefined ) && ( cube.lightChar !== undefined ) ){
cube.lightChar.writeValue( buf );
}
}
const turnOnLightRed = ( cube ) => {
// Red light
const buf = new Uint8Array([ 0x03, 0x00, 0x01, 0x01, 0xFF, 0x00, 0x00 ]);
if( ( cube !== undefined ) && ( cube.lightChar !== undefined ) ){
cube.lightChar.writeValue( buf );
}
}