Exemple #1
0
v8::Handle<v8::Value> GPIO::SetMode(const v8::Arguments& args) {
    v8::HandleScope scope;
    GPIO* obj = ObjectWrap::Unwrap<GPIO>(args.This());
    if (!obj->opened) {
        std::string err_msg = "GPIO " + obj->pin_num + " is not opened.";
        v8::ThrowException(v8::Exception::Error(v8::String::New(err_msg.c_str())));
        return scope.Close(v8::Undefined());
    }
    if (args.Length() < 1) {
        v8::ThrowException(v8::Exception::TypeError(v8::String::New("Wrong number of arguments")));
        return scope.Close(v8::Undefined());
    }
    if (!args[0]->IsString()) {
        v8::ThrowException(v8::Exception::TypeError(v8::String::New("Wrong arguments")));
        return scope.Close(v8::Undefined());
    }
    std::string mode(*v8::String::Utf8Value(args[0]));
    int res = obj->setMode(mode);
    if (res < 0) {
        std::string err_msg = "OPERATION FAILED: Unable to change GPIO " + obj->pin_num + " mode.";
        v8::ThrowException(v8::Exception::Error(v8::String::New(err_msg.c_str())));
        return scope.Close(v8::Undefined());
    }
    obj->log("GPIO " + obj->pin_num + " mode changed to " + mode + ".");
    return scope.Close(v8::Integer::New(res));
}
Exemple #2
0
v8::Handle<v8::Value> GPIO::Close(const v8::Arguments& args)
{
    v8::HandleScope scope;
    GPIO* obj = ObjectWrap::Unwrap<GPIO>(args.This());
    int res = obj->close();
    if (res < 0) {
        std::string err_msg = "OPERATION FAILED: Unable to close GPIO " + obj->pin_num + ".";
        v8::ThrowException(v8::Exception::Error(v8::String::New(err_msg.c_str())));
        return scope.Close(v8::Undefined());
    }
    obj->log("GPIO " + obj->pin_num + " closed.");
    return scope.Close(v8::Integer::New(res));
}
Exemple #3
0
v8::Handle<v8::Value> GPIO::Read(const v8::Arguments& args) {
    v8::HandleScope scope;
    GPIO* obj = ObjectWrap::Unwrap<GPIO>(args.This());
    if (!obj->opened) {
        std::string err_msg = "GPIO " + obj->pin_num + " is not opened.";
        v8::ThrowException(v8::Exception::Error(v8::String::New(err_msg.c_str())));
        return scope.Close(v8::Undefined());
    }
    if (obj->mode == GPIO::OUT) {
        std::string err_msg = "GPIO " + obj->pin_num + " is not in the right mode";
        v8::ThrowException(v8::Exception::Error(v8::String::New(err_msg.c_str())));
        return scope.Close(v8::Undefined());
    }
    int res = obj->read();
    std::ostringstream ss;
    ss << res;
    if (res < 0) {
        std::string err_msg = "OPERATION FAILED: Unable to change GPIO " + obj->pin_num + " value.";
        v8::ThrowException(v8::Exception::Error(v8::String::New(err_msg.c_str())));
        return scope.Close(v8::Undefined());
    }
    obj->log("GPIO " + obj->pin_num + " value changed to " + ss.str() + ".");
    return scope.Close(v8::Integer::New(res));
}
Exemple #4
0
v8::Handle<v8::Value> GPIO::Write(const v8::Arguments& args) {
    v8::HandleScope scope;
    GPIO* obj = ObjectWrap::Unwrap<GPIO>(args.This());
    if (args.Length() < 1) {
        v8::ThrowException(v8::Exception::TypeError(v8::String::New("Wrong number of arguments")));
        return scope.Close(v8::Undefined());
    }
    if (!args[0]->IsNumber()) {
        v8::ThrowException(v8::Exception::TypeError(v8::String::New("Wrong arguments")));
        return scope.Close(v8::Undefined());
    }
    v8::Local<v8::Integer> param1 = args[0]->ToInteger();
    int value = param1->IntegerValue();
    int res = obj->write(value);
    if (res < 0) {
        std::string err_msg = "OPERATION FAILED: Unable to change GPIO " + obj->pin_num + " value.";
        v8::ThrowException(v8::Exception::Error(v8::String::New(err_msg.c_str())));
        return scope.Close(v8::Undefined());
    }
    std::ostringstream ss;
    ss << value;
    obj->log("GPIO " + obj->pin_num + " value changed to " + ss.str() + ".");
    return scope.Close(v8::Integer::New(res));
}