#include "arduino.h"
#include "nodeCodeHeader.h"

nodeReadings::nodeReadings() {
}

void nodeReadings::setup(){
    // make serial connection at 115200 baud
    Serial.begin(115200);

    // tell display what settings to use
    display.begin(i2c_adress, true);
    display.clearDisplay();

    // tell sensors to start reading
    dht.begin();
    sgp.begin();

    pinMode(MICPIN, INPUT);
    pinMode(DHTPIN, INPUT);

}

void nodeReadings::loop() {
    // loop the websocket connection so it stays alive
    webSocket.loop();

    // update when interval is met
    if (currentMillis - lastMillis >= interval){
        lastMillis = millis();
        update();
    }

    // update the counter
    currentMillis = millis();
}

void nodeReadings::resetValues() {
    counter = 0;
    eCO2 = 0;
    TVOC = 0;
    temperature = 0;
    humidity = 0;
    currentMillis = 0;
    lastMillis = 0;
    errorSGP30 = false;
    errorDHT11 = false;
    noise = false;
}

// hexdump function for websockets binary handler
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
	const uint8_t* src = (const uint8_t*) mem;
	USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
	for(uint32_t i = 0; i < len; i++) {
		if(i % cols == 0) {
			USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
		}
		USE_SERIAL.printf("%02X ", *src);
		src++;
	}
	USE_SERIAL.printf("\n");
}