<commit_before>#include "Sds011.h"

Sds011::Sds011(Stream &out) : _out(out)
{
}

String Sds011::firmware_version(void)
{
    _send_cmd(CMD_FIRMWARE, NULL, 0);
    _read_response();

    return String(_buf[3])+"_"+String(_buf[4])+"_"+String(_buf[5]);
}

void Sds011::set_mode(Report_mode mode)
{
    uint8_t data[] = {0x1, mode};
    _send_cmd(CMD_MODE, data, 2);
    _ignore_response();
}

void Sds011::set_sleep(bool sleep)
{
    uint8_t data[] = {0x1, !sleep};
    _send_cmd(CMD_SLEEP, data, 2);
    _ignore_response();
}

void Sds011::query_data(int *pm25, int *pm10)
{
    _send_cmd(CMD_QUERY_DATA, NULL, 0);
    _read_response();

    *pm25 = _buf[2] | _buf[3]<<8;
    *pm10 = _buf[4] | _buf[5]<<8;
}

void Sds011::query_data_auto(int *pm25, int *pm10, int n)
{
    int pm25_table[n];
    int pm10_table[n];
    int ok;

    for (int i = 0; i<n; i++) {
	query_data(&pm25_table[i], &pm10_table[i]);
	ok = crc_ok();
	if (!ok) {
	    n--, i--;
	    continue;
	}
	delay(1000);
    }

    _filter_data(n, pm25_table, pm10_table, pm25, pm10);
}

bool Sds011::crc_ok(void)
{
    uint8_t crc = 0 ;
    for (int i=2; i<8; i++) {
	crc+=_buf[i];
    }
    return crc==_buf[8];
}

void Sds011::_send_cmd(uint8_t cmd, uint8_t *data, uint8_t len)
{
    uint8_t i, crc;

    _buf[0] = 0xAA;
    _buf[1] = 0xB4;
    _buf[2] = cmd;
    _buf[15] = 0xff;
    _buf[16] = 0xff;
    _buf[18] = 0xAB;

    crc = cmd + _buf[15] + _buf[16];

    for (i=0; i<12; i++) {
	if (i<len) {
	    _buf[3+i] = data[i];
	} else {
	    _buf[3+i] = 0;
	}
	crc += _buf[3+i];
    }

    _buf[17] = crc;

    for (i = 0; i < 19; i++) {
	_out.write(_buf[i]);
    }
    _out.flush();
}

uint8_t Sds011::_read_byte(void)
{
    while (!_out.available())
	delay(1);
    return _out.read();
}

void Sds011::_ignore_response(void)
{
    delay(100);
    while (_out.available())
	_out.read();
}

void Sds011::_read_response(void)
{
    uint8_t i = 1, b;

    while ((b=_read_byte())!=0xAA)
	;

    _buf[0] = b;

    for(i = 1; i<10; i++) {
	_buf[i] = _read_byte();
    }
}

String Sds011::_buf_to_string(void)
{<commit_msg>100ms is not enough to ignore response.<commit_after>#include "Sds011.h"

Sds011::Sds011(Stream &out) : _out(out)
{
}

String Sds011::firmware_version(void)
{
    _send_cmd(CMD_FIRMWARE, NULL, 0);
    _read_response();

    return String(_buf[3])+"_"+String(_buf[4])+"_"+String(_buf[5]);
}

void Sds011::set_mode(Report_mode mode)
{
    uint8_t data[] = {0x1, mode};
    _send_cmd(CMD_MODE, data, 2);
    _ignore_response();
}

void Sds011::set_sleep(bool sleep)
{
    uint8_t data[] = {0x1, !sleep};
    _send_cmd(CMD_SLEEP, data, 2);
    _ignore_response();
}

void Sds011::query_data(int *pm25, int *pm10)
{
    _send_cmd(CMD_QUERY_DATA, NULL, 0);
    _read_response();

    *pm25 = _buf[2] | _buf[3]<<8;
    *pm10 = _buf[4] | _buf[5]<<8;
}

void Sds011::query_data_auto(int *pm25, int *pm10, int n)
{
    int pm25_table[n];
    int pm10_table[n];
    int ok;

    for (int i = 0; i<n; i++) {
	query_data(&pm25_table[i], &pm10_table[i]);
	ok = crc_ok();
	if (!ok) {
	    n--, i--;
	    continue;
	}
	delay(1000);
    }

    _filter_data(n, pm25_table, pm10_table, pm25, pm10);
}

bool Sds011::crc_ok(void)
{
    uint8_t crc = 0 ;
    for (int i=2; i<8; i++) {
	crc+=_buf[i];
    }
    return crc==_buf[8];
}

void Sds011::_send_cmd(uint8_t cmd, uint8_t *data, uint8_t len)
{
    uint8_t i, crc;

    _buf[0] = 0xAA;
    _buf[1] = 0xB4;
    _buf[2] = cmd;
    _buf[15] = 0xff;
    _buf[16] = 0xff;
    _buf[18] = 0xAB;

    crc = cmd + _buf[15] + _buf[16];

    for (i=0; i<12; i++) {
	if (i<len) {
	    _buf[3+i] = data[i];
	} else {
	    _buf[3+i] = 0;
	}
	crc += _buf[3+i];
    }

    _buf[17] = crc;

    for (i = 0; i < 19; i++) {
	_out.write(_buf[i]);
    }
    _out.flush();
}

uint8_t Sds011::_read_byte(void)
{
    while (!_out.available())
	delay(1);
    return _out.read();
}

void Sds011::_ignore_response(void)
{
    delay(300);
    while (_out.available())
	_out.read();
}

void Sds011::_read_response(void)
{
    uint8_t i = 1, b;

    while ((b=_read_byte())!=0xAA)
	;

    _buf[0] = b;

    for(i = 1; i<10; i++) {
	_buf[i] = _read_byte();
    }
}

String Sds011::_buf_to_string(void)
{
{'fraction_non_alphanumeric': 0.1943116634799235, 'fraction_numerical': 0.07050669216061185, 'mean_word_length': 3.1517857142857144, 'pattern_counts': {'":': 0, '<': 23, '<?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': '3976913', 'n_tokens_mistral': 2146, 'n_tokens_neox': 1979, 'n_words': 360}