Skip to content

Commit

Permalink
Fix broken tests - still need tests for callback
Browse files Browse the repository at this point in the history
  • Loading branch information
miketrebilcock committed Jan 31, 2017
1 parent 48e44ae commit f362891
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 132 deletions.
58 changes: 41 additions & 17 deletions js-pigpio/_callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const TIMEOUT = 2;

//notification flags
const NTFY_FLAGS_EVENT = (1 << 7);
const NTFY_FLAGS_ALIVE = (1 << 6);
//const NTFY_FLAGS_ALIVE = (1 << 6);
const NTFY_FLAGS_WDOG = (1 << 5);
const NTFY_FLAGS_GPIO = 31;

Expand All @@ -25,14 +25,16 @@ const NTFY_FLAGS_GPIO = 31;
* @private
* @class
*/
/*
NOT USED YET
class _event_ADT {
constructor(event, func) {
"use strict";
this.event = event;
this.func = func;
this.bit = 1 << event;
}
}
}*/

/**
* An ADT class to hold callback information.
Expand All @@ -59,10 +61,10 @@ class _callback_ADT {
class _callback {
/**
*
* @param {Set} notify
* @param userGpio
* @param edge
* @param cb
* @param {Set} notify - Set of callbacks current registered.
* @param {number} userGpio - Broadcom GPIO number.
* @param {number} edge - Either EITHER_EDGE, RISING_EDGE or FALLING_EDGE.
* @param {callback} cb - A user function taking three arguments (GPIO, level, tick).
*/
constructor(notify, userGpio, edge, cb) {
"use strict";
Expand Down Expand Up @@ -128,6 +130,9 @@ module.exports._callback = _callback;
* @param cb
* @private
*/
/*
NOT USED YET
class _event {
constructor (notify, event, cb) {
"use strict";
Expand All @@ -145,6 +150,7 @@ class _event {
* Cancels a event callback by removing it from the
* notification thread.
*/
/*
cancel (){
"use strict";
this._notify.remove_event(this.callback);
Expand All @@ -155,6 +161,7 @@ class _event {
*
* @private
*/
/*
_tally () {
"use strict";
if (this._reset) {
Expand All @@ -171,6 +178,7 @@ class _event {
* The count will be zero if the user has supplied their own
* callback function.
*/
/*
tally () {
"use strict";
return this.count;
Expand All @@ -182,6 +190,7 @@ class _event {
this.count = 0;
}
}
*/

/**
* Encapsulates waiting for GPIO edges.
Expand All @@ -193,15 +202,18 @@ class _event {
* @private
* @class
*/
/*
NOT NEEDED YET
class _wait_for_edge {
constructor (notify, gpio, edge, timeout) {
"use strict";
this._notify = notify;
this.callback = _callback_ADT(gpio, edge, this.func);
this.trigger = false;
this._notify.append(this.callback);
this.start = time.time();
while (this.trigger === false && (time.time() - this.start) < timeout) {
const d = new Date();
this.start = d.getTime();
while (this.trigger === false && (d.getTime() - this.start) < timeout) {
}
this._notify.remove(this.callback);
Expand All @@ -210,7 +222,7 @@ class _wait_for_edge {
"use strict";
this.trigger = true;
}
}
}*/

/**
* Encapsulates waiting for an event.
Expand All @@ -221,15 +233,18 @@ class _wait_for_edge {
* @param timeout
* @private
*/
/*
NOT NEEDED YET
class _wait_for_event {
constructor (notify, event, timeout) {
"use strict";
this._notify = notify;
this.callback = new _event_ADT(event, this.func);
this.trigger = false;
this._notify.append(this.callback);
this.start = time.time();
while (this.trigger === false && (time.time() - this.start) < timeout) {
const d = new Date();
this.start = d.getTime();
while (this.trigger === false && (d.getTime() - this.start) < timeout) {
}
this._notify.remove(this.callback);
}
Expand All @@ -238,9 +253,18 @@ class _wait_for_event {
"use strict";
this.trigger = true;
}
}
}*/

class _callback_thread {

/**
* Class to manage the notifications from remote gpio.
*
* @param {Object} control - Socketlock for main socket.
* @param {string} host - Remote Server name.
* @param {number} port - Remote Server port.
* @param {callback} cb - User function to be run after callback initialised.
*/
constructor (control, host, port, cb) {
"use strict";
const that = this;
Expand Down Expand Up @@ -323,8 +347,7 @@ class _callback_thread {
/**
* Adds a callback to the notification thread.
*
* @param self
* @param callb
* @param {callback} callb - Function to be run.
*/
append(callb) {
this.callbacks.add(callb);
Expand All @@ -334,7 +357,8 @@ class _callback_thread {

/**
* Removes a callback from the notification thread.
* @param callb
*
* @param {callback} callb - Function to be run.
*/
remove(callb) {
if (this.callbacks.has(callb)) {
Expand All @@ -353,7 +377,7 @@ class _callback_thread {
/**
* Adds an event callback to the notification thread.
*
* @param callb
* @param {callback} callb - Function to be run.
*/
append_event(callb) {
this.events.append(callb);
Expand All @@ -364,7 +388,7 @@ class _callback_thread {
/**
* Removes an event callback from the notification thread.
*
* @param callb
* @param {callback} callb - Function to be run.
*/
remove_event(callb) {
if (this.events.has(callb)) {
Expand Down
3 changes: 1 addition & 2 deletions js-pigpio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pigpio.prototype.read = function (userGpio, cb) {
*
* @param {number} userGpio - The number of the gpio to address. 0-31.
* @param {number} level - The output level 0 or 1.
* @param {callback} cb - The function to be called with the result.
*/
pigpio.prototype.write = function (userGpio, level, cb) {
"use strict";
Expand Down Expand Up @@ -355,9 +356,7 @@ pigpio.prototype.set_mode = function (gpio, mode) {
"use strict";
assert_gpio_pin_in_range(gpio,0,53);
assert([this.INPUT, this.OUTPUT, this.ALT0, this.ALT1, this.ALT2, this.ALT3, this.ALT4, this.ALT5].includes(mode), "Mode must be INPUT, OUTPUT, ALT0, ALT1, ALT2, ALT3, ALT4, ALT5");
console.log("Sending GPIO Command");
_pi_gpio_command(this.sl,def.PI_CMD_MODES, gpio, mode);
console.log("GPIO Command Sent");
};

/**
Expand Down
15 changes: 9 additions & 6 deletions js-pigpio/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,19 @@ exports._pi_gpio_command = function(socketlock, command, parameter1, parameter2,
.word32le(parameter1)
.word32le(parameter2)
.word32le(0);

socketlock._acquireLock();

if (next !== undefined) {
socketlock._next[command] = next;
}

if(!socketlock.s.write(cmd.buffer())) {
next(new Error("Error Sending Command to Pi: "+command));
}
if(wait_for_response) {
socketlock._next[command] = next;
} else {

if(!wait_for_response) {
socketlock._releaseLock();
if( next !== undefined) {
next();
}
}

};
34 changes: 28 additions & 6 deletions test/pigpio.advanced.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ describe('advanced', () => {
socket.on("data", (data) => {
if(parseInt((data.toString('hex')).substr(0,2),16)===99) {
event_port = socket.remotePort;
socket.write(data);
}

if(socket.remotePort !== event_port) {
last_command = data.toString('hex');
if (server_response !== "") {
var replyData = server_response;
socket.write(replyData);
server_response = "";
const replyData = server_response;
if(socket.write(replyData)) {
server_response = "";
} else {
throw new Error;
}
}
}
});
Expand All @@ -36,16 +40,34 @@ describe('advanced', () => {
}).
listen(port);

function lastCommand() {
const size = last_command.length;
return parseInt(last_command.substr(size-32,2),16);
}

function lastParameter1() {
const size = last_command.length;
return parseInt(last_command.substr(size-30,8),16);
}

function lastParameter2() {
const size = last_command.length;
return parseInt(last_command.substr(size-22,8),16);
}

function assert_correct_message_sent(command, parameter1, parameter2) {
assert(lastCommand() === command, "Wrong Command Sent");
assert(lastParameter1() === parameter1, "Wrong Parameter1 Sent");
assert(lastParameter2() === parameter2, "Wrong Parameter2 Sent");
}

it('set_glitch_filter command is sent', (done) => {
const pigpio = new PiGPIO();
pigpio.pi('127.0.0.1', port, () => {
"use strict";
pigpio.set_glitch_filter(2, 5);
setTimeout((done)=>{
assert(parseInt(last_command.substr(0,2),16)===97, "Wrong Command Sent");
assert(last_command[9]==='2', "Wrong GPIO Pin Sent");
assert(last_command[17]==='5', "Wrong Argument Sent");
assert_correct_message_sent(97, 2, 5);
done();
}, 100, done);
pigpio.close();
Expand Down
Loading

0 comments on commit f362891

Please sign in to comment.