<commit_before>/*
 * PiCAM Main Project file
 */
#include <chrono> //sleeping
#include <thread> //sleeping
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <string>
#include <signal.h>
#include <raspicam/raspicam.h>
#include <wiringPi.h>
#include <sys/stat.h>

#include <vector>
#include <complex>
#include "gpu_fft/motion.h"
#include "gpu_fft/gfft.h"

#include "neopixelring.h"

using namespace std;

#define RED_LED 6
#define BLU_LED 19
#define GRN_LED 13
#define M_PI    3.14159265358979323846

static void ctrl_c_handler(int signum)
{
		neopixelClose();
		//Camera->release();
		cout << "CTL-C Handled." << endl;
		exit(1);
}

static void setup_handlers(void)
{
    struct sigaction sa;
		sa.sa_handler = ctrl_c_handler;

    sigaction(SIGINT, &sa, NULL);
    sigaction(SIGTERM, &sa, NULL);
}

void millisleep(uint t){
	std::this_thread::sleep_for(std::chrono::milliseconds(t));
}

void microsleep(uint t){
	std::this_thread::sleep_for(std::chrono::microseconds(t));
}

void gpioInit() {
	wiringPiSetupGpio();

	pinMode(RED_LED, OUTPUT);
	pinMode(BLU_LED, OUTPUT);
	pinMode(GRN_LED, OUTPUT);
}

void ledPulse(int ledpin, uint onTime=3850) {
	digitalWrite(ledpin, HIGH);
	microsleep(onTime);
	digitalWrite(ledpin, LOW);
}

void led2Pulse(int led1pin, int led2pin, uint onTime=3850) {
	digitalWrite(led1pin, HIGH);
	digitalWrite(led2pin, HIGH);
	microsleep(onTime);
	digitalWrite(led1pin, LOW);
	digitalWrite(led2pin, LOW);
}

void flashGrab(raspicam::RaspiCam* cam, int led) {
	cout<<"Capturing image"<<endl;

	if (!cam->grab_dontwait()) {
		cerr<<"Error grabbing frame"<<endl;
	}
	ledPulse(led);
}

uint looptime;
uint count;

void ledPulseTime(uint onTime, uint offTime, uint totalTime, int led1, int led2, int led3) {
	looptime = ((onTime+offTime)*2/1000);
  count = (totalTime/looptime)+1;
	//cout<<"Doing "<<count<<" loops @ "<<looptime<<"ms each"<<endl;

	for (uint x=0;x<=count;x++)
	{
		ledPulse(led1,onTime);
		microsleep(offTime);
		//led2Pulse(led2,led3,onTime);
		ledPulse(led3,onTime);
		microsleep(offTime);
		//ledPulse(led3,onTime);
		//microsleep(offTime);
	}
}

void flashRGBGrab(raspicam::RaspiCam* cam, int led1, int led2, int led3, uint onTime, uint offTime) {
	//cout<<"Capturing RGB image"<<endl;

	ledPulseTime(onTime,offTime,20,led1,led2,led3);
	if (!cam->grab_dontwait()) {
		cerr<<"Error grabbing frame"<<endl;
	}
	//msleep(5);
	ledPulseTime(onTime,offTime,100,led1,led2,led3);

	//msleep(50);
}

void writeOut(raspicam::RaspiCam* cam, std::string filename, uint filesize) {
	unsigned char *data = new unsigned char[cam->getImageTypeSize( raspicam::RASPICAM_FORMAT_RGB )];
	cam->retrieve( data );
	std::ofstream outFile ( filename.c_str(), std::ios::binary );

	outFile<<"P6\n"<<cam->getWidth() <<" "<<cam->getHeight() <<" 255\n";
	outFile.write( (char*)data, filesize );
	//cout<<"Image saved as "<<filename<<endl;
	delete data;
}

/*void testCB(void* x) {
	cout<<"Test Callback"<<endl;
	//return 0;
}*/

void camInit(raspicam::RaspiCam* Camera) {
	cout<<"Initing cam"<<endl;

	// override camera settings here
	Camera->setWidth(640);
	Camera->setHeight(480);
	//Camera->setShutterSpeed(5000);
	//Camera->setExposure(raspicam::RASPICAM_EXPOSURE_VERYLONG);
	//uint tdata = 1;
	//Camera->setUserCallback(&testCB,(void*)&tdata);

	if ( !Camera->open()) {
		cerr<<"Error opening camera"<<endl;
		exit(1);
	}

	millisleep(2000); //2s
	Camera->grab();
	millisleep(100); // 0.1s
	Camera->grab();
	millisleep(100); // 0.1s

	struct timespec start, finish;
	double elapsed;
	clock_gettime(CLOCK_MONOTONIC, &start);
	Camera->grab();
	clock_gettime(CLOCK_MONOTONIC, &finish);
	elapsed = (finish.tv_sec - start.tv_sec);
  elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
	cout<<"grab timing: "<<elapsed<<endl;

}

