Skip to content

Commit

Permalink
1.x cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
sombriks committed Feb 25, 2024
1 parent 64786c5 commit 8b9e46f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 101 deletions.
52 changes: 20 additions & 32 deletions src/chip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,82 +16,70 @@ NAN_MODULE_INIT(Chip::Init) {
}

Chip::Chip(const char *device) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
chip = gpiod_chip_open_lookup(device);
DOUT( "%s %s():%d %p\n", __FILE__, __FUNCTION__, __LINE__, chip);
if (!chip) Nan::ThrowError("Unable to open device");
}

Chip::~Chip() {
DOUT( "%s %s():%d %p\n", __FILE__, __FUNCTION__, __LINE__, chip);
if ( !chip) return;
DOUT( "%s %s():%d %p\n", __FILE__, __FUNCTION__, __LINE__, chip);
if (!chip) return;
gpiod_chip_close(chip);
DOUT( "%s %s():%d %p\n", __FILE__, __FUNCTION__, __LINE__, chip);
chip = NULL;
}

NAN_METHOD(Chip::New) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
if (info.IsConstructCall()) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Nan::Utf8String device(info[0]);
Chip *obj = new Chip(*device);
DOUT( "%s %s():%d %p\n", __FILE__, __FUNCTION__, __LINE__, obj);
if ( !obj->chip) return;
DOUT( "%s %s():%d %p\n", __FILE__, __FUNCTION__, __LINE__, obj);
if (!obj->chip) return;
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
const int argc = 1;
v8::Local<v8::Value> argv[argc] = {info[0]};
v8::Local<v8::Function> cons = Nan::New(constructor);
info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
}
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
}

NAN_METHOD(Chip::getNumberOfLines) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Chip *obj = Nan::ObjectWrap::Unwrap<Chip>(info.This());
if ( !obj->chip) {
Nan::ThrowError( "::getNumberOfLines() for chip==NULL");
if (!obj->chip) {
Nan::ThrowError("::getNumberOfLines() for chip==NULL");
return;
}
int ret = gpiod_chip_num_lines(obj->getNativeChip());
if(-1 == ret) {
Nan::ThrowError( "::getNumberOfLines() failed");
} else info.GetReturnValue().Set(ret);
if (-1 == ret) {
Nan::ThrowError("::getNumberOfLines() failed");
} else
info.GetReturnValue().Set(ret);
}

NAN_METHOD(Chip::getChipName) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Chip *obj = Nan::ObjectWrap::Unwrap<Chip>(info.This());
if ( !obj->chip) {
Nan::ThrowError( "::getChipName() for chip==NULL");
if (!obj->chip) {
Nan::ThrowError("::getChipName() for chip==NULL");
return;
}
const char *name = gpiod_chip_name(obj->getNativeChip());
if(!name) {
Nan::ThrowError( "::getChipName() failed");
} else info.GetReturnValue().Set(Nan::New<v8::String>(name).ToLocalChecked());
if (!name) {
Nan::ThrowError("::getChipName() failed");
} else
info.GetReturnValue().Set(Nan::New<v8::String>(name).ToLocalChecked());
}

NAN_METHOD(Chip::getChipLabel) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Chip *obj = Nan::ObjectWrap::Unwrap<Chip>(info.This());
if ( !obj->chip) {
Nan::ThrowError( "::getChipLabel() for chip==NULL");
if (!obj->chip) {
Nan::ThrowError("::getChipLabel() for chip==NULL");
return;
}
const char *label = gpiod_chip_label(obj->getNativeChip());
if(!label) {
Nan::ThrowError( "::getChipLabel() failed");
} else info.GetReturnValue().Set(Nan::New<v8::String>(label).ToLocalChecked());
if (!label) {
Nan::ThrowError("::getChipLabel() failed");
} else
info.GetReturnValue().Set(Nan::New<v8::String>(label).ToLocalChecked());
}

