Skip to content
Browse files

Add: functional code, and test

  • Loading branch information...
1 parent e5b3e6b commit a9f0e314730a496103960a56794bae028946cb6f @tmpvar committed
Showing with 110 additions and 0 deletions.
  1. +69 −0 index.js
  2. +20 −0 package.json
  3. +21 −0 test/test.js
View
69 index.js
@@ -0,0 +1,69 @@
+var EventEmitter = require('events').EventEmitter;
+
+var parse = module.exports = function(string, fn) {
+ var
+ ev = new EventEmitter();
+ parts = string.split(':');
+
+ string = string.replace(/[\r\n]/g,'');
+
+ process.nextTick(function() {
+ parts.forEach(function(line) {
+ if (!line) {
+ return;
+ }
+
+ var data = {
+ size : parseInt(line.substring(0,2), 16),
+ // TODO: test this
+ address : parseInt(line.substring(2, 6), 16),
+ type : parseInt(line.substring(6,8), 16),
+ bytes : []
+ };
+
+ switch (data.type) {
+ // Data record
+ case 0:
+ for (var i = 0, p = 0; i<data.size*2; i+=2, p++) {
+ var byte = parseInt(line.substring(i+8, i+10), 16);
+ data.bytes.push(byte);
+ }
+ data.checksum = parseInt(line.substring(8 + data.size*2,8 + data.size*2 + 2), 16);
+ ev.emit('data', data);
+ break;
+
+ // End of file
+ case 1:
+ ev.emit('end');
+ break;
+
+ // Extended Segment Address Record
+ case 2:
+ ev.emit('error', new Error('TODO: Extended segment Address record'));
+ break;
+
+ // Start Segment Address Record
+ case 3:
+ ev.emit('error', new Error('TODO: Start Segment Address Record'));
+ break;
+
+ // Extended Linear Address Record
+ case 4:
+ ev.emit('error', new Error('TODO: Extended Linear Address Record'));
+ break;
+
+ // Start Linear Address Record
+ case 5:
+ ev.emit('error', new Error('TODO: Start Linear Address Record'));
+ break;
+
+ // Invalid format
+ default:
+ ev.emit('error', new Error('Invalid Intel Hex format'));
+ break;
+ }
+ });
+ });
+
+ return ev;
+};
View
20 package.json
@@ -0,0 +1,20 @@
+{
+ "author": "Elijah Insua <tmpvar@gmail.com> (http://tmpvar.com)",
+ "name": "intelhex",
+ "description": "an intelhex to binary converter",
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/tmpvar/intelhex.git"
+ },
+ "main": "index.js",
+ "scripts": {
+ "test": "node test/test.js"
+ },
+ "dependencies": {},
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ }
+}
View
21 test/test.js
@@ -0,0 +1,21 @@
+var
+ assert = require('assert'),
+ intelhex = require('../index');
+
+// Sanity test
+var sanity = intelhex(':101AB0000001000400050002000300010000000016:00000001FF');
+
+sanity.on('data', function(d) {
+ assert.strictEqual(
+ d.bytes.join(''),
+ '0104050203010000'
+ );
+});
+
+var timeout = setTimeout(function() {
+ assert.ok(false, 'End was never called')
+}, 100)
+
+sanity.on('end', function() {
+ clearTimeout(timeout);
+});

0 comments on commit a9f0e31

Please sign in to comment.
Something went wrong with that request. Please try again.