1-wire-js 中文文档教程

发布于 9年前 浏览 24 项目主页 更新于 3年前

1-Wire® for Javascript

Project

使用 chrome.udb 在 Chrome 的 Javascript 中实现 1-Wire® 通信。

Getting Started

您有以下选项可以开始:

  • Download the latest release
  • Clone the repo: git clone git://github.com/KeiserCorp/1-Wire-JS.git
  • Install with NPM: npm install 1-wire-js

Loading

每个版本都包含一个缩小的库分发版本,可以使用模块加载器加载,或者作为一个独立的库。

使用 CommonJS 模块加载库:

var ow = require('1-wire-js');

将库作为独立库包含在内:

<script src="ow.min.js"></script>
var ow = window.ow;

API

所有 API 使用 Q promise 库,因此大多数函数返回一个具有 .then() 方法的 promise。 .then() 方法接受两个回调。 第一个回调在成功时调用,第二个回调在失败时调用。

ow.permission.request().then(success, failure);

Permission

permission.check()

检查 Chrome 以获取访问 USB 设备的权限。

ow.permission.check().then(gotPermission);

permission.request()

请求 Chrome 获得访问 USB 设备的权限。 方法必须由用户事件(例如按下按钮)激活。

ow.permission.request().then(gotPermission, failedPermission);

Device

device.open()

尝试打开 USB 设备。

ow.device.open().then(deviceOpened);

device.close()

尝试关闭 USB 设备。

ow.device.close().then(deviceClosed);

device.onDeviceAdded

添加 USB 设备时触发的事件侦听器。

addListener(callback) 向事件侦听器添加回调。

removeListener(callback) 从事件侦听器中删除回调。

ow.device.onDeviceAdded.addListener(deviceConnected);

device.onDeviceRemoved

移除 USB 设备时触发的事件侦听器。

addListener(callback) 向事件侦听器添加回调。

removeListener(callback) 从事件侦听器中删除回调。

ow.device.onDeviceRemoved.addListener(deviceRemoved);

device.reset()

执行设备重置,重置设备速度并取消所有操作。

ow.device.reset().then(deviceReady);

device.getStatus()

将设备状态寄存器对象传递给回调。

ow.device.getStatus()
  .then(function (status) {
    if (status.ResultRegisters.DetectKey){
      console.log('Key Detected');
    }
  });

Transfer

transferInfo

传递给某些传输方法的通用传输对象。

direction 是传输方向(“in”“out”)。

endpoint 是目标端点地址。

length (可选) 是要接收的数据量(仅输入传输需要)。

data (可选) 是要传输的数据(仅输出传输需要)。

var transferInfo = {
  "direction": "in",
  "endpoint": 1,
  "length": 0x20
};

device.interruptTransfer()

执行设备中断传输。

ow.device.interruptTransfer().then(interruptTransferComplete);

device.controlTransfer(transferInfo)

执行设备控制传输。

ow.device.controlTransfer(transferInfo).then(controlTransferComplete);

device.bulkTransfer(transferInfo)

执行设备批量传输。

ow.device.bulkTransfer(transferInfo).then(bulkTransferComplete);

Wire

wire.detectShort()

检测线路短路并将结果传递给回调。

ow.wire.detectShort()
  .then(function (shorted) {
    if (shorted) {
      throw new Error("Short Detected");
    }
  });

wire.setSpeed(overdrive)

根据传入的布尔值 overdrive 将速度设置为正常或超速;

ow.wire.setSpeed(true).then(speedSet);

wire.rest()

发送重置,然后检查电线是否短路。

ow.wire.rest().then(resetComplete);

wire.write(data, clearWire)

将数据写入线路。

data 必须是 Uint8Array 类型,否则可能会丢失数据。

true 值作为 clearWire 参数传递,以便在写入操作后清除线路。

ow.wire.write(data).then(writeComplete);

wire.writeBit(bit)

将一个位写入线上。

ow.wire.writeBit(bit).then(writeBitComplete);

wire.read(byteCount)

从线路中读取一段由 byteCount 定义的数据并将其传递给回调。

ow.wire.read(0x20)
  .then(function(data){
    storeData(data);
  });

wire.readBit()

从线路中读取一位数据并将其传递给回调。

ow.wire.readBit()
  .then(function(bitSet){
    test = bitSet;
  });

wire.clearByte()

从线路中清除一个字节的数据。

ow.wire.clearByte().then(wireCleared);

Key

key.romCommand(match, keyRom, overdrive)

在网络上执行密钥 ROM 匹配操作。

match 确定命令是否应针对特定的关键 ROM (true) 或命令是否应针对所有设备 (false)。

如果 matchtruekeyRom 应包含目标设备的 ROM。

如果命令应该以超速执行,overdrive 应该设置为 true

ow.key.romCommand(true, keyRom, true).then(keyRomMatched);

key.romMatch(keyRom)

以正常速度执行密钥 ROM 匹配。

ow.key.romCommand(true, keyRom, false)

ow.key.romMatch(keyRom).then(keyRomMatched);

