<commit_before>(function() {

// Fix up prefixing
if (typeof navigator == "undefined") return; // not running in a web browser
if (!navigator.bluetooth) {
  console.log("No navigator.bluetooth - Web Bluetooth not enabled");
  return;
}

var NORDIC_SERVICE = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
var NORDIC_TX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e";
var NORDIC_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e";

function ab2str(buf) {
  return String.fromCharCode.apply(null, new Uint8Array(buf));
}

function str2ab(str) {
  var buf = new ArrayBuffer(str.length);
  var bufView = new Uint8Array(buf);
  for (var i=0, strLen=str.length; i<strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}


var btServer = undefined;
var connectionDisconnectCallback;

var txCharacteristic;
var rxCharacteristic;
var txDataQueue = undefined;

  function init() {
    Espruino.Core.Config.add("WEB_BLUETOOTH", {
      section : "Communications",
      name : "Connect over Bluetooth Smart (Web Bluetooth)",
      descriptionHTML : 'Allow connection to Espruino via BLE with the Nordic UART implementation',
      type : "boolean",
      defaultValue : true, 
    });
  }  

  var getPorts = function(callback) {
    if (Espruino.Config.WEB_BLUETOOTH)
      callback(['Web Bluetooth']);
    else
      callback();
  };
  
  var openSerial=function(serialPort, openCallback, receiveCallback, disconnectCallback) {
    connectionDisconnectCallback = disconnectCallback;

    var btService;

    navigator.bluetooth.requestDevice({filters:[{services:[ NORDIC_SERVICE ]}]}).then(function(device) {
      console.log('BT>  Device Name:       ' + device.name);
      console.log('BT>  Device InstanceID: ' + device.instanceID);
      console.log('BT>  Device Paired:     ' + device.paired);
      console.log('BT>  Device Class:      ' + device.deviceClass);
      console.log('BT>  Device UUIDs:      ' + device.uuids.join('\n' + ' '.repeat(21)));
      return device.connectGATT();
    }).then(function(server) {
      console.log("BT> Connected");
      btServer = server;  
      return server.getPrimaryService(NORDIC_SERVICE);
    }).then(function(service) {
      console.log("BT> Got service");
      btService = service;
      return btService.getCharacteristic(NORDIC_RX);
    }).then(function (s) {
      rxCharacteristic = s;
      console.log("BT> RX characteristic:"+JSON.stringify(rxCharacteristic));
      rxCharacteristic.addEventListener('characteristicvaluechanged', function(event) {
        var characteristic = event.target;
        var data = ab2str(characteristic.value);
        console.log("BT> RX:"+JSON.stringify(data));
        receiveCallback(data);
      });
      return rxCharacteristic.startNotifications();
    }).then(function() {
      return btService.getCharacteristic(NORDIC_TX);
    }).then(function (s) {
      txCharacteristic = s;
      console.log("BT> TX characteristic:"+JSON.stringify(txCharacteristic));          
    }).then(function() {
      txDataQueue = undefined;
      setTimeout(function() {
        openCallback("All ok");
      }, 500);
    }).catch(error => {
      console.log('BT> ERROR: ' + error);
      if (btServer) {
        // we should have this, but chromebook doesn't seem to 
      if (btServer.disconnect) btServer.disconnect();
        btServer = undefined;
        txCharacteristic = undefined;
        rxCharacteristic = undefined;
      }      
      openCallback(undefined);
    });
  };
 
  var closeSerial=function() {
    if (btServer) {
      // we should have this, but chromebook doesn't seem to 
      if (btServer.disconnect) btServer.disconnect();
      btServer = undefined;
      txCharacteristic = undefined;
      rxCharacteristic = undefined;
    }
    connectionDisconnectCallback();
  };

  // Throttled serial write<commit_msg>Fixed invalid syntax of function declaration<commit_after>(function() {

// Fix up prefixing
if (typeof navigator == "undefined") return; // not running in a web browser
if (!navigator.bluetooth) {
  console.log("No navigator.bluetooth - Web Bluetooth not enabled");
  return;
}

var NORDIC_SERVICE = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
var NORDIC_TX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e";
var NORDIC_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e";

function ab2str(buf) {
  return String.fromCharCode.apply(null, new Uint8Array(buf));
}

function str2ab(str) {
  var buf = new ArrayBuffer(str.length);
  var bufView = new Uint8Array(buf);
  for (var i=0, strLen=str.length; i<strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}


var btServer = undefined;
var connectionDisconnectCallback;

var txCharacteristic;
var rxCharacteristic;
var txDataQueue = undefined;

  function init() {
    Espruino.Core.Config.add("WEB_BLUETOOTH", {
      section : "Communications",
      name : "Connect over Bluetooth Smart (Web Bluetooth)",
      descriptionHTML : 'Allow connection to Espruino via BLE with the Nordic UART implementation',
      type : "boolean",
      defaultValue : true, 
    });
  }  

  var getPorts = function(callback) {
    if (Espruino.Config.WEB_BLUETOOTH)
      callback(['Web Bluetooth']);
    else
      callback();
  };
  
  var openSerial=function(serialPort, openCallback, receiveCallback, disconnectCallback) {
    connectionDisconnectCallback = disconnectCallback;

    var btService;

    navigator.bluetooth.requestDevice({filters:[{services:[ NORDIC_SERVICE ]}]}).then(function(device) {
      console.log('BT>  Device Name:       ' + device.name);
      console.log('BT>  Device InstanceID: ' + device.instanceID);
      console.log('BT>  Device Paired:     ' + device.paired);
      console.log('BT>  Device Class:      ' + device.deviceClass);
      console.log('BT>  Device UUIDs:      ' + device.uuids.join('\n' + ' '.repeat(21)));
      return device.connectGATT();
    }).then(function(server) {
      console.log("BT> Connected");
      btServer = server;  
      return server.getPrimaryService(NORDIC_SERVICE);
    }).then(function(service) {
      console.log("BT> Got service");
      btService = service;
      return btService.getCharacteristic(NORDIC_RX);
    }).then(function (s) {
      rxCharacteristic = s;
      console.log("BT> RX characteristic:"+JSON.stringify(rxCharacteristic));
      rxCharacteristic.addEventListener('characteristicvaluechanged', function(event) {
        var characteristic = event.target;
        var data = ab2str(characteristic.value);
        console.log("BT> RX:"+JSON.stringify(data));
        receiveCallback(data);
      });
      return rxCharacteristic.startNotifications();
    }).then(function() {
      return btService.getCharacteristic(NORDIC_TX);
    }).then(function (s) {
      txCharacteristic = s;
      console.log("BT> TX characteristic:"+JSON.stringify(txCharacteristic));          
    }).then(function() {
      txDataQueue = undefined;
      setTimeout(function() {
        openCallback("All ok");
      }, 500);
    }).catch(function(error) {
      console.log('BT> ERROR: ' + error);
      if (btServer) {
        // we should have this, but chromebook doesn't seem to 
      if (btServer.disconnect) btServer.disconnect();
        btServer = undefined;
        txCharacteristic = undefined;
        rxCharacteristic = undefined;
      }      
      openCallback(undefined);
    });
  };
 
  var closeSerial=function() {
    if (btServer) {
      // we should have this, but chromebook doesn't seem to 
      if (btServer.disconnect) btServer.disconnect();
      btServer = undefined;
      txCharacteristic = undefined;
      rxCharacteristic = undefined;
    }
    connectionDisconnectCallback();
  };

  // Throttled serial write
{'fraction_non_alphanumeric': 0.12773722627737227, 'fraction_numerical': 0.017726798748696558, 'mean_word_length': 3.2604108828428653, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6410240', 'n_tokens_mistral': 2448, 'n_tokens_neox': 2359, 'n_words': 496}