/*******************************************************************************
* Function Name	: 	nokia5110_writeString
* Description	: 	Write string to LCD at current position.
* Input			: 	s => string pointer
* Output		: 	None
* Return		: 	None
* Attention		: 	String must be null terminated.
*******************************************************************************/
void nokia5110_writeString(uint8_t *s){
	unsigned char ch;

	while (*s != '\0') {
		ch = *s;
		nokia5110_writeChar(ch);
		s++;
	}
}
// This state is blocking, and can't be interrupted by other states
void inline set_alarm_state() {
	nokia5110_clear();
	nokia5110_gotoXY(3,0);
	nokia5110_writeString("Set Alarm "); 
	nokia5110_writeChar(state == set_alarm_A?'A':'B');
	nokia5110_gotoXY(0,4);
	nokia5110_writeString("[ ]Tone");
	nokia5110_gotoXY(0,5);
	nokia5110_writeString("[ ]Radio");
	
	err = 0;
	
	time_t *ptr;
	if (state == set_alarm_A) {
		time_alarm_A.hour = eeprom_read_byte(&alarm_A_hours);
		time_alarm_A.minute = eeprom_read_byte(&alarm_A_minutes);
		ptr = &time_alarm_A;
	} else {
		time_alarm_B.hour = eeprom_read_byte(&alarm_B_hours);
		time_alarm_B.minute = eeprom_read_byte(&alarm_B_minutes);
		ptr = &time_alarm_B;
	}
	if (!err) err |= set_time_variable(hours, ptr);
	if (!err) err |= set_time_variable(minutes, ptr);
	if (!err) {
		if ((state == set_alarm_A && alarm_A_is_beep) || (state == set_alarm_B && alarm_B_is_beep)) {
			nokia5110_gotoXY(7,4);
			nokia5110_writeChar(126);
		} else {
			nokia5110_gotoXY(7,5);
			nokia5110_writeChar(127);
		}
		wd_clock = 0; // reset activity
		while(!bns[1] && (wd_clock <= 60)) {
			if (bns[0] || bns[2]) {
				if ((state == set_alarm_A && alarm_A_is_beep) || (state == set_alarm_B && alarm_B_is_beep)) {
					nokia5110_gotoXY(7,4);
					nokia5110_writeChar(' ');
				} else {
					nokia5110_gotoXY(7,5);
					nokia5110_writeChar(' ');
				}
				bns[0] = bns[2] = false;
				alarm_A_is_beep ^= (state == set_alarm_A);
				alarm_B_is_beep ^= (state == set_alarm_B);
				if ((state == set_alarm_A && alarm_A_is_beep) || (state == set_alarm_B && alarm_B_is_beep)) {
					nokia5110_gotoXY(7,4);
					nokia5110_writeChar(126);
				} else {
					nokia5110_gotoXY(7,5);
					nokia5110_writeChar(127);
				}
			}
			watchdog_entertain();
		}
		if (bns[1]) {
			bns[1] = false;
		} else {
			err = 1;
		}
	}
	if (!err) {
		if (state == set_alarm_A) {
			//alarm_A_enabled = true;
			eeprom_update_byte(&alarm_A_hours, ptr->hour);
			eeprom_update_byte(&alarm_A_minutes, ptr->minute);
			eeprom_update_byte(&alarm_A_tone_setting, alarm_A_is_beep);
		} else {
			//alarm_B_enabled = true;
			eeprom_update_byte(&alarm_B_hours, ptr->hour);
			eeprom_update_byte(&alarm_B_minutes, ptr->minute);
			eeprom_update_byte(&alarm_B_tone_setting, alarm_B_is_beep);
		}
	}
	
	state = last_state;
}