Implemeeeeeent
This commit is contained in:
8
rust_native_module/index.d.ts
vendored
8
rust_native_module/index.d.ts
vendored
@@ -1,6 +1,6 @@
|
||||
declare module rust_native_module {
|
||||
type Result<T> =
|
||||
{ type: "success" } & T
|
||||
{ type: "success", value: T }
|
||||
| { type: "error", message: string };
|
||||
|
||||
type Option<T> =
|
||||
@@ -26,8 +26,8 @@ declare module rust_native_module {
|
||||
}
|
||||
|
||||
type OutputHandle = {
|
||||
set: (heads: Array<MovingHeadState>) => Result<{}>,
|
||||
close: () => Result<{}>,
|
||||
set: (heads: Array<MovingHeadState>) => Result<never>,
|
||||
close: () => Result<never>,
|
||||
}
|
||||
|
||||
type BeatTrackerHandle = {
|
||||
@@ -36,7 +36,7 @@ declare module rust_native_module {
|
||||
}
|
||||
|
||||
function listPorts(): Array<string>;
|
||||
function openOutput(): Result<OutputHandle>;
|
||||
function openOutput(port: string): Result<OutputHandle>;
|
||||
|
||||
function getBeatTracker(): Result<BeatTrackerHandle>;
|
||||
}
|
||||
|
||||
@@ -11,19 +11,22 @@ impl Finalize for Metronome {}
|
||||
|
||||
pub fn get_beat_tracker(mut cx: FunctionContext) -> JsResult<JsObject> {
|
||||
let obj = cx.empty_object();
|
||||
let value_obj = cx.empty_object();
|
||||
|
||||
let boxed_tracker = cx.boxed(RefCell::new(Metronome::new(Duration::from_secs(2))));
|
||||
value_obj.set(&mut cx, "_rust_ptr", boxed_tracker)?;
|
||||
|
||||
let tap_function = JsFunction::new(&mut cx, tap)?;
|
||||
value_obj.set(&mut cx, "tap", tap_function)?;
|
||||
|
||||
let get_progress_function = JsFunction::new(&mut cx, get_progress)?;
|
||||
value_obj.set(&mut cx, "getProgress", get_progress_function)?;
|
||||
|
||||
obj.set(&mut cx, "value", value_obj)?;
|
||||
|
||||
let success_string = cx.string("success".to_string());
|
||||
obj.set(&mut cx, "type", success_string)?;
|
||||
|
||||
let boxed_tracker = cx.boxed(RefCell::new(Metronome::new(Duration::from_secs(2))));
|
||||
obj.set(&mut cx, "_rust_ptr", boxed_tracker)?;
|
||||
|
||||
let tap_function = JsFunction::new(&mut cx, tap)?;
|
||||
obj.set(&mut cx, "tap", tap_function)?;
|
||||
|
||||
let get_progress_function = JsFunction::new(&mut cx, get_progress)?;
|
||||
obj.set(&mut cx, "getProgress", get_progress_function)?;
|
||||
|
||||
Ok(obj)
|
||||
}
|
||||
|
||||
|
||||
@@ -29,31 +29,33 @@ pub fn open_output(mut cx: FunctionContext) -> JsResult<JsObject> {
|
||||
let path = path.value(&mut cx);
|
||||
|
||||
let obj = cx.empty_object();
|
||||
let success_string;
|
||||
let value_obj = cx.empty_object();
|
||||
|
||||
match Controller::new(path) {
|
||||
Ok(controller) => {
|
||||
success_string = cx.string("success");
|
||||
let success_string = cx.string("success");
|
||||
obj.set(&mut cx, "type", success_string)?;
|
||||
|
||||
let boxed_controller = cx.boxed(RefCell::new(controller));
|
||||
obj.set(&mut cx, "_rust_ptr", boxed_controller)?;
|
||||
value_obj.set(&mut cx, "_rust_ptr", boxed_controller)?;
|
||||
|
||||
let set_output_function = JsFunction::new(&mut cx, set_output)?;
|
||||
obj.set(&mut cx, "set", set_output_function)?;
|
||||
value_obj.set(&mut cx, "set", set_output_function)?;
|
||||
|
||||
let close_function = JsFunction::new(&mut cx, close_output)?;
|
||||
obj.set(&mut cx, "close", close_function)?;
|
||||
value_obj.set(&mut cx, "close", close_function)?;
|
||||
|
||||
obj.set(&mut cx, "value", value_obj)?;
|
||||
}
|
||||
Err(e) => {
|
||||
success_string = cx.string("error");
|
||||
let success_string = cx.string("error");
|
||||
obj.set(&mut cx, "type", success_string)?;
|
||||
|
||||
let error_message = cx.string(e.to_string());
|
||||
obj.set(&mut cx, "message", error_message)?;
|
||||
}
|
||||
}
|
||||
|
||||
obj.set(&mut cx, "type", success_string)?;
|
||||
|
||||
Ok(obj)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,63 +2,65 @@ const mod = require('.')
|
||||
|
||||
const o = mod.openOutput("/dev/ttyUSB0")
|
||||
|
||||
if (o.type !== "success") {
|
||||
return
|
||||
}
|
||||
if (o.type == 'success') {
|
||||
|
||||
const movingHeads = [
|
||||
{
|
||||
startAddress: 1,
|
||||
pan: 0,
|
||||
tilt: 0,
|
||||
brightness: {
|
||||
type: "dimmer",
|
||||
value: 1,
|
||||
const output = o.value;
|
||||
|
||||
const movingHeads = [
|
||||
{
|
||||
startAddress: 1,
|
||||
pan: 0,
|
||||
tilt: 0,
|
||||
brightness: {
|
||||
type: "dimmer",
|
||||
value: 1,
|
||||
},
|
||||
rgbw: [255, 0, 0, 0],
|
||||
speed: 0,
|
||||
reset: false,
|
||||
},
|
||||
rgbw: [255, 0, 0, 0],
|
||||
speed: 0,
|
||||
reset: false,
|
||||
},
|
||||
{
|
||||
startAddress: 15,
|
||||
pan: 0,
|
||||
tilt: 0,
|
||||
brightness: {
|
||||
type: "dimmer",
|
||||
value: 1,
|
||||
{
|
||||
startAddress: 15,
|
||||
pan: 0,
|
||||
tilt: 0,
|
||||
brightness: {
|
||||
type: "dimmer",
|
||||
value: 1,
|
||||
},
|
||||
rgbw: [255, 0, 0, 0],
|
||||
speed: 0,
|
||||
reset: false,
|
||||
},
|
||||
rgbw: [255, 0, 0, 0],
|
||||
speed: 0,
|
||||
reset: false,
|
||||
},
|
||||
{
|
||||
startAddress: 29,
|
||||
pan: 0,
|
||||
tilt: 0,
|
||||
brightness: {
|
||||
type: "dimmer",
|
||||
value: 1,
|
||||
{
|
||||
startAddress: 29,
|
||||
pan: 0,
|
||||
tilt: 0,
|
||||
brightness: {
|
||||
type: "dimmer",
|
||||
value: 1,
|
||||
},
|
||||
rgbw: [255, 0, 0, 0],
|
||||
speed: 0,
|
||||
reset: false,
|
||||
},
|
||||
rgbw: [255, 0, 0, 0],
|
||||
speed: 0,
|
||||
reset: false,
|
||||
},
|
||||
{
|
||||
startAddress: 43,
|
||||
pan: 0,
|
||||
tilt: 0,
|
||||
brightness: {
|
||||
type: "dimmer",
|
||||
value: 1,
|
||||
},
|
||||
rgbw: [0, 255, 0, 0],
|
||||
speed: 1,
|
||||
reset: false,
|
||||
}
|
||||
];
|
||||
{
|
||||
startAddress: 43,
|
||||
pan: 0,
|
||||
tilt: 0,
|
||||
brightness: {
|
||||
type: "dimmer",
|
||||
value: 1,
|
||||
},
|
||||
rgbw: [0, 255, 0, 0],
|
||||
speed: 1,
|
||||
reset: false,
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
r = o.set(movingHeads);
|
||||
console.log(r);
|
||||
let r = output.set(movingHeads);
|
||||
console.log(r);
|
||||
|
||||
o.close();
|
||||
output.close();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user