<commit_before>/*
 *
 *  Derived from CanDemo.pde, example code that came with the
 *  chipKIT Network Shield libraries.
 */

#include "chipKITCAN.h"
#include "chipKITUSBDevice.h"
#include "bitfield.h"
#include "canutil_chipkit.h"
#include "usbutil.h"
#include "cJSON.h"
#include "signals.h"

#define VERSION_CONTROL_COMMAND 0x80
#define RESET_CONTROL_COMMAND 0x81

// USB
#define DATA_ENDPOINT 1

extern char* MESSAGE_SET;
extern int CAN_BUS_COUNT;
extern int SIGNAL_COUNT;

char* VERSION = "2.0-pre";
CAN can1(CAN::CAN1);
CAN can2(CAN::CAN2);

// USB

#define DATA_ENDPOINT 1

USB_HANDLE USB_OUTPUT_HANDLE = 0;
CanUsbDevice usbDevice = {USBDevice(usbCallback), DATA_ENDPOINT, ENDPOINT_SIZE};

int receivedMessages = 0;
unsigned long lastSignificantChangeTime;
int receivedMessagesAtLastMark = 0;

/* Forward declarations */

void initializeAllCan();
void receiveCan(CanBus*);
void receiveWriteRequest(char*);
void decodeCanMessage(int id, uint8_t* data);
void checkIfStalled();

void setup() {
    Serial.begin(115200);

    initializeUsb(&usbDevice);
    initializeAllCan();
    lastSignificantChangeTime = millis();
}

void loop() {
    for(int i = 0; i < CAN_BUS_COUNT; i++) {
        receiveCan(&getCanBuses()[i]);
    }
    USB_OUTPUT_HANDLE = readFromHost(
            &usbDevice, USB_OUTPUT_HANDLE, &receiveWriteRequest);
    checkIfStalled();
}

void initializeAllCan() {
    for(int i = 0; i < CAN_BUS_COUNT; i++) {
        initializeCan(&(getCanBuses()[i]));
    }
}

void mark() {
    lastSignificantChangeTime = millis();
    receivedMessagesAtLastMark = receivedMessages;
}

void checkIfStalled() {
    // a workaround to stop CAN from crashing indefinitely
    // See these tickets in Redmine:
    // https://fiesta.eecs.umich.edu/issues/298
    // https://fiesta.eecs.umich.edu/issues/244
    if(receivedMessagesAtLastMark + 10 < receivedMessages) {
        mark();
    }

    if(receivedMessages > 0 && receivedMessagesAtLastMark > 0
            && millis() > lastSignificantChangeTime + 500) {
        Serial.print("timeNow: ");
        Serial.println(millis(), DEC);
        Serial.print("lastSignificantChangeTime: ");
        Serial.println(lastSignificantChangeTime, DEC);
        Serial.print("receivedMessages: ");
        Serial.println(receivedMessages, DEC);
        Serial.print("receivedMessagesAtLastMark: ");
        Serial.println(receivedMessagesAtLastMark, DEC);
        initializeAllCan();
        delay(200);
        mark();
    }
}

void receiveWriteRequest(char* message) {
    if(message != NULL) {
        cJSON *root = cJSON_Parse(message);
        if(root != NULL) {
            Serial.print("Received write request: ");
            Serial.println(message);

            char* name = cJSON_GetObjectItem(root, "name")->valuestring;
            CanSignal* signal = lookupSignal(name, getSignalList(),
                    SIGNAL_COUNT);
            if(signal != NULL) {
                sendCanSignal(signal,
                        cJSON_GetObjectItem(root, "value"),
                        signal->writeHandler, getSignalList(),
                        SIGNAL_COUNT);<commit_msg>Remove debugging print statements.<commit_after>/*
 *
 *  Derived from CanDemo.pde, example code that came with the
 *  chipKIT Network Shield libraries.
 */

#include "chipKITCAN.h"
#include "chipKITUSBDevice.h"
#include "bitfield.h"
#include "canutil_chipkit.h"
#include "usbutil.h"
#include "cJSON.h"
#include "signals.h"

#define VERSION_CONTROL_COMMAND 0x80
#define RESET_CONTROL_COMMAND 0x81

// USB
#define DATA_ENDPOINT 1

extern char* MESSAGE_SET;
extern int CAN_BUS_COUNT;
extern int SIGNAL_COUNT;

char* VERSION = "2.0-pre";
CAN can1(CAN::CAN1);
CAN can2(CAN::CAN2);

// USB

#define DATA_ENDPOINT 1

USB_HANDLE USB_OUTPUT_HANDLE = 0;
CanUsbDevice usbDevice = {USBDevice(usbCallback), DATA_ENDPOINT, ENDPOINT_SIZE};

int receivedMessages = 0;
unsigned long lastSignificantChangeTime;
int receivedMessagesAtLastMark = 0;

/* Forward declarations */

void initializeAllCan();
void receiveCan(CanBus*);
void receiveWriteRequest(char*);
void decodeCanMessage(int id, uint8_t* data);
void checkIfStalled();

void setup() {
    Serial.begin(115200);

    initializeUsb(&usbDevice);
    initializeAllCan();
    lastSignificantChangeTime = millis();
}

void loop() {
    for(int i = 0; i < CAN_BUS_COUNT; i++) {
        receiveCan(&getCanBuses()[i]);
    }
    USB_OUTPUT_HANDLE = readFromHost(
            &usbDevice, USB_OUTPUT_HANDLE, &receiveWriteRequest);
    checkIfStalled();
}

void initializeAllCan() {
    for(int i = 0; i < CAN_BUS_COUNT; i++) {
        initializeCan(&(getCanBuses()[i]));
    }
}

void mark() {
    lastSignificantChangeTime = millis();
    receivedMessagesAtLastMark = receivedMessages;
}

void checkIfStalled() {
    // a workaround to stop CAN from crashing indefinitely
    // See these tickets in Redmine:
    // https://fiesta.eecs.umich.edu/issues/298
    // https://fiesta.eecs.umich.edu/issues/244
    if(receivedMessagesAtLastMark + 10 < receivedMessages) {
        mark();
    }

    if(receivedMessages > 0 && receivedMessagesAtLastMark > 0
            && millis() > lastSignificantChangeTime + 500) {
        initializeAllCan();
        delay(200);
        mark();
    }
}

void receiveWriteRequest(char* message) {
    if(message != NULL) {
        cJSON *root = cJSON_Parse(message);
        if(root != NULL) {
            Serial.print("Received write request: ");
            Serial.println(message);

            char* name = cJSON_GetObjectItem(root, "name")->valuestring;
            CanSignal* signal = lookupSignal(name, getSignalList(),
                    SIGNAL_COUNT);
            if(signal != NULL) {
                sendCanSignal(signal,
                        cJSON_GetObjectItem(root, "value"),
                        signal->writeHandler, getSignalList(),
                        SIGNAL_COUNT);
{'fraction_non_alphanumeric': 0.11921768707482994, 'fraction_numerical': 0.014285714285714285, 'mean_word_length': 3.150317572335921, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 4, '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': '2206766', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1811, 'n_words': 392}