//Main routine
void ICACHE_FLASH_ATTR user_init(void) {

	stdoutInit();	
	os_delay_us(100000);
	CFG_Load();
	ioInit();
	
	WIFI_Connect(wifiConnectCb);
	
	httpdInit(builtInUrls, sysCfg.httpd_port);
	
	if(sysCfg.ntp_enable==1) {
		sntp_init(sysCfg.ntp_tz);	//timezone
	}
	
	if(sysCfg.mqtt_enable==1) {
		MQTT_InitConnection(&mqttClient, (uint8_t *)sysCfg.mqtt_host, sysCfg.mqtt_port, sysCfg.mqtt_use_ssl );
		MQTT_InitClient(&mqttClient, (uint8_t *)sysCfg.mqtt_devid, (uint8_t *)sysCfg.mqtt_user, (uint8_t *)sysCfg.mqtt_pass, sysCfg.mqtt_keepalive,1);
		MQTT_OnConnected(&mqttClient, mqttConnectedCb);
		MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
		MQTT_OnPublished(&mqttClient, mqttPublishedCb);		
		MQTT_OnData(&mqttClient, mqttDataCb);
		
	}
	
	if(sysCfg.sensor_dht22_enable) 
		DHTInit(SENSOR_DHT22, 30000);
		
	if(sysCfg.sensor_ds18b20_enable) 
		ds_init(30000);

	broadcastd_init();

	thermostat_init(30000);

/*
	//Netbios to set the name
	struct softap_config wiconfig;
	os_memset(netbios_name, ' ', sizeof(netbios_name)-1);
	if(wifi_softap_get_config(&wiconfig)) {
		int i;
		for(i = 0; i < sizeof(netbios_name)-1; i++) {
			if(wiconfig.ssid[i] < ' ') break;
			netbios_name[i] = wiconfig.ssid[i];
		};
	}
	else os_sprintf(netbios_name, "ESP8266");
	netbios_name[sizeof(netbios_name)-1]='\0';
	netbios_init();
*/
		
	os_printf("\nRelay Board Ready\n");	
	os_printf("Free heap size:%d\n",system_get_free_heap_size());

	
#ifdef CGIPWM_H	
	//Mind the PWM pin!! defined in pwm.h
	duty=0;
	pwm_init( 50, &duty);
	pwm_set_duty(duty, 0);
    pwm_start();
#endif
	
	//OLEDInit();
	
}
iram void gpios_periodic(void)
{
	gpio_t *gpio;
	const gpio_config_entry_t *cfg;
	unsigned int current;
	unsigned int duty;
	bool_t pwm_changed = false;

	for(current = 0; current < gpio_size; current++)
	{
		gpio = &gpios[current];
		cfg = get_config(gpio);

		if((cfg->mode == gpio_counter) && (gpio->counter.debounce != 0))
		{
			if(gpio->counter.debounce >= 10)
				gpio->counter.debounce -= 10; // 10 ms per tick
			else
				gpio->counter.debounce = 0;

			if(gpio->counter.debounce == 0)
				arm_counter(gpio);
		}

		if((cfg->mode == gpio_timer) && (gpio->timer.delay > 0))
		{
			if(gpio->timer.delay >= 10)
				gpio->timer.delay -= 10; // 10 ms per tick
			else
				gpio->timer.delay = 0;

			if(gpio->timer.delay == 0)
			{
				set_output(gpio, !get_input(gpio));

				if(cfg->timer.repeat)
					gpio->timer.delay = cfg->timer.delay;
			}
		}

		if((cfg->mode == gpio_pwm) && (gpio->pwm.delay_top > 0))
		{
			if(++gpio->pwm.delay_current > gpio->pwm.delay_top)
			{
				gpio->pwm.delay_current = 0;

				duty = pwm_get_duty(gpio->pwm.channel);

				if(gpio->pwm.direction == gpio_up)
				{
					if(duty < gpio->pwm.min_duty)
						duty = gpio->pwm.min_duty;

					if(duty < 16)
						duty = 16;

					duty *= 115;
					duty /= 100;

					if(duty >= gpio->pwm.max_duty)
					{
						duty = gpio->pwm.max_duty;
						gpio->pwm.direction = gpio_down;
					}
				}
				else
				{
					if(duty > gpio->pwm.max_duty)
						duty = gpio->pwm.max_duty;

					duty *= 100;
					duty /= 115;

					if(duty <= gpio->pwm.min_duty)
					{
						duty = gpio->pwm.min_duty;
						gpio->pwm.direction = gpio_up;
					}

					if(duty < 16)
					{
						duty = 16;
						gpio->pwm.direction = gpio_up;
					}
				}

				pwm_changed = true;
				pwm_set_duty(duty, gpio->pwm.channel);
			}
		}
	}

	if(pwm_changed)
		pwm_start();
}
irom app_action_t application_function_gpio_set(application_parameters_t ap)
{
	unsigned int gpio_index;
	gpio_t *gpio;
	const gpio_config_entry_t *cfg;

	gpio_index = atoi((*ap.args)[1]);

	if(!(gpio = find_gpio(gpio_index)))
	{
		snprintf(ap.dst, ap.size, "gpio-set: invalid gpio %u\n", gpio_index);
		return(app_action_error);
	}

	cfg = get_config(gpio);

	switch(cfg->mode)
	{
		case(gpio_disabled):
		{
			snprintf(ap.dst, ap.size, "gpio-set: gpio %s is disabled\n", gpio->name);
			return(app_action_error);
		}

		case(gpio_input):
		{
			snprintf(ap.dst, ap.size, "gpio-set: gpio %s is input\n", gpio->name);
			return(app_action_error);
		}

		case(gpio_counter):
		{
			if(ap.nargs < 3)
				gpio->counter.count = 0;
			else
				gpio->counter.count = atoi((*ap.args)[2]);

			break;
		}

		case(gpio_output):
		{
			if(ap.nargs < 3)
			{
				snprintf(ap.dst, ap.size, "gpio-set: missing arguments\n");
				return(app_action_error);
			}

			set_output(gpio, !!atoi((*ap.args)[2]));

			break;
		}

		case(gpio_timer):
		{
			if(ap.nargs == 3)
				trigger_timer(gpio, !!atoi((*ap.args)[2]));
			else
				trigger_timer(gpio, !gpio->timer.delay);

			break;
		}

		case(gpio_pwm):
		{
			unsigned int min_duty;
			unsigned int max_duty;
			unsigned int delay;

			min_duty = 0;
			max_duty = 0;
			delay = 0;

			if(ap.nargs > 2)
				min_duty = atoi((*ap.args)[2]);

			if(ap.nargs > 3)
				max_duty = atoi((*ap.args)[3]);

			if(ap.nargs > 4)
				delay = atoi((*ap.args)[4]);

			if(min_duty > 65535)
			{
				snprintf(ap.dst, ap.size, "gpio-set(pwm): min_duty too large: %u > 65535\n", min_duty);
				return(app_action_error);
			}

			if(max_duty > 65535)
			{
				snprintf(ap.dst, ap.size, "gpio-set(pwm): max_duty too large: %u > 65535\n", max_duty);
				return(app_action_error);
			}

			if(delay > 100)
			{
				snprintf(ap.dst, ap.size, "gpio-set: gpio %s, delay %u%% > 100%%\n", gpio->name, delay);
				return(app_action_error);
			}

			gpio->pwm.min_duty = min_duty;
			gpio->pwm.max_duty = max_duty;
			gpio->pwm.delay_top = delay;
			gpio->pwm.direction = gpio_up;

			pwm_set_duty(min_duty, gpio->pwm.channel);
			pwm_start();

			break;
		}

		case(gpio_i2c):
		{
			snprintf(ap.dst, ap.size, "gpio-set: gpio %s is reserved for i2c\n", gpio->name);
			return(app_action_error);
		}

		default:
		{
			snprintf(ap.dst, ap.size, "gpio-set: cannot set gpio %u\n", cfg->mode);
			return(app_action_error);
		}
	}

	dump(&config->gpios, gpio, ap.size, ap.dst);
	return(app_action_normal);
}
Exemple #4
0
int led_set_brightness(enum ec_led_id led_id, const uint8_t *brightness)
{
	pwm_set_duty(PWM_CH_LED_RED, brightness[EC_LED_COLOR_RED]);
	pwm_set_duty(PWM_CH_LED_GREEN, brightness[EC_LED_COLOR_GREEN]);
	return EC_SUCCESS;
}
Exemple #5
0
static void _exec_spindle_speed(float *value, float *flag)
{
	cm_set_spindle_speed_parameter(MODEL, value[0]);
	pwm_set_duty(PWM_1, cm_get_spindle_pwm(cm.gm.spindle_mode) ); // update spindle speed if we're running
}
Exemple #6
0
static void pwm_kblight_shutdown(void)
{
	pwm_set_duty(PWM_CH_KBLIGHT, 0);
}
Exemple #7
0
/**
 * Set LED color
 *
 * @param color		Enumerated color value
 */
