Beispiel #1
0
unsigned char ds1307_setdate(unsigned char year, unsigned char month, unsigned char day, unsigned char hour, unsigned char minute, unsigned char second)
{
	//sanitize data
	if (second < 0 || second > 59 ||
		minute < 0 || minute > 59 ||
		hour < 0 || hour > 23 ||
		day < 1 || day > 31 ||
		month < 1 || month > 12 ||
		year < 0 || year > 99)
		return 8;

	//sanitize day based on month
	if(day > pgm_read_byte(ds1307_daysinmonth + month - 1))
		return 0;

	//get day of week
    unsigned char dayofweek = ds1307_getdayofweek(year, month, day);

	//write date
	i2c_start_wait(DS1307_ADDR | I2C_W);
	i2c_wrt(0x00);//stop oscillator
	i2c_wrt(ds1307_dec2bcd(second));
	i2c_wrt(ds1307_dec2bcd(minute));
	i2c_wrt(ds1307_dec2bcd(hour));
	i2c_wrt(ds1307_dec2bcd(dayofweek));
	i2c_wrt(ds1307_dec2bcd(day));
	i2c_wrt(ds1307_dec2bcd(month));
	i2c_wrt(ds1307_dec2bcd(year));
	i2c_wrt(0x00); //start oscillator
	i2c_stop();

return 1;
}
void inline home_state() {
	if (init) {
		schedule_insert(system_schedule, 0, ms_clock, get_time);
		schedule_insert(system_schedule, 0, ms_clock, display_home_screen);
		schedule_insert(system_schedule, 0, ms_clock, display_temp_and_alarm_settings);
		schedule_insert(system_schedule, 0, ms_clock, start_ADC_conversion);
		
		nokia5110_clear();
		if (radio_is_on) {
			si4705_power_off();
			radio_is_on = false;
		}
		what_the_beep = false;
		set_volume(0);
		
		#ifndef USING_PRINTF
		PORTD |= _BV(0); // Turn the radio LED off
		PORTD |= _BV(1); // Turn the stereo status LED off
		#endif
	}
	if(bns[0]) {
		bns[0] = false;
	}
	if(bns[1]) {
		bns[1] = false;
		state = menu;
	}
	if(bns[2]) {
		bns[2] = false;
	}
	if (event_is_ready) {
		switch(next_event.id) {
			case start_ADC_conversion:
				ADC_start_conversion();
				set_brightness(photo_avg/PHOTO_AVG_RESOLUTION);
				schedule_insert(system_schedule, 20, ms_clock, start_ADC_conversion);
				break;
		
			case get_time:
				ds1307_getdate_s(&time);
				err = ds1307_getdayofweek(time.year, time.month, time.day);
				schedule_insert(system_schedule, 1000, ms_clock, get_time);
				//printf("%d:%d:%d\n", time.hour, time.minute, time.second);
				break;
		
			case display_home_screen:
				snprintf(line, 12, "%02d:%02d", time.hour - 12*(time.hour>12), time.minute);
				nokia5110_gotoXY(12,1);
				nokia5110_writeString_megaFont(line);
		
				snprintf(line, 12, " %s  ", days_of_week[err]);
				nokia5110_gotoXY(day_pixel_offsets[err],0);
				nokia5110_writeString(line);
		
				snprintf(line, 12, "%s", time.hour<12?"am":"pm");
				nokia5110_gotoXY(68,3);
				nokia5110_writeString(line);
		
				snprintf(line, 9, "%d/%d/%d   ", time.month, time.day, time.year);
				nokia5110_gotoXY(0,4);
				nokia5110_writeString(line);
		
				schedule_insert(system_schedule, 1000, ms_clock, display_home_screen);
				break;
		
			case display_temp_and_alarm_settings:
				init = false;
				snprintf(line, 8, "%02d.%01d%cF  ", (uint8_t)(temperature_avg/THERM_AVG_RESOLUTION/10), (uint8_t)(temperature_avg/THERM_AVG_RESOLUTION%10), 123);
				nokia5110_gotoXY(0,5);
				nokia5110_writeString(line);
			
				nokia5110_gotoXY(63,4);
				if (hol[3]) {
					snprintf(line, 4, "A:%c", alarm_A_is_beep?124+(ADC_values[0]>80)+(ADC_values[0]>180):127);
				} else {
					snprintf(line, 4, "A: ");
				}
				nokia5110_writeString(line);
				nokia5110_gotoXY(63,5);
				if (hol[4]) {
					snprintf(line, 4, "B:%c", alarm_B_is_beep?124+(ADC_values[0]>80)+(ADC_values[0]>180):127);
				} else {
					snprintf(line, 4, "B: ");
				}
				nokia5110_writeString(line);
				schedule_insert(system_schedule, 100, ms_clock, display_temp_and_alarm_settings);
				break;
			
			default:
				break;
		}
	}
	watchdog_feed();
}