//#include <Arduino.h>



/*/
/*/

#include "TestBlink.h"


#include "Debug.h"
#include "Tests.h"
#include "Sensor.hpp"
#include "CompositeSensor.hpp"
#include "QueueArray.hpp"
#include "ZonesManager.hpp"
#include "CompositeSensorBuilder.hpp"
#include "AbstractSensor.hpp"
#include "FakeCompositeSensorBuilder.hpp"

#include "MemoryFree.h"

#include "SDManager.hpp"

//#include "NormalSensor.hpp"



#ifdef ON_ESP32
	#include "BluetoothManager.hpp"
#endif


#ifndef ON_ESP32
	//#include <AltSoftSerial.h>
	#define SIGNAL_PIN LED_BUILTIN //Pin del segnale del motorino vibrante

#elif defined(ON_ARDUINO)

#endif

//#define NUM_OF_SENSORS_CS1	4 //Numero di sensori per ogni Composite Sensor
//#define NUM_OF_COMPSENSS	3 //Numero di sensori composti (CS).

//using namespace MyBlindView;
//using namespace MemTrack;

ZonesManager* zm = NULL;
#ifndef ON_ESP32
	//AltSoftSerial mySerial;
#endif

int i = 0;
int CSPin = 5;
boolean stopBT = false;

//static_assert(ON_ESP32>0, "Prova");

void setup_Sensors() {
	PTESTLNF("Mamma0");
	//CompositeSensor** css = CompositeSensorBuilder::createCSs();
	CompositeSensor** css = CompositeSensorBuilder::setup();
	//int num = sizeof
	int n = sizeof(css)/sizeof(css[0]);
	PTESTLNF("Mamma1");
	zm = new ZonesManager(css, n, CSPin);
	PTESTLNF("Mamma2");
}


void setup_FakeSensors() {
	//PTESTLNF("Mamma0");
	CompositeSensor** css = FakeCompositeSensorBuilder::setup(SensorType::linear);
	//CompositeSensor** css = FakeCompositeSensorBuilder1::setup(SensorType::linear);
	//int num = sizeof
	int n = FakeCompositeSensorBuilder::getCompositeSensorNumber();
	//PTESTLNF("Mamma1");
	//Serial.print("Numero Csensori:");
	//Serial.println(n);
	zm = new ZonesManager(css, n, CSPin);
	//PTESTLNF("Mamma2");
}

void startSensors() {
	//PTESTLNF("!!!START Sensors!!!");
	zm->manageCompositeSensors();	
}

void printSensors() {
	CompositeSensor** css = zm->getCompositeSensors();
	int n = FakeCompositeSensorBuilder::getCompositeSensorNumber();
	for (int i = 0; i < n; i++) {
		PTESTLN(css[i]->toString());
	}
}

void manageBluetooth() {
	BluetoothManager* bm = BluetoothManager::getInstance();
	int ret = bm->manage();
	if (ret != -2000) {
		Serial.print("Comando = ");
		Serial.println(ret);
	}
	//delay1.start(200);
}

void startBluetooth() {
#ifdef ON_ESP32
	BluetoothManager* bm = BluetoothManager::getInstance();
	//VirtualDelay delay1;
	while (!stopBT) {
		//if (delay1.elapsed() || !delay1.running) {
			int ret = bm->manage();
			PTESTF("Comando = ");
			PTESTLN(ret);
			//delay1.start(200);
		//}
	}
#endif
}

void testSD(){
	SDManager::setup(5);
	SDManager::appendFile(SDManager::path, " - Misura effettiva: " + STRING(10));
	SDManager::listDir("/",0);
}

void testPrint(){
	PRF("mamma %d",1);
	//PRFTESTF("Zio %f",0.3);
}

//*/
void tests() {
	//testPins();
	//testOutputPin(4);
	//testInputPin(4);
	//testHCSR04SinglePin();
	//testSquareWaver();
	//testPrint();
	/**/testFREERTOS();
	//testBlink();
	//manageBluetooth();
	//startBluetooth();/*/
}


void setup() {
	Serial.begin(115200);
	pinMode(LED_BUILTIN, OUTPUT);
	//PRFTEST("Setup: priority = ", uxTaskPriorityGet(NULL));
	/*/tests();
	/*/
	PRINT_MEMORY("Memory free BEFORE Setup()");
	//setup_Sensors();
	setup_FakeSensors();
	PRINT_MEMORY("Memory free AFTER Setup()");
	PTESTLNF("Print current sensor setup PRE");
	printSensors();
	PTESTLNF("Print current sensor setup POST");
	//setup_Serial();
	startSensors();//*/
	//startBluetooth();	
}


void loop() {
	delay(5000);
	Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!LOOP!!!!!!!!!!!!!!!!!!!!!!!!!!");
	//tests();
	/*if (delay1.elapsed() || !delay1.running) {
		manageBluetooth();
		PF("Ciso!\n");
	}*/
}