Beispiel #1
0
void Send(uint8_t user)
{
	uint16_t stemp;
	uint8_t sn = true;
	uint8_t cnt = 1;
	uint8_t Temp[17];
	sprintf(Temp, "AT+CIPSEND=%c,3\r\n", user);
	
	while(usart_write_buffer_job(&usart_instance, Temp, sizeof(Temp)) != STATUS_OK){}
	while(sn)
	if(usart_read_wait(&usart_instance, &stemp) == STATUS_OK)
	{
		if(stemp == '>')
		{
			while(usart_write_buffer_wait(&usart_instance, num, sizeof(num)) != STATUS_OK){}
			while(true)
			if(usart_read_wait(&usart_instance, &stemp) == STATUS_OK)
			{
				if(stemp == 'K')
				{
					sn = false;
					break;
				}
				else if(cnt == 1000)
				{
					Send(user);
					sn = false;
					break;
				}
				cnt ++;
			}
		}
	}
}
Beispiel #2
0
int main(void)
{
	system_init();

//! [setup_init]
	configure_usart();
//! [setup_init]

//! [main]
//! [main_send_string]
	uint8_t string[] = "Hello World!\r\n";
	usart_write_buffer_wait(&usart_instance, string, sizeof(string));
//! [main_send_string]

//! [main_rec_var]
	uint16_t temp;
//! [main_rec_var]

//! [main_loop]
	while (true) {
//! [main_read]
		if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {
//! [main_read]
//! [main_write]
			while (usart_write_wait(&usart_instance, temp) != STATUS_OK) {
			}
//! [main_write]
		}
	}
//! [main_loop]
//! [main]
}
bool PHPROBE_read_ph(float* ph){
	if(index_data < 8){
		return false;
	}
	float temp = ENV_TEMP_get_temp();
	char calib[8];
	sprintf( calib , "T,%4.2f\r" , temp);
	usart_write_buffer_wait(&PHPROBE_uart , (unsigned char*) calib , 8);
	int j;
	int i;
	char temporary[10];
	memset(temporary , 0 , 10);
	i = index_data -1;
	while(buffer[i] != '\r'  && i >= 7){
		i--;
	}
	
	j = i - 1;
	while(buffer[j] != '\r' && j > 0){
		j--;
	}
	
	if(buffer[j] != '\r') {
		memset(buffer , 0 , 20);
		usart_read_job(&PHPROBE_uart , &get_char_buffer);
	}
	
	strncpy(temporary , buffer + j + 1 , i-j-1);
	*ph = atof(temporary);
	memset(buffer , 0 , 20);
	index_data = 0;
	usart_read_job(&PHPROBE_uart , &get_char_buffer);
	return true;
}
Beispiel #4
0
/*---------------------------------------------------------------------------*/
static int
write_buffer(const unsigned char * buffer, uint8_t len)
{
  port_pin_set_output_level(RS485_TXE, true);
  if (usart_write_buffer_wait(&usart_instance, buffer, len) == STATUS_OK) {
    port_pin_set_output_level(RS485_TXE, false);
    return len;
  } else {
    return -1;
  }
}
void print_to_terminal(const char* format, ...)
{
    va_list a_list;
    char serial_message[100];
    uint8_t length = 0;

    va_start(a_list, format);
    vsnprintf(serial_message, sizeof(serial_message), format, a_list);
    va_end(a_list);
    while (serial_message[length++]);
    usart_write_buffer_wait(&usart_instance, (uint8_t*) serial_message, length);
}
Beispiel #6
0
int main (void)
{
	system_init();
	//delay_init();
	configure_port_pins();
	configure_usart();
	
	uint8_t string[] = "Testing usart\n";
	
	usart_write_buffer_wait(&usart_instance, string,sizeof(string));
	port_pin_set_output_level(_RESET, 1);
	port_pin_set_output_level(_RESET, 0);
	delay_ms(100);
	port_pin_set_output_level(_RESET, 1);
	delay_ms(1000);
	
	port_pin_set_output_level(PIN_PA02, !false);
	
	uint8_t ndef_data[] = COSY_DEFAULT_DATA;
	
	rf430_init();
	rf430_write_ndef(ndef_data);
	
	port_pin_set_output_level(PIN_PA02, false);

	// Insert application code here, after the board has been initialized.
	
	//example application with cosytech data. (This data should be fetched from web/bluetooth)
	

	
	while(1){
		delay_ms(500);
		port_pin_set_output_level(PIN_PA02, true);
		delay_ms(500);
		port_pin_set_output_level(PIN_PA02, false);
	}
}
Beispiel #7
0
/**
 * \brief Main Application Routine                              \n
 * - Initialize the system clocks                               \n
 * NOTE: The clock should be configured in conf_clock.h         \n
 * - Configure port pins (PA14 and PA16) are used here          \n
 * - Enable Global Interrupt                                    \n
 * - Configure and enable USART                                 \n
 * - Configure and enable ADC                                   \n
 * - Configure and enable DMAC and EVSYS if DMAC mode is chosen \n
 * - Start first ADC conversion                                 \n
 * - Count idle loop count in forever loop                      \n
 */
