switch (c) { case 'w': case ' ': //ROTATE tetris_rotate(); break; case 's': //DOWN tetris_gravity(); break; case 'd': //RIGHT tetris_move_right(); break; case 'a': //LEFT tetris_move_left(); break; default: break; } if (c == 'n') { c=0; //Seed random function so we do not get same start condition //for each new game. In essence we will not start a new game //exactly at the same time. srand(tick); //New Game is_running = true; terminal_cursor_off(); terminal_clear(); tetris_init(); tetris_new_block(); terminal_cursor_home(); tetris_print(); }
static void board_init(void) { /*Configure IO pins: * - UART pins * - SW pin * - LED pin */ DDRD &= ~USART_RX_PIN_bm; DDRD |= USART_TX_PIN_bm; PORTD |= USART_TX_PIN_bm; PORTB |= SW_PIN_bm; DDRB &= ~SW_PIN_bm; /*Disable all modules we will not use*/ PRR = (1 << PRTWI) | (1 << PRTIM2) | (1 << PRTIM0) | (1 << PRSPI) | (1 << PRADC); }
static void configure_console(void) { struct usart_config usart_conf; usart_get_config_defaults(&usart_conf); usart_conf.mux_setting = CONF_STDIO_MUX_SETTING; usart_conf.pinmux_pad0 = CONF_STDIO_PINMUX_PAD0; usart_conf.pinmux_pad1 = CONF_STDIO_PINMUX_PAD1; usart_conf.pinmux_pad2 = CONF_STDIO_PINMUX_PAD2; usart_conf.pinmux_pad3 = CONF_STDIO_PINMUX_PAD3; usart_conf.baudrate = CONF_STDIO_BAUDRATE; stdio_serial_init(&cdc_uart_module, CONF_STDIO_USART_MODULE, &usart_conf); } enum status_code usart_enable_rx_interrupt( struct usart_module *const module, uint8_t *rx_data) { // Sanity check arguments Assert(module); Assert(rx_data); // Issue internal asynchronous read // Get a pointer to the hardware module instance SercomUsart *const usart_hw = &(module->hw->USART); module->rx_buffer_ptr = rx_data; // Enable the RX Complete Interrupt usart_hw->INTENSET.reg = SERCOM_USART_INTFLAG_RXC; return STATUS_OK; } void configure_usart_callbacks(void) { usart_register_callback(&cdc_uart_module, USART_RX_callback, USART_CALLBACK_BUFFER_RECEIVED); usart_enable_callback(&cdc_uart_module, USART_CALLBACK_BUFFER_RECEIVED); }
void init_timer(void) { /*Start timer used to iterate game and seed random function*/ TIFR1 = 1 << OCF1A; TIMSK1 = 1 << OCIE1A; OCR1A = TIMER_TOP_VALUE; TCCR1B = (1 << WGM12) | (1 << CS12) | (1 << CS10); } ISR(TIMER1_COMPA_vect, ISR_BLOCK) { ++tick; iterate_game = true; }
/** Configures TC function with the driver. */ static void configure_tc(void) { struct tc_config config_tc; tc_get_config_defaults(&config_tc); config_tc.counter_size = TC_COUNTER_SIZE_16BIT; config_tc.wave_generation = TC_WAVE_GENERATION_MATCH_FREQ; config_tc.counter_16_bit.compare_capture_channel[0] = 2000; config_tc.clock_prescaler=TC_CLOCK_PRESCALER_DIV1024; tc_init(&tc_instance, CONF_TC_INSTANCE, &config_tc); tc_enable(&tc_instance); } /** Registers TC callback function with the driver. */ static void configure_tc_callbacks(void) { tc_register_callback(&tc_instance, tc_callback_to_counter, TC_CALLBACK_CC_CHANNEL0); tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0); } static void tc_callback_to_counter( struct tc_module *const module_inst) { ++tick; iterate_game = true; }
Source: https://habr.com/ru/post/275401/
All Articles