Пример #1
0
void set_time_t(time_t *sometime, char *buffer) {
  LOCK_CLOCK();
  sometime->hours = char_to_digit(buffer[0]) * 10 + char_to_digit(buffer[1]);
  sometime->deci_seconds = char_to_digit(buffer[2]) * 10 + char_to_digit(buffer[3]);  
  sometime->deci_seconds *= 600;
  UNLOCK_CLOCK();
  state = VIEW_CLOCK;
}
Пример #2
0
boolean enter_time(char key, time_t *sometime, char *buffer, unsigned *positionptr, boolean validate) {
  unsigned digit = char_to_digit(key);
  unsigned position = *positionptr;

  if (!isdigit(key)) {
    error_beep(); // Show that the entry is invalid.
    return false;
  }
  
  if (validate) {
    if ((position == 0 && digit > 2) ||
	    (position == 1 && buffer[position-1] == '2' && digit > 3) ||
	    (digit > 5 && position == 2)) {
      error_beep(); // Show that the entry is invalid.
      return false;  
	}
  }
  
  buffer[position++] = key;
  buffer[position] = '\0';

  if (position == 4) {
    *positionptr = 0;
    return true;
  } else {
    *positionptr = position;
    return false;
  }
}
Пример #3
0
static err_t reg_write (reg_t * regs, char_t suffix)
	{
	err_t err;
	byte_t index;

	context_t * context;
	reg_t val;

	while (1)
		{
		index = char_to_digit (suffix);
		if (index > REG_MAX)
			{
			err = E_INDEX;
			break;
			}

		context = (context_t *) regs;
		val = context->val;

		regs [index] = val;

		err = E_OK;
		break;
		}

	return err;
	}
Пример #4
0
static err_t val_write (reg_t * regs, char_t * token, byte_t len)
	{
	err_t err;
	char_t *pos;
	digit_t d;

	reg_t val;
	context_t *context;

	context = (context_t *) regs;

	err = E_OK;
	val = 0;

	for (pos = token; pos < token + len; pos++)
		{
		d = char_to_digit (*pos);
		if (d > 0xF)
			{
			err = E_VALUE;
			break;
			}

		val = (val << 4) + d;
		}

	if (err != E_VALUE)
		{
		context->val = val;
		}

	return err;
	}
Пример #5
0
// Converts a string constant to digit squence 
int string_to_digit (char *s) {
	int digit = 0;
	while (*s != '\0') {
		digit = digit * 10 + char_to_digit(*s);
		s++;
	}
	return digit;
}
Пример #6
0
void keypress_timer(char key) {
  int minutes, seconds;
  int ds, hrs;
  
  if (enter_time(key, &timer, set_timer_buffer, &set_timer_position, false)) {
	minutes = char_to_digit(set_timer_buffer[0]) * 10 + char_to_digit(set_timer_buffer[1]);
	seconds = char_to_digit(set_timer_buffer[2]) * 10 + char_to_digit(set_timer_buffer[3]);
	
    LOCK_CLOCK();
	ds = time.deci_seconds + minutes * 600 + seconds * 10;
	hrs = time.hours;
	timer.hours = (hrs + ds / MAX_DECISECONDS) % 24;
	timer.deci_seconds = ds % MAX_DECISECONDS;
    UNLOCK_CLOCK();
	
	clear_time_prompt(set_timer_buffer);
  } 
}
Пример #7
0
int main(void){

    char asd[] = "ajeje";
    char foo[] = "FOOBAR";
    
    printf("%d\n", is_vowel('A'));
    printf("%d\n", is_vowel('b'));
    printf("%d\n", char_to_digit('5'));
    printf("%s\n", string_toupper(asd));
    printf("%s\n", string_tolower(foo));
    
    return 0;
}
Пример #8
0
void insert (trie_node_adt *root, char *word) {
	int i, digit;
	int length = strlen (word);
	//printf("%d", length);
	trie_node *node_pointer;
	node_pointer = root->pointer ;

	for (i = 0; i < length; i++) {
		digit = char_to_digit (*(word + i)) - 2;
		
		//printf("hello\n");
		if (node_pointer->children[digit] == NULL) {
			//printf("hey\n");
			node_pointer->children [digit] = initialize_node();
		}
		node_pointer = node_pointer->children[digit];
		//printf("%d\n", digit);
	}
	//printf("forloopend");
	node_pointer->word = word;		// Final node
	printf("%s\n", node_pointer->word);
}
Пример #9
0
int             parse_and_read_uint_base(const char **string_pointer,
                                         unsigned long *result_pointer,
                                         int base)
{
  int           digit;
  const char    *begin;

  begin = *string_pointer;
  *result_pointer = 0;
  while (**string_pointer)
    {
      digit = char_to_digit(**string_pointer, base);
      if (digit == -1)
        break;
      if (mul_and_add(result_pointer, base, digit))
        return (-1);
      (*string_pointer)++;
    }
  if (begin == *string_pointer)
    return (-1);
  return (0);
}
Пример #10
0
/**
 * \brief Application entry point for RTC example.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint8_t uc_key;

	/* Initialize the SAM system */
	sysclk_init();
	board_init();

	/* Initialize the console uart */
	configure_console();

	/* Output example information */
	puts(STRING_HEADER);

	/* Default RTC configuration, 24-hour mode */
	rtc_set_hour_mode(RTC, 0);

	/* Configure RTC interrupts */
	NVIC_DisableIRQ(RTC_IRQn);
	NVIC_ClearPendingIRQ(RTC_IRQn);
	NVIC_SetPriority(RTC_IRQn, 0);
	NVIC_EnableIRQ(RTC_IRQn);
	rtc_enable_interrupt(RTC, RTC_IER_SECEN | RTC_IER_ALREN);

	/* Refresh display once */
	refresh_display();

	/* Handle keypresses */
	while (1) {

		while (uart_read(CONSOLE_UART, &uc_key));

		/* Set time */
		if (uc_key == 't') {
			gs_ul_state = STATE_SET_TIME;

			do {
				puts("\n\r\n\r Set time(hh:mm:ss): ");
			} while (get_new_time());

			/* If valid input, none of the variables for time is 0xff. */
			if (gs_ul_new_hour != 0xFFFFFFFF && (gs_uc_rtc_time[2] == ':')
					&& (gs_uc_rtc_time[5] == ':')) {
				if (rtc_set_time(RTC, gs_ul_new_hour, gs_ul_new_minute,
						gs_ul_new_second)) {
					puts("\n\r Time not set, invalid input!\r");
				}
			} else {
				gs_uc_rtc_time[2] = ':';
				gs_uc_rtc_time[5] = ':';
				puts("\n\r Time not set, invalid input!\r");
			}

			gs_ul_state = STATE_MENU;
			gs_ul_menu_shown = 0;
			refresh_display();
		}

		/* Set date */
		if (uc_key == 'd') {
			gs_ul_state = STATE_SET_DATE;

			do {
				puts("\n\r\n\r Set date(mm/dd/yyyy): ");
			} while (get_new_date());

			/* If valid input, none of the variables for date is 0xff(ff). */
			if (gs_ul_new_year != 0xFFFFFFFF && (gs_uc_date[2] == '/')
					&& (gs_uc_date[5] == '/')) {
				if (rtc_set_date(RTC, gs_ul_new_year, gs_ul_new_month,
						gs_ul_new_day, gs_ul_new_week)) {
					puts("\n\r Date not set, invalid input!\r");
				}
			} else {
				gs_uc_date[2] = '/';
				gs_uc_date[5] = '/';
				puts("\n\r Time not set, invalid input!\r");
			}

			/* Only 'mm/dd' is input. */
			if (gs_ul_new_month != 0xFFFFFFFF &&
						gs_ul_new_year == 0xFFFFFFFF) {
				puts("\n\r Not Set for no year field!\r");
			}

			gs_ul_state = STATE_MENU;
			gs_ul_menu_shown = 0;
			refresh_display();
		}

		/* Set time alarm */
		if (uc_key == 'i') {
			gs_ul_state = STATE_SET_TIME_ALARM;

			rtc_clear_date_alarm(RTC);

			do {
				puts("\n\r\n\r Set time alarm(hh:mm:ss): ");
			} while (get_new_time());

			if (gs_ul_new_hour != 0xFFFFFFFF && (gs_uc_rtc_time[2] == ':')
					&& (gs_uc_rtc_time[5] == ':')) {
				if (rtc_set_time_alarm(RTC, 1, gs_ul_new_hour,
						1, gs_ul_new_minute, 1, gs_ul_new_second)) {
					puts("\n\r Time alarm not set, invalid input!\r");
				} else {
					printf("\n\r Time alarm is set at %02u:%02u:%02u!",
						(unsigned int)gs_ul_new_hour, (unsigned int)gs_ul_new_minute,
						(unsigned int)gs_ul_new_second);
				}
			} else {
				gs_uc_rtc_time[2] = ':';
				gs_uc_rtc_time[5] = ':';
				puts("\n\r Time not set, invalid input!\r");
			}
			gs_ul_state = STATE_MENU;
			gs_ul_menu_shown = 0;
			gs_ul_alarm_triggered = 0;
			refresh_display();
		}

		/* Set date alarm */
		if (uc_key == 'm') {
			gs_ul_state = STATE_SET_DATE_ALARM;

			rtc_clear_time_alarm(RTC);

			do {
				puts("\n\r\n\r Set date alarm(mm/dd/yyyy): ");
			} while (get_new_date());

			if (gs_ul_new_year != 0xFFFFFFFF && (gs_uc_date[2] == '/')
					&& (gs_uc_date[5] == '/')) {
				if (rtc_set_date_alarm(RTC, 1, gs_ul_new_month, 1,
						gs_ul_new_day)) {
					puts("\n\r Date alarm not set, invalid input!\r");
				} else {
					printf("\n\r Date alarm is set on %02u/%02u/%4u!",
							(unsigned int)gs_ul_new_month, (unsigned int)gs_ul_new_day,
							(unsigned int)gs_ul_new_year);
				}
			} else {
				gs_uc_date[2] = '/';
				gs_uc_date[5] = '/';
				puts("\n\r Date alarm not set, invalid input!\r");
			}
			gs_ul_state = STATE_MENU;
			gs_ul_menu_shown = 0;
			gs_ul_alarm_triggered = 0;
			refresh_display();
		}

#if ((SAM3S8) || (SAM3SD8) || (SAM4S) || (SAM4C) || (SAM4CP) || (SAM4CM))
		/* Generate Waveform */
		if (uc_key == 'w') {
			gs_ul_state = STATE_WAVEFORM;
			puts("\n\rMenu:\n\r"
					"  0 - No Waveform\n\r"
					"  1 - 1 Hz square wave\n\r"
					"  2 - 32 Hz square wave\n\r"
					"  3 - 64 Hz square wave\n\r"
					"  4 - 512 Hz square wave\n\r"
					"  5 - Toggles when alarm flag rise\n\r"
					"  6 - Copy of the alarm flag\n\r"
					"  7 - Duty cycle programmable pulse\n\r"
					"  8 - Quit\r");

			while (1) {
				while (uart_read(CONSOLE_UART, &uc_key));

				if ((uc_key >= '0') && (uc_key <= '7')) {
					rtc_set_waveform(RTC, 0, char_to_digit(uc_key));
				}

				if (uc_key == '8') {
					gs_ul_state = STATE_MENU;
					gs_ul_menu_shown = 0;
					refresh_display();
					break;
				}
			}
		}
#endif
		/* Clear trigger flag */
		if (uc_key == 'c') {
			gs_ul_alarm_triggered = 0;
			gs_ul_menu_shown = 0;
			refresh_display();
		}

	}

}
Пример #11
0
/**
 * \brief Get new time. Successful value is put in gs_ul_new_year,
 * gs_ul_new_month, gs_ul_new_day, gs_ul_new_week.
 */
static uint32_t get_new_date(void)
{
	uint8_t uc_key;
	uint32_t i = 0;

	/* Clear setting variable */
	gs_ul_new_year = 0xFFFFFFFF;
	gs_ul_new_month = 0xFFFFFFFF;
	gs_ul_new_day = 0xFFFFFFFF;
	gs_ul_new_week = 0xFFFFFFFF;

	/* Use gs_uc_rtc_time[] as a format template */
	while (1) {

		while (uart_read(CONSOLE_UART, &uc_key));

		/* End input */
		if (uc_key == 0x0d || uc_key == 0x0a) {
			puts("\r");
			break;
		}

		/* DEL or BACKSPACE */
		if (uc_key == 0x7f || uc_key == 0x08) {
			if (i > 0) {
				/* End of date[], then one more back of index */
				if (!gs_uc_date[i]) {
					--i;
				}

				puts("\b \b");
				--i;

				/* Delimiter '/' for date is uneditable */
				if (!((gs_uc_date[i]) >= '0' && (gs_uc_date[i]) <='9')
						&& i > 0) {
					puts("\b \b");
					--i;
				}
			}
		}

		/*
		 * End of gs_uc_rtc_time[], no more input except the above DEL/BS,
		 * or enter to end.
		 */
		if (!gs_uc_date[i]) {
			continue;
		}

		while (uart_write(CONSOLE_UART, uc_key));
		gs_uc_date[i++] = uc_key;

	}

	if (i == 0) {
		return 0;
	}

	if (i != 0 && gs_uc_date[i] != '\0' && i != 6) {
		/* Failure input */
		return 1;
	}

	/* MM-DD-YY */
	gs_ul_new_month = char_to_digit(gs_uc_date[0]) * 10
			+ char_to_digit(gs_uc_date[1]);
	gs_ul_new_day = char_to_digit(gs_uc_date[3]) * 10
			+ char_to_digit(gs_uc_date[4]);
	if (i != 6) {
		/* For 'Set Date' option, get the input new year and new week. */
		gs_ul_new_year = char_to_digit(gs_uc_date[6]) * 1000 +
				char_to_digit(gs_uc_date[7]) * 100 +
				char_to_digit(gs_uc_date[8]) * 10 +
				char_to_digit(gs_uc_date[9]);
		gs_ul_new_week = calculate_week(gs_ul_new_year, gs_ul_new_month,
				gs_ul_new_day);
	}

	/*
	 * Success input. Verification of data is left to RTC internal Error
	 * Checking.
	 */
	return 0;
}
Пример #12
0
/**
 * \brief Get new time. Successful value is put in gs_ul_new_hour,
 * gs_ul_new_minute, gs_ul_new_second.
 */
static uint32_t get_new_time(void)
{
	uint8_t uc_key;
	uint32_t i = 0;

	/* Clear setting variable. */
	gs_ul_new_hour = 0xFFFFFFFF;
	gs_ul_new_minute = 0xFFFFFFFF;
	gs_ul_new_second = 0xFFFFFFFF;

	/* Use gs_uc_rtc_time[] as a format template. */
	while (1) {

		while (uart_read(CONSOLE_UART, &uc_key));

		/* End input */
		if (uc_key == 0x0d || uc_key == 0x0a) {
			puts("\r");
			break;
		}

		/* DEL or BACKSPACE */
		if (uc_key == 0x7f || uc_key == 0x08) {
			if (i > 0) {
				/* End of gs_uc_rtc_time[], then one more back of index */
				if (!gs_uc_rtc_time[i]) {
					--i;
				}

				puts("\b \b");
				--i;

				/* Delimiter ':' for time is uneditable */
				if (!((gs_uc_rtc_time[i]) >= '0' && (gs_uc_rtc_time[i]) <= '9')
						&& i > 0) {
					puts("\b \b");
					--i;
				}
			}
		}

		/*
		 * End of gs_uc_rtc_time[], no more input except the above DEL/BS,
		 * or enter to end.
		 */
		if (!gs_uc_rtc_time[i]) {
			continue;
		}

		while (uart_write(CONSOLE_UART, uc_key));
		gs_uc_rtc_time[i++] = uc_key;

	}

	if (i == 0) {
		return 0;
	}

	if (i != 0 && gs_uc_rtc_time[i] != '\0') {
		/* Failure input */
		return 1;
	}

	gs_ul_new_hour = char_to_digit(gs_uc_rtc_time[0]) * 10 +
			char_to_digit(gs_uc_rtc_time[1]);
	gs_ul_new_minute = char_to_digit(gs_uc_rtc_time[3]) * 10 +
			char_to_digit(gs_uc_rtc_time[4]);
	gs_ul_new_second = char_to_digit(gs_uc_rtc_time[6]) * 10 +
			char_to_digit(gs_uc_rtc_time[7]);

	/* Success input. Verification of data is left to RTC internal Error Checking. */
	return 0;
}