<commit_before>#include "pins-ctrl-2015.hpp"
#include "robot-config.hpp"
#include "TaskSignals.hpp"

#include <rtos.h>
#include <CommModule.hpp>
#include <CommPort.hpp>
#include <CC1201Radio.hpp>
#include <CC1201Config.hpp>
#include <logger.hpp>

/*
 * Information about the radio protocol can be found at:
 * https://www.overleaf.com/2187548nsfdps
 */

/*
* Example of sending a packet.

RTP_t pck;						// Declare a new packet structure

pck.port = 8;					// What port should the packet be routed to?
pck.subclass = 1;				// What subclass of the port is this for?
pck.ack = false;				// Do we need an acknowledgment or is this an acknowledgment response?
pck.sfs = false;				// Is the size field Significant? (Almost always 'No')

// The lines above can be set with the 'RTP_HEADER' macro as an alternative.
// RTP_HEADER(port, subclass, ack, sfs)
//
// pck.header_link = RTP_HEADER(8, 1, false, false);

pck.address = 255;				// Who are we sending the packet to? (255 is broadcast address)
pck.payload_size = 25;			// How many bytes are in the payload of the packet?

memset(pck.payload, 0xFF, pck.payload_size);	// fill with 25 arbitrary bytes

CommModule.send(pck);			// Send it!

// Note: A packet with a requested ACK must be accounted for within the RX callback function.
*/

static const uint8_t BASE_STATION_ADDR = 0x01;

#define ACK_RESPONSE_CODE 0xAA


/**
 * [rx_callback This is executed for a successfully received radio packet.]
 * @param p [none]
 */
void rxCallbackLinkTest(RTP_t* p)
{
	if (p->payload_size > 0) {
		LOG(INIT, "Radio RX working!.\r\n    Received %u byte packet.", p->payload_size);
	}
}


/**
 * [Task_CommCtrl description]
 * @param args [description]
 */
