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デバイスを開く
|
|
|
|
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-08-29 14:44:58 +00:00
|
|
|
unsigned int gpioDev = info[0].As<Number>().DoubleValue();
|
|
|
|
return Number::New(env, lgGpiochipOpen(gpioDev));
|
2020-01-21 07:10:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
// gpiochipデバイスを閉じる
|
|
|
|
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-08-29 14:44:58 +00:00
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
return Number::New(env, lgGpiochipClose(handle));
|
2020-01-21 07:10:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
// GPIO のモードを出力にする(ことを要求?)
|
|
|
|
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-08-29 14:44:58 +00:00
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
unsigned int gpio = info[1].As<Number>().DoubleValue();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgGpioClaimOutput(handle, 0, gpio, 0)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
// GPIO のモードを入力にする(ことを要求?)
|
|
|
|
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-08-29 14:44:58 +00:00
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
unsigned int gpio = info[1].As<Number>().DoubleValue();
|
|
|
|
unsigned int lflag = info[2].As<Number>().DoubleValue();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgGpioClaimInput(handle, lflag, gpio)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
// GPIOの電圧を読む
|
|
|
|
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-08-29 14:44:58 +00:00
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
unsigned int gpio = info[1].As<Number>().DoubleValue();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgGpioRead(handle, gpio)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
// GPIO の電圧をセットする
|
|
|
|
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-08-29 14:44:58 +00:00
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
unsigned int gpio = info[1].As<Number>().DoubleValue();
|
|
|
|
unsigned int level = info[2].As<Number>().DoubleValue();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgGpioWrite(handle, gpio, level)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:44:58 +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-08-29 14:44:58 +00:00
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
unsigned int gpio = info[1].As<Number>().DoubleValue();
|
|
|
|
unsigned int pulseWidth = info[2].As<Number>().DoubleValue();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +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を設定して出力
|
|
|
|
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-08-29 14:44:58 +00:00
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
unsigned int gpio = info[1].As<Number>().DoubleValue();
|
|
|
|
unsigned int pwmFrequency = info[2].As<Number>().DoubleValue();
|
|
|
|
unsigned int pwmDutyCycle = info[3].As<Number>().DoubleValue();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgTxPwm(handle, gpio, pwmFrequency, pwmDutyCycle, 0,0)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// シリアルポートを開く
|
2021-08-29 14:44:58 +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();
|
2020-01-21 07:10:10 +00:00
|
|
|
// char *c = new char[ser_tty.size()+1];
|
|
|
|
// std::strcpy(c, ser_tty.c_str());
|
|
|
|
// &ser_tty[0] で参照できるらしいけど危険?
|
|
|
|
|
|
|
|
unsigned int baud = info[1].As<Number>().DoubleValue();
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgSerialOpen(&tty[0], baud, 0)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// シリアルポートを閉じる
|
2021-08-29 14:44:58 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgSerialClose(handle)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
// シリアルデバイスからデータを受け取る
|
2020-01-21 07:10:10 +00:00
|
|
|
Value SerialRead(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();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
unsigned int count = info[1].As<Number>().DoubleValue();
|
2021-08-29 14:44:58 +00:00
|
|
|
char rxBuf[count+1];
|
2020-01-21 07:10:10 +00:00
|
|
|
for (unsigned int i = 0; i <= count; i++)
|
|
|
|
{
|
2021-08-29 14:44:58 +00:00
|
|
|
rxBuf[i] = 0;
|
2020-01-21 07:10:10 +00:00
|
|
|
}
|
2021-08-29 14:44:58 +00:00
|
|
|
lgSerialRead(handle, rxBuf, count);
|
2020-01-21 07:10:10 +00:00
|
|
|
return String::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
rxBuf
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
// シリアルデバイスにバイト列を送る(data: string)
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
if (!info[0].IsNumber() || !info[1].IsString())
|
|
|
|
{
|
|
|
|
TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
|
|
|
|
return env.Null();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
2021-08-29 14:44:58 +00:00
|
|
|
std::string txBuf = info[1].As<String>().Utf8Value();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
unsigned int count = txBuf.length();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgSerialWrite(handle, &txBuf[0], count)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2Cバスアドレスのデバイスのハンドルを返す
|
2021-08-29 14:44:58 +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-08-29 14:44:58 +00:00
|
|
|
unsigned int i2cDev = info[0].As<Number>().DoubleValue();
|
|
|
|
unsigned int i2cAddr = info[1].As<Number>().DoubleValue();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgI2cOpen(i2cDev, i2cAddr, 0)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// オープン済みI2Cハンドルを閉じる
|
2021-08-29 14:44:58 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgI2cClose(handle)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-07 12:18:50 +00:00
|
|
|
// デバイスに1バイトを送る
|
|
|
|
Value i2cWriteByte(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();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
unsigned int byteVal = info[1].As<Number>().DoubleValue();
|
|
|
|
|
|
|
|
return Number::New(env,
|
|
|
|
lgI2cWriteByte(handle, byteVal)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// デバイスから1バイトを受け取る
|
|
|
|
Value i2cReadByte(const CallbackInfo& info)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
|
|
|
|
return Number::New(env,
|
|
|
|
lgI2cReadByte(handle)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-21 07:10:10 +00:00
|
|
|
// I2Cハンドルに関連付けられているデバイスの指定されたレジスタに1バイトを書き込む
|
2021-08-29 14:44:58 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
2021-08-29 14:44:58 +00:00
|
|
|
unsigned int i2cReg = info[1].As<Number>().DoubleValue();
|
|
|
|
unsigned int byteVal = info[2].As<Number>().DoubleValue();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgI2cWriteByteData(handle, i2cReg, byteVal)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// I2Cハンドルに関連付けられているデバイスの指定されたレジスタから1バイトを読み込む
|
2021-08-29 14:44:58 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
2021-08-29 14:44:58 +00:00
|
|
|
unsigned int i2cReg = info[1].As<Number>().DoubleValue();
|
2020-01-21 07:10:10 +00:00
|
|
|
|
|
|
|
return Number::New(env,
|
2021-08-29 14:44:58 +00:00
|
|
|
lgI2cReadByteData(handle, i2cReg)
|
2020-01-21 07:10:10 +00:00
|
|
|
);
|
|
|
|
}
|
2021-08-29 14:44:58 +00:00
|
|
|
/*
|
2020-01-21 07:10:10 +00:00
|
|
|
// I2Cハンドルに関連付けられているデバイスの指定されたレジスタから単一の16ビットワードを読み取る
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int handle = info[0].As<Number>().DoubleValue();
|
|
|
|
unsigned int i2c_reg = info[1].As<Number>().DoubleValue();
|
|
|
|
|
|
|
|
return Number::New(env,
|
|
|
|
i2c_read_word_data(pi, handle, i2c_reg)
|
|
|
|
);
|
|
|
|
}
|
2021-08-29 14:44:58 +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));
|
|
|
|
|
|
|
|
// exports.Set(String::New(env, "i2c_read_word_data"), Function::New(env, I2cReadWordData));
|
|
|
|
|
2020-01-21 07:10:10 +00:00
|
|
|
return exports;
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:44:58 +00:00
|
|
|
NODE_API_MODULE( ocoge_lgpio, Init )
|