-
So, run-tested the unedited code on the Espruino-Wifi,
here are errors encountered and how I fixed them.
Line 52 - .p.cmd0() to p.cmd0()Uncaught ReferenceError: "id" is not defined
at line 1 col 16
this.id = (id) ? id : 1; // holds on to the device idit turns out that the tenary expression ?: didn't work so I passed the Id directly
main issue:
Uncaught Error: Constructor should be a function, but is Object at line 1 col 16 var fpr1 = new FPR(); ^ =undefined Uncaught Error: Field or method "connect" does not already exist, and can't create it on undefined at line 70 col 7 fpr1.connect(Serial1,cb,1); ^ in function called from system
also, Line 21 and Line 34 has this.open as property and this.open as method respectively.
Anyway, just to get it running, this was my minor fix (as long it works am happy at the moment (: );// begin module inline var C = // for now just command stuff... but it could include more and become sub structured. { OPEN: 0x01 , CLOSE: 0x02 , LED: 0x12 , EXTRA: 0x01 , LED_ON: 0x01 , LED_OFF: 0x00 }; // Constructor for a GT522C3 Device // id: optional device ID, an Integer 16 bit, default 1 0x0001 // name: optional device name / location var D = function(id,loc) { this.id = 1; // holds on to the device id this.name = "Enrolment office"; // holds on to the optional name / location this.ser = undefined; // holds on to the serial the device is connected to this.opened = false; // holds on to the state the device is in (open/closed) }, p = D.prototype; // .connect(serial, cb) - connect w/ optional initialize (open, etc...) // cb: a callback function(err, data) // err: undefined when ok, otherwise err object (tbd - to be defined) // data: response object when ok (tbd) // xtra: optional booly truy for extra info // (booly: something that evaluates to boolean true or false, therefore also called // booly: absent, false, undefined, null, "", and 0 are all falsy, otherwise truy) // Note: if cb absent, only serial is stored and initialization has to be called separately p.connect = function(serial, cb, xtra) { serial.on("data", function(data){console.log(data);}); if (cb) { this.open(cb, xtra); } }; // .open(cb, xtra) - open w/ optional extra information in response object // cb: a callback function(err, data) // err: undefined when ok, otherwise err object: { _: this, ... } // (for this fpr instance, and more tbd - to be defined) // data: response object when ok, w/ xtra: // falsy: { _: this } // truy: { _: this, ver: aNumber, mxSize: aNumber, sn: aString } // for this fpr instance, version number, isoAreaMaxSize, serial number // xtra: optional truy for extra info p.open = function (cb, xtra) { this.cb = cb; // (incorrectly) for now... this.wri(this.cmd0(C.OPEN, xtra), cb); } // .cmd0(cmd, xtra) - build command of structure 0 - only extra parm p.cmd0 = function(cmd, xtra) { var c = [0x55,0xAA,this.id&0xFF,this.id>>8,(xtra)?0x01:0x00,0x00,0x00,0x00]; return this.chk(c,1); }; // .chk(data, add) - add (append) or validate checksum byte // data: a command without checksum byte or a response with checksum byte // add: truy: data is a command and is returned with calculated checksum byte added // falsy: data is response and true is returned when checksum byte is ok, else false p.chk = function(data, add) { var s = 0, c = data.length, i = c - ((add) ? 0 : 1); while (i-- > 0) s += data[i]; s&=0xFF; if (add) { data.push(s); return data; } else return (data[c] == s); }; // .wri(data) - write data to device - (incorrectly) for now... p.wri = function(data) { this.cb(undefined, data); }; //Modules.addCached("GT522C3",D); // later when as module: exports = D; // end module (inline) //var FPR = require("GT522C3"); var fpr1 = new D(); // or: ...(1,"Enrollment Office"); var cb = function(err, data) { console.log((err) ? err : data); }; // ...for now :( function onInit() { Serial1.setup(9600, { tx:B6, rx:B7 }); fpr1.connect(Serial1,cb,1); // open with extra info (version,...,serial number) } setTimeout(onInit,1000);
with printed on console 85, 170, 1, 0, 1, 0, 0, 0, 1
Great! It worked. It is quite an adventure to aim without having things at hand.
Something I missed is that you have an Espruino-Wifi (by the way: an excellent choice). But you managed to translate (to the same pins and 5V to VUSB). From now on I know.
So let's get started with building up some infrastructure / module that we then want to use this way
And this is some 'get going code' you paste into the right hand pane - the editor - and upload to the board. The last line is there only for development time (EDIT: code fixed based on post #8, thanks @user81574).
This is nowhere near basic... but it is basics... (showing how to implement commands).
When uploaded, it should print stuff - including the firmwareVersion, isoAreaMaxSize and device serialNumber in the console.
I'm sure you can take it from here and add the .led(on, cb) (w/ on truy/falsy for on and off) and play around.
To handle the response, it gets a little bit more tricky, since a response block has to be detected, analyzed, and the application callback be called. In other words the application passed in callback has to be feed then in a promise (fullfilled) based on response block becoming available event. In addition to the 'callback hell' that can be managed with promises - nicely available in Espruino - you have also to deal with timeout, since a response block may not show (corrtectly / completely) within (variably) expected time... The timeout would then have to kill the promise(s) in work.
You may for sure take a look at the node.js implementation for guidance - or even more. Just be aware that you deal with less resources than you have in a regular node.js environment.
PS: since I do not have a device - and not time to write an emulator for it (I anyway ran out of time already) - this code is not run-tested... so please provide me with feedback (...tested code).