<commit_before>'use strict';

const SerialPort = require('serialport');
const EventEmitter = require('events');

const stx = 85;
const requestBytes = [ stx, 16, 1, 49, 0, 0, 0 ];
requestBytes.push(requestBytes.reduce((a, b) => a + b) % 256);
const requestBuffer = Buffer.from(requestBytes);
const connectAckBuffer = Buffer.from('OK-V');
const connectBuffer = Buffer.from('UNeuLog!');

function getResultChar(n)
{
	switch (n)
	{
		case 10: return '.';
		case 11: return '+';
		case 12: return '-';
		default: return n;
	}
}

function getResultValue(resultSlice)
{
	let num = '';
	for (const i of resultSlice)
	{
		num += getResultChar(Math.floor(i / 16));
		num += getResultChar(i % 16);
	}

	return parseFloat(num);
}

class NeuLogGsr extends EventEmitter
{
	constructor(port, options = { interval: 100 })
	{
		super();

		this.options = options;
		this.serial = new SerialPort(port, {
			baudRate: 115200,
			stopBits: 2,
			dataBits: 8,
			autoOpen: false,
			parser: (emitter, buffer) =>
			{
				const currentParser = this.connected ? this.delimitedParser : this.connectParser;
				currentParser(emitter, buffer);
			},
		});

		this.delimitedParser = SerialPort.parsers.byteDelimiter([ stx ] );
		this.connectParser = SerialPort.parsers.byteLength(7);
		
		this.connected = false;

		this.serial.on('data', (chunk) =>
		{
			if (this.connected)
			{
				this.onDataReceived(chunk);
			}
			else
			{
				this.onAckReceived(chunk);
			}
		});
	}

	start()
	{
		this.serial.open(() =>
		{
			this.serial.write(connectBuffer);
		});
	}

	onAckReceived(chunk)
	{
		const message = chunk.slice(0, connectAckBuffer.length);
		if (!message.equals(connectAckBuffer))
		{
			throw new Error(`Got incorrect ack msg: ${message}`);
		}

		this.connected = true;

		setInterval(() =>
		{
			this.serial.write(requestBuffer);
		}, this.options.interval);
	}

	onDataReceived(chunk)
	{
		if (chunk.length !== 8)
		{
			return;
		}

		const result = getResultValue(chunk.slice(3, chunk.length - 2));
		this.emit('data', result, Date.now());
	}
}

module.exports = NeuLogGsr;<commit_msg>Switched data chunks to fixed length parser<commit_after>'use strict';

const SerialPort = require('serialport');
const EventEmitter = require('events');

const stx = 85;
const requestBytes = [ stx, 16, 1, 49, 0, 0, 0 ];
requestBytes.push(requestBytes.reduce((a, b) => a + b) % 256);
const requestBuffer = Buffer.from(requestBytes);
const connectAckBuffer = Buffer.from('OK-V');
const connectBuffer = Buffer.from('UNeuLog!');

function getResultChar(n)
{
	switch (n)
	{
		case 10: return '.';
		case 11: return '+';
		case 12: return '-';
		default: return n;
	}
}

function getResultValue(resultSlice)
{
	let num = '';
	for (const i of resultSlice)
	{
		num += getResultChar(Math.floor(i / 16));
		num += getResultChar(i % 16);
	}

	return parseFloat(num);
}

class NeuLogGsr extends EventEmitter
{
	constructor(port, options = { interval: 100 })
	{
		super();

		this.options = options;
		this.connected = false;
		this.dataParser = SerialPort.parsers.byteLength(8);
		this.connectParser = SerialPort.parsers.byteLength(7);

		this.serial = new SerialPort(port, {
			baudRate: 115200,
			stopBits: 2,
			dataBits: 8,
			autoOpen: false,
			parser: (emitter, buffer) =>
			{
				const currentParser = this.connected ? this.dataParser : this.connectParser;
				currentParser(emitter, buffer);
			},
		});

		this.serial.on('data', (chunk) =>
		{
			if (this.connected)
			{
				this.onDataReceived(chunk);
			}
			else
			{
				this.onAckReceived(chunk);
			}
		});
	}

	start()
	{
		this.serial.open(() =>
		{
			this.serial.write(connectBuffer);
		});
	}

	onAckReceived(chunk)
	{
		const message = chunk.slice(0, connectAckBuffer.length);
		if (!message.equals(connectAckBuffer))
		{
			throw new Error(`Got incorrect ack msg: ${message}`);
		}

		this.connected = true;

		setInterval(() =>
		{
			this.serial.write(requestBuffer);
		}, this.options.interval);
	}

	onDataReceived(chunk)
	{
		if (chunk.length !== 8 || chunk[0] !== stx)
		{
			throw new Error(`Expected data chunk, got ${chunk}`);
		}

		const result = getResultValue(chunk.slice(4, chunk.length - 1));
		this.emit('data', result, Date.now());
	}
}

module.exports = NeuLogGsr;
{'fraction_non_alphanumeric': 0.15747099218565, 'fraction_numerical': 0.01894387875917594, 'mean_word_length': 3.688124306326304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2301796', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1474, 'n_words': 312}