<commit_before>#ifndef FPGA_COMM_CPP
#define FPGA_COMM_CPP

#include "FPGAComm.h"

#include <wirish/wirish.h>
#include <string.h>

// declare the singleton object
FPGAComm FPGAComm::singleton;

// last packet and interrupt handlers
volatile FPGAInPacket lastPacket;
void FPGACommIH();

FPGAComm* FPGAComm::getInstance()
{
	if(singleton.initialized) {
		return &singleton;
	}

	return NULL;
}

FPGAInPacket FPGAComm::getLastPacket()
{
	newData = false;

	FPGAInPacket* returnPacket = (FPGAInPacket*)&lastPacket;
	return *returnPacket;
}

void FPGAComm::init(FPGAPinInLayout inLayout, FPGAPinOutLayout outLayout)
{
	in = inLayout;
	out = outLayout;

	// configure the inputs
	pinMode(in.leftWall, INPUT);
	pinMode(in.frontWall, INPUT);
	pinMode(in.rightWall, INPUT);

	// attach the interrupt
	attachInterrupt(in.interrupt, FPGACommIH, RISING);

	// configure the outputs
	pinMode(out.drive, OUTPUT);
	pinMode(out.turn, OUTPUT);
	
	pinMode(out.dataStart, OUTPUT);
	pinMode(out.dataStart + 1, OUTPUT);
	pinMode(out.dataStart + 2, OUTPUT);
	pinMode(out.dataStart + 3, OUTPUT);
	
	pinMode(out.interrupt, OUTPUT);

	// zero out the last packet
	memset((FPGAInPacket*)(&lastPacket), 0, sizeof(FPGAInPacket));
}

void FPGAComm::deinit()
{
	detachInterrupt(in.interrupt);

	this->initialized = false;
}

void FPGAComm::send(FPGAOutPacket outPacket)
{
	// write the command
	if (outPacket.drive) {
		digitalWrite(out.drive, HIGH);
		digitalWrite(out.turn, LOW);
	}
	else {
		digitalWrite(out.drive, LOW);
		digitalWrite(out.turn, HIGH);
	}

	// write the data
	for (int n = 0; n < 4; n++) {
		if ((outPacket.data >> n) & 1) {
			digitalWrite(out.dataStart + n, HIGH);
		}
		else {
			digitalWrite(out.dataStart + n, LOW);
		}
	}

	// bring int high, allow time to settle
	digitalWrite(out.interrupt, HIGH);
	delay(100);
	digitalWrite(out.interrupt, LOW);
}

void FPGAComm::receive(FPGAInPacket* inPacket)
{
	inPacket->leftWall = digitalRead(in.leftWall);
	inPacket->frontWall = digitalRead(in.frontWall);
	inPacket->rightWall = digitalRead(in.rightWall);

	newData = true;
}

bool FPGAComm::isNewData()
{
	return newData;
}

// intterrupt handler
void FPGACommIH()
{
	// get an instance of the comm protocol
	FPGAComm* comm = FPGAComm::getInstance();

	comm->receive((FPGAInPacket*)&lastPacket);<commit_msg>getInstance always returns singleton Need an instance to init.<commit_after>#ifndef FPGA_COMM_CPP
#define FPGA_COMM_CPP

#include "FPGAComm.h"

#include <wirish/wirish.h>
#include <string.h>

// declare the singleton object
FPGAComm FPGAComm::singleton;

// last packet and interrupt handlers
volatile FPGAInPacket lastPacket;
void FPGACommIH();

FPGAComm* FPGAComm::getInstance()
{
	return &singleton;
}

FPGAInPacket FPGAComm::getLastPacket()
{
	newData = false;

	FPGAInPacket* returnPacket = (FPGAInPacket*)&lastPacket;
	return *returnPacket;
}

void FPGAComm::init(FPGAPinInLayout inLayout, FPGAPinOutLayout outLayout)
{
	in = inLayout;
	out = outLayout;

	// configure the inputs
	pinMode(in.leftWall, INPUT);
	pinMode(in.frontWall, INPUT);
	pinMode(in.rightWall, INPUT);

	// attach the interrupt
	attachInterrupt(in.interrupt, FPGACommIH, RISING);

	// configure the outputs
	pinMode(out.drive, OUTPUT);
	pinMode(out.turn, OUTPUT);
	
	pinMode(out.dataStart, OUTPUT);
	pinMode(out.dataStart + 1, OUTPUT);
	pinMode(out.dataStart + 2, OUTPUT);
	pinMode(out.dataStart + 3, OUTPUT);
	
	pinMode(out.interrupt, OUTPUT);

	// zero out the last packet
	memset((FPGAInPacket*)(&lastPacket), 0, sizeof(FPGAInPacket));
}

void FPGAComm::deinit()
{
	detachInterrupt(in.interrupt);

	this->initialized = false;
}

void FPGAComm::send(FPGAOutPacket outPacket)
{
	// write the command
	if (outPacket.drive) {
		digitalWrite(out.drive, HIGH);
		digitalWrite(out.turn, LOW);
	}
	else {
		digitalWrite(out.drive, LOW);
		digitalWrite(out.turn, HIGH);
	}

	// write the data
	for (int n = 0; n < 4; n++) {
		if ((outPacket.data >> n) & 1) {
			digitalWrite(out.dataStart + n, HIGH);
		}
		else {
			digitalWrite(out.dataStart + n, LOW);
		}
	}

	// bring int high, allow time to settle
	digitalWrite(out.interrupt, HIGH);
	delay(100);
	digitalWrite(out.interrupt, LOW);
}

void FPGAComm::receive(FPGAInPacket* inPacket)
{
	inPacket->leftWall = digitalRead(in.leftWall);
	inPacket->frontWall = digitalRead(in.frontWall);
	inPacket->rightWall = digitalRead(in.rightWall);

	newData = true;
}

bool FPGAComm::isNewData()
{
	return newData;
}

// intterrupt handler
void FPGACommIH()
{
	// get an instance of the comm protocol
	FPGAComm* comm = FPGAComm::getInstance();

	comm->receive((FPGAInPacket*)&lastPacket);

	digitalWrite(BOARD_LED_PIN, HIGH);
{'fraction_non_alphanumeric': 0.13493091537132987, 'fraction_numerical': 0.004317789291882556, 'mean_word_length': 4.932138284250961, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '4402619', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1744, 'n_words': 364}