Example #1
0
int main() {
    log_mc.open(base_log_dir + "logs_mc.txt", std::ios::out | std::ios::app);
    log_bms.open(base_log_dir + "logs_bms.txt", std::ios::out | std::ios::app);
    log_main_ard.open(base_log_dir + "logs_main_arduino.txt",
            std::ios::out | std::ios::app);
    log_evdc.open(base_log_dir + "logs_evdc.txt",
            std::ios::out | std::ios::app);
    log_rear_ard.open(base_log_dir + "logs_read_arduino.txt",
            std::ios::out | std::ios::app);

    gettimeofday(&log_mc_prev_time, NULL);
    gettimeofday(&log_bms_prev_time, NULL);
    gettimeofday(&log_main_ard_prev_time, NULL);
    gettimeofday(&log_evdc_prev_time, NULL);
    gettimeofday(&log_rear_ard_prev_time, NULL);

    CAN can;
    BT bt(10);
    bt.connect();

    bzero(amp_history, TIME_LEFT_HIST_LEN * sizeof(uint16_t));
    gettimeofday(&prev_time, NULL);
    curr_time = prev_time;

    canframe_t *frame = (canframe_t*) malloc(sizeof(canframe_t));
    uint8_t bt_buffer[BT::DATA_LENGTH];

    while (1) {
        if (can.read(frame) > 0) {
            std::cout << "Error reading message or no message to read" << std::endl;
        } else {
            if (0 == process_data_for_sending(bt_buffer, frame)) {
                if (bt.send(bt_buffer) == -1) {
                    std::cout << "Attempting to reconnect" << std::endl;
                    bt.connect();
                }
            }
            log_to_file(frame);
        }
    }

    free(frame);
    bt.disconnect();
    log_mc.close();
    log_bms.close();
    log_main_ard.close();
    log_evdc.close();
    log_rear_ard.close();
    return 0;
}
bool assertCan(CAN& can, unsigned int expectedId, const int* expectedData, const int expectedLen, int timeoutMs)
{
    Timer t;
    CANMessage msg;
    t.start();

    while (t.read_ms() < timeoutMs)
    {
        if (can.read(msg) && msg.id == expectedId)
        {
            if (expectedLen != msg.len)
            {
                return false;
            }

            return std::memcmp(msg.data, expectedData, msg.len);
        }
    }

    return false;
}