#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

#include "deepgate.h"

int main(int argc, char *argv[]) {

	unsigned int output_layer_size = 14;

	spiEnable();

	int rows = 1;
	int columns = 516;

	std::ifstream inputFile;
	inputFile.open("input_files/input.csv", std::ios::in);

	for (int i = 0; i < rows; ++i) {

		std::string line;
		std::getline(inputFile, line);

		std::stringstream lineSS(line);

		std::stringstream fileName;
		fileName << "output_files/input_tx_" << i << ".txt";

		std::ofstream outputFile;
		outputFile.open(fileName.str(), std::ios::out | std::ios::trunc);

		for (int j = 0; j < columns; ++j) {

			std::string val;
			std::getline(lineSS, val, ',');

			outputFile << (char)std::stoi(val);
		}

		outputFile.close();

	}

	inputFile.close();

	uint8_t * output = new uint8_t[output_layer_size];

	output = deepgateClassify(14);
	
	std::ofstream outputFile;
	outputFile.open("capture.txt", std::ios::out | std::ios::trunc | std::ios::binary);

	for (unsigned int i = 0; i < output_layer_size; ++i){

		char outputPre = output[i];
		
		float outputPost = 0.0f;

		for (int j = 0; j < 8; j++) {

			if ((outputPre >> (7 - j)) & 0x01)
				outputPost += pow(2, -(j + 1));

		}

		if (i % 4 == 0) {
			outputFile << std::endl;
		}

		outputFile << std::fixed << std::setprecision(5) << outputPost << "\t";

	}

	outputFile.close();

	delete[] output;

	spiDisable();

	return 0;

}