/*
 * The main function and everything related to starting the application
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#include "inc-gen/system.h"
#include "inc/CanConnector.h"
#include "inc/Gamepad.h"
#include "inc/PWM.h"

#define PARAM_MAXCMP 5
#define PARAM_MASTER "master"
#define PARAM_SLAVE "slave"

int main(int argc, char* argv[]) {
    // init the system
    initialize_system();

    // start a gamepad thread
    pthread_t pt_gamepad;
    pthread_create(&pt_gamepad, NULL, gp_worker, NULL);

    // start a pwm thread
    pthread_t pt_pwm;
    pthread_create(&pt_pwm, NULL, pwm_worker, NULL);

    // MASTER MODE (master as parameter)
    if(argc >= 2 && strncmp(argv[1], PARAM_MASTER, PARAM_MAXCMP) == 0) {
        // TODO do master stuff
    }

    // SLAVE MODE (slave as parameter)
    else if(argc >= 2 && strncmp(argv[1], PARAM_SLAVE, PARAM_MAXCMP) == 0) {
        // TODO do slave stuff
    }

    // DEFAULT MODE (no parameter)
    else {
        // run endlessly
        while(1) {
            run_system();
            usleep(1000);
        }
    }

    
}