// This function initializes the LCD plus any AVR ports etc void lcd_init() { // Initialise the AVR ports and any other hardware bits DATA_PORT.OUT = 0x00; // initial data lines all low DATA_PORT.DIRSET = 0xff; // set the 8-bit data port to all outputs COMM_PORT.OUTCLR = LCD_E | LCD_RS; // all LCD control lines low COMM_PORT.DIRSET = LCD_E | LCD_RS; // set the LCD control line pins to outputs // Now do the actual LCD initialisations // startup delay - make it long to allow time for power to settle // (you want wish to remove this line) _delay_ms(500); // LCD display initialization by instruction magic sequence lcd_write_byte(0x38); // function set _delay_us(50); lcd_write_byte(0x0c); // display on/off control _delay_us(50); lcd_write_byte(0x01); // display clear _delay_ms(5); lcd_write_byte(0x06); // entry mode set _delay_ms(5); lcd_write_byte(0x14); // Cursor shift lcd_clear_and_home(); // LCD cleared and cursor is brought to // the beginning of 1st line }
void bghighscore_new(int8_t rank, uint16_t score) { int8_t i; nkbuttons_t button_state; uint8_t pressed_buttons; nkbuttons_clear(&button_state); for (i = 0; i < INITIALS; i++) highscores[rank].initials[i] = 'a'; highscores[rank].score = score; lcd_clear_and_home(); lcd_goto_position(0, 2); lcd_write_string(PSTR("NEW HIGH SCORE")); bghighscore_display_line(rank, 2); lcd_goto_position(2, 6); nklcd_start_blinking(); i = 0; while(1) { if (nktimer_animate()) { pressed_buttons = nkbuttons_read(&button_state); if(pressed_buttons) { nklcd_stop_blinking(); alter_highscore_initials(pressed_buttons, rank, i); if(bghighscore_move_cursor(pressed_buttons, &i)) break; bghighscore_display_line(rank, 2); lcd_goto_position(2, 6+i); nklcd_start_blinking(); } } } bghighscore_write(); }
// ----------------------------------------------------------------- int main() { // Initialize the lcd lcd_init(); fdev_setup_stream(&lcd_stream, lcd_putchar, 0, _FDEV_SETUP_WRITE); lcd_clear_and_home(); // initialize eeprom and TWI/I2C eeprom_init(); // specify 7 bit device address of eeprom chip #define EEPROM_DEVICE_ID 0b1010000 fprintf_P(&lcd_stream, PSTR("24LC256 ADDR: %02X"), EEPROM_DEVICE_ID); // specify eeprom memory address for data storage #define EEPROM_DATA_ADDRESS 0x0000 // create local copy of eeprom data EEPROM_DATA e_data; // read eeprom contents at location 0000 e_data = read_eeprom(0); // show what we read from the eeprom - // note: the very first read on a new eeprom // will show uninitalized data show_eeprom_data(&e_data); // process data if(e_data.need_initalization){ // set all data item values if first time e_data.need_initalization = false; strcpy_P(e_data.author, PSTR("Noter")); e_data.read_count = 1; e_data.brightness = 0x33; e_data.version = 1.01; } else { // check contents against the values written when initializing if((e_data.version != 1.01)|| (strcmp_P(e_data.author, PSTR("Noter")) !=0)|| (e_data.brightness != 0x33)){ lcd_line_four(); fprintf_P(&lcd_stream, PSTR("DATA ERROR - ")); while(true); // and freeze } else { // increment read_count e_data.read_count += 1; } } // write data to eeprom memory at location 0000 write_eeprom(0, &e_data); // and show the read count lcd_line_two(); fprintf_P(&lcd_stream, PSTR("READ COUNT = %d"), e_data.read_count); // done while(true); }
void bggame_over(uint16_t score) { // game is over (no more moves) nklcd_stop_blinking(); lcd_clear_and_home(); lcd_goto_position(0, 6); lcd_write_string(PSTR("GAME OVER")); lcd_goto_position(2, 4); lcd_write_string(PSTR("score: ")); lcd_write_int16(score); nktimer_simple_delay(300); }
void bghighscore_screen() { int8_t s; // game is over (no more moves) lcd_clear_and_home(); nklcd_stop_blinking(); lcd_goto_position(0, 5); lcd_write_string(PSTR("HIGH SCORES")); for (s = 0; s < HIGH_SCORES; s++) { bghighscore_display_line(s, 1+s); } nktimer_simple_delay(300); }
void bggame_play(game_t *game) { // row and column of the cursor point_t cursor; // read state nkbuttons_t button_state; // latest new presses uint8_t pressed_buttons; // selection state point_t selection; // idle/sleep timer int16_t idle = 0; nkbuttons_clear(&button_state); cursor.row = 0; cursor.column = 0; bggame_invalidate_selection(&selection); bggame_board_init(game); lcd_clear_and_home(); bggame_animate_clear_sets(game); game->score = 0; // no points for tiles removed before play starts lcd_goto_position(cursor.row, cursor.column); nklcd_start_blinking(); uint8_t move_exists = bggame_valid_move_exists(*game); // now let play begin while(move_exists) { if (nktimer_animate()) { pressed_buttons = nkbuttons_read(&button_state); if(pressed_buttons) { idle = 0; nklcd_stop_blinking(); bggame_move_cursor(*game, pressed_buttons, &cursor); if(bggame_select(game, pressed_buttons, cursor, &selection)) { bggame_animate_clear_sets(game); move_exists = bggame_valid_move_exists(*game); } lcd_goto_position(cursor.row, cursor.column); nklcd_start_blinking(); } else if (++idle > 3600) { // go to sleep after a minute of no activity idle = 0; nksleep_standby(); // display comes out of standby with blink disabled nklcd_start_blinking(); } } } }
int main() { DDRC |= (1<<PC4); _delay_ms(100); lcd_init(); FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0,_FDEV_SETUP_WRITE); int i = 0; lcd_clear_and_home(); lcd_write_string(PSTR("Alex + Christina")); while (1) { lcd_line_two(); if (i) { lcd_write_string(PSTR(" I LOVE YOU")); } else { lcd_write_string(PSTR(" ")); } i = (i+1) % 2; _delay_ms(1000); } return 0; }