2021-08-29 14:44:58 +00:00
|
|
|
|
/** lgpio を Node.js から利用するモジュール ** */
|
|
|
|
|
/** 関数名・書式は lgpio Python に準拠 ******************* */
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
#include <napi.h>
|
2021-08-29 14:44:58 +00:00
|
|
|
|
#include <lgpio.h>
|
2020-01-21 07:10:10 +00:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
using namespace Napi;
|
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
|
// gpiochipデバイスを開く
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value gpiochipOpen(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 1)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int gpioDev = info[0].As<Number>().Int32Value();
|
2021-08-29 14:44:58 +00:00
|
|
|
|
return Number::New(env, lgGpiochipOpen(gpioDev));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
|
// gpiochipデバイスを閉じる
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value gpiochipClose(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 1)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
2021-08-29 14:44:58 +00:00
|
|
|
|
return Number::New(env, lgGpiochipClose(handle));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
|
// GPIO のモードを出力にする(ことを要求?)
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value gpioClaimOutput(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 2)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int gpio = info[1].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgGpioClaimOutput(handle, 0, gpio, 0));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
|
// GPIO のモードを入力にする(ことを要求?)
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value gpioClaimInput(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (info.Length() < 3)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber())
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int gpio = info[1].As<Number>().Int32Value();
|
|
|
|
|
int lflag = info[2].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgGpioClaimInput(handle, lflag, gpio));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
|
// GPIOの電圧を読む
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value gpioRead(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 2)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int gpio = info[1].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgGpioRead(handle, gpio));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
|
// GPIO の電圧をセットする
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value gpioWrite(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (info.Length() < 3)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber())
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int gpio = info[1].As<Number>().Int32Value();
|
|
|
|
|
int level = info[2].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgGpioWrite(handle, gpio, level));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
|
// サーボパルス幅をセットする
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value txServo(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (info.Length() < 3)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber())
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int gpio = info[1].As<Number>().Int32Value();
|
|
|
|
|
int pulseWidth = info[2].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgTxServo(handle, gpio, pulseWidth, 50, 0, 0));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
|
// PWMを設定して出力
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value txPwm(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (info.Length() < 4)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber() || !info[3].IsNumber())
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int gpio = info[1].As<Number>().Int32Value();
|
|
|
|
|
float pwmFrequency = info[2].As<Number>().FloatValue();
|
|
|
|
|
float pwmDutyCycle = info[3].As<Number>().FloatValue();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgTxPwm(handle, gpio, pwmFrequency, pwmDutyCycle, 0, 0));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// シリアルポートを開く
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value serialOpen(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (info.Length() < 2)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (!info[0].IsString() || !info[1].IsNumber())
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
|
std::string tty = info[0].As<String>().Utf8Value();
|
2021-09-18 14:29:06 +00:00
|
|
|
|
// char *c = new char[tty.size()+1];
|
|
|
|
|
// std::strcpy(c, tty.c_str());
|
2020-01-21 07:10:10 +00:00
|
|
|
|
// &ser_tty[0] で参照できるらしいけど危険?
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int baud = info[1].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgSerialOpen(tty.c_str(), baud, 0));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// シリアルポートを閉じる
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value serialClose(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 1)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgSerialClose(handle));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
|
// シリアルデバイスからデータを受け取る
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value SerialRead(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 2)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int count = info[1].As<Number>().Int32Value();
|
|
|
|
|
|
2021-09-22 12:35:08 +00:00
|
|
|
|
char rxBuf[count];
|
|
|
|
|
int rxCount = lgSerialRead(handle, rxBuf, count);
|
|
|
|
|
auto outBuf = Buffer<char>::Copy(env, rxBuf, rxCount);
|
|
|
|
|
|
|
|
|
|
return outBuf;
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
// シリアルデバイスにバイト列を送る(data: buffer)
|
|
|
|
|
Value serialWrite(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 2)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
2021-09-18 14:29:06 +00:00
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsBuffer())
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
auto txBuf = info[1].As<Buffer<char>>();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int count = txBuf.Length();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgSerialWrite(handle, txBuf.Data(), count));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// I2Cバスアドレスのデバイスのハンドルを返す
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value i2cOpen(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (info.Length() < 2)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
2021-08-29 14:44:58 +00:00
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber())
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int i2cDev = info[0].As<Number>().Int32Value();
|
|
|
|
|
int i2cAddr = info[1].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgI2cOpen(i2cDev, i2cAddr, 0));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// オープン済みI2Cハンドルを閉じる
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value i2cClose(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 1)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgI2cClose(handle));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-07 12:18:50 +00:00
|
|
|
|
// デバイスに1バイトを送る
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value i2cWriteByte(const CallbackInfo &info)
|
2021-09-07 12:18:50 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 2)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int byteVal = info[1].As<Number>().Int32Value();
|
2021-09-07 12:18:50 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgI2cWriteByte(handle, byteVal));
|
|
|
|
|
}
|
2021-09-07 12:18:50 +00:00
|
|
|
|
// デバイスから1バイトを受け取る
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value i2cReadByte(const CallbackInfo &info)
|
2021-09-07 12:18:50 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 1)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
2021-09-07 12:18:50 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgI2cReadByte(handle));
|
|
|
|
|
}
|
2021-09-07 12:18:50 +00:00
|
|
|
|
|
2020-01-21 07:10:10 +00:00
|
|
|
|
// I2Cハンドルに関連付けられているデバイスの指定されたレジスタに1バイトを書き込む
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value i2cWriteByteData(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 3)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int i2cReg = info[1].As<Number>().Int32Value();
|
|
|
|
|
int byteVal = info[2].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgI2cWriteByteData(handle, i2cReg, byteVal));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// I2Cハンドルに関連付けられているデバイスの指定されたレジスタから1バイトを読み込む
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value i2cReadByteData(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 2)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int i2cReg = info[1].As<Number>().Int32Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgI2cReadByteData(handle, i2cReg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// I2Cハンドルに関連付けられているデバイスの指定されたレジスタからcountバイトを読み込む。countは1~32。
|
|
|
|
|
Value i2cReadI2cBlockData(const CallbackInfo &info)
|
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 3)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int i2cReg = info[1].As<Number>().Int32Value();
|
|
|
|
|
int count = info[2].As<Number>().Int32Value();
|
|
|
|
|
|
2021-09-22 12:35:08 +00:00
|
|
|
|
char rxBuf[count];
|
|
|
|
|
int rxCount = lgI2cReadI2CBlockData(handle, i2cReg, rxBuf, count);
|
|
|
|
|
auto outBuf = Buffer<char>::Copy(env, rxBuf, rxCount);
|
2021-09-18 14:29:06 +00:00
|
|
|
|
|
2021-09-22 12:35:08 +00:00
|
|
|
|
return outBuf;
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
2021-09-18 14:29:06 +00:00
|
|
|
|
|
2020-01-21 07:10:10 +00:00
|
|
|
|
// I2Cハンドルに関連付けられているデバイスの指定されたレジスタから単一の16ビットワードを読み取る
|
2021-09-18 14:29:06 +00:00
|
|
|
|
Value I2cReadWordData(const CallbackInfo &info)
|
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 2)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int i2cReg = info[1].As<Number>().Int32Value();
|
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
|
|
|
|
lgI2cReadWordData(handle, i2cReg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// i2c デバイスからデータを受け取る
|
|
|
|
|
Value I2cReadDevice(const CallbackInfo &info)
|
2020-01-21 07:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 2)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsNumber())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
int count = info[1].As<Number>().Int32Value();
|
|
|
|
|
|
2021-09-22 12:35:08 +00:00
|
|
|
|
char rxBuf[count];
|
|
|
|
|
int rxCount = lgI2cReadDevice(handle, rxBuf, count);
|
|
|
|
|
auto outBuf = Buffer<char>::Copy(env, rxBuf, rxCount);
|
2021-09-18 14:29:06 +00:00
|
|
|
|
|
2021-09-22 12:35:08 +00:00
|
|
|
|
return outBuf;
|
2021-09-18 14:29:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// i2c デバイスにバイト列を送る(data: buffer)
|
|
|
|
|
Value I2cWriteDevice(const CallbackInfo &info)
|
|
|
|
|
{
|
|
|
|
|
Env env = info.Env();
|
|
|
|
|
if (info.Length() < 2)
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong number of arguments")
|
|
|
|
|
.ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsBuffer())
|
|
|
|
|
{
|
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
|
return env.Null();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int handle = info[0].As<Number>().Int32Value();
|
|
|
|
|
auto txBuf = info[1].As<Buffer<char>>();
|
|
|
|
|
|
|
|
|
|
int count = txBuf.Length();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-09-18 14:29:06 +00:00
|
|
|
|
lgI2cWriteDevice(handle, txBuf.Data(), count));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
}
|
2021-09-18 14:29:06 +00:00
|
|
|
|
|
2020-01-21 07:10:10 +00:00
|
|
|
|
Object
|
|
|
|
|
Init(Env env, Object exports)
|
|
|
|
|
{
|
2021-08-29 14:44:58 +00:00
|
|
|
|
exports.Set(String::New(env, "gpiochip_open"), Function::New(env, gpiochipOpen));
|
|
|
|
|
exports.Set(String::New(env, "gpiochip_close"), Function::New(env, gpiochipClose));
|
|
|
|
|
exports.Set(String::New(env, "gpio_claim_input"), Function::New(env, gpioClaimInput));
|
|
|
|
|
exports.Set(String::New(env, "gpio_claim_output"), Function::New(env, gpioClaimOutput));
|
|
|
|
|
exports.Set(String::New(env, "gpio_read"), Function::New(env, gpioRead));
|
|
|
|
|
exports.Set(String::New(env, "gpio_write"), Function::New(env, gpioWrite));
|
|
|
|
|
exports.Set(String::New(env, "tx_servo"), Function::New(env, txServo));
|
|
|
|
|
exports.Set(String::New(env, "tx_pwm"), Function::New(env, txPwm));
|
|
|
|
|
exports.Set(String::New(env, "serial_open"), Function::New(env, serialOpen));
|
|
|
|
|
exports.Set(String::New(env, "serial_close"), Function::New(env, serialClose));
|
2020-01-21 07:10:10 +00:00
|
|
|
|
exports.Set(String::New(env, "serial_read"), Function::New(env, SerialRead));
|
2021-08-29 14:44:58 +00:00
|
|
|
|
exports.Set(String::New(env, "serial_write"), Function::New(env, serialWrite));
|
|
|
|
|
exports.Set(String::New(env, "i2c_open"), Function::New(env, i2cOpen));
|
|
|
|
|
exports.Set(String::New(env, "i2c_close"), Function::New(env, i2cClose));
|
2021-09-07 12:18:50 +00:00
|
|
|
|
exports.Set(String::New(env, "i2c_write_byte"), Function::New(env, i2cWriteByte));
|
|
|
|
|
exports.Set(String::New(env, "i2c_read_byte"), Function::New(env, i2cReadByte));
|
2021-08-29 14:44:58 +00:00
|
|
|
|
exports.Set(String::New(env, "i2c_write_byte_data"), Function::New(env, i2cWriteByteData));
|
|
|
|
|
exports.Set(String::New(env, "i2c_read_byte_data"), Function::New(env, i2cReadByteData));
|
2021-09-18 14:29:06 +00:00
|
|
|
|
exports.Set(String::New(env, "i2c_read_i2c_block_data"), Function::New(env, i2cReadI2cBlockData));
|
|
|
|
|
exports.Set(String::New(env, "i2c_read_word_data"), Function::New(env, I2cReadWordData));
|
|
|
|
|
exports.Set(String::New(env, "i2c_read_device"), Function::New(env, I2cReadDevice));
|
|
|
|
|
exports.Set(String::New(env, "i2c_write_device"), Function::New(env, I2cWriteDevice));
|
2021-08-29 14:44:58 +00:00
|
|
|
|
|
2020-01-21 07:10:10 +00:00
|
|
|
|
return exports;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 14:29:06 +00:00
|
|
|
|
NODE_API_MODULE(lgpio, Init)
|