volantus/mcp3008

用于从模数转换器 MCP3008 读取值的 PHP 库

0.1.2 2018-03-21 21:12 UTC

This package is auto-updated.

Last update: 2024-09-22 21:46:25 UTC


README

Latest Stable Version Build status

用于从模数转换器 MCP3008 读取值的 PHP 库

依赖

该库需要安装以下依赖之一

安装

可以使用 Composer 安装此库

composer require volantus/mcp3008

用法

所有测量都通过 Reader 类进行。

构造函数需要一个 SpiInterface 对象和 ADC 引用电压,默认为树莓派上的 3.3V。

使用 Pigpio 守护进程(套接字)

如果您想使用原生 SPI 通道(推荐),可以使用 RegularSpiDevice

use Volantus\MCP3008\Reader;
use Volantus\Pigpio\Client;
use Volantus\Pigpio\SPI\RegularSpiDevice;

$spiInterface = new RegularSpiDevice(new Client(), 1, 32000, 0);
$reader = new Reader($spiInterface, 3.3);

如果您想使用任何其他 GPIO 引脚,请使用 BitBaningSpiDevice

use Volantus\MCP3008\Reader;
use Volantus\Pigpio\Client;
use Volantus\Pigpio\SPI\BitBangingSpiDevice;

$spiInterface = new BitBangingSpiDevice(new Client(), 12, 16, 20, 21, 32000);
$reader = new Reader($spiInterface, 3.3);

通过 berry-spi 扩展进行直接通信

如果您想使用原生 SPI 通道(推荐),可以使用 RegularInterface

use Volantus\BerrySpi\RegularInterface;
use Volantus\MCP3008\Reader;

$spiInterface = new RegularInterface(1, 32000, 0);
$reader = new Reader($spiInterface, 3.3);

如果您想使用任何其他 GPIO 引脚,请使用 BitBangingInterface

use Volantus\BerrySpi\BitBangingInterface;
use Volantus\MCP3008\Reader;

$spiInterface = new BitBangingInterface(12, 16, 20, 21, 32000, 0);
$reader = new Reader($spiInterface, 3.3);

读取值

通过通道读取值,返回值为 Measurement 对象

// Reading value of ADC channel 4
$value = $reader->read(4);

// Getting the raw value, e.g. 789
$value->getRawValue();

// Getting the calculated voltage depending on reference voltage
// e.g. 2.54V in case of 3.3V ref. voltage 
$value->calculateVoltage();

SPI 打开/关闭行为

默认情况下,读者在构造函数中打开 SPI 接口,并在析构函数中关闭它。但仅在需要时,因此您可以自行控制它

$spiInterface = new RegularInterface(1, 32000, 0);
$spiInterface->open();

$reader = new Reader($spiInterface, 3.3);

$spiInterface->close();
unset($reader);

贡献

以错误报告、建议或 pull 请求的形式进行的贡献非常受欢迎!