key.romMatchOverdrive(keyRom)

的别名以超速执行关键 ROM 匹配。

ow.key.romCommand(true, keyRom, true)

ow.key.romMatch(keyRom).then(keyRomMatched);

key.romSkip()

的别名以正常速度执行密钥 ROM 跳过。

ow.key.romCommand(false, null, false)

ow.key.romSkip().then(keyRomSkipped);

key.romSkipOverdrive()

的别名以超速执行关键 ROM 跳过。

ow.key.romCommand(false, null, true) 的别名在

ow.key.romSkipOverdrive().then(keyRomSkipped);

key.searchFirst()

网络中搜索密钥并将第一个密钥 ROM 传递给回调。

ow.key.searchFirst()
  .then(function(rom){
      keyROM = rom;
  });

key.searchNext()

在网络中搜索密钥并将下一个密钥 ROM 传递给回调。

此方法将遍历密钥 ROM

ow.key.searchNext()
  .then(function(rom){
      keyRom = rom;
  });

key.readAll(keyRom, overdrive)

keyRom 所针对的密钥中读取所有数据并将数据传递给回调。

keyRom 是存储为Uint8Array 的目标密钥ROM。

overdrive 是一个决定运行速度的布尔值。 (默认值:false)

ow.key.readAll(keyRom, true)
  .then(function(data){
      keyData = data;
  });

key.write(keyRom, offset, data, overdrive)

data 写入keyRom 的目标键。

keyRom 是存储为Uint8Array 的目标密钥ROM。

offset 是要写入数据的内存偏移量。

data 是要写入存储为 Uint8Array 的密钥存储器的数据。

overdrive 是一个决定运行速度的布尔值。 (默认值:false)

ow.key.write(keyRom, 0x00, data, true).then(writeComplete);

key.writeAll(keyRom, data, overdrive)

data 写入 keyRom 的目标键,从内存开始。

keyRom 是存储为Uint8Array 的目标密钥ROM。

data 是要写入存储为 Uint8Array 的密钥存储器的数据。

overdrive 是一个决定运行速度的布尔值。 (默认值:false)

ow.key.writeAll(keyRom, data, true).then(writeComplete);

key.writeDiff(keyRom, newData, oldData, overdrive)

newData 写入 keyRom 的目标密钥,从内存开始使用差异算法来加速写入。

keyRom 是存储为Uint8Array 的目标密钥ROM。

newData 是要写入存储为 Uint8Array 的密钥存储器的数据。

oldData 是存储为Uint8Array 的当前密钥内存。

overdrive 是一个决定运行速度的布尔值。 (默认值:false)

ow.key.writeAll(keyRom, newData, lastDump, true).then(writeComplete);

Contributors

Copyright and License

根据 MIT 许可证 获得许可。

1-Wire® for Javascript

Project

1-Wire® Communication implemented in Javascript for Chrome using chrome.udb.

Getting Started

You have following options to get started:

  • Download the latest release
  • Clone the repo: git clone git://github.com/KeiserCorp/1-Wire-JS.git
  • Install with NPM: npm install 1-wire-js

Loading

Each release includes a minified distribution version of the library which can be loaded with a module loader, or as a stand alone library.

Module load the library with CommonJS:

var ow = require('1-wire-js');

Including the library as a stand-alone library:

<script src="ow.min.js"></script>
var ow = window.ow;

API

All APIs utilize the Q promise library, so most functions return a promise which has a .then() method. .then() methods accept two callbacks. The first callback is called on success, and the second is called on failure.

ow.permission.request().then(success, failure);

Permission

permission.check()

Checks Chrome for permission to access USB device.

ow.permission.check().then(gotPermission);

permission.request()

Requests Chrome for permission to access USB device. Method must be activated by a user event (such as a button press).

ow.permission.request().then(gotPermission, failedPermission);

Device

device.open()

Attempts to open the USB device.

ow.device.open().then(deviceOpened);

device.close()

Attempts to close the USB device.

ow.device.close().then(deviceClosed);

device.onDeviceAdded

Event listener which triggers upon the addition of a USB device.

addListener(callback) adds a callback to the event listener.

removeListener(callback) removes a callback from the event listener.

ow.device.onDeviceAdded.addListener(deviceConnected);

device.onDeviceRemoved

Event listener which triggers upon the removal of a USB device.

addListener(callback) adds a callback to the event listener.

removeListener(callback) removes a callback from the event listener.

ow.device.onDeviceRemoved.addListener(deviceRemoved);

device.reset()

Performs a device reset which resets device speed and cancels all actions.

ow.device.reset().then(deviceReady);

device.getStatus()

Passes device state registers object into callback.

ow.device.getStatus()
  .then(function (status) {
    if (status.ResultRegisters.DetectKey){
      console.log('Key Detected');
    }
  });

Transfer

transferInfo

A generic transfer object passed to some transfer methods.

direction is the transfer direction ("in" or "out").

endpoint is the target endpoint address.

length (optional) is the amount of data to receive (required only by input transfers).

