예제 #1
0
파일: clock.c 프로젝트: dakk/spiderpig-os
/**
 * Imposta l'ora del kernel, aggiornata dal pit
 */
static void clock_callback(regs_t *r)
{
	clock_data.hours = clock_get_hours();
	clock_data.minutes = clock_get_minutes();
	clock_data.seconds = clock_get_seconds();
	clock_data.month = clock_get_month();
	clock_data.year = clock_get_year();
	clock_data.day = clock_get_day();
}
예제 #2
0
///////////////////////////////////////////////////////////////////////
// Main function
//  The code works interrupt based, so the main loop just goes over
//  the state variables to check if action is required.
///////////////////////////////////////////////////////////////////////
void main()
{

	char input;
	char uart_hour;
	char uart_min;
	char uart_sec;
	char uart_day;
	char report_clock_was_set = 0;
	char switch_updated;
	char new_incoming;
    char new_update;
	char settings_retrieved = 0;
	char update_eeprom = 0;
	char i;
	switch_point swpoint;

	// Some hardware init first
	init();
		
	// Main loop
	while (1){

		// Set output LED indicator
		output_led = !output;
		
		// Act depending on the UART state
		if (uart_state == STRING_RECEIVED){
			uart_state = WAIT_FOR_DISCONNECT;
			if (rx_buffer[1] == '-' && rx_buffer[4] == ':') {
				// We have received a valid string with time information, parse it
				//uart_state = WAIT_FOR_DISCONNECT;

				// Get the info
				uart_day = rx_buffer[0] - 0x30;
				uart_hour = (rx_buffer[2] - 0x30) * 10 + (rx_buffer[3] - 0x30);
				uart_min  = (rx_buffer[5] - 0x30) * 10 + (rx_buffer[6] - 0x30);
				uart_sec  = (rx_buffer[8] - 0x30) * 10 + (rx_buffer[9] - 0x30);
				clock_set(uart_day, uart_hour, uart_min, uart_sec);
				report_clock_was_set = 1;
				check_timer_table    = 1;
			}  else if (rx_buffer[1] == '*') {
				update_eeprom = 1;
			}	
		}

		// Report status to incoming connection
		// When a user telnets into the XPORT, current internal time and
        // switch points are reported.
		if (uart_state == INCOMING && new_incoming){
			clock_print();
			print_switch_list();
			new_incoming = 0;
		}
		if (uart_state == IDLE) {
			new_incoming = 1;
		}

		// Update the switch status if there is need to, based on the current time
        // and the rules defined in the EEPROM
		if (check_timer_table){
			check_timer_table = 0;
			switch_updated |= update_switch_state(clock_get_day(), clock_get_hours(), clock_get_minutes());
		}

		// Check if we need to sync our internal clock to the web server.
		// This is done one minute after the odd hour mark when the UART is IDLE
		if ( (uart_state == IDLE) && (clock_get_minutes() == 0x01) && /*(clock_get_hours() & 0x01 == 0) &&*/ new_update) {
			new_update = 0;
			web_php_interface(REQUEST_TIME);
		}
		if (clock_get_minutes() == 0x00) {
			new_update = 1;      // Set at minute 0 so that we only request time once after the one minute mark
		}
		
		// Report to the web that the clock was set
		if (uart_state == IDLE && report_clock_was_set) {
			report_clock_was_set = 0;
			web_php_interface(REPORT_CLOCK_SET);
		}

		// Report to the web that the switch state changed
		if (uart_state == IDLE && switch_updated) {
			switch_updated = 0;
			web_php_interface(REPORT_SWITCH_STATE);
		}

		// Request the new settings from the web server
		if (uart_state == IDLE && request_settings) {
			request_settings = 0;
			web_php_interface(REQUEST_SETTINGS);
		}

		// We can only update the EEPROM after we're in IDLE state because interrupts get disabled during 
        // EEPROM write and we would otherwise miss UART RX interrupts.
		if (uart_state == IDLE && update_eeprom) {
			for (i=0; i<rx_buffer[0]; i++){
				swpoint.hour   = rx_buffer[2+(i*4)];
				swpoint.minute = rx_buffer[3+(i*4)];
				swpoint.mask   = rx_buffer[4+(i*4)];
				swpoint.action = rx_buffer[5+(i*4)];
				swpoint.position = i; 
				put_switch_point(swpoint);
			}
			eeprom_write(POINT_COUNT_ADDRESS, rx_buffer[0]);
			settings_retrieved = 1;
			update_eeprom = 0;
			
		}

		// Report that new settings were loaded
		if (uart_state == IDLE && settings_retrieved) {
			settings_retrieved = 0;
			web_php_interface(SETTINGS_RETRIEVED);
		}
	}
	
}