std::vector<std::vector<std::vector<double> > > toSquareImage(unsigned char *input, unsigned width, unsigned height, int log2_N) {
	unsigned N = 1<<log2_N;

	if(N>height || N>width) {
		cerr << "Cannot crop image for convolution. 2^(Log2(N)) must be less than image witdth and height!" << endl;
		//return;
	}

	unsigned bufferUD = (height-N)/2;
	unsigned bufferLR = (width-N)/2;

	unsigned startIndex = width*bufferUD*3+bufferLR;

	std::vector<std::vector<std::vector<double> > > out;
	out.push_back(std::vector<std::vector<double> > ());
	out.push_back(std::vector<std::vector<double> > ());
	out.push_back(std::vector<std::vector<double> > ());

	unsigned x, y, i=startIndex;
	for(y=0;y<N;y++) {
		out.at(0).push_back(std::vector<double> ());
		out.at(1).push_back(std::vector<double> ());
		out.at(2).push_back(std::vector<double> ());
		for(x=0; x<N; x++) {
			out.at(0).at(y).push_back((double)input[i]/255);
			out.at(1).at(y).push_back((double)input[i+1]/255);
			out.at(2).at(y).push_back((double)input[i+2]/255);
			i+= 3;
		}
		i += bufferLR*6; // get from right edge of crop to next left edge of crop
	}

	return out;

}