data (optional) is the data to transmit (required only by output transfers).

var transferInfo = {
  "direction": "in",
  "endpoint": 1,
  "length": 0x20
};

device.interruptTransfer()

Performs a device interrupt transfer.

ow.device.interruptTransfer().then(interruptTransferComplete);

device.controlTransfer(transferInfo)

Performs a device control transfer.

ow.device.controlTransfer(transferInfo).then(controlTransferComplete);

device.bulkTransfer(transferInfo)

Performs a device bulk transfer.

ow.device.bulkTransfer(transferInfo).then(bulkTransferComplete);

Wire

wire.detectShort()

Detects short in the line and passes the result into callback.

ow.wire.detectShort()
  .then(function (shorted) {
    if (shorted) {
      throw new Error("Short Detected");
    }
  });

wire.setSpeed(overdrive)

Sets the speed to either normal or overdrive based on passed in boolean value overdrive;

ow.wire.setSpeed(true).then(speedSet);

wire.rest()

Sends a reset and then checks for a wire short.

ow.wire.rest().then(resetComplete);

wire.write(data, clearWire)

Writes data onto the wire.

data must be type Uint8Array or data loss may occur.

Pass a true value as the clearWire parameter to have the wire cleared after the write operation.

ow.wire.write(data).then(writeComplete);

wire.writeBit(bit)

Writes a single bit onto the wire.

ow.wire.writeBit(bit).then(writeBitComplete);

wire.read(byteCount)

Read a length of data defined by byteCount from the wire and passes it to callback.

ow.wire.read(0x20)
  .then(function(data){
    storeData(data);
  });

wire.readBit()

Reads a single bit of data from the wire and passes it to callback.

ow.wire.readBit()
  .then(function(bitSet){
    test = bitSet;
  });

wire.clearByte()

Clears a single byte of data from the wire.

ow.wire.clearByte().then(wireCleared);

Key

key.romCommand(match, keyRom, overdrive)

Performs a key ROM match operation on the network.

match determines if commands should target a specific key ROM (true) or if commands should target all devices (false).

keyRom should contain the ROM of the device being targeted if match is true.

overdrive should be set to true if commands should be performed in overdrive speed.

ow.key.romCommand(true, keyRom, true).then(keyRomMatched);

key.romMatch(keyRom)

Performs a key ROM match at normal speed.

Alias for ow.key.romCommand(true, keyRom, false)

ow.key.romMatch(keyRom).then(keyRomMatched);

key.romMatchOverdrive(keyRom)

Performs a key ROM match at overdrive speed.

Alias for ow.key.romCommand(true, keyRom, true)

ow.key.romMatch(keyRom).then(keyRomMatched);

key.romSkip()

Performs a key ROM skip at normal speed.

Alias for ow.key.romCommand(false, null, false)

ow.key.romSkip().then(keyRomSkipped);

key.romSkipOverdrive()

Performs a key ROM skip at overdrive speed.

Alias for ow.key.romCommand(false, null, true)

ow.key.romSkipOverdrive().then(keyRomSkipped);

key.searchFirst()

Searches network for keys and passes the first key ROM to the callback.

ow.key.searchFirst()
  .then(function(rom){
      keyROM = rom;
  });

key.searchNext()

Searches network for keys and passes the next key ROM to the callback.

This method will loop through key ROMs

ow.key.searchNext()
  .then(function(rom){
      keyRom = rom;
  });

key.readAll(keyRom, overdrive)

Reads all of the data from the key targeted by keyRom and passes the data to the callback.

keyRom is the target key ROM stored as Uint8Array.

overdrive is a boolean value determining operation speed. (Default: false)

ow.key.readAll(keyRom, true)
  .then(function(data){
      keyData = data;
  });

key.write(keyRom, offset, data, overdrive)

Writes data to the key targeted by keyRom.

keyRom is the target key ROM stored as Uint8Array.

offset is the memory offset where the data is to be written.

data is the data to be written to the key memory stored as Uint8Array.

overdrive is a boolean value determining operation speed. (Default: false)

ow.key.write(keyRom, 0x00, data, true).then(writeComplete);

key.writeAll(keyRom, data, overdrive)

Writes data to the key targeted by keyRom starting at the memory beginning.

keyRom is the target key ROM stored as Uint8Array.

data is the data to be written to the key memory stored as Uint8Array.

overdrive is a boolean value determining operation speed. (Default: false)

ow.key.writeAll(keyRom, data, true).then(writeComplete);

key.writeDiff(keyRom, newData, oldData, overdrive)

Writes newData to the key targeted by keyRom starting at the memory beginning using a diffing algorithm to speed up writes.

keyRom is the target key ROM stored as Uint8Array.

newData is the data to be written to the key memory stored as Uint8Array.

oldData is the current key memory stored as Uint8Array.

overdrive is a boolean value determining operation speed. (Default: false)

ow.key.writeAll(keyRom, newData, lastDump, true).then(writeComplete);

Contributors

Copyright and License

Licensed under the MIT license.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文