int main (void) { uint8_t menu_status; struct keyboard_event input; static usart_rs232_options_t SERIAL_OPTIONS = { .baudrate = 57600, .charlength = USART_CHSIZE_8BIT_gc, .paritytype = USART_PMODE_DISABLED_gc, .stopbits = false }; sysclk_init(); board_init(); solenoid_init(); gfx_mono_init(); usart_init_rs232(USART_SERIAL, &SERIAL_OPTIONS); gpio_toggle_pin(NHD_C12832A1Z_BACKLIGHT); while(true) { gfx_mono_menu_init(&main_menu); do { do { keyboard_get_key_state(&input); } while (input.type != KEYBOARD_RELEASE); menu_status = gfx_mono_menu_process_key(&main_menu, input.keycode); } while (menu_status == GFX_MONO_MENU_EVENT_IDLE); //Determine what song to play based on //the input from the user controlling the A3BU switch(menu_status) { case 0: song_menu(0); play_song_with_input(SAMPLE_SONG, SAMPLE_SONG_LENGTH); break; case 1: song_menu(1); play_song_with_input(STAIRWAY, 6); break; case 2: song_menu(2); play_song_with_input(MISERLOU, 1); break; case 3: song_menu(3); play_serial(); break; } } }
int main(void) { // Set the system clock to the full 120MHz uint32_t sysClkFreq = SysCtlClockFreqSet(SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480, 120000000); debug_init(sysClkFreq); status_led_init(); network_driver_init(sysClkFreq); networking_init(); telemetry_init(); transducer_init(); thermocouple_init(); solenoid_init(); debug_print("Initialization complete. starting main loop.\r\n"); // Set up the SysTick timer and its interrupts SysTickPeriodSet(6000); // 40 kHz SysTickIntRegister(sys_tick); SysTickIntEnable(); SysTickEnable(); uint32_t loopIterations = 0; uint32_t frame_start = systick_clock; while(1) { status_led_periodic(); network_driver_periodic(); telemetry_periodic(); solenoid_periodic(); // debug_print_u32(systick_clock); //count loop iterations per second loopIterations++; if(systick_clock - frame_start >= 1000) { loops_per_second = loopIterations; loopIterations = 0; frame_start = systick_clock; } } }
int main(void){ //Initialization of UART module UART_init(); //Enable interrupts DDRE &= ~(1 << PE4); EIMSK |= (1 << INT4); EICRB |= (1 << ISC41); sei(); //Initialization of CAN module SPI_MasterInit(); CANInit_normal(); //Initialization of PWM module PWM_init(); //Initialization of ADC module ADC_init(); //Initialization of Solenoid module solenoid_init(); //Initialization of Motor module motor_init(); //Variables uint8_t ready = 0; uint8_t game_over = 0; uint8_t some_counter = 0; // Used for delay on IR, so that not a single low read causes game to end uint8_t some_other_counter = 0; //Solenoid should not spam. It kills everything. Mad solenoid //This is where new received joystick values are stored joystick_position joy_pos; //Message is passed back to node 1 upon game over message.length = 1; message.ID = 0x01; while(1){ /* PHASE 0: INITIALIZATION */ ready = 0; game_over = 0; joy_pos.x = 128; joy_pos.y = 128; joy_pos.button_pressed = 0; //Set to some start value PWM_set_value(joy_pos); motor_send(joy_pos); /* PHASE 1: PRE-GAME */ /* Wait for node 1 to make contact */ printf("Waiting for node 1 to initiate game\n"); while(!ready){ cli(); if(CAN_received){ received = CAN_read(); CAN_received = 0; if(received.data[3] == 1){ //If this is set, node 1 wants to start a game received.data[3] = 0; ready = 1; } } sei(); _delay_ms(5); } printf("Node 1 is ready to go!\n"); printf("Starting game\n"); /* PHASE 2: IN-GAME */ /* Follow input over CAN and return a message when we have a game over. It is not a matter of if, only when! */ while(!game_over){ cli(); //Interrupts are disabled while we read from the CAN bus. Bad values were sometimes introduced otherwise if(CAN_received){ //If updated controller info is ready, read it into our struct received = CAN_read(); CAN_received = 0; joy_pos.y = received.data[0]; //first spot contains y value joy_pos.x = received.data[1]; //second spot contains x value joy_pos.button_pressed = received.data[2]; //third spot contains button value } if(joy_pos.y > 128 - margin && joy_pos.y < 128 + margin){ joy_pos.y = 128; //This is done to avoid noisy PWM input } /* Apply new numbers to the system */ //Use information available to control PWM, motor and solenoid some_other_counter++; /********************************************************/ /* Solenoid pulse function is omitted to avoid spamming. Spamming is more likely to introduce errors */ if(joy_pos.button_pressed){ solenoid_push(); some_other_counter = 0; //solenoid_pulse(); //Apply solenoid pulse. Good luck joy_pos.button_pressed = 0; } if(some_other_counter > 10){ solenoid_pull(); } /**********************************************************/ PWM_set_value(joy_pos); //update servo motor_send(joy_pos); //update motor if(ADC_check_goal()){ //Evaluates to 1 if goal is detected some_counter++; if(some_counter == 20){ some_counter = 0; game_over = 1; //Start procedure all over CAN_reset(); //Tell node 1 about the failure message.data[0] = 1; CAN_send(message); } } sei(); //Allow interrupts to occur _delay_ms(20); } /* GAME ENDED. WAIT FOR NEW ONE TO START */ } }