int main(void)
{

	/* Initialize system clocks */
	system_init();

#if defined(ENABLE_PORT_TOGGLE)
	/* Configure PORT pins PA14 and PA16 are configured here
	 * NOTE: Use oscilloscope to probe the pin.
	 */
	configure_port();
#endif

	/* ENable Global interrupt */
	system_interrupt_enable_global();

	/* Start SysTick Timer */
	systick_init();
 
	/* Configure SERCOM - USART */
	configure_usart();

	/* Configure and enable ADC */
	configure_adc();

	/* Configure and enable EVSYS */
	configure_event();

	/* Configure and enable DMA channel and descriptor */
	configure_dma();

	/* Get the time stamp 1 before starting ADC transfer */
	time_stamp1 = SysTick->VAL;
	

	/*
	 * Trigger first ADC conversion through software.
	 * NOTE: In case of using DMA, further conversions are triggered through
	 * event generated when previous ADC result is transferred to destination 
	 * (can be USART DATA register [or] RAM buffer).
	 * When DMA is not used, further conversions are triggered via software in 
	 * ADC handler after each result ready.
	 */
	adc_start_conversion(&adc_instance);

	while (1){

		#if defined (ENABLE_PORT_TOGGLE)
			/* 	 Use oscilloscope to probe the pin. */
			port_base->OUTTGL.reg = (1UL << PIN_PA16 % 32 );
		#endif
		
		/* Increment idle count whenever application reached while(1) loop */
		idle_loop_count++;

		/*
		 * Check if 1024 bytes transfer is done in either case (I.e. with or without
		 * using DMA.
		 * 'adc_conv_done' flag is set to true in the ADC handler once 
		 * 'adc_sample_count' reaches BLOCK_COUNT.
		 * 'adc_dma_transfer_is_done' is set to true once DMA transfer is done 
		 * in DMA call back for channel zero when 'ADC_DMAC_USART' is chosen.
		 * When choosing ADC_DMAC_MEM_MEM_USART mode, 'adc_dma_transfer_is_done' 
		 * is set to true in DMA channel call back for channel 2.
		 * DMA channel is disabled once reaching BLOCK_COUNT (with DMA cases).
		 * ADC is disabled once reaching BLOBK_COUNT samples (without DMA cases).
		 */
		if (adc_dma_transfer_is_done == true){
			
			/*
			 * Calculate number of cycles taken from the time stamp 
			 * taken before start of the conversion and after 1024 transfer
			 * is completed.
			 * NOTE: This value in relation to the idle_loop_count is 
			 * used in calculating CPU usage.
			 */
			cycles_taken = calculate_cycles_taken(time_stamp1,time_stamp2);

			/* Write the CPU cycles taken on USART */
			usart_write_buffer_wait(&usart_instance, (uint8_t *)&cycles_taken, sizeof(cycles_taken));
			/* Print idle loop count on USART */
			usart_write_buffer_wait(&usart_instance,(uint8_t *)&idle_loop_count, sizeof(idle_loop_count));

			/*
			 * Enter into forever loop as all transfers are completed and 
			 * DMAC/ADC is disabled 
			 */
			while(1);
		}
	}

}//end of main
Beispiel #8
0
int main (void)
{
	system_init();

	// Initialize local variables used in main
	int failed = 0;
	uint8_t debug_string[24] = "y:     x:     z:   c:   ";
	uint8_t jx, jy;
	char z, c, lz, lc;
	jx = jy = z = c = lz = lc = 0;
	int cycle_index = 0;
	uint8_t cycle = 0;
	uint8_t light_mode = 0; 
	int light_modes = 1;
	uint16_t head, brake = 0;
	uint8_t LIGHTS_ON = 0;
	uint8_t FWD = 0;
	uint8_t HEADLIGHTS = 0;
	uint8_t DEBUG_BLE = 1;
	uint16_t BLE_temp = '0';
	//uint16_t light_sens;

	// Configure Devices
	configure_port_pins();
	configure_LED_PWM();
	configure_usart();
	configure_i2c_slave();
	initIMU();
	failed = !beginIMU();
	configure_i2c_slave_callbacks();
	//configure_ADC();

	// Set the BLE module name to "long-itude"
	uint8_t string[17] = "AT+NAMElong-itude";
	usart_write_buffer_wait(&usart_instance, string, sizeof(string));
	for(int i = 0; i < 50000; ++i);

	// Switch LED direction to FWD
	setFWD();

	while(1)
	{
		//readAccel();
		//readGyro();
		//readMag();
		//light_sens = getLightSens();

		// Read data from BLE
		if (usart_read_wait(&usart_instance, &BLE_temp) == STATUS_OK) {
			switch(BLE_temp)
			{
				case '0':
					LIGHTS_ON = 0;
					break;
				case '1':
					LIGHTS_ON = 1;
					break;
				case '2':
					DEBUG_BLE = 0;
					break;
				case '3':
					DEBUG_BLE = 1;
					break;
				case '4':
					setFWD();
					break;
				case '5':
					setREV();
					break;
			}
		}

		if(DEBUG_BLE)
		{
				uint8_t temp = I2C_slave_read_buffer[0];
				debug_string[4] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				debug_string[3] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				debug_string[2] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				temp = I2C_slave_read_buffer[1];
				debug_string[11] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				debug_string[10] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				debug_string[9] = '0' + temp%10;
				temp = (temp - temp%10)/10;
				debug_string[16] = '0' + I2C_slave_read_buffer[2];
				debug_string[21] = '0' + I2C_slave_read_buffer[3];
				debug_string[22] = '\r';
				debug_string[23] = '\n'; 
				usart_write_buffer_wait(&usart_instance, debug_string, sizeof(debug_string));
		}

		jx = I2C_slave_read_buffer[0];
		jy = I2C_slave_read_buffer[1];
		z = I2C_slave_read_buffer[2];
		c = I2C_slave_read_buffer[3];

		if(z == 0 && lz != 0)
		{
			if(jy > 200)
			{
				setFWD();
				if(HEADLIGHTS && FWD)
					HEADLIGHTS = 0;
				else if(!HEADLIGHTS && FWD)
					HEADLIGHTS = 1;
				else if(HEADLIGHTS && !FWD)
				{
					FWD = 1;
				}
				else if(!HEADLIGHTS && !FWD)
				{
					HEADLIGHTS = 1;
					FWD = 1;
				}
			}
			else if(jy < 55)
			{
				setREV();
				if(HEADLIGHTS && !FWD)
					HEADLIGHTS = 0;
				else if(!HEADLIGHTS && !FWD)
					HEADLIGHTS = 1;
				else if(HEADLIGHTS && FWD)
				{
					FWD = 0;
				}
				else if(!HEADLIGHTS && FWD)
				{
					HEADLIGHTS = 1;
					FWD = 0;
				}
			}
			else if(jy > 110 && jy < 150 && jx > 110 && jx < 150)
			{
				LIGHTS_ON = !LIGHTS_ON;
			}
			else if(jx > 200)
			{
				light_mode++;
				if(light_mode >= light_modes)
					light_mode = 0;
			}
			else if(jx < 55)
			{
				light_mode--;
				if(light_mode <= -1)
					light_mode = light_modes-1;
			}
		}

		if(LIGHTS_ON)
		{
			if(light_mode == 0)
			{
				if(cycle == 0)
				{
					setLeftRGB(cycle_index,0,0xCFFF-cycle_index);
					setRightRGB(cycle_index,0,0xCFFF-cycle_index);
				}
				if(cycle == 1)
				{
					setLeftRGB(0xCFFF-cycle_index,cycle_index,0);
					setRightRGB(0xCFFF-cycle_index,cycle_index,0);
				}
				if(cycle == 2)
				{
					setLeftRGB(0,0xCFFF-cycle_index,cycle_index);
					setRightRGB(0,0xCFFF-cycle_index,cycle_index);
				}

				cycle_index += 250;
				if(cycle_index >= 0xCFFF)
				{
					cycle_index = 0;
					cycle += 1;
					if(cycle == 3)
						cycle = 0;
				}
			}

			if(HEADLIGHTS)
			{
				head = 0xFFFF;
				setWhite(head);
				if(jy < 120)
				{
					brake = (0xFFFF/120)*(120-jy);
					setRed(brake);
				}
				else
					setRed(0);
			}
			else
			{
				setWhite(0);
				setRed(0);
			}
		}
		else
		{
			setWhite(0);
			setRed(0);
			setLeftRGB(0,0,0);
			setRightRGB(0,0,0);
		}

		lc = c;
		lz = z;
	}
}
Beispiel #9
0
void SensorReport(StrainSensorSet *SensorSet, int SensorIndex){
	/*
	uint8_t string[] = "Hello World!\r\n";
	//usart_write_buffer_job(&usart_instance, string, sizeof(string));
	usart_write_buffer_wait(&usart_instance, string, sizeof(string));
	*/
	char string[6] = " ";
	char string_IDvalue[6] = " ";
	char string_strain0[6] = " ";
	char string_strain45[6] = " ";
	char string_strain90[6] = " ";
	char string_strain_max[6] = " ";
	char string_strain_min[6] = " ";
	char string_angle_deg[6] = " ";
	
	usart_write_buffer_wait(&usart_instance, string_ID, sizeof(string_ID)-1);

	sprintf(string_IDvalue,"%05d\n",SensorSet->p[SensorIndex].ID);
	usart_write_buffer_wait(&usart_instance, (uint8_t*)string_IDvalue, sizeof(string_IDvalue));
	
	sprintf(string,"%05d\n",0);
	usart_write_buffer_wait(&usart_instance, (uint8_t*)string, sizeof(string));
	
	sprintf(string,"%05d\n",0);
	usart_write_buffer_wait(&usart_instance, (uint8_t*)string, sizeof(string));
	
	sprintf(string_strain0,"%05d\n",SensorSet->p[SensorIndex].strain0);
	usart_write_buffer_wait(&usart_instance, (uint8_t*)string_strain0, sizeof(string_strain0));
	
	sprintf(string_strain45,"%05d\n",SensorSet->p[SensorIndex].strain45);
	usart_write_buffer_wait(&usart_instance, (uint8_t*)string_strain45, sizeof(string_strain45));
	
	sprintf(string_strain90,"%05d\n",SensorSet->p[SensorIndex].strain90);
	usart_write_buffer_wait(&usart_instance, (uint8_t*)string_strain90, sizeof(string_strain90));
	
	sprintf(string_strain_max,"%05d\n",SensorSet->p[SensorIndex].strain_max);
	usart_write_buffer_wait(&usart_instance, (uint8_t*)string_strain_max, sizeof(string_strain_max));
	
	sprintf(string_strain_min,"%05d\n",SensorSet->p[SensorIndex].strain_min);
	usart_write_buffer_wait(&usart_instance, (uint8_t*)string_strain_min, sizeof(string_strain_min));
	
	sprintf(string_angle_deg,"%05d\n",(int)(SensorSet->p[SensorIndex].angle_deg));
	usart_write_buffer_wait(&usart_instance, (uint8_t*)string_angle_deg, sizeof(string_angle_deg));

	usart_write_buffer_wait(&usart_instance, string_NEXT, sizeof(string_NEXT)-1);
	//printf(string_angle_deg);
	
}