當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。