static void set_color(enum led_color color)
{
	pwm_set_duty(PWM_CH_LED_RED, color_brightness[color][0]);
	pwm_set_duty(PWM_CH_LED_GREEN, color_brightness[color][1]);
}
Exemple #8
0
static void _exec_spindle_speed(uint8_t i, float speed)
{
	cm_set_spindle_speed_parameter(speed);
	pwm_set_duty(PWM_1, cm_get_spindle_pwm(gm.spindle_mode) ); // update spindle speed if we're running
}
Exemple #9
0
static void pwm_kblight_suspend(void)
{
	pwm_set_duty(PWM_CH_KBLIGHT, 0);
}
Exemple #10
0
/*
  void pid_loop()

    Calculates the updated PID
    output and parameters for each
    iteration.
*/
void pid_loop()
{
  if (pid_state != PID_RUNNING)
    return;

  uint32 dummy_time;
  boolean is_update_pid =
    timer_is_elapsed32((pid_last_timestamp + PID_LOOP_WAIT_IN_NS), &dummy_time);
  if (!is_update_pid)
  {
    DEBUG_MSG_HIGH(DEBUG_MODULE_PID, "Timer not expired. Time: %d", dummy_time);
    return;
  }

  /* Update the last time PID was read for scheduling next iteration. */
  pid_last_timestamp = timer_read32();

  /* Read updated temperatures. If last reading was invalid,
     skip this PID loop iteration. */
  therm_reading_t *temp = &(pid_params.temp);
  therm_read(temp);
  if (!temp->is_valid)
  {
    DEBUG_MSG_HIGH(DEBUG_MODULE_PID, "Temperature read failed");
    return;
  }

  /* Do PID calculation. */
  float t_current = pid_params.temp.temperature;
  float t_desired = pid_params.temp_setpoint;
  pid_gain_params *gain_p = &(pid_params.gain);
  pid_error_params *error_p = &(pid_params.error);

  float err, err_i, err_d;
  err   = t_desired - t_current;
  err_i = err + error_p->err_i;  
  err_d = err - error_p->err;

  float p, i, d;
  p = gain_p->k_p * err;
  i = gain_p->k_i * err_i;  
  d = gain_p->k_d * err_d;

  /* Scale/saturate the PID output and set the duty cycle. */
  float pid_raw = p + i + d;

  /* Add 0.5 to do rounding with truncation. */
  float pid_scaled = pid_raw + 0.5;
  pid_scaled = MAX(pid_raw, 0);
  pid_scaled = MIN(pid_scaled, 100);


  pwm_set_duty((uint32)pid_scaled);

  /* Calculate saturation error for anti-windup loop. */
  float err_saturate = (pid_scaled - pid_raw) * (gain_p->k_windup);

  /* Update error for next iteration. */
  error_p->err = err;
  error_p->err_d = err_d;
  error_p->err_i = err_i + err_saturate;

  DEBUG_MSG_MED(DEBUG_MODULE_PID, "err: %5.2f, err_d: %5.2f, err_i: %5.2f, err_sat: %5.2f",
                 error_p->err, error_p->err_d, error_p->err_i, err_saturate);
  DEBUG_MSG_MED(DEBUG_MODULE_PID, "pid_raw: %5.2f, pid_scaled: %5.2f",
                 pid_raw, pid_scaled);
}
Exemple #11
0
// initialize the custom stuff that goes beyond esp-link
void user_init()
{
	// Initialize the GPIO subsystem.
	gpio_init();

	/* ====================================== */
	/* UART                                   */
	/* ====================================== */

	// Initialize UART0 and UART1
	/* NOTE: UART1 and I2S share same GPIO. Cannot use simultaneously. */
	uart_init( BIT_RATE_115200, BIT_RATE_115200 );

//	uart0_sendStr( "\nUART0 - USED TO PROGRAM THE MODULE\n" );

	os_printf("\n===================\nUART1 - DEBUG OUPUT\n===================\n");

	/* NOTE: PWM CANNOT BE USED SIMULTANEOUSLY WITH HW TIMER */
#if 0
	/* ====================================== */
	/* PWM                                    */
	/* ====================================== */

    uint32  pwm_period = 1000;
	uint32 pwm_duty[PWM_CHANNEL] = {0};


    uint32 io_info[][3] = {   {PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM},
    		                  {PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM},
    		              };

	/* PIN FUNCTION INIT FOR PWM OUTPUT */
	pwm_init(pwm_period, pwm_duty, PWM_CHANNEL, io_info);

	/* set pwm_duty cycle */
	pwm_set_duty (14185, 0);
	pwm_set_duty (22222, 1); // todo: explain why 22222 is the highest possible value

	/* start PWM */
	pwm_start(); // NOTE: PWM causes spikes in other GPIOs
#endif


	/* ====================================== */
	/* GPIO INTERRPUT                         */
	/* ====================================== */

	/* Set GPIO12 in GPIO mode */
	PIN_FUNC_SELECT( PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12 );

	/* Set GPIO12 as input */
	GPIO_DIS_OUTPUT( GPIO_ID_PIN(12) );

	/* Disable all GPIO interrupts */
	ETS_GPIO_INTR_DISABLE();

	/* Set a GPIO callback function */
	ETS_GPIO_INTR_ATTACH( gpioCallback, NULL );

	/* Configure the type of edge */
	gpio_pin_intr_state_set( GPIO_ID_PIN(12), GPIO_PIN_INTR_ANYEDGE );

	ETS_GPIO_INTR_ENABLE();


	/* ====================================== */
	/* SOFTWARE TIMER                         */
	/* ====================================== */

	// Set GPIO0 to output mode
	PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);

	//Set GPIO0 low
	gpio_output_set(0, RELAY_PIN, RELAY_PIN, 0);

	/* disarm timer */
	os_timer_disarm((ETSTimer*)&some_timer);

	/* set callback */
	os_timer_setfn((ETSTimer*)&some_timer, (os_timer_func_t *) softwareTimerCallback, NULL);

	/* arm the timer -> os_timer_arm(<pointer>, <period in ms>, <fire periodically>) */
	os_timer_arm((ETSTimer*)&some_timer, 10, 1);

	/* ====================================== */
	/* OS TASK                                */
	/* ====================================== */

	/* setup OS task */
//	system_os_task(user_procTask, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen);

	/* send a message to OS task (fire task) */
//	system_os_post(user_procTaskPrio, 0, 0 );


	/* ====================================== */
	/* HARDWARE TIMER                         */
	/* ====================================== */

	/* The hardware timer is used to indicate when a complete IR message frame should have
	 * arrived in order to process the received data and calculate the IR command.
	 *
	 * It is configured in "one-shot" mode. It is started when the beginning of an
	 * IR message frame is detected and stopped after the complete message frame has been read.
	 * This means that the duration of the HW timer should be longer than the duration of
	 * the longest message frame. In the NEC IR tranmission protocol all message frames have
	 * a duration of approximately 67.5ms.
	 */

	/* load the HW TIMER */
	uint32 ticks = usToTicks(70000); // 70ms
	RTC_REG_WRITE(FRC1_LOAD_ADDRESS, ticks);

	/* register callback function */
	ETS_FRC_TIMER1_INTR_ATTACH( hwTimerCallback, NULL );

	/* enable interrupts */
	TM1_EDGE_INT_ENABLE();
	ETS_FRC1_INTR_ENABLE();

	/* don't start timer yet */
	/* the timer is started inside the GPIO INT callback */


	/* ====================================== */
	/* UDP SERVER                         	  */
	/* ====================================== */

	/* usage:	echo <data> | nc -wl -u <ip address> <port>
	 * example: echo "foo" | nc -w1 -u 192.168.1.187 7777 */

	/* allocate space for server */
	pUdpServer = (struct espconn *) os_zalloc(sizeof(struct espconn));

	/* clear allocated memory */
	ets_memset(pUdpServer, 0, sizeof(struct espconn));

	/* create the server */
	espconn_create(pUdpServer);

	/* set the type of server */
	pUdpServer->type = ESPCONN_UDP;

	/* allocate memory for UDP settings */
	pUdpServer->proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp));

	/* set the port that the server will be listening to */
	pUdpServer->proto.udp->local_port = 7777;

	/* register the callback */
	espconn_regist_recvcb(pUdpServer, udpServerRxCb);

	/* start listening */
	if (espconn_create(pUdpServer))
	{
		while (1) { os_printf("Error creating a UDP server\n"); }
	}


	/* ====================================== */
	/* WIFI                         	  	  */
	/* ====================================== */

	wifi_set_opmode(STATION_MODE);

	wifi_station_get_config_default(&stconf);

//	os_strncpy((char*) stconf.ssid, "TP-LINK_2.4GHz_FC2E51", 32);
//	os_strncpy((char*) stconf.password, "tonytony", 64);

	os_strncpy((char*) stconf.ssid, "WLAN-PUB", 32);
	os_strncpy((char*) stconf.password, "", 64);

//	os_strncpy((char*) stconf.ssid, "MAD air", 32);
//	os_strncpy((char*) stconf.password, "glioninlog", 64);

	stconf.bssid_set = 0;
	wifi_station_set_config(&stconf);

//	/* ====================================== */
//	/* WS2812 LED STRIP                	  	  */
//	/* ====================================== */
//
//	/* NOTE: UART1 and I2S share same GPIO. Cannot use simultaneously. */
//	ws2812_init();
//
//	/*						G		R		B			*/
//	uint8_t ledout[] = 	{
//							0xff,	0x00,	0x00, 		//4th
////							0xff,	0x00,	0x00,		//3rd
////							0x00,	0xff,	0x00,		//2nd
////							0x00,	0x00,	0xff, 		//1st
//						};
//
//#if 0
//		os_printf("\r\nB R G: %x %x %x\r\n", ledout[0], ledout[1],ledout[2]);
//#endif
//
//	ws2812_push( ledout, sizeof( ledout ) );

	/* ====================================== */
	/* TCP CONNECTION                    	  */
	/* ====================================== */

	/* allocate space for server */
	pTcpConn = (struct espconn *) os_zalloc(sizeof(struct espconn));

	/* clear allocated memory */
	ets_memset(pTcpConn, 0, sizeof(struct espconn));

	/* set the type of connection */
	pTcpConn->type = ESPCONN_TCP;

	/* set state to NONE */
	pTcpConn->state = ESPCONN_NONE;

	/* allocate memory for TCP settings */
	pTcpConn->proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp));

	/* set the port that the connection will be listening to */
	pTcpConn->proto.tcp->local_port = espconn_port();

	/* set the remote port and IP address */
	pTcpConn->proto.tcp->remote_port = 80;
	os_memcpy(pTcpConn->proto.tcp->remote_ip, server_ip_address, sizeof(server_ip_address));

	/* register callbacks */
	espconn_regist_connectcb(pTcpConn, tcpConnCb);
	espconn_regist_reconcb(pTcpConn, tcpReconnCb);

	/* disarm timer */
	os_timer_disarm((ETSTimer*)&wifi_setup_timer);

	/* set callback */
	os_timer_setfn((ETSTimer*)&wifi_setup_timer, (os_timer_func_t *) wifiConnectTimerCb, NULL);

	/* arm the timer -> os_timer_arm(<pointer>, <period in ms>, <fire periodically>) */
	os_timer_arm((ETSTimer*)&wifi_setup_timer, 5000, 1);

	return;

}
Exemple #12
0
int main (void)
{	
	/* variables for the UART0 (USB connection) */
	unsigned int c = 0, c2 = 0, c3 = 0; // Variable for reading UARTS
	char buffer[MAX_MSG_SIZE];
	char buffer2[MAX_MSG_SIZE];
	char buffer3[MAX_MSG_SIZE];
	int  idx = 0, idx2 = -1, idx3 = -1;
	int	 len2 = 0;
	int	 len3 = 0;
	char meas_buffer[TX_BUFF_SIZE];
	int txi = 0;
	int txtop=0;
	unsigned int i = 0;
	char *ptr;
	unsigned char hli_mutex = 0;
	unsigned int gps = 0;
	unsigned int imu = 0;
	signed int ratio = 0;
	uint16_t xacc = 0;
	uint8_t xacca[2];
	char s[64];
	char rmc[256];

	awake_flag = 0;

	#ifdef RF_TEST_IDX
	uint8_t gps_rf_test_idx = 0;
	uint8_t imu_rf_test_idx = 0;
	#endif


  /* set outputs */
	PORTL = 0xff; // Turn off LEDS
  DDRL = (1<<LED1) | (1<<LED2) | (1<<LED3) | (1<<LED4); // Set pins for LED as output

	pwm_init();
	spiInit();

	/* initialize UARTS */
  uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); // USB connection
  uart2_init( UART_BAUD_SELECT(UART2_BAUD_RATE,F_CPU) ); // APC220 radio
  uart3_init( UART_BAUD_SELECT(UART3_BAUD_RATE,F_CPU) ); // UP-501 GPS

	/* Interrupt stuff for ADIS */
	PCICR |= 1<<PCIE2; // Enable interrupt PORTK
	PCMSK2 |= (1<<PCINT23); // interrupt in PCINT23

  /* now enable interrupt, since UART library is interrupt controlled */
  sei();

	spiTransferWord(0xBE80); // ADSI software reset
	_delay_ms(500);
	/* Set GPS to a faster baud and update UART speed */
	//uart3_puts("$PMTK251,115200*1F");
	uart3_puts("$PMTK251,57600*2C\r\n");
	//uart3_puts("$PMTK251,38400*27");
	//uart3_puts("$PMTK251,0*28");
	_delay_ms(500);
	//	uart3_init( UART_BAUD_SELECT(115200,F_CPU) );
	uart3_init( UART_BAUD_SELECT(57600,F_CPU) );
	//uart3_init( UART_BAUD_SELECT(38400,F_CPU) );
	/* 115200 seems to be a little bit unstable, at least testing via radio*/

	adis_reset_factory();
	adis_set_sample_rate();

  while (1) {
		/* Read each UART serially and check each of them for data, if there is handle it */ 	
		c = uart_getc();
		c2 = uart2_getc();
		c3 = uart3_getc();

		// Stop motors when connection is lost
		if (awake_flag > AWAKE_THRESHOLD) {
			pwm_set_duty(RC1, 0 );
			pwm_set_duty(RC2, 0 );
		};

		if(tx_counter >= TX_READY) {
			//empty buffer
			for (txi = 0; txi < txtop; txi++) {
				uart2_putc(meas_buffer[txi]); // Sending buffered data to RF
			}
			txtop = 0;
			#ifdef AUTO_SHUTDOWN_ENABLE
			awake_flag++;
			#endif
			PORTL ^= (1<<LED2);
			tx_counter -= TX_READY;
		}


		if (adis_ready_counter >= ADIS_READY) {
			adis_decode_burst_read_pack(&adis_data_decoded);
			adis_reduce_decoded_burst(); // Reduce data ammount
			#ifdef LOG_ENABLE
			hli_send(package(sizeof(adis8_t), 0x14, 0x0D, &adis_data_decoded), sizeof(adis8_t)); // Log to SD card
			#endif

			#ifdef RF_TEST_IDX
			memcpy(&adis_data_decoded_reduced.zgyro[0],&imu_rf_test_idx,1);
			if (imu_rf_test_idx == 255)
				imu_rf_test_idx = 1;
			else
				imu_rf_test_idx++;
			memcpy(&meas_buffer[txtop],	(char *)package(sizeof(adis8_reduced_t), 0x14, 0x0F, &adis_data_decoded_reduced),sizeof(adis8_reduced_t)+6);
			#endif
			#ifndef RF_TEST_IDX
			memcpy(&meas_buffer[txtop],	(char *)package(sizeof(adis8_reduced_t), 0x14, 0x0E, &adis_data_decoded_reduced),sizeof(adis8_reduced_t)+6);
			#endif
			txtop=txtop+sizeof(adis8_reduced_t)+6;

			adis_ready_counter -= ADIS_READY;

			PORTL ^= (1<<LED4);
		}


		/* Reading from radio */
		if ( c2 & UART_NO_DATA ) {} else // Data available
		{ //if data is $, set a flag, read next byte, set that value as the length, read while incrementing index until length reached, parse
//uart_putc(c2);
			if (idx2 == 0) { // We should buffer a packet
				len2 = c2+5; // Set length
			}

			if ( (idx2 < len2) && (idx2 >= 0)) { // We are buffering
				buffer2[idx2] = c2;
				idx2++;

				if (idx2 == len2) { // We now have a full packet

					if (parse(&rfmsg, buffer2)) {
						PORTL ^= (1<<LED1);
						process(&rfmsg);
					}

					idx2 = -1; // Set flag in new packet mode

					#ifdef DEBUG
					//puts_msg(&rfmsg);
					#endif
				}
			}

			if (c2 == '$') { // We have a possible message comming
//				PORTL ^= (1<<LED4);
				idx2 = 0; // Set "flag"
			}
		}

		/* Reading from GPS */
		if ( c3 & UART_NO_DATA ) {} else  // Data available
		{
			/* Transmitting NMEA GPS sentences to the HLI */
			if (c3 == '$') { // We have a possible message comming
				//PORTL ^= (1<<LED3);
				len3 = 0; // Set "flag"
			}

			if (len3 >= 0) { // We are buffering
				buffer3[len3] = c3;
				len3++;
				if (c3 == '\n') { // We now have a full packet
					if(buffer3[4] != 'S') { // Disable GSV and GSA messages
						#ifdef LOG_ENABLE
						hli_send(package(len3, 0x1E, 0x06, buffer3), len3); // Log to SD card
						#endif
						if (rmc_cut(buffer3,rmc)) {
							// Invalid RMC data
						} else {
							#ifdef RF_TEST_IDX
							memcpy(&rmc[0],&gps_rf_test_idx,1);
							if (gps_rf_test_idx == 255)
								gps_rf_test_idx = 1;
							else
								gps_rf_test_idx++;
							memcpy(&meas_buffer[txtop],	(char *)package(rmc_idx, 30, 31, rmc),rmc_idx+6);
							#endif
							#ifndef RF_TEST_IDX
							memcpy(&meas_buffer[txtop],	(char *)package(rmc_idx, 30, 6, rmc),rmc_idx+6);
							#endif
							txtop=txtop+rmc_idx+6;

							PORTL ^= (1<<LED3);
						}

						len3 = -1; // Set flag in new packet mode
					}
				}
			}
		}
  }

  return 1;
}
Exemple #13
0
int main(int ac, char** av)
{
  unsigned int i = 0;
  pwm_t pwm;

  if (pwm_init(&pwm) == -1)
  {
    return -1;
  }

  if (pwm_mux_init() == -1)
  {
    pwm_fini(&pwm);
    return -1;
  }

  pwm_disable(&pwm);
  pwm_set_clk_div(&pwm, (unsigned int)(19200000.0 / 19200.0));
  pwm_set_freq(&pwm, 500);
#if 0
  /* problem */
  pwm_mux_sel(0);
#else
  /* no problem */
  pwm_mux_sel(1);
#endif

  while (1)
  {
    static unsigned int i = 0;
    unsigned int delay_us;
    /* const char c = getchar(); */
    static const char fu[] = { 'l', 'r' };
    const char c = fu[(++i) & 1];
    unsigned int do_pwm = 0;
    if (c == 'l')
    {
      printf("-- l\n");
      pwm_set_duty(&pwm, 39);
      delay_us = 50000;
/*       delay_us = 10000; */
      do_pwm = 1;
    }
    else if (c == 'r')
    {
      printf("-- r\n");
      pwm_set_duty(&pwm, 10);
      delay_us = 10000;
      do_pwm= 1;
    }

    if (do_pwm)
    {
      pwm_enable(&pwm);
      usleep(delay_us);
      pwm_disable(&pwm);
      usleep(1000000);
    }
  }

  pwm_mux_fini();
  pwm_fini(&pwm);

  return 0;
}
Exemple #14
0
void lcd_close_backlight(void)
{
	pwm_set_duty(LCD_BACKLIGHT_PWM_CHN, 0);
}