void Task_CommCtrl(void const* args)
{
	// Store the thread's ID
	osThreadId threadID = Thread::gettid();

	// Store our priority so we know what to reset it to if ever needed
	osPriority threadPriority;

	if (threadID != nullptr)
		threadPriority  = osThreadGetPriority(threadID);
	else
		threadPriority = osPriorityIdle;

	// Startup the CommModule interface
	CommModule::Init();

	// Create a new physical hardware communication link
	CC1201 radio(
	    RJ_SPI_BUS,
	    RJ_RADIO_nCS,
	    RJ_RADIO_INT
	);

	if (radio.isConnected() == true) {
		// Load the configuration onto the radio transceiver
		CC1201Config* radioConfig = new CC1201Config();
		radioConfig = CC1201Config::resetConfiguration(radioConfig);
		CC1201Config::loadConfiguration(radioConfig, &radio);
		CC1201Config::verifyConfiguration(radioConfig, &radio);

		// Update the frequency offset
		radio.freqUpdate();

		LOG(INIT, "Radio interface ready on %3.2fMHz!\r\n    Thread ID:\t%u\r\n    Priority:\t%d", radio.freq(), threadID, threadPriority);

		Thread::wait(400);

		// Open a socket for running tests across the link layer
		CommModule::RxHandler(&rxCallbackLinkTest, COMM_PORT_LINK_TEST);
		CommModule::TxHandler((CommLink*)&radio, &CommLink::sendPacket, COMM_PORT_LINK_TEST);
		CommModule::openSocket(COMM_PORT_LINK_TEST);

		Thread::wait(400);

		// The usual way of opening a port.
		CommModule::RxHandler(&rxCallbackLinkTest, COMM_PORT_GAMEPLAY_STROBE);
		CommModule::TxHandler((CommLink*)&radio, &CommLink::sendPacket, COMM_PORT_GAMEPLAY_STROBE);
		CommModule::openSocket(COMM_PORT_GAMEPLAY_STROBE);		// returns true if port was successfully opened.

		// This port won't open since there's no RX callback to invoke. The packets are simply dropped.
		CommModule::RxHandler(&rxCallbackLinkTest, COMM_PORT_CONTROLLER);
		CommModule::TxHandler((CommLink*)&radio, &CommLink::sendPacket, COMM_PORT_CONTROLLER);
		CommModule::openSocket(COMM_PORT_CONTROLLER);

		// There's no TX callback for this port, but it will still open when invoked since it knows where to send an RX packet.
		CommModule::TxHandler((CommLink*)&radio, &CommLink::sendPacket, COMM_PORT_SETPOINT);
		CommModule::RxHandler(&rxCallbackLinkTest, COMM_PORT_SETPOINT);
		CommModule::openSocket(COMM_PORT_SETPOINT);

	} else {
		LOG(FATAL, "No radio interface found!\r\n    Terminating main radio thread.");
		// TODO: Turn on radio error LED here

		// Always keep the link test port open regardless<commit_msg>commenting out ports. something is causing a runtime error whenever one is setup now. need to go back and look at what change broke everything<commit_after>#include "pins-ctrl-2015.hpp"
#include "robot-config.hpp"
#include "TaskSignals.hpp"

#include <rtos.h>
#include <CommModule.hpp>
#include <CommPort.hpp>
#include <CC1201Radio.hpp>
#include <CC1201Config.hpp>
#include <logger.hpp>

/*
 * Information about the radio protocol can be found at:
 * https://www.overleaf.com/2187548nsfdps
 */

/*
* Example of sending a packet.

RTP_t pck;						// Declare a new packet structure

pck.port = 8;					// What port should the packet be routed to?
pck.subclass = 1;				// What subclass of the port is this for?
pck.ack = false;				// Do we need an acknowledgment or is this an acknowledgment response?
pck.sfs = false;				// Is the size field Significant? (Almost always 'No')

// The lines above can be set with the 'RTP_HEADER' macro as an alternative.
// RTP_HEADER(port, subclass, ack, sfs)
//
// pck.header_link = RTP_HEADER(8, 1, false, false);

pck.address = 255;				// Who are we sending the packet to? (255 is broadcast address)
pck.payload_size = 25;			// How many bytes are in the payload of the packet?

memset(pck.payload, 0xFF, pck.payload_size);	// fill with 25 arbitrary bytes

CommModule.send(pck);			// Send it!

// Note: A packet with a requested ACK must be accounted for within the RX callback function.
*/

static const uint8_t BASE_STATION_ADDR = 0x01;

#define ACK_RESPONSE_CODE 0xAA


/**
 * [rx_callback This is executed for a successfully received radio packet.]
 * @param p [none]
 */
void rxCallbackLinkTest(RTP_t* p)
{
	if (p->payload_size > 0) {
		LOG(INIT, "Radio RX working!.\r\n    Received %u byte packet.", p->payload_size);
	}
}


/**
 * [Task_CommCtrl description]
 * @param args [description]
 */
void Task_CommCtrl(void const* args)
{
	// Store the thread's ID
	osThreadId threadID = Thread::gettid();

	// Store our priority so we know what to reset it to if ever needed
	osPriority threadPriority;

	if (threadID != nullptr)
		threadPriority  = osThreadGetPriority(threadID);
	else
		threadPriority = osPriorityIdle;

	// Startup the CommModule interface
	CommModule::Init();

	// Create a new physical hardware communication link
	CC1201 radio(
	    RJ_SPI_BUS,
	    RJ_RADIO_nCS,
	    RJ_RADIO_INT
	);

	if (radio.isConnected() == true) {
		// Load the configuration onto the radio transceiver
		CC1201Config* radioConfig = new CC1201Config();
		radioConfig = CC1201Config::resetConfiguration(radioConfig);
		CC1201Config::loadConfiguration(radioConfig, &radio);
		CC1201Config::verifyConfiguration(radioConfig, &radio);

		LOG(INIT, "Radio interface ready on %3.2fMHz!\r\n    Thread ID:\t%u\r\n    Priority:\t%d", radio.freq(), threadID, threadPriority);

		/*
		// Open a socket for running tests across the link layer
		CommModule::RxHandler(&rxCallbackLinkTest, COMM_PORT_LINK_TEST);
		CommModule::TxHandler((CommLink*)&radio, &CommLink::sendPacket, COMM_PORT_LINK_TEST);
		CommModule::openSocket(COMM_PORT_LINK_TEST);

		// The usual way of opening a port.
		CommModule::RxHandler(&rxCallbackLinkTest, COMM_PORT_GAMEPLAY_STROBE);
		CommModule::TxHandler((CommLink*)&radio, &CommLink::sendPacket, COMM_PORT_GAMEPLAY_STROBE);
		CommModule::openSocket(COMM_PORT_GAMEPLAY_STROBE);		// returns true if port was successfully opened.

		// This port won't open since there's no RX callback to invoke. The packets are simply dropped.
		CommModule::RxHandler(&rxCallbackLinkTest, COMM_PORT_CONTROLLER);
		CommModule::TxHandler((CommLink*)&radio, &CommLink::sendPacket, COMM_PORT_CONTROLLER);
		CommModule::openSocket(COMM_PORT_CONTROLLER);

		// There's no TX callback for this port, but it will still open when invoked since it knows where to send an RX packet.
		CommModule::TxHandler((CommLink*)&radio, &CommLink::sendPacket, COMM_PORT_SETPOINT);
		CommModule::RxHandler(&rxCallbackLinkTest, COMM_PORT_SETPOINT);
		CommModule::openSocket(COMM_PORT_SETPOINT);
		*/

	} else {
		LOG(FATAL, "No radio interface found!\r\n    Terminating main radio thread.");
		// TODO: Turn on radio error LED here

		// Always keep the link test port open regardless
{'fraction_non_alphanumeric': 0.10941811637672466, 'fraction_numerical': 0.016556688662267546, 'mean_word_length': 4.459070072036673, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '6716032', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2661, 'n_words': 818}