void writeOutCrop(raspicam::RaspiCam* cam, std::string filename, uint filesize) {
	unsigned log2_N = 8;
	unsigned N = 1<<log2_N;

	unsigned char *data = new unsigned char[cam->getImageTypeSize( raspicam::RASPICAM_FORMAT_RGB )];
	cam->retrieve( data );<commit_msg>Cropping fix<commit_after>/*
 * PiCAM Main Project file
 */
#include <chrono> //sleeping
#include <thread> //sleeping
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <string>
#include <signal.h>
#include <raspicam/raspicam.h>
#include <wiringPi.h>
#include <sys/stat.h>

#include <vector>
#include <complex>
#include "gpu_fft/motion.h"
#include "gpu_fft/gfft.h"

#include "neopixelring.h"

using namespace std;

#define RED_LED 6
#define BLU_LED 19
#define GRN_LED 13
#define M_PI    3.14159265358979323846

static void ctrl_c_handler(int signum)
{
		neopixelClose();
		//Camera->release();
		cout << "CTL-C Handled." << endl;
		exit(1);
}

static void setup_handlers(void)
{
    struct sigaction sa;
		sa.sa_handler = ctrl_c_handler;

    sigaction(SIGINT, &sa, NULL);
    sigaction(SIGTERM, &sa, NULL);
}

void millisleep(uint t){
	std::this_thread::sleep_for(std::chrono::milliseconds(t));
}

void microsleep(uint t){
	std::this_thread::sleep_for(std::chrono::microseconds(t));
}

void gpioInit() {
	wiringPiSetupGpio();

	pinMode(RED_LED, OUTPUT);
	pinMode(BLU_LED, OUTPUT);
	pinMode(GRN_LED, OUTPUT);
}

void ledPulse(int ledpin, uint onTime=3850) {
	digitalWrite(ledpin, HIGH);
	microsleep(onTime);
	digitalWrite(ledpin, LOW);
}

void led2Pulse(int led1pin, int led2pin, uint onTime=3850) {
	digitalWrite(led1pin, HIGH);
	digitalWrite(led2pin, HIGH);
	microsleep(onTime);
	digitalWrite(led1pin, LOW);
	digitalWrite(led2pin, LOW);
}

void flashGrab(raspicam::RaspiCam* cam, int led) {
	cout<<"Capturing image"<<endl;

	if (!cam->grab_dontwait()) {
		cerr<<"Error grabbing frame"<<endl;
	}
	ledPulse(led);
}

uint looptime;
uint count;

void ledPulseTime(uint onTime, uint offTime, uint totalTime, int led1, int led2, int led3) {
	looptime = ((onTime+offTime)*2/1000);
  count = (totalTime/looptime)+1;
	//cout<<"Doing "<<count<<" loops @ "<<looptime<<"ms each"<<endl;

	for (uint x=0;x<=count;x++)
	{
		ledPulse(led1,onTime);
		microsleep(offTime);
		//led2Pulse(led2,led3,onTime);
		ledPulse(led3,onTime);
		microsleep(offTime);
		//ledPulse(led3,onTime);
		//microsleep(offTime);
	}
}

void flashRGBGrab(raspicam::RaspiCam* cam, int led1, int led2, int led3, uint onTime, uint offTime) {
	//cout<<"Capturing RGB image"<<endl;

	ledPulseTime(onTime,offTime,20,led1,led2,led3);
	if (!cam->grab_dontwait()) {
		cerr<<"Error grabbing frame"<<endl;
	}
	//msleep(5);
	ledPulseTime(onTime,offTime,100,led1,led2,led3);

	//msleep(50);
}

void writeOut(raspicam::RaspiCam* cam, std::string filename, uint filesize) {
	unsigned char *data = new unsigned char[cam->getImageTypeSize( raspicam::RASPICAM_FORMAT_RGB )];
	cam->retrieve( data );
	std::ofstream outFile ( filename.c_str(), std::ios::binary );

	outFile<<"P6\n"<<cam->getWidth() <<" "<<cam->getHeight() <<" 255\n";
	outFile.write( (char*)data, filesize );
	//cout<<"Image saved as "<<filename<<endl;
	delete data;
}

/*void testCB(void* x) {
	cout<<"Test Callback"<<endl;
	//return 0;
}*/

void camInit(raspicam::RaspiCam* Camera) {
	cout<<"Initing cam"<<endl;

	// override camera settings here
	Camera->setWidth(640);
	Camera->setHeight(480);
	//Camera->setShutterSpeed(5000);
	//Camera->setExposure(raspicam::RASPICAM_EXPOSURE_VERYLONG);
	//uint tdata = 1;
	//Camera->setUserCallback(&testCB,(void*)&tdata);

	if ( !Camera->open()) {
		cerr<<"Error opening camera"<<endl;
		exit(1);
	}

	millisleep(2000); //2s
	Camera->grab();
	millisleep(100); // 0.1s
	Camera->grab();
	millisleep(100); // 0.1s

	struct timespec start, finish;
	double elapsed;
	clock_gettime(CLOCK_MONOTONIC, &start);
	Camera->grab();
	clock_gettime(CLOCK_MONOTONIC, &finish);
	elapsed = (finish.tv_sec - start.tv_sec);
  elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
	cout<<"grab timing: "<<elapsed<<endl;

}

std::vector<std::vector<std::vector<double> > > toSquareImage(unsigned char *input, unsigned width, unsigned height, int log2_N) {
	unsigned N = 1<<log2_N;

	if(N>height || N>width) {
		cerr << "Cannot crop image for convolution. 2^(Log2(N)) must be less than image witdth and height!" << endl;
		//return;
	}

	unsigned bufferUD = (height-N)/2;
	unsigned bufferLR = (width-N)/2;

	unsigned startIndex = (width*bufferUD+bufferLR)*3;

	std::vector<std::vector<std::vector<double> > > out;
	out.push_back(std::vector<std::vector<double> > ());
	out.push_back(std::vector<std::vector<double> > ());
	out.push_back(std::vector<std::vector<double> > ());

	unsigned x, y, i=startIndex;
	for(y=0;y<N;y++) {
		out.at(0).push_back(std::vector<double> ());
		out.at(1).push_back(std::vector<double> ());
		out.at(2).push_back(std::vector<double> ());
		for(x=0; x<N; x++) {
			out.at(0).at(y).push_back((double)input[i]/255);
			out.at(1).at(y).push_back((double)input[i+1]/255);
			out.at(2).at(y).push_back((double)input[i+2]/255);
			i+= 3;
		}
		i += bufferLR*6; // get from right edge of crop to next left edge of crop
	}

	return out;

}

void writeOutCrop(raspicam::RaspiCam* cam, std::string filename, uint filesize) {
	unsigned log2_N = 8;
	unsigned N = 1<<log2_N;

	unsigned char *data = new unsigned char[cam->getImageTypeSize( raspicam::RASPICAM_FORMAT_RGB )];
	cam->retrieve( data );
{'fraction_non_alphanumeric': 0.18118399379905048, 'fraction_numerical': 0.02926073054936537, 'mean_word_length': 5.427148194271482, 'pattern_counts': {'":': 0, '<': 211, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '2901789', 'n_tokens_mistral': 4433, 'n_tokens_neox': 4163, 'n_words': 773}