gpiod_chip *Chip::getNativeChip() {
DOUT( "%s %s():%d %p\n", __FILE__, __FUNCTION__, __LINE__, chip);
return chip;
}
12 changes: 2 additions & 10 deletions src/chip.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
#include <gpiod.h>
#include <nan.h>

#define USE_PRINTF 0

#if USE_PRINTF
#define DOUT(fmt,args...) printf(fmt,##args)
#else
#define DOUT(fmt,args...)
#endif

class Chip : public Nan::ObjectWrap {
public:
static NAN_MODULE_INIT(Init);
Expand All @@ -28,7 +20,7 @@ class Chip : public Nan::ObjectWrap {
static NAN_METHOD(New);
static Nan::Persistent<v8::Function> constructor;

gpiod_chip *chip;
gpiod_chip* chip;
};

#endif // CHIP_HH
#endif // CHIP_HH
100 changes: 41 additions & 59 deletions src/line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,165 +31,147 @@ NAN_MODULE_INIT(Line::Init) {
}

Line::Line(Chip *chip, unsigned int pin) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
line = gpiod_chip_get_line(chip->getNativeChip(), pin);
DOUT( "%s %s():%d %p\n", __FILE__, __FUNCTION__, __LINE__, line);
if (!line) Nan::ThrowError("Unable to open GPIO line ");
}

Line::~Line() {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
if ( !line) return;
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
if (!line) return;
gpiod_line_close_chip(line);
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
line = NULL;
}

NAN_METHOD(Line::New) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
if (info.IsConstructCall()) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Chip *chip = Nan::ObjectWrap::Unwrap<Chip>(Nan::To<v8::Object>(info[0]).ToLocalChecked());
unsigned int pin = Nan::To<unsigned int>(info[1]).FromJust();
Line *obj = new Line(chip, pin);
DOUT( "%s %s(%d):%d %p\n", __FILE__, __FUNCTION__, pin, __LINE__, obj);
if ( !obj->line) return;
DOUT( "%s %s(%d):%d %p->%p\n", __FILE__, __FUNCTION__, pin, __LINE__, obj, obj->line);
if (!obj->line) return;
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
const int argc = 1;
DOUT( "%s %s():%d !construct\n", __FILE__, __FUNCTION__, __LINE__);
v8::Local<v8::Value> argv[argc] = {info[0]};
v8::Local<v8::Function> cons = Nan::New(constructor);
info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
}
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
}

NAN_METHOD(Line::getLineOffset) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if ( !obj->line) {
Nan::ThrowError( "::getLineOffset() for line==NULL");
if (!obj->line) {
Nan::ThrowError("::getLineOffset() for line==NULL");
return;
}
int ret = gpiod_line_offset(obj->getNativeLine());
if(-1 == ret) {
Nan::ThrowError( "::getLineOffset() failed");
} else info.GetReturnValue().Set(ret);
if (-1 == ret) {
Nan::ThrowError("::getLineOffset() failed");
} else
info.GetReturnValue().Set(ret);
}

NAN_METHOD(Line::getLineName) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if ( !obj->line) {
Nan::ThrowError( "::getLineName() for line==NULL");
if (!obj->line) {
Nan::ThrowError("::getLineName() for line==NULL");
return;
}
const char *name = gpiod_line_name(obj->getNativeLine());
if(!name) info.GetReturnValue().Set(Nan::Undefined());
else info.GetReturnValue().Set(Nan::New<v8::String>(name).ToLocalChecked());
if (!name)
info.GetReturnValue().Set(Nan::Undefined());
else
info.GetReturnValue().Set(Nan::New<v8::String>(name).ToLocalChecked());
}

NAN_METHOD(Line::getLineConsumer) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if ( !obj->line) {
Nan::ThrowError( "::getLineConsumer() for line==NULL");
if (!obj->line) {
Nan::ThrowError("::getLineConsumer() for line==NULL");
return;
}
const char *name = gpiod_line_consumer(obj->getNativeLine());
if(!name) info.GetReturnValue().Set(Nan::Undefined());
else info.GetReturnValue().Set(Nan::New<v8::String>(name).ToLocalChecked());
if (!name)
info.GetReturnValue().Set(Nan::Undefined());
else
info.GetReturnValue().Set(Nan::New<v8::String>(name).ToLocalChecked());
}

