Ejemplo n.º 1
0
int main()
{
    int nums[] = {100, -35, 0, -2, 14, 73, 88, 
                  -25,  0,  0, 15, 3,  10, -25, 
                  73, 100, 29, 66, 39, 243, 42};
    
    assert(arr_search(nums, ARRAY_SIZE, 0, 1024) == -1);

    assert(arr_search(nums, ARRAY_SIZE, 0, 100) == 0);

    assert(arr_search(nums, ARRAY_SIZE, 0, 42) == ARRAY_SIZE-1);

    assert(arr_search(nums, ARRAY_SIZE, 0, -25) == 7);
    assert(arr_search(nums, ARRAY_SIZE, 8, -25) == 13);
    assert(arr_search(nums, ARRAY_SIZE, 14, -25) == -1);

    assert(arr_search(nums, 0, 0, -25) == -1);
    assert(arr_search(nums, 7, 0, 42) == -1);

    return 0;
}
Ejemplo n.º 2
0
int main()
{

	int arr[] = {1,2,3,4,5,6,7,8,9,10};
	unsigned size = 10;
	int foo = 50;

	int result = g (arr, size,foo);

	printf("Result = %d.\n", result);

	result = g_expanded (arr, size,foo);

	printf("Result2 = %d.\n", result);

	result = arr_search (arr, size,foo);

	printf("Result3 = %d.\n", result);

	return 0;
}
Ejemplo n.º 3
0
int main(void)
{

    int n,
        cport_nr = 4,        /* 6 (COM5 on windows) */
        bdrate = 38400;       /* 9600 baud */

    uint8_t buf[BUF_SIZE + 1];
    uint8_t data[MAX_DATA_SIZE];

    char mode[] = {'8','N','1',0};

    if (RS232_OpenComport(cport_nr, bdrate, mode)) {
        printf("Can not open comport\n");
        return(0);
    }
    int received_cnt = 0;

    while (1) {

      	n = RS232_PollComport(cport_nr, buf, BUF_SIZE);

	    if (n > 0) {
	    	/* always put a "null" at the end of a string! */
		    buf[n] = 0;   
		
		    printf("Received %i bytes: %s\n", n, (char *) buf);

		    if (arr_search("TKENDTKENDTKENDTKEND", BUF_SIZE, buf, n) > 0) {
		    	printf("%s\n", "Starting reception...");
		    	memset(data, 0, MAX_DATA_SIZE);	/* Initialize the array */
		    	received_cnt = 0;
		    }

		    else {
			    int pos = -1;
			    /* If receiving the end of current compressed buffer, send & reinitialize */
			    if ((pos = arr_search("TKENDTKENDTKENDTKE", DELIMITER_LEN, buf, n)) >= 0) {	
			    	int eff_len = pos - DELIMITER_LEN + 1;
			    	uint8_t temp[eff_len];
			    	memcpy(temp, buf, eff_len);
			    	memcpy(data + received_cnt, temp, eff_len);
			    	received_cnt += eff_len;
			    	/* Discard the last five bytes of indicators*/
			    	char size_buf[2];
			    	memcpy(size_buf, buf + pos + 1, 2);
			    	int size = size_buf[1] + (size_buf[0] << 4);
			    	printf("Received data total size: %d\n", size);

			    	uint8_t comp[size];
			    	memcpy(comp, data, size);
			    	decompress(comp, size);

			    	/* Clean up */
			    	memset(data, 0, MAX_DATA_SIZE);
			    	received_cnt = 0;	
			    	/* Also need to store rest of the data to avoid loss */
			    	// uint8_t lost[n - eff_len - 5];
			    	// memcpy(lost, buf + pos + 1, n - pos - 1);
			    	// memcpy(data, lost, n - pos - 1);
			    	// received_cnt += n - pos - 1;
			    } else {	/* If regular data packets, store it*/
			    	memcpy(data + received_cnt, buf, n);
			    	received_cnt = received_cnt + n;
			    }
			}
    	}
		#ifdef _WIN32
		    Sleep(100);
		#else
		    usleep(100000);  /* sleep for 100 milliSeconds */
		#endif
	}
    return (0);
}