当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Processing I2C.read()用法及代码示例


Processing, 类I2C中的read()用法介绍。

用法

  • .read(len)

参数

  • len (int) 要读取的字节数

返回

  • byte[]

说明

从连接的设备读取字节



您必须在调用此函数之前调用beginTransmission()。此函数还结束当前传输并发送之前使用write() 排队的任何数据。没有必要在 read() 之后调用 endTransmission()

例子

import processing.io.*;
I2C compass;

void setup() {
  //printArray(I2C.list());
  compass = new I2C(I2C.list()[0]);
}

void draw() {
  // read the heading over I2C from a compass module
  // with address 33 (hex 0x21)
  compass.beginTransmission(0x21);
  // first send a command byte
  compass.write(0x41);
  // read in two bytes
  byte[] in = compass.read(2);

  // put bytes together to tenth of degrees
  // & 0xff makes sure the byte is not interpreted as a negative value
  int deg = (in[0] & 0xff) << 8 | (in[1] & 0xff);
  println((deg / 10.0));
}

相关用法


注:本文由纯净天空筛选整理自processing.org大神的英文原创作品 I2C.read()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。