<commit_before>class Impeeduino {
	
	static version = [0, 0, 2]
	
	static BAUD_RATE = 115200;
	
	// PWM enabled pins. -1: No PWM, otherwise 
	static PWM_PINMAP = [-1, -1, -1, 0, -1, 1, 2, -1, -1, 3, 4, 5, -1, -1];
	
	static MASK_OP = 0xF0;
	static OP_CONFIGURE  = 0x80;
	static OP_DIGITAL_READ  = 0x90;
	static OP_DIGITAL_WRITE_0  = 0xA0;
	static OP_DIGITAL_WRITE_1 = 0xB0;
	static OP_ANALOG = 0xC0;        
	static OP_ARB = 0xD0;
	static OP_CALL = 0xE0;

	static MASK_CONFIG = 0x0F;
	static CONFIG_INPUT = 0x00;
	static CONFIG_INPUT_PULLUP = 0x01;
	static CONFIG_OUTPUT = 0x02;
	static CONFIG_OUTPUT_PWM = 0x03;
	
	static MASK_DIGITAL_ADDR = 0x0F;
	static MASK_DIGITAL_WRITE = 0x10;
	static MASK_ANALOG_W = 0x08;
	static MASK_ANALOG_ADDR = 0x07;
	static MASK_CALL = 0x1F;
    
	_serial  = null; // UART bus to communicate with AVR
    _reset   = null; // AVR reset pin
    
    _rxBuf   = null; // Buffer for incoming data
    _funcBuf = null; // Buffer for function return values
    
    _functioncb = null; // Table of function return callbacks
    _digitalReadcb = null; // Table of digital read return callbacks
    _analogReadcb = null; // Table of analog read return callbacks
    
    constructor(serial = hardware.uart57, reset = hardware.pin1) {
    	_serial = serial;
    	_serial.configure(BAUD_RATE, 8, PARITY_NONE, 1, NO_CTSRTS, uartEvent.bindenv(this));
    	
    	_reset = reset;
	    _reset.configure(DIGITAL_OUT);
	    
	    _funcBuf = blob();
	    _rxBuf = blob();
	    
	    _functioncb = {};
	    _digitalReadcb = {};
	    _analogReadcb = {};
	    
	    this.reset();
	}
	
	function parseRXBuffer() {
		local buf = _rxBuf;
		_rxBuf = blob();
		buf.seek(0, 'b');
		local readByte = 0;
		
		while (!buf.eos()) {
			readByte = buf.readn('b');
			if (readByte & 0x80) {
				// Interpret as Opcode
				server.log(format("Opcode: 0x%02X", readByte));
				switch (readByte & MASK_OP) {
				case OP_DIGITAL_WRITE_0:
					local addr = readByte & MASK_DIGITAL_ADDR;
					if (addr in _digitalReadcb) {
						imp.wakeup(0, function() {
							(delete _digitalReadcb[addr])(0);
						}.bindenv(this));
					}
					break;
				case OP_DIGITAL_WRITE_1:
					local addr = readByte & MASK_DIGITAL_ADDR;
					if (addr in _digitalReadcb) {
						imp.wakeup(0, function() {
							(delete _digitalReadcb[addr])(1);
						}.bindenv(this));
					}
					break;
				case OP_ANALOG:
					local addr = readByte & MASK_ANALOG_ADDR;
					local value = blob(2);
					value[0] = (buf.readn('b') & 0x0F) | ((buf.readn('b') & 0x0F) << 4);
					value[1] = buf.readn('b') & 0x0F;
					if (addr in _analogReadcb) {
						imp.wakeup(0, function() {
							(delete _analogReadcb[addr])(value);
						}.bindenv(this));
					}
					break;<commit_msg>Fix analogRead callback's parameter type<commit_after>class Impeeduino {
	
	static version = [0, 0, 2]
	
	static BAUD_RATE = 115200;
	
	// PWM enabled pins. -1: No PWM, otherwise 
	static PWM_PINMAP = [-1, -1, -1, 0, -1, 1, 2, -1, -1, 3, 4, 5, -1, -1];
	
	static MASK_OP = 0xF0;
	static OP_CONFIGURE  = 0x80;
	static OP_DIGITAL_READ  = 0x90;
	static OP_DIGITAL_WRITE_0  = 0xA0;
	static OP_DIGITAL_WRITE_1 = 0xB0;
	static OP_ANALOG = 0xC0;        
	static OP_ARB = 0xD0;
	static OP_CALL = 0xE0;

	static MASK_CONFIG = 0x0F;
	static CONFIG_INPUT = 0x00;
	static CONFIG_INPUT_PULLUP = 0x01;
	static CONFIG_OUTPUT = 0x02;
	static CONFIG_OUTPUT_PWM = 0x03;
	
	static MASK_DIGITAL_ADDR = 0x0F;
	static MASK_DIGITAL_WRITE = 0x10;
	static MASK_ANALOG_W = 0x08;
	static MASK_ANALOG_ADDR = 0x07;
	static MASK_CALL = 0x1F;
    
	_serial  = null; // UART bus to communicate with AVR
    _reset   = null; // AVR reset pin
    
    _rxBuf   = null; // Buffer for incoming data
    _funcBuf = null; // Buffer for function return values
    
    _functioncb = null; // Table of function return callbacks
    _digitalReadcb = null; // Table of digital read return callbacks
    _analogReadcb = null; // Table of analog read return callbacks
    
    constructor(serial = hardware.uart57, reset = hardware.pin1) {
    	_serial = serial;
    	_serial.configure(BAUD_RATE, 8, PARITY_NONE, 1, NO_CTSRTS, uartEvent.bindenv(this));
    	
    	_reset = reset;
	    _reset.configure(DIGITAL_OUT);
	    
	    _funcBuf = blob();
	    _rxBuf = blob();
	    
	    _functioncb = {};
	    _digitalReadcb = {};
	    _analogReadcb = {};
	    
	    this.reset();
	}
	
	function parseRXBuffer() {
		local buf = _rxBuf;
		_rxBuf = blob();
		buf.seek(0, 'b');
		local readByte = 0;
		
		while (!buf.eos()) {
			readByte = buf.readn('b');
			if (readByte & 0x80) {
				// Interpret as Opcode
				server.log(format("Opcode: 0x%02X", readByte));
				switch (readByte & MASK_OP) {
				case OP_DIGITAL_WRITE_0:
					local addr = readByte & MASK_DIGITAL_ADDR;
					if (addr in _digitalReadcb) {
						imp.wakeup(0, function() {
							(delete _digitalReadcb[addr])(0);
						}.bindenv(this));
					}
					break;
				case OP_DIGITAL_WRITE_1:
					local addr = readByte & MASK_DIGITAL_ADDR;
					if (addr in _digitalReadcb) {
						imp.wakeup(0, function() {
							(delete _digitalReadcb[addr])(1);
						}.bindenv(this));
					}
					break;
				case OP_ANALOG:
					local addr = readByte & MASK_ANALOG_ADDR;
					local value = blob(2);
					value[0] = (buf.readn('b') & 0x0F) | ((buf.readn('b') & 0x0F) << 4);
					value[1] = buf.readn('b') & 0x0F;
					if (addr in _analogReadcb) {
						imp.wakeup(0, function() {
							(delete _analogReadcb[addr])(value.readn('w'));
						}.bindenv(this));
					}
					break;
{'fraction_non_alphanumeric': 0.12286441294074882, 'fraction_numerical': 0.036713922210105415, 'mean_word_length': 2.7537517053206004, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '7473868', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2076, 'n_words': 497}