<commit_before>/**
 * Arduino and Arduino compitible boards using Firmata protocol to communicatewith software on the host computer,
 * hince, this adaptor implements most parts of Firmata protocol based on npm module 'firmata'
 */
const Board = require('firmata'),
	Adaptor = require('../../adaptor');

const Arduino = module.exports = class Arduino extends Adaptor {
	
	constructor(opts) {
		super(opts);
		// constants used to set a digital pin's valtage
		this.HIGT = Board.HIGT;
		this.LOW = Board.LOW;
		this.i2cReady = false;
		this._board = null;
	}

	connect(callback) {
		try {
			this._board = new Board(this.port);
			this._board.on('ready', () => {
				this._respond('connect', callback);
			});
		} catch(err) {
			if (err && callback) {
				callback(err);
			}
			this._respond('connect', callback, err);
		}
	}

	disconnect(callback) {
		this._board.reset();
		this._respond('disconnect', callback);
	}

	/*
	 * Configures the specifed pin to behave either as an input or an output.
	 * @param {Number} [pin] the number of the pin
	 * @param {Enum} [mode] standard pin mode of firmata protocol
	 * @return {void}
	 */
	pinMode(pin, mode = this._board.MODES.INPUT, callback) {
		this._board.pinMode(pin, mode);
	}

	/**
	 * Register to get the digital value (board.HIGH or board.LOW). The value is reported via the callback whenever it changes
	 * @param {Number} [pin] the number of the pin
	 * @param {Function} [callback] function(err, value, pin) to handle the return value from the specified pin
	 * @return {void}
	 */
	digitalRead(pin, callback) {
		this.pinMode(pin, this._board.MODES.INPUT);
		this._board.digitalRead(pin, (value) => {
			console.log(value);
			this._respond('digitalRead', callback, null, value, pin);
		});
	}

	/**
	 * Get current pin value (LOW or HIGH)
	 * @param {Number} [pin] the pin number
	 * @return {Number} the value of the pin (LOW or HIGH)
	 */
	getCurrentPinValue(pin) {
		return this._board.pins[pin].value;
	}

	/*
	 * Write a HIGH or a LOW value to a digital pin
	 * @param {Number} [pin] the number of the pin
	 * @param {Number} [value] 0 is LOW, non-zero is HIGH
	 * @return {void}
	 */
	digitalWrite(pin, value) {
		this.pinMode(pin, this._board.MODES.OUTPUT);
		this._board.digitalWrite(pin, value);
	}

	/**
	 * Reads the analog value (0-1023) from a specified pin
	 * @param {Number} [pin] the number of the pin
	 * @param {Function} [callback] function(value, pin) to handle the return value
	 * @return {void}
	 */
	analogRead(pin, callback) {
		this._board.analogRead(pin, (value) => {
			this._respond('analogRead', callback, null, value, pin);
		});
	}

	/**
	 * Write an output to an anlog pin(PWM)
	 * @param {Number} [pin] the number of the pin (which should support pwm write)
	 * @param {Number} [value] the analog value(between 0 to 1) to write. It will be scaled to 0 to 255
	 */
	pwmWrite(pin, value) {
		value = toScale(value, 0, 255);
		// analog pin mapping to normal pin
		this.pinMode(pin, this._board.MODES.PWM);
		this._board.pwmWrite(pin, value);
	}

	/**
	 * Set sampling interval, default is 19ms
	 * @param {Number} [interval] sampling interval in milliseconds
	 * @param {Function} [callback] function(interval) after set sampling interval
	 * @return {void}
	 */
	setSamplingInterval(interval, callback) {<commit_msg>arduino - add analogWrite method for Ax(x=0,1,...,n) pins<commit_after>/**
 * Arduino and Arduino compitible boards using Firmata protocol to communicatewith software on the host computer,
 * hince, this adaptor implements most parts of Firmata protocol based on npm module 'firmata'
 */
const Board = require('firmata'),
	Adaptor = require('../../adaptor');

const Arduino = module.exports = class Arduino extends Adaptor {
	
	constructor(opts) {
		super(opts);
		// constants used to set a digital pin's valtage
		this.HIGT = Board.HIGT;
		this.LOW = Board.LOW;
		this.i2cReady = false;
		this._board = null;
	}

	connect(callback) {
		let connectionTimeout = setTimeout(() => {
			this._respond('connectedTimeout', callback, 
				new Error('Connected to connection ' + this.name + ' timeout'));
		}, 120000);
		try {
			this._board = new Board(this.port);
			this._board.on('ready', () => {
				this._respond('connect', callback);
				clearTimeout(connectedTimeout);
			});
		} catch(err) {
			if (err && callback) {
				callback(err);
			}
			this._respond('connect', callback, err);
			clearTimeout(connectedTimeout);
		}
	}

	disconnect(callback) {
		this._board.reset();
		this._respond('disconnect', callback);
	}

	/*
	 * Configures the specifed pin to behave either as an input or an output.
	 * @param {Number} [pin] the number of the pin
	 * @param {Enum} [mode] standard pin mode of firmata protocol
	 * @return {void}
	 */
	pinMode(pin, mode = this._board.MODES.INPUT, callback) {
		this._board.pinMode(pin, mode);
	}

	/**
	 * Register to get the digital value (board.HIGH or board.LOW). The value is reported via the callback whenever it changes
	 * @param {Number} [pin] the number of the pin
	 * @param {Function} [callback] function(err, value, pin) to handle the return value from the specified pin
	 * @return {void}
	 */
	digitalRead(pin, callback) {
		this.pinMode(pin, this._board.MODES.INPUT);
		this._board.digitalRead(pin, (value) => {
			console.log(value);
			this._respond('digitalRead', callback, null, value, pin);
		});
	}

	/**
	 * Get current pin value (LOW or HIGH)
	 * @param {Number} [pin] the pin number
	 * @return {Number} the value of the pin (LOW or HIGH)
	 */
	getCurrentPinValue(pin) {
		return this._board.pins[pin].value;
	}

	/*
	 * Write a HIGH or a LOW value to a digital pin
	 * @param {Number} [pin] the number of the pin
	 * @param {Number} [value] 0 is LOW, non-zero is HIGH
	 * @return {void}
	 */
	digitalWrite(pin, value) {
		this.pinMode(pin, this._board.MODES.OUTPUT);
		this._board.digitalWrite(pin, value);
	}

	/**
	 * Reads the analog value (0-1023) from a specified pin
	 * @param {Number} [pin] the number of the pin
	 * @param {Function} [callback] function(value, pin) to handle the return value
	 * @return {void}
	 */
	analogRead(pin, callback) {
		this._board.analogRead(pin, (value) => {
			this._respond('analogRead', callback, null, value, pin);
		});
	}

	/**
	 * Write an output to an anlog pin(PWM)
	 * @param {Number} [pin] the number of the pin (which should support pwm write)
	 * @param {Number} [value] the analog value(between 0 to 1) to write. It will be scaled to 0 to 255
	 */
	pwmWrite(pin, value) {
		value = toScale(value, 0, 255);
		// analog pin mapping to normal pin
		this.pinMode(pin, this._board.MODES.PWM);
		this._board.pwmWrite(pin, value);
	}

	/**
	 * Write to an analog pin(A0, A1, .., An)
	 * @param {Number} [pin] the number of the pin  (A0 = 0, A1 = 1, ..., An = n)
	 * @param {Number} [value] the analog value(between 0 to 1) to write. It will be scaled to 0 to 1023
	 */
	analogWrite(pin, value) {
		value = toScale(value, 0, 1023);
		this.pinMode(pin, this._board.MODES.ANALOG);
		this._board.analogWrite(this._board.analogPins[pin], value);
		this.respond("analogWrite", callback, null, value, pin);
	}

	/**
	 * Set sampling interval, default is 19ms
	 * @param {Number} [interval] sampling interval in milliseconds
	 * @param {Function} [callback] function(interval) after set sampling interval
	 * @return {void}
	 */
	setSamplingInterval(interval, callback) {
{'fraction_non_alphanumeric': 0.13739837398373983, 'fraction_numerical': 0.008672086720867209, 'mean_word_length': 3.627586206896552, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '5086780', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2366, 'n_words': 783}