Skip to content

Commit

Permalink
add servo support
Browse files Browse the repository at this point in the history
added servo support , used @chnewso 's fork for reference
tested on BealeBone Blue BeagleBoard.org Debian Image 2018-03-05
Linux beaglebone 4.9.105-ti-r113
rc_version : 0.4.4
fix indendation issues
  • Loading branch information
vaishnavachath committed Jul 18, 2018
1 parent 3451a61 commit 4216a54
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
10 changes: 5 additions & 5 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
{
"target_name": "roboticscape",
"variables": {
'RC_version': '<!(rc_version)',
'RC_version': '<!(rc_version)'
},
"conditions": [
['RC_version=="0.3.4"', {
'sources': [
'rc-node-bindings034.cc'
],
'sources': [
'rc-node-bindings034.cc'
]
}],
['RC_version!="0.3.4"', {
'sources': [
'rc-node-bindings041.cc'
],
]
}]
],
"cflags": [ "-Wall", "-g", "-std=c++1y" ],
Expand Down
41 changes: 41 additions & 0 deletions rc-node-bindings041.cc
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,44 @@ namespace rc {
info.GetReturnValue().Set(value);
}

void RCservo(const Nan::FunctionCallbackInfo<v8::Value>& info) {
int result = 0;
if (info.Length() == 1) {
if (info[0]->IsString()) {
v8::String::Utf8Value str(info[0]->ToString());
char * s = (char *)*str;
if (!strcmp(s, "ENABLE")) result = rc_servo_init();
else if (!strcmp(s, "POWER_RAIL_ENABLE")) result = rc_servo_power_rail_en(1);
else if (!strcmp(s, "POWER_RAIL_DISABLE")) result = rc_servo_power_rail_en(0);
else if (!strcmp(s, "DISABLE")) result = rc_servo_cleanup();
else {
Nan::ThrowTypeError("Wrong value (should be "\
"'ENABLE', 'DISABLE' "\
"'POWER_RAIL_ENABLE', 'POWER_RAIL_DISABLE' or a numeric channel no)");
}
info.GetReturnValue().Set(result);
}
else {
Nan::ThrowTypeError("Wrong type for argument (should be String)");
return;
}
}
else {
if (!info[0]->IsInt32()) {
Nan::ThrowTypeError("Wrong type for argument (should be integer)");
return;
}
int channel = (int)info[0]->ToInt32()->Value();
float value = (float)info[1]->ToNumber()->Value();
if (channel < 0 || channel > 8) {
Nan::ThrowTypeError("Wrong value for argument 1 (should be between 0 and 8)");
return;
}
result = rc_servo_send_pulse_normalized(channel, value );
info.GetReturnValue().Set(result);
}
}

void ModuleInit(v8::Local<v8::Object> exports) {
/* Init and Cleanup */
exports->Set(Nan::New("initialize").ToLocalChecked(),
Expand All @@ -352,6 +390,9 @@ namespace rc {
/* ADC */
exports->Set(Nan::New("adc").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(RCadc)->GetFunction());
/* Servo */
exports->Set(Nan::New("servo").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(RCservo)->GetFunction());
node::AtExit(RCexit);
}

Expand Down

0 comments on commit 4216a54

Please sign in to comment.