NAN_METHOD(Line::getValue) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if ( !obj->line) {
Nan::ThrowError( "::getValue() for line==NULL");
if (!obj->line) {
Nan::ThrowError("::getValue() for line==NULL");
return;
}
int ret = gpiod_line_get_value(obj->getNativeLine());
if(-1 == ret) {
Nan::ThrowError( "::getValue() failed");
} else info.GetReturnValue().Set(ret);
if (-1 == ret) {
Nan::ThrowError("::getValue() failed");
} else
info.GetReturnValue().Set(ret);
}

NAN_METHOD(Line::setValue) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if ( !obj->line) {
Nan::ThrowError( "::setValue() for line==NULL");
if (!obj->line) {
Nan::ThrowError("::setValue() for line==NULL");
return;
}
unsigned int value = Nan::To<unsigned int>(info[0]).FromJust();
if(-1 == gpiod_line_set_value(obj->getNativeLine(), value))
Nan::ThrowError( "::setValue() failed");
if (-1 == gpiod_line_set_value(obj->getNativeLine(), value))
Nan::ThrowError("::setValue() failed");
}

NAN_METHOD(Line::requestInputMode) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if (!obj->line) {
Nan::ThrowError( "::requestInputMode() for line==NULL");
Nan::ThrowError("::requestInputMode() for line==NULL");
return;
}
Nan::Utf8String consumer(info[0]);
if (-1 == gpiod_line_request_input(obj->getNativeLine(), *consumer))
Nan::ThrowError( "::requestInputMode() failed");
Nan::ThrowError("::requestInputMode() failed");
}

NAN_METHOD(Line::requestInputModeFlags) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if (!obj->line) {
Nan::ThrowError( "::requestInputModeFlags() for line==NULL");
Nan::ThrowError("::requestInputModeFlags() for line==NULL");
return;
}
Nan::Utf8String consumer(info[0]);
int flags = Nan::To<int>(info[1]).FromJust();
if (-1 == gpiod_line_request_input_flags(obj->getNativeLine(), *consumer, flags))
Nan::ThrowError( "::requestInputModeFlags() failed");
Nan::ThrowError("::requestInputModeFlags() failed");
}

NAN_METHOD(Line::requestOutputMode) {
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
if (!obj->line) {
Nan::ThrowError( "::requestOutputMode() for line==NULL");
return;
}
Nan::ThrowError("::requestOutputMode() for line==NULL");
return;
}
unsigned int value = 0;
v8::Local<v8::Value> defaultValue = info[0];
if (!defaultValue->IsUndefined() && defaultValue->IsNumber()) {
unsigned int val = Nan::To<unsigned int>(defaultValue).FromJust();
if (val > 1) {
Nan::ThrowError( "::requestOutputMode() value is not in {0,1} range");
Nan::ThrowError("::requestOutputMode() value is not in {0,1} range");
return;
}
value = val;
}
DOUT( "%s %s():%d %p\n", __FILE__, __FUNCTION__, __LINE__, obj);

Nan::Utf8String consumer(info[1]);
if (-1 == gpiod_line_request_output(obj->getNativeLine(), *consumer, value))
Nan::ThrowError( "::requestOutputMode() failed");
Nan::ThrowError("::requestOutputMode() failed");
}


NAN_METHOD(Line::release) {
Line *obj = Nan::ObjectWrap::Unwrap<Line>(info.This());
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
if ( !obj->getNativeLine()) return;
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
if (!obj->getNativeLine()) return;
gpiod_line_release(obj->getNativeLine());
DOUT( "%s %s():%d\n", __FILE__, __FUNCTION__, __LINE__);
obj->line = NULL;
}

gpiod_line *Line::getNativeLine() {
DOUT( "%s %s():%d %p\n", __FILE__, __FUNCTION__, __LINE__, line);
return line;
}

0 comments on commit 8b9e46f

Please sign in to comment.