Hi, I finally managed to make the fingerprint sensor working, with print recognition and code for enrollment. Here is the code :
var C = { FINGERPRINT_OK : 0x00, FINGERPRINT_PACKETRECIEVEERR : 0x01, FINGERPRINT_NOFINGER : 0x02, FINGERPRINT_IMAGEFAIL : 0x03, FINGERPRINT_IMAGEMESS : 0x06, FINGERPRINT_FEATUREFAIL : 0x07, FINGERPRINT_NOMATCH : 0x08, FINGERPRINT_NOTFOUND : 0x09, FINGERPRINT_ENROLLMISMATCH : 0x0A, FINGERPRINT_BADLOCATION : 0x0B, FINGERPRINT_DBRANGEFAIL : 0x0C, FINGERPRINT_UPLOADFEATUREFAIL : 0x0D, FINGERPRINT_PACKETRESPONSEFAIL : 0x0E, FINGERPRINT_UPLOADFAIL : 0x0F, FINGERPRINT_DELETEFAIL : 0x10, FINGERPRINT_DBCLEARFAIL : 0x11, FINGERPRINT_PASSFAIL : 0x13, FINGERPRINT_INVALIDIMAGE : 0x15, FINGERPRINT_FLASHERR : 0x18, FINGERPRINT_INVALIDREG : 0x1A, FINGERPRINT_ADDRCODE : 0x20, FINGERPRINT_PASSVERIFY : 0x21, // FINGERPRINT_STARTCODE : 0xEF01, // FINGERPRINT_COMMANDPACKET : 0x1, FINGERPRINT_DATAPACKET : 0x2, FINGERPRINT_ACKPACKET : 0x7, FINGERPRINT_ENDDATAPACKET : 0x8, // FINGERPRINT_TIMEOUT : 0xFF, FINGERPRINT_BADPACKET : 0xFE, // FINGERPRINT_GETIMAGE : 0x01, FINGERPRINT_IMAGE2TZ : 0x02, FINGERPRINT_REGMODEL : 0x05, FINGERPRINT_STORE : 0x06, FINGERPRINT_EMPTY : 0x0D, FINGERPRINT_VERIFYPASSWORD : 0x13, FINGERPRINT_HISPEEDSEARCH : 0x1B, FINGERPRINT_TEMPLATECOUNT : 0x1D, // DEFAULTTIMEOUT : 5000 // milliseconds }; var serialTTL = null; var thePassword = 0; var theAddress = 0xFFFFFFFF; function connect(serial) { serialTTL = serial; serialTTL.setup(57600); } function checkSensor(callback) { var packet = [ C.FINGERPRINT_VERIFYPASSWORD, (thePassword >> 24) & 0xFF, (thePassword >> 16) & 0xFF, (thePassword >> 8) & 0xFF, thePassword & 0xFF ]; sendPacket(packet, callback); } function getImage(callback) { sendPacket([C.FINGERPRINT_GETIMAGE], callback); } function image2Tz(slot, callback) { sendPacket([C.FINGERPRINT_IMAGE2TZ, (slot & 0xFF)], callback); } function createModel(callback) { sendPacket([C.FINGERPRINT_REGMODEL], callback); } function storeModel(id, callback) { sendPacket([C.FINGERPRINT_STORE, 0x01, (id >> 8) & 0xFF, id & 0xFF], callback); } function emptyDatabase(callback) { sendPacket([C.FINGERPRINT_EMPTY], callback); } function fingerFastSearch(callback) { var fingerID = 0xFFFF; var confidence = 0xFFFF; var packet = [C.FINGERPRINT_HISPEEDSEARCH, 0x01 & 0xFF, 0x00 & 0xFF, 0x00 & 0xFF, 0x00 & 0xFF, 0xA3 & 0xFF]; writePacket(theAddress, C.FINGERPRINT_COMMANDPACKET, packet); getReply(packet, function(len, packet) { if ((len != 1) && (packet[0] != C.FINGERPRINT_ACKPACKET)) { callback(-1, null); return; } fingerID = packet[2]; fingerID <<= 8; fingerID |= packet[3]; confidence = packet[4]; confidence <<= 8; confidence |= packet[5]; callback(null, {'responseCode': packet[1], 'fingerID': fingerID, 'confidence': confidence}); }); } function getTemplateCount(callback) { var templateCount = 0xFFFF; var packet = [C.FINGERPRINT_TEMPLATECOUNT]; writePacket(theAddress, C.FINGERPRINT_COMMANDPACKET, packet); getReply(packet, function(len, packet) { if ((len != 1) && (packet[0] != C.FINGERPRINT_ACKPACKET)) { callback(-1, null); return; } templateCount = packet[2]; templateCount <<= 8; templateCount |= packet[3]; callback(null, {'responseCode': packet[1], 'templateCount': templateCount}); }); } function writePacket(addr, packetType, packet) { var len = packet.length+2; serialTTL.write([ (C.FINGERPRINT_STARTCODE >> 8) & 0xFF, C.FINGERPRINT_STARTCODE & 0xFF, (addr >> 24) & 0xFF, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF, packetType, (len >> 8) & 0xFF, len &0xFF ]); var sum = (len>>8) + (len&0xFF) + packetType; serialTTL.write(packet); for (var i in packet) sum += packet[i]; serialTTL.write([(sum>>8)&0xFF, sum&0xFF]); } function getReply(packet, callback) { var receivedData = []; serialTTL.onData(function (e) { receivedData.push(e.data.charCodeAt(0)); if ( (idx === 0) && (receivedData[0] != ( (C.FINGERPRINT_STARTCODE >> 8) & 0xFF) )) return; if (receivedData.length >= 9) { if (( receivedData[0] != (C.FINGERPRINT_STARTCODE >> 8)) || ( receivedData[1] != (C.FINGERPRINT_STARTCODE & 0xFF))) { callback(-1, {'responseCode': C.FINGERPRINT_BADPACKET}); return; } var packettype = receivedData[6]; var len = receivedData[7]; len <<= 8; len |= receivedData[8]; len -= 2; if (receivedData.length <= (len+10)) return; packet[0] = packettype; for (var i=0; i<len; i++) { packet[1+i] = receivedData[9+i]; } callback(len, packet); } }); } function sendPacket(packet, callback) { writePacket(theAddress, C.FINGERPRINT_COMMANDPACKET, packet); getReply(packet, function(len, packet) { if ((len != 1) && (packet[0] != C.FINGERPRINT_ACKPACKET)) callback(-1, null); else callback(len, {'responseCode': packet[1]}); }); } function getErrorCode(responseCode) { var res = null; switch (responseCode & 0xFF) { case C.FINGERPRINT_OK: res = null; break; case C.FINGERPRINT_NOFINGER: res = "No Finger"; break; case C.FINGERPRINT_PACKETRECIEVEERR: res = "Communication error"; break; case C.FINGERPRINT_IMAGEFAIL: res = "Imaging error"; break; case C.FINGERPRINT_IMAGEMESS: res = "Image too messy"; break; case C.FINGERPRINT_FEATUREFAIL: res = "Could not find fingerprint features"; break; case C.FINGERPRINT_INVALIDIMAGE: res = "Could not find fingerprint features"; break; case C.FINGERPRINT_ENROLLMISMATCH: res = "Prints did not match"; break; case C.FINGERPRINT_BADLOCATION: res = "Could not store in that location"; break; case C.FINGERPRINT_BADPACKET: res = "Communication error"; break; case C.FINGERPRINT_FLASHERR: res = "Error writing to flash"; break; default: res = "Unknown error"; } return res; }
@ChrisB started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi,
I finally managed to make the fingerprint sensor working,
with print recognition and code for enrollment.
Here is the code :