Example #1
0
void init_sensor (void)
{
    init_human_sensor ();
    init_light_sensor ();
}
Example #2
0
void main(void) {
	// Have to init the clock first
	init_clock();

	// These initialize variables
	// Mostly just zeroing them
	okay_to_transmit = FALSE;
	server_wants_header = FALSE;
	inside_non_blocking_interrupt = FALSE;
	last_connect_time = 0;
	check_in_period = 0;
	setup_button_pressed = FALSE;
	setup_button_time_trigger = 0;
	setup_button_time_duration = 0;
	main_mode = MAIN_MODE_INIT;
	init_internal_wattage_sensor();
	init_temperature_sensor();
	init_audio_sensor();
	init_light_sensor();
	uint8_t itor = 0;
	for (itor = 0; itor < NUMBER_OF_AUX_PORTS; itor++) {
		aux_sensor[itor] = 0;
	}

	// These initialize functions and interrupts
	init_timer();
	init_time();
	init_leds();
	init_relay();
	init_uart();
	init_transmits();
	init_buttons();
	init_roving(&roving_call_back);

	setup_button_time_trigger = new_time();
	setup_button_time_duration = new_time();
	time_set_seconds(setup_button_time_duration, 2);

	_enable_interrupts();

	// confirm roving works
	while (!enter_command_mode()) {
		reset_roving();
		wait(500);
	}
	init_adc();
	init_pll();

	// This is for the check in
	// If we are disconnected, we wait 5 seconds before trying to reconnect
	last_connect_time = new_time();
	check_in_period = new_time();
	time_set_seconds(check_in_period, 5);


	// And go!!!

	set_led_anim(led_start);

	// MAIN LOOP
	while(1) {
		handle_roving_input();

		// Check if it a long hold
		if(setup_button_pressed) {
			// If the button is pressed down
			if(time_cmp(global_time(), setup_button_time_trigger) >= 0) {
				// If enough time has passed
				if(in_setup_mode()) {
					leave_setup_mode();
				} else {
					start_setup_mode();
				}
				// Only want to check this once
				setup_button_pressed = FALSE;
			}
		}


		// MAIN BEHAVIOR
		// There is setup_mode and main_mode.
		// Setup mode is for configuring WiFi SSID and PASS
		// Main mode is for sampling
		if (in_setup_mode()) {
			if(main_mode != MAIN_MODE_SETUP) {
				stop_sampling();
				set_led_anim(led_setup_start);
				main_mode = MAIN_MODE_SETUP;
			}
			do_setup();

		} else if (in_main_mode()) { //regular mode
			if(main_mode != MAIN_MODE_SAMPLE) {
				set_led_anim(led_main_start);
				start_sampling();
				main_mode = MAIN_MODE_SAMPLE;
			}

			if(!is_associated()) {
				associate();
			} else if (!have_dhcp()) {
				get_dhcp();
			} else {
				if(is_connected()) set_led_anim(led_main_connected);
				else set_led_anim(led_main_assoc);

				if(!is_connected() && (time_cmp(global_time(), last_connect_time) >= 0) ) {
					// If we are not connected, and enough time has passed, connect
					connect();
					add_time_to_time(last_connect_time, check_in_period);
				}

				if(server_wants_header) {
					led_ping();
					exit_command_mode();
					transmit_header();
					wait(100);
				}

				if(okay_to_transmit && have_data_to_transmit()) {
					led_ping();
					exit_command_mode();
					transmit_data();
				}


			}
		} else {
			main_mode = MAIN_MODE_INIT;
			set_led_anim(led_error);
			stop_sampling();
		}
	}
}