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

Convert sdk code from C++ to JavaScript #796

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
308 changes: 308 additions & 0 deletions bindings/javascript/examples/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
<!DOCTYPE html>
<html>
<title>Aditof JS Client</title>
<link rel="shortcut icon" href="#">
<script type="text/javascript" src="../bundle.js"></script>

<script type="text/javascript">
async function main() {
let status;
const ip_address = '192.168.0.106';

let net = new Network(ip_address);
status = await net.ServerConnect();

var nse = new NetworkSensorEnumerator(net);
status = await nse.searchSensors();

console.log(nse);

let depth_sensor = nse.getDepthSensors()[1][0];
let storage = nse.getStorages()[1][0];
let temp_sensor = nse.getTemperatureSensors()[1][0];

console.log(depth_sensor);
console.log(storage);
console.log(temp_sensor);

let address, data, length, bytesCount;

//depth sensor
console.log('');
console.log('depth sensor testing');
console.log('');

console.log('opened: ', depth_sensor.m_opened);
status = await depth_sensor.open();
console.log('status open: ', status, depth_sensor.m_opened);

let firmware, size;
firmware = 'asd';
size = 3;
status = await depth_sensor.program(firmware, size);
console.log('status program: ', status);

// doesn 't work
// status = await depth_sensor.start();
// console.log('status start: ', status);

[status, details] = await depth_sensor.getAvailableFrameTypes();
console.log('status getAvailableFrameTypes: ', status);

status = await depth_sensor.setFrameType(new Object());
console.log('status setFrameType: ', status);

// doesn 't work
// status = await depth_sensor.getFrame();
// console.log('status getFrame: ', status);

address = "asd";
data = "qwe";
length = 3;
status = await depth_sensor.writeAfeRegisters(address, data, length);
console.log('status writeAfeRegisters: ', status);

status = await depth_sensor.readAfeRegisters(address, data, length);
console.log('status readAfeRegisters: ', status);

status = await depth_sensor.stop();
console.log('status stop: ', status);

//storage
console.log("");
console.log('storage testing');
console.log("");

status = await storage.open();
console.log('status open: ', status);

address = 1;
data = new Uint8Array();
bytesCount = 3;
status = await storage.write(address, data, bytesCount);
console.log('status write: ', status);

[status, data] = await storage.read(address, bytesCount);
console.log('status read: ', status);

status = await storage.close();
console.log('status close: ', status);


//temperature sensor
console.log('');
console.log('temp sensor testing');
console.log('');

status = await temp_sensor.open();
console.log('status open: ', status);

[status, temp] = await temp_sensor.read();
console.log('status read: ', status);

status = await temp_sensor.close();
console.log('status close: ', status);

}

// main();

async function first_frame_network(ip_address) {
let status;

console.clear();
console.log('\n\n\n\n');
console.log('Hello World! ... First frame network running ... ');

// const ip_address = '10.42.0.65';


// let net = new Network(ip_address);
// status = await net.ServerConnect();

// var nse = new NetworkSensorEnumerator(net);
// status = await nse.searchSensors();

// console.log(nse);

// let depth_sensor = nse.getDepthSensors()[1][0];
// let storage = nse.getStorages()[1][0];
// let temp_sensor = nse.getTemperatureSensors()[1][0];

// console.log(depth_sensor);
// console.log(storage);
// console.log(temp_sensor);

// let address, data, length, bytesCount;

// //depth sensor
// console.log('');
// console.log('depth sensor testing');
// console.log('');

// console.log('opened: ', depth_sensor.m_opened);
// status = await depth_sensor.open();
// console.log('status open: ', status, depth_sensor.m_opened);

// let firmware, size;
// firmware = 'asd';
// size = 3;
// status = await depth_sensor.program(firmware, size);
// console.log('status program: ', status);

// // doesn 't work
// // status = await depth_sensor.start();
// // console.log('status start: ', status);

// [status, details] = await depth_sensor.getAvailableFrameTypes();
// console.log('status getAvailableFrameTypes: ', status);

// status = await depth_sensor.setFrameType(new Object());
// console.log('status setFrameType: ', status);

// // doesn 't work
// // status = await depth_sensor.getFrame();
// // console.log('status getFrame: ', status);

// address = "asd";
// data = "qwe";
// length = 3;
// status = await depth_sensor.writeAfeRegisters(address, data, length);
// console.log('status writeAfeRegisters: ', status);

// status = await depth_sensor.readAfeRegisters(address, data, length);
// console.log('status readAfeRegisters: ', status);

// status = await depth_sensor.stop();
// console.log('status stop: ', status);

// //storage
// console.log("");
// console.log('storage testing');
// console.log("");

// status = await storage.open();
// console.log('status open: ', status);

// address = 1;
// data = new Uint8Array();
// bytesCount = 3;
// status = await storage.write(address, data, bytesCount);
// console.log('status write: ', status);

// [status, data] = await storage.read(address, bytesCount);
// console.log('status read: ', status);

// status = await storage.close();
// console.log('status close: ', status);


// //temperature sensor
// console.log('');
// console.log('temp sensor testing');
// console.log('');

// status = await temp_sensor.open();
// console.log('status open: ', status);

// [status, temp] = await temp_sensor.read();
// console.log('status read: ', status);

// status = await temp_sensor.close();
// console.log('status close: ', status);


let system = new System();
let cameras = [];

[status, cameras] = await system.getCameraListAtIp(ip_address);
if (!cameras.length) {
console.log('WARNING: No cameras found');
return 0;
}

let camera = cameras[0];
status = camera.initialize();
if (status !== Status.OK) {
console.log('ERROR: Could not initialize camera!');
return 0;
}

let frameTypes = [];
[status, frameTypes] = camera.getAvailableFrameTypes();
if (!frameTypes.length) {
console.log('ERROR: no frame type available!');
return 0;
}

status = camera.setFrameType(frameTypes[0]);
if (status !== Status.OK) {
console.log('ERROR: Could not set camera frame type!');
return 0;
}

let modes = [];
[status, modes] = camera.getAvailableModes();
if (!modes.length) {
console.log('ERROR: no camera modes available!');
return 0;
}

status = camera.setMode(modes[0]);
if (status !== Status.OK) {
console.log('ERROR: Could not set camera mode!');
return 0;
}

let frame = new Frame();
[status, frame] = camera.requestFrame();
if (status !== Status.OK) {
console.log('ERROR: Could not request frame!');
return 0;
} else {
console.log('INFO: succesfully requested frame!');
}

// uint16_t *
let frameData;
[status, frameData] = frame.getData(FrameDataType.FULL_DATA);

if (status !== Status.OK) {
console.log('ERROR: Could not get frame data!');
return 0;
}

if (!frameData) {
console.log('ERROR: no memory allocated in frame');
return 0;
}

let fDetails = new FrameDetails();
[status, fDetails] = frame.getDetails();
for (let i = 0; i < fDetails.height; i++) {
for (let j = 0; j < fDetails.width; j++) {
console.log(frameData[i * fDetails.width + j]);
}
}

}

// first_frame_network();
</script>


<body>
<div>
<h1> AdiTof JS Client </h1>
<input id="ip_input">
<button onclick='first_frame_network(document.getElementById("ip_input").value)'> Connect to the camera </button>
<br>
<br>
<!-- <input id="fun_name_input"> -->
<!-- <button onclick='net.SendCommand();'> Send </button> -->
</div>
</body>

</html>

</html>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const WebSocketAsPromised = require('websocket-as-promised');
const BufferProtobuf = require('../buffer_pb');
const BufferProtobuf = require('./buffer_pb');


// class NetworkHandle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class NetworkSensorEnumerator extends SensorEnumeratorInterface {

console.log("status: " + status + "\ncameraType: " + cameraType);

this.m_cameraType = cameraType;
return [status, cameraType];
}

Expand All @@ -88,6 +89,10 @@ class NetworkSensorEnumerator extends SensorEnumeratorInterface {
console.log(`ERROR: invalid connection string: ${connectionString}`);
return Status.GENERIC_ERROR;
}


console.log('before getCameraType()');

[status, this.m_cameraType] = await this.getCameraType();
if (status !== Status.OK) {
console.log("WARNING: Failed to find out the camera type on target. Assumming it's camera: AD-96TOF1-EBZ");
Expand All @@ -98,6 +103,8 @@ class NetworkSensorEnumerator extends SensorEnumeratorInterface {
this.m_network.send_buff.setFuncName("FindSensors");
this.m_network.send_buff.setExpectReply(true);

console.log('before findSensor call');

if ((await this.m_network.SendCommand()) !== 0) {
console.log("WARNING: Send Command Failed");
return Status.INVALID_ARGUMENT;
Expand All @@ -108,6 +115,8 @@ class NetworkSensorEnumerator extends SensorEnumeratorInterface {
return Status.GENERIC_ERROR;
}

console.log('after findSensors');

const sensorsInfo = this.m_network.recv_buff.getSensorsInfo();
let name, id;
for (let i = 0; i < sensorsInfo.getImageSensorsList().length; i++) {
Expand All @@ -128,7 +137,9 @@ class NetworkSensorEnumerator extends SensorEnumeratorInterface {
this.m_temperatureSensorsInfo.push({ name, id });
}

return this.m_network.recv_buff.getStatus();
let val = this.m_network.recv_buff.getStatus();
console.log('val: ', val);
return val;
}

getDepthSensors() {
Expand Down Expand Up @@ -166,10 +177,18 @@ class NetworkSensorEnumerator extends SensorEnumeratorInterface {
let storages = [];
let storage;
let status = Status.OK;
let i = 0;
console.log('the storages are: ');
for (const nameAndId of this.m_storagesInfo) {
// if (i == 0)
// continue;
let { name, id } = nameAndId;
console.log('name: ', name, 'id: ', id);

storage = new NetworkStorage(name, id, this.m_network);
storages.push(storage);
console.log(i + ' --- ' + storages[i].getName());
i++;
}

return [status, storages];
Expand All @@ -192,5 +211,4 @@ class NetworkSensorEnumerator extends SensorEnumeratorInterface {
return [Status.OK, this.m_cameraType];
}
}
window.NetworkSensorEnumerator = NetworkSensorEnumerator;

window.NetworkSensorEnumerator = NetworkSensorEnumerator;
Loading