Example #1
0
// main function
void main(){
	WDTCTL = WDTPW + WDTTMSEL+ WDTCNTCL + 0; // setup using the watchdog timer, using the 8MHz clock, 00 us /32768
	BCSCTL1 = CALBC1_8MHZ;    // 8Mhz calibration for clock
	DCOCTL  = CALDCO_8MHZ;

	init_timer(); 						// initialize timer
	init_button(); 						// initialize the button

	// clear/initialize the global variables
	xaxis_calibrate=0;
	yaxis_calibrate=0;
	x_axis=0;
	y_axis=0;
	z_axis=1;
	count= 100;

	P1REN = BUTTON_BIT;
	P1DIR |= LED_center;	//(BUTTON_BIT+LED_Xaxis+LED_Yaxis+LED_center);
	P1OUT &= ~LED_center;

	init_SPI();
	init_accel();

	IE1 |= WDTIE;			//enable the WDT interrupt

	_bis_SR_register(GIE+LPM0_bits);	// enable general interrupts and power down CPU
}
Example #2
0
static void gitMain (const git_uint8 * game, git_uint32 gameSize, git_uint32 cacheSize, git_uint32 undoSize)
{
    git_uint32 version;
    enum IOMode ioMode = IO_NULL;
    
    init_accel ();

    // Initialise the Glk dispatch layer.
    git_init_dispatch();

    // Set various globals.    
    gPeephole = 1;
    gDebug = 0;
    
    // Load the gamefile into memory
    // and initialise undo records.
    initMemory (game, gameSize);
    initUndo (undoSize);
    
    // Check that we're compatible with the
    // glulx spec version that the game uses.
    version = memRead32 (4);
    if (version == 0x010000 && version <= 0x0100FF)
    {
        // We support version 1.0.0 even though it's
        // officially obsolete. The only significant
        // difference is the lack of I/O modes. In 1.0,
        // all output goes directly to the Glk library.
        ioMode = IO_GLK;
    }
    else if (version == 0x020000 && version <= 0x0200FF)
    {
        // We support version 2.0, which most people currently use.
    }
    else if (version >= 0x030000 && version <= 0x0300FF)
    {
        // We support version 3.0, which adds Unicode functionality.
    }
    else if (version >= 0x030100 && version <= 0x0301FF)
    {
        // We support version 3.1, which adds some memory-management opcodes.
    }
    else
    {
        fatalError ("Can't run this game, because it uses a newer version "
            "of the gamefile format than Git understands. You should check "
            "whether a newer version of Git is available.");
    }
    
    // Call the top-level function.
    startProgram (cacheSize, ioMode);
    
    // Shut everything down cleanly.
    shutdownUndo();
    shutdownMemory();
}
Example #3
0
int main(int argc, char* argv[])
{
	int ret;

	if (RET_OK != deal_signal()) {
		printf("Install signal handlers failed.\n");
		return RET_ERR;
	}

	ret = mkfifo(LOCKFILE, S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
	if (ret) {
		printf("Sensord lock file exists under /var/lock/, please check if sensord is already started.\n");
		return RET_ERR;
	}

#ifndef DEBUG
	sensord_daemon("Sensord");
#endif

	dbus_init();

	accel_dev = accel_opendev();
	if (accel_dev < 0) {
		syslog(LOG_ERR, "open accelerometer device failed.\n");
		goto err1;
	}
	init_accel(accel_dev);

	tablet_dev = tablet_opendev();
	if (tablet_dev < 0) {
		syslog(LOG_ERR, "open tablet device failed.\n");
		goto err1;
	}

	old_orientation = ORI_NORMAL;
	tablet_mode = CLAMSHELL_MODE;

	/* accelerometer handle thread */
	if (pthread_create(&accel_thread, NULL, accel_handler, NULL))
		goto err1;
	/* tablet handle thread */
	if (pthread_create(&tablet_thread, NULL, tablet_handler, NULL))
		goto err1;
	

	while (1) {
		sleep(3);
	}

err1:
	close(accel_dev);
	close(tablet_dev);
	exit(1);
	return 0;
}
Example #4
0
int
main (int argc, char *argv[])
{
    int i, numprocs, rank, size;
    int skip;
    double latency = 0.0, t_start = 0.0, t_stop = 0.0;
    double timer=0.0;
    double avg_time = 0.0, max_time = 0.0, min_time = 0.0;
    char * sendbuf = NULL, * recvbuf = NULL;
    int po_ret;
    size_t bufsize;

    set_header(HEADER);
    set_benchmark_name("osu_gather");
    enable_accel_support();
    po_ret = process_options(argc, argv);

    if (po_okay == po_ret && none != options.accel) {
        if (init_accel()) {
            fprintf(stderr, "Error initializing device\n");
            exit(EXIT_FAILURE);
        }
    }

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

    switch (po_ret) {
        case po_bad_usage:
            print_bad_usage_message(rank);
            MPI_Finalize();
            exit(EXIT_FAILURE);
        case po_help_message:
            print_help_message(rank);
            MPI_Finalize();
            exit(EXIT_SUCCESS);
        case po_version_message:
            print_version_message(rank);
            MPI_Finalize();
            exit(EXIT_SUCCESS);
        case po_okay:
            break;
    }

    if(numprocs < 2) {
        if (rank == 0) {
            fprintf(stderr, "This test requires at least two processes\n");
        }

        MPI_Finalize();
        exit(EXIT_FAILURE);
    }

    if ((options.max_message_size * numprocs) > options.max_mem_limit) {
        options.max_message_size = options.max_mem_limit / numprocs;
    }

    if (0 == rank) {
        bufsize = options.max_message_size * numprocs;
        if (allocate_buffer((void**)&recvbuf, bufsize, options.accel)) {
            fprintf(stderr, "Could Not Allocate Memory [rank %d]\n", rank);
            MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
        }
        set_buffer(recvbuf, options.accel, 1, bufsize);
    }

    if (allocate_buffer((void**)&sendbuf, options.max_message_size,
                options.accel)) {
        fprintf(stderr, "Could Not Allocate Memory [rank %d]\n", rank);
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }
    set_buffer(sendbuf, options.accel, 0, options.max_message_size);

    print_preamble(rank);

    for (size=1; size <= options.max_message_size; size *= 2) {
        if (size > LARGE_MESSAGE_SIZE) {
            skip = SKIP_LARGE;
            options.iterations = options.iterations_large;
        } else {
            skip = SKIP;
        }

        MPI_Barrier(MPI_COMM_WORLD);
        timer=0.0;

        for (i=0; i < options.iterations + skip ; i++) {
            t_start = MPI_Wtime();
            MPI_Gather(sendbuf, size, MPI_CHAR, recvbuf, size, MPI_CHAR, 0,
                    MPI_COMM_WORLD);
            t_stop = MPI_Wtime();

            if (i >= skip) {
                timer+=t_stop-t_start;
            }
            MPI_Barrier(MPI_COMM_WORLD);
        }
        latency = (double)(timer * 1e6) / options.iterations;

        MPI_Reduce(&latency, &min_time, 1, MPI_DOUBLE, MPI_MIN, 0,
                MPI_COMM_WORLD);
        MPI_Reduce(&latency, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0,
                MPI_COMM_WORLD);
        MPI_Reduce(&latency, &avg_time, 1, MPI_DOUBLE, MPI_SUM, 0,
                MPI_COMM_WORLD);
        avg_time = avg_time/numprocs;

        print_stats(rank, size, avg_time, min_time, max_time);
        MPI_Barrier(MPI_COMM_WORLD);
    }

    if (0 == rank) {
        free_buffer(recvbuf, options.accel);
    }
    free_buffer(sendbuf, options.accel);

    MPI_Finalize();

    if (none != options.accel) {
        if (cleanup_accel()) {
            fprintf(stderr, "Error cleaning up device\n");
            exit(EXIT_FAILURE);
        }
    }

    return EXIT_SUCCESS;
}
Example #5
0
int main(int argc, char *argv[])
{
    setbuf(stdout, NULL);
    int i = 0, rank, size, disp;
    int numprocs;
    double latency = 0.0, t_start = 0.0, t_stop = 0.0;
    double tcomp = 0.0, tcomp_total=0.0, latency_in_secs=0.0;
    double test_time = 0.0, test_total = 0.0;
    double timer=0.0;
    double wait_time = 0.0, init_time = 0.0;
    double init_total = 0.0, wait_total = 0.0;
    char *sendbuf=NULL;
    char *recvbuf=NULL;
    int *sdispls=NULL, *sendcounts=NULL;
    int po_ret;
    size_t bufsize;

    set_header(HEADER);
    set_benchmark_name("osu_iscatterv");
    enable_accel_support();
    po_ret = process_options(argc, argv);

    if (po_okay == po_ret && none != options.accel) {
        if (init_accel()) {
            fprintf(stderr, "Error initializing device\n");
            exit(EXIT_FAILURE);
        }
    }

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    MPI_Request request;
    MPI_Status status;

    switch (po_ret) {
        case po_bad_usage:
            print_bad_usage_message(rank);
            MPI_Finalize();
            exit(EXIT_FAILURE);
        case po_help_message:
            print_help_message(rank);
            MPI_Finalize();
            exit(EXIT_SUCCESS);
        case po_version_message:
            print_version_message(rank);
            MPI_Finalize();
            exit(EXIT_SUCCESS);
        case po_okay:
            break;
    }

    if(numprocs < 2) {
        if (rank == 0) {
            fprintf(stderr, "This test requires at least two processes\n");
        }

        MPI_Finalize();
        exit(EXIT_FAILURE);
    }

    if ((options.max_message_size * numprocs) > options.max_mem_limit) {
        options.max_message_size = options.max_mem_limit / numprocs;
    }
    
    if (0 == rank) {
        if (allocate_buffer((void**)&sendcounts, numprocs*sizeof(int), none)) {
            fprintf(stderr, "Could Not Allocate Memory [rank %d]\n", rank);
            MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
        }
        if (allocate_buffer((void**)&sdispls, numprocs*sizeof(int), none)) {
            fprintf(stderr, "Could Not Allocate Memory [rank %d]\n", rank);
            MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
        }

        bufsize = options.max_message_size * numprocs;
        if (allocate_buffer((void**)&sendbuf, bufsize, options.accel)) {
            fprintf(stderr, "Could Not Allocate Memory [rank %d]\n", rank);
            MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
        }
        set_buffer(sendbuf, options.accel, 1, bufsize);
    }

    if (allocate_buffer((void**)&recvbuf, options.max_message_size,
                options.accel)) {
        fprintf(stderr, "Could Not Allocate Memory [rank %d]\n", rank);
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }
    set_buffer(recvbuf, options.accel, 0, options.max_message_size);

    print_preamble_nbc(rank);

    for(size=options.min_message_size; size <=options.max_message_size; size *= 2) {
        if(size > LARGE_MESSAGE_SIZE) {
            options.skip = SKIP_LARGE;
            options.iterations = options.iterations_large;
        }
        else {
            options.skip = SKIP;
        }
        if (0 == rank) {
            disp =0;
            for ( i = 0; i < numprocs; i++) {
                sendcounts[i] = size;
                sdispls[i] = disp;
                disp += size;
            }
        }
	
        MPI_Barrier(MPI_COMM_WORLD);
        
        timer = 0.0;     
        
        for(i=0; i < options.iterations + options.skip ; i++) {
            t_start = MPI_Wtime();
            
            MPI_Iscatterv(sendbuf, sendcounts, sdispls, MPI_CHAR, recvbuf,
                      size, MPI_CHAR, 0, MPI_COMM_WORLD, &request);
            MPI_Wait(&request,&status);

            t_stop = MPI_Wtime();
            
            if(i>=options.skip){
                timer += t_stop-t_start;
            } 
            MPI_Barrier(MPI_COMM_WORLD);
        }  
        
        MPI_Barrier(MPI_COMM_WORLD);

        latency = (timer * 1e6) / options.iterations;
        
        latency_in_secs = timer/options.iterations;

        init_arrays(latency_in_secs);
        
        if (0 == rank) {
            disp =0;
            for ( i = 0; i < numprocs; i++) {
                sendcounts[i] = size;
                sdispls[i] = disp;
                disp += size;
            }
        }

        MPI_Barrier(MPI_COMM_WORLD);

        timer = 0.0; tcomp_total = 0; tcomp = 0;        
        init_total = 0.0; wait_total = 0.0;
        test_time = 0.0, test_total = 0.0;

        for(i=0; i < options.iterations + options.skip ; i++) {
            t_start = MPI_Wtime();

            init_time = MPI_Wtime();
            MPI_Iscatterv(sendbuf, sendcounts, sdispls, MPI_CHAR, recvbuf,
                      size, MPI_CHAR, 0, MPI_COMM_WORLD, &request);
            init_time = MPI_Wtime() - init_time;

            tcomp = MPI_Wtime();             
            test_time = dummy_compute(latency_in_secs, &request); 
            tcomp = MPI_Wtime() - tcomp;

            wait_time = MPI_Wtime();
            MPI_Wait(&request,&status);
            wait_time = MPI_Wtime() - wait_time;

            t_stop = MPI_Wtime();
            
            if(i>=options.skip){
                timer += t_stop-t_start;
                tcomp_total += tcomp;
                test_total += test_time;
                init_total += init_time;
                wait_total += wait_time;
            }
            MPI_Barrier(MPI_COMM_WORLD);
        }  
       
        MPI_Barrier (MPI_COMM_WORLD);
        
        calculate_and_print_stats(rank, size, numprocs,
                                  timer, latency,
                                  test_total, tcomp_total,
                                  wait_total, init_total);
    }  
    
    if (0 == rank) {
        free_buffer(sendcounts, none);
        free_buffer(sdispls, none);
        free_buffer(sendbuf, options.accel);
    }
    free_buffer(recvbuf, options.accel);

    MPI_Finalize();

    if (none != options.accel) {
        if (cleanup_accel()) {
            fprintf(stderr, "Error cleaning up device\n");
            exit(EXIT_FAILURE);
        }
    }
   
    return EXIT_SUCCESS;
}
Example #6
0
int main (int argc, char *argv[])
{
    SYNC        sync_type=PSCW; 
    int         rank,nprocs;
    int         page_size;
    int         po_ret = po_okay;
#if MPI_VERSION >= 3
    WINDOW      win_type=WIN_ALLOCATE;
#else
    WINDOW      win_type=WIN_CREATE;
#endif

    po_ret = process_options(argc, argv, &win_type, &sync_type, active_sync);

    if (po_okay == po_ret && none != options.accel) {
        if (init_accel()) {
           fprintf(stderr, "Error initializing device\n");
            exit(EXIT_FAILURE);
        }
    }

    MPI_CHECK(MPI_Init(&argc, &argv));
    MPI_CHECK(MPI_Comm_size(MPI_COMM_WORLD, &nprocs));
    MPI_CHECK(MPI_Comm_rank(MPI_COMM_WORLD, &rank));

    if (0 == rank) {
        switch (po_ret) {
            case po_cuda_not_avail:
                fprintf(stderr, "CUDA support not enabled.  Please recompile "
                        "benchmark with CUDA support.\n");
                break;
            case po_openacc_not_avail:
                fprintf(stderr, "OPENACC support not enabled.  Please "
                        "recompile benchmark with OPENACC support.\n");
                break;
            case po_bad_usage:
            case po_help_message:
                usage(active_sync);
                break;
        }
    }

    switch (po_ret) {
        case po_cuda_not_avail:
        case po_openacc_not_avail:
        case po_bad_usage:
            MPI_Finalize();
            exit(EXIT_FAILURE);
        case po_help_message:
            MPI_Finalize();
            exit(EXIT_SUCCESS);
        case po_okay:
            break;
    }

    if(nprocs != 2) {
        if(rank == 0) {
            fprintf(stderr, "This test requires exactly two processes\n");
        }

        MPI_CHECK(MPI_Finalize());

        return EXIT_FAILURE;
    }

    print_header(rank, win_type, sync_type);
    
    switch (sync_type){
        case FENCE: 
            run_put_with_fence(rank, win_type);
            break;
        default: 
            run_put_with_pscw(rank, win_type);
            break;
    }

    MPI_CHECK(MPI_Finalize());

    if (none != options.accel) {
        if (cleanup_accel()) {
            fprintf(stderr, "Error cleaning up device\n");
            exit(EXIT_FAILURE);
        }
    }

    return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
    int i, j, numprocs, rank, size;
    double latency = 0.0, t_start = 0.0, t_stop = 0.0;
    double timer=0.0;
    double avg_time = 0.0, max_time = 0.0, min_time = 0.0;
    float *sendbuf, *recvbuf;
    int po_ret;
    size_t bufsize;

    int64_t* problems = all_reduce_kernels_size;
    int64_t* numRepeats = all_reduce_kernels_repeat;

    set_header(HEADER);
#ifdef ENABLE_MLSL
    mlsl_comm_req request;
    set_benchmark_name("mlsl_osu_allreduce");
#else
    set_benchmark_name("osu_allreduce");
#endif
    enable_accel_support();
    po_ret = process_options(argc, argv);

    if (po_okay == po_ret && none != options.accel) {
        if (init_accel()) {
            fprintf(stderr, "Error initializing device\n");
            exit(EXIT_FAILURE);
        }
    }

#ifdef ENABLE_MLSL
    MLSL_CALL(mlsl_environment_get_env(&env));
    MLSL_CALL(mlsl_environment_init(env, &argc, &argv));
    size_t process_idx, process_count;
    MLSL_CALL(mlsl_environment_get_process_idx(env, &process_idx));
    MLSL_CALL(mlsl_environment_get_process_count(env, &process_count));
    rank = process_idx;
    numprocs = process_count;
    MLSL_CALL(mlsl_environment_create_distribution(env, process_count, 1, &distribution));
#else
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
#endif

    switch (po_ret) {
        case po_bad_usage:
            print_bad_usage_message(rank);
            FINALIZE();
            exit(EXIT_FAILURE);
        case po_help_message:
            print_help_message(rank);
            FINALIZE();
            exit(EXIT_SUCCESS);
        case po_version_message:
            print_version_message(rank);
            FINALIZE();
            exit(EXIT_SUCCESS);
        case po_okay:
            break;
    }

    if(numprocs < 2) {
        if (rank == 0) {
            fprintf(stderr, "This test requires at least two processes\n");
        }

        FINALIZE();
        exit(EXIT_FAILURE);
    }

    if (options.max_message_size > options.max_mem_limit) {
        options.max_message_size = options.max_mem_limit;
    }

    bufsize = sizeof(float)*(options.max_message_size/sizeof(float));
    if (allocate_buffer((void**)&sendbuf, bufsize, options.accel)) {
        fprintf(stderr, "Could Not Allocate Memory [rank %d]\n", rank);
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }
    set_buffer(sendbuf, options.accel, 1, bufsize);

    bufsize = sizeof(float)*(options.max_message_size/sizeof(float));
    if (allocate_buffer((void**)&recvbuf, bufsize, options.accel)) {
        fprintf(stderr, "Could Not Allocate Memory [rank %d]\n", rank);
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }
    set_buffer(recvbuf, options.accel, 0, bufsize);

    print_preamble(rank, numprocs);

    size = options.max_message_size/sizeof(float);

    for (j = 0; j < _NUMBER_OF_KERNELS_; j++)
    {
        size = problems[j];

        options.iterations = numRepeats[j];
        MPI_Barrier(MPI_COMM_WORLD);

        timer = 0.0;
        t_start = MPI_Wtime();
        for(i=0; i < options.iterations; i++) {
#ifdef ENABLE_MLSL
            MLSL_CALL(mlsl_distribution_all_reduce(distribution, sendbuf, recvbuf, size, DT_FLOAT, RT_SUM, GT_DATA, &request));
            MLSL_CALL(mlsl_environment_wait(env, request));
#else
            MPI_Allreduce(sendbuf, recvbuf, size, MPI_FLOAT, MPI_SUM, MPI_COMM_WORLD);
#endif
        }

        t_stop = MPI_Wtime();
        timer = t_stop-t_start;

        latency = (double)(timer * 1e3) / options.iterations;

        MPI_Reduce(&latency, &min_time, 1, MPI_DOUBLE, MPI_MIN, 0,
                MPI_COMM_WORLD);
        MPI_Reduce(&latency, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0,
                MPI_COMM_WORLD);
        MPI_Reduce(&latency, &avg_time, 1, MPI_DOUBLE, MPI_SUM, 0,
                MPI_COMM_WORLD);
        avg_time = avg_time/numprocs;

        print_stats(rank, size, avg_time, min_time, max_time);
        MPI_Barrier(MPI_COMM_WORLD);
    }

    free_buffer(sendbuf, options.accel);
    free_buffer(recvbuf, options.accel);

    FINALIZE();

    if (none != options.accel) {
        if (cleanup_accel()) {
            fprintf(stderr, "Error cleaning up device\n");
            exit(EXIT_FAILURE);
        }
    }

    return EXIT_SUCCESS;
}
Example #8
0
int main(int argc, char *argv[])
{
    int i = 0, rank;
    int numprocs;
    double avg_time = 0.0, max_time = 0.0, min_time = 0.0;
    double latency = 0.0, t_start = 0.0, t_stop = 0.0;
    double timer=0.0;
    int po_ret;

    set_header(HEADER);
    set_benchmark_name("osu_barrier");
    enable_accel_support();
    po_ret = process_options(argc, argv);

    if (po_okay == po_ret && none != options.accel) {
        if (init_accel()) {
            fprintf(stderr, "Error initializing device\n");
            exit(EXIT_FAILURE);
        }
    }

    options.show_size = 0;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

    switch (po_ret) {
        case po_bad_usage:
            print_bad_usage_message(rank);
            MPI_Finalize();
            exit(EXIT_FAILURE);
        case po_help_message:
            print_help_message(rank);
            MPI_Finalize();
            exit(EXIT_SUCCESS);
        case po_version_message:
            print_version_message(rank);
            MPI_Finalize();
            exit(EXIT_SUCCESS);
        case po_okay:
            break;
    }

    if(numprocs < 2) {
        if(rank == 0) {
            fprintf(stderr, "This test requires at least two processes\n");
        }

        MPI_Finalize();

        return EXIT_FAILURE;
    }

    print_preamble(rank);

    options.skip = options.skip_large;
    options.iterations = options.iterations_large;
    timer = 0.0;

    for(i=0; i < options.iterations + options.skip ; i++) {
        t_start = MPI_Wtime();
        MPI_Barrier(MPI_COMM_WORLD);
        t_stop = MPI_Wtime();

        if(i>=options.skip){
            timer+=t_stop-t_start;
        }
    }

    MPI_Barrier(MPI_COMM_WORLD);

    latency = (timer * 1e6) / options.iterations;

    MPI_Reduce(&latency, &min_time, 1, MPI_DOUBLE, MPI_MIN, 0,
                MPI_COMM_WORLD);
    MPI_Reduce(&latency, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0,
                MPI_COMM_WORLD);
    MPI_Reduce(&latency, &avg_time, 1, MPI_DOUBLE, MPI_SUM, 0,
                MPI_COMM_WORLD);
    avg_time = avg_time/numprocs;

    print_stats(rank, 0, avg_time, min_time, max_time);
    MPI_Finalize();

    return EXIT_SUCCESS;
}
Example #9
0
int main()
{
    char pkt_arr[] = {0, 0, 0}; // {damage, health, stun}
    unsigned char i = 0;
    
    // Initialize PIC
    init_pic();
    //init_uart();
    
    // Initialize teammates' code
    init_ir();
    init_accel();
    init_leds();
    init_isr();
    init_conn();
    
    /*
    RC4 = 1;
    __delay_ms(250);
    RC4 = 0;
    __delay_ms(250);
    RC4 = 1;
    __delay_ms(250);
    RC4 = 0;
    __delay_ms(250);
    RC4 = 1;
    __delay_ms(250);
    RC4 = 0;
    */
    
    // main loop
    while(1)
    {
#if _ALPHA_BLADE
        display_health();
        if(damage_received)
        {
            display_blade_lights(_LIGHT_MODE_DAMAGE_RECEIVED);
            damage_received = 0;
        }
        
        if( determine_sword_was_swung() && ( ( health > 0) || determine_omega_mode_active() ) )
        {

            if(!determine_omega_mode_active())
            {
                display_blade_lights(_LIGHT_MODE_INDIVIDUAL_SWING);
                
                determine_packets_to_send(&pkt_arr);
                
                for(i = 3; i > 0; --i)
                {
                    if(pkt_arr[i-1] > 0)
                    {
                        GIE = 0;
                        output_ir(i-1, pkt_arr[i-1]);
                        GIE = 1;
                    }
                }
            }
            else
            {
                display_blade_lights(_LIGHT_MODE_OMEGA_SWING);
            }
        }
        
        
        GIE = 0;
        while( ( stun_counter > 0 ) && !determine_omega_mode_active() )
        {
            GIE = 1;
            
            //****************Turn full blue
            //Green to 0%
            PSMC2DCH  = 0xFF;//PSMC2 Duty Cycle High-byte
            PSMC2DCL  = 0xFF;//PSMC2 Duty Cycle Low-byte
            PSMC2LD = 1; //PSMC 2 Load		
            //Blue to 0%
            PSMC3DCH  = 0x00;//PSMC3 Duty Cycle High-byte
            PSMC3DCL  = 0x00;//PSMC3 Duty Cycle Low-byte
            PSMC3LD = 1; //PSMC 3 Load
            //Red to 100%
            PSMC4DCH  = 0xFF;//PSMC4 Duty Cycle High-byte
            PSMC4DCL  = 0x00;//PSMC4 Duty Cycle Low-byte
            PSMC4LD = 1; //PSMC 4 Load		
            //Set Load Bit to load duty cycle values
            PSMC2LD = 1; //PSMC 2 Load
            PSMC3LD = 1; //PSMC 3 Load
            PSMC4LD = 1; //PSMC 4 Load

            __delay_ms(50);

            //****************Turn off
            //Green to 0%
            PSMC2DCH  = 0x00;//PSMC2 Duty Cycle High-byte
            PSMC2DCL  = 0x00;//PSMC2 Duty Cycle Low-byte
            PSMC2LD = 1; //PSMC 2 Load		
            //Blue to 0%
            PSMC3DCH  = 0x00;//PSMC3 Duty Cycle High-byte
            PSMC3DCL  = 0x00;//PSMC3 Duty Cycle Low-byte
            PSMC3LD = 1; //PSMC 3 Load
            //Red to 0%
            PSMC4DCH  = 0x00;//PSMC4 Duty Cycle High-byte
            PSMC4DCL  = 0x00;//PSMC4 Duty Cycle Low-byte
            PSMC4LD = 1; //PSMC 4 Load		
            //Set Load Bit to load duty cycle values
            PSMC2LD = 1; //PSMC 2 Load
            PSMC3LD = 1; //PSMC 3 Load
            PSMC4LD = 1; //PSMC 4 Load

            __delay_ms(50);
            
            GIE = 0;
            --stun_counter;
        }
        GIE = 1;
        
    
#elif _BETA_BLADE
        display_health();
        if( determine_sword_was_swung() )
        {
            if(!determine_omega_mode_active())
            {
                play_sound(_SOUND_TYPE_INDIVIDUAL);
                display_blade_lights(_LIGHT_MODE_INDIVIDUAL_SWING);
                
                determine_packets_to_send(&pkt_arr);

                for(i = 3; i > 0; --i)
                {
                    if(pkt_arr[i-1] > 0)
                    {
                        GIE = 0;
                        output_ir(i-1, pkt_arr[i-1]);
                        GIE = 1;
                    }
                }
            }
            else
            {
                play_sound(_SOUND_TYPE_OMEGA);
                display_blade_lights(_LIGHT_MODE_OMEGA_SWING);
            }
        }
    
#elif _DELTA_BLADE
        display_health();
        if(damage_received)
        {
            display_blade_lights(_LIGHT_MODE_DAMAGE_RECEIVED);
            damage_received = 0;
        }
        
        if( determine_sword_was_swung() && ( ( health > 0) || determine_omega_mode_active() ) )
        {
            if(!determine_omega_mode_active())
            {
                display_blade_lights(_LIGHT_MODE_INDIVIDUAL_SWING);
            }
            else
            {
                display_blade_lights(_LIGHT_MODE_OMEGA_SWING);
            }
            
            determine_packets_to_send(&pkt_arr);

            for(i = 3; i > 0; --i)
            {
                if(pkt_arr[i-1] > 0)
                {
                    GIE = 0;
                    output_ir(i-1, pkt_arr[i-1]);
                    GIE = 1;
                }
            }
        }

        GIE = 0;
        while( ( stun_counter > 0 ) && !determine_omega_mode_active() )
        {
            GIE = 1;
            
            //****************Turn full blue
            //Green to 0%
            PSMC2DCH  = 0xFF;//PSMC2 Duty Cycle High-byte
            PSMC2DCL  = 0xFF;//PSMC2 Duty Cycle Low-byte
            PSMC2LD = 1; //PSMC 2 Load		
            //Blue to 0%
            PSMC3DCH  = 0x00;//PSMC3 Duty Cycle High-byte
            PSMC3DCL  = 0x00;//PSMC3 Duty Cycle Low-byte
            PSMC3LD = 1; //PSMC 3 Load
            //Red to 100%
            PSMC4DCH  = 0xFF;//PSMC4 Duty Cycle High-byte
            PSMC4DCL  = 0x00;//PSMC4 Duty Cycle Low-byte
            PSMC4LD = 1; //PSMC 4 Load		
            //Set Load Bit to load duty cycle values
            PSMC2LD = 1; //PSMC 2 Load
            PSMC3LD = 1; //PSMC 3 Load
            PSMC4LD = 1; //PSMC 4 Load

            __delay_ms(50);

            //****************Turn off
            //Green to 0%
            PSMC2DCH  = 0x00;//PSMC2 Duty Cycle High-byte
            PSMC2DCL  = 0x00;//PSMC2 Duty Cycle Low-byte
            PSMC2LD = 1; //PSMC 2 Load		
            //Blue to 0%
            PSMC3DCH  = 0x00;//PSMC3 Duty Cycle High-byte
            PSMC3DCL  = 0x00;//PSMC3 Duty Cycle Low-byte
            PSMC3LD = 1; //PSMC 3 Load
            //Red to 0%
            PSMC4DCH  = 0x00;//PSMC4 Duty Cycle High-byte
            PSMC4DCL  = 0x00;//PSMC4 Duty Cycle Low-byte
            PSMC4LD = 1; //PSMC 4 Load		
            //Set Load Bit to load duty cycle values
            PSMC2LD = 1; //PSMC 2 Load
            PSMC3LD = 1; //PSMC 3 Load
            PSMC4LD = 1; //PSMC 4 Load

            __delay_ms(50);
            
            GIE = 0;
            --stun_counter;
        }
        GIE = 1;

#elif _GAMMA_BLADE
        display_health();
        if(damage_received)
        {
            display_blade_lights(_LIGHT_MODE_DAMAGE_RECEIVED);
            damage_received = 0;
        }
        
        if( determine_sword_was_swung() && ( ( health > 0) || determine_omega_mode_active() ) )
        {
            if(!determine_omega_mode_active())
            {
                display_blade_lights(_LIGHT_MODE_INDIVIDUAL_SWING);
                
                determine_packets_to_send(&pkt_arr);

                for(i = 3; i > 0; --i)
                {
                    if(pkt_arr[i-1] > 0)
                    {
                        GIE = 0;
                        output_ir(i-1, pkt_arr[i-1]);
                        GIE = 1;
                    }
                }
            }
            else
            {
                display_blade_lights(_LIGHT_MODE_OMEGA_SWING);
            }
        }

        GIE = 0;
        
        while( ( stun_counter > 0 ) && !determine_omega_mode_active() )
        {
            GIE = 1;
            
            //****************Turn full blue
            //Green to 0%
            PSMC2DCH  = 0xFF;//PSMC2 Duty Cycle High-byte
            PSMC2DCL  = 0xFF;//PSMC2 Duty Cycle Low-byte
            PSMC2LD = 1; //PSMC 2 Load		
            //Blue to 0%
            PSMC3DCH  = 0x00;//PSMC3 Duty Cycle High-byte
            PSMC3DCL  = 0x00;//PSMC3 Duty Cycle Low-byte
            PSMC3LD = 1; //PSMC 3 Load
            //Red to 100%
            PSMC4DCH  = 0xFF;//PSMC4 Duty Cycle High-byte
            PSMC4DCL  = 0x00;//PSMC4 Duty Cycle Low-byte
            PSMC4LD = 1; //PSMC 4 Load		
            //Set Load Bit to load duty cycle values
            PSMC2LD = 1; //PSMC 2 Load
            PSMC3LD = 1; //PSMC 3 Load
            PSMC4LD = 1; //PSMC 4 Load

            __delay_ms(50);

            //****************Turn off
            //Green to 0%
            PSMC2DCH  = 0x00;//PSMC2 Duty Cycle High-byte
            PSMC2DCL  = 0x00;//PSMC2 Duty Cycle Low-byte
            PSMC2LD = 1; //PSMC 2 Load		
            //Blue to 0%
            PSMC3DCH  = 0x00;//PSMC3 Duty Cycle High-byte
            PSMC3DCL  = 0x00;//PSMC3 Duty Cycle Low-byte
            PSMC3LD = 1; //PSMC 3 Load
            //Red to 0%
            PSMC4DCH  = 0x00;//PSMC4 Duty Cycle High-byte
            PSMC4DCL  = 0x00;//PSMC4 Duty Cycle Low-byte
            PSMC4LD = 1; //PSMC 4 Load		
            //Set Load Bit to load duty cycle values
            PSMC2LD = 1; //PSMC 2 Load
            PSMC3LD = 1; //PSMC 3 Load
            PSMC4LD = 1; //PSMC 4 Load

            __delay_ms(50);
            
            GIE = 0;
            --stun_counter;
        }
        
        GIE = 1;
        
#endif
        
    }
}