Exemple #1
0
int main (int argc, char *argv[]) {
	struct timespec ts_curr;

	signal(SIGINT, handle_signal);
	signal(SIGTERM, handle_signal);

	int fd= 0;
	fd = serial_port_open(device_tty);
	if (fd<0) {
		printf("cannot open %s\n",device_tty);
		return 0;
	} else {
		printf("%s is open with the file descriptor = %d\n",device_tty,fd);
	}

	unsigned char * buf = malloc(160);
	memset(buf, 0, 160);
	size_t len = 138, r=0;
	while (start_sampling(fd)<0) {
		printf("?");
	}
	volatile int s_idx=0;
	long long last_time, delta_time, curr_time;
	while (!stop_main) {
		r = read(fd, buf, len);
		if(r>0) {
			if(buf[0] == (unsigned char)0x5) {
				// do not print the same index twice
				if (s_idx != get_unsigned_short(buf[4],buf[5])) {
					// update curr_time on new Frame
					clock_gettime(CLOCK_REALTIME, &ts_curr);
					curr_time = ts_curr.tv_sec * 1000 +
						    ts_curr.tv_nsec / 1000000;
					// update the last printed index
					s_idx = get_unsigned_short(buf[4],buf[5]);
					print_data(buf,r, curr_time);
					delta_time = curr_time - last_time;
					last_time = curr_time;
				} else {
					// TODO warn that a frame has the last frame's index
					// printf("warning: last_idx: %d \tcurr_idx: %d\n",s_idx, get_unsigned_short(buf[4],buf[5]));
				}
			} else {
				print_received_buffer(buf,r);
			}
		}
		//sleep(1);
	}

	while(stop_sampling(fd)<0) {
		printf("!");
	}

	if(fd>=0) {
		printf("closing the file descriptor for %s\n",device_tty);
		close(fd);
	}
	free(buf);
	return 0;
}
Exemple #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();
		}
	}
}