Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add readbytedata method #89

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/i2c.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class i2c extends EventEmitter
tick ->
callback err, data

readByteData: (position, callback) ->
@setAddress @address
wire.readByteData position, (err, data) ->
tick ->
callback err, data

readBytes: (cmd, len, callback) ->
@setAddress @address
wire.readBlock cmd, len, null, (err, actualBuffer) ->
Expand Down
28 changes: 28 additions & 0 deletions src/i2c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,32 @@ void ReadByte(const Nan::FunctionCallbackInfo<v8::Value>& info) {
info.GetReturnValue().Set(data);
}

void ReadByteData(const Nan::FunctionCallbackInfo<v8::Value>& info) {
Nan::HandleScope scope;

Local<Value> data;
Local<Value> err = Nan::New<Value>(Nan::Null());

int32_t position = info[0]->Int32Value();

int32_t res = i2c_smbus_read_byte_data(fd, position);

if (res == -1) {
err = Nan::Error(Nan::New("Cannot read device").ToLocalChecked());
} else {
data = Nan::New<Integer>(res);
}

if (info[1]->IsFunction()) {
const unsigned argc = 2;
Local<Function> callback = Local<Function>::Cast(info[1]);
Local<Value> argv[argc] = { err, data };
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, argc, argv);
}

info.GetReturnValue().Set(data);
}

void ReadBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
Nan::HandleScope scope;

Expand Down Expand Up @@ -287,6 +313,8 @@ void Init(Handle<Object> exports) {
Nan::New<v8::FunctionTemplate>(Read)->GetFunction());
exports->Set(Nan::New("readByte").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ReadByte)->GetFunction());
exports->Set(Nan::New("readByteData").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ReadByteData)->GetFunction());
exports->Set(Nan::New("readBlock").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ReadBlock)->GetFunction());

Expand Down