// printf_and_scanf.c
// for NerdKits with ATmega168
// mrobbins@mit.edu

#define F_CPU 14745600

#include <stdio.h>
#include <math.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"

// PIN DEFINITIONS:
//

int main() {
  // start up the LCD
  lcd_init();
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  lcd_home();

  // start up the serial port
  uart_init();
  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;

  // initialize variables
  int16_t y = 0;
  double z = 0.0;
  
  // loop doing this forever!
  while(1) {
    // ask some questions and collect responses
    printf_P(PSTR("\r\nHow old are you? "));
    scanf_P(PSTR("%d"), &y);
    printf_P(PSTR("\r\nWhat's your favorite number? "));
    scanf_P(PSTR("%lf"), &z);
    
    // send results back over the serial port
    printf_P(PSTR("\r\nCool!\r\n You are %d years old and like the number %f."), y, z);
    
    // send results to the LCD as well
    lcd_clear_and_home();
    lcd_line_one();
    fprintf_P(&lcd_stream, PSTR("Age: %d"), y);
    lcd_line_two();
    fprintf_P(&lcd_stream, PSTR("Fav. Number: %7f"), z);
    lcd_line_four();
    fprintf_P(&lcd_stream, PSTR("  www.NerdKits.com  "));
  } 


  return 0;
}
