void keyboard_int_handler(unsigned pc){ if (gpio_check_and_clear_event(CLK)){ int bit = gpio_read(DATA); //Check start, parity and stop bit if ( (cnt == 0 && bit == 1) || (cnt == 9 && (numOfOne & 1) && bit) || (cnt == 9 && (!(numOfOne & 1) && !bit)) || (cnt == 10 && bit == 0) ){ cir_push(0); currScancode = 0; cnt = 0; return; } if (cnt > 0 && cnt < 9){ if (bit) numOfOne ++; currScancode |= (bit << (cnt-1)); } PS2Code[cnt++] = bit; //Package received if (cnt == 11){ cir_push(currScancode); /*printf("%08x\n", currScancode); */ cnt = 0; currScancode = 0; } } else{ timer_int_handler(pc); } }
int timer_test_int(unsigned long time) { if (timer_subscribe_int()) { return 1; } int r, ipc_status; message msg; time_counter = 0; while (time_counter <= time * TIMER_DEFAULT_FREQ) { /* Get a request message. */ if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.NOTIFY_ARG & BIT(TIMER0_HOOK_BIT)) { /////// /* subscribed interrupt */ timer_int_handler(); if(time_counter % TIMER_DEFAULT_FREQ == 0) { printf("Segundos decorridos: %d\n", time_counter / TIMER_DEFAULT_FREQ); } } break; default: break; /* no other notifications expected: do nothing */ } } else { /* received a standard message, not a notification */ /* no standard messages expected: do nothing */ } } return timer_unsubscribe_int(); }
int start() { unsigned short character = 0x00; int ipc_status; message msg; int irq_set_kb = kb_subscribe_int(); int irq_set_timer = timer_subscribe_int(); int irq_set_mouse = mouse_subscribe_int(); int r; unsigned char packet[3]; unsigned short counter = 0; space_invaders_font = font_init("spaceinvader_font_transparent.bmp"); srand(1); options_load(); mouse_init(); init_state(); highscore_load(); vg_init(VBE_VIDEO_MODE); while (1) { //TODO change condition /* Get a request message. */ if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.NOTIFY_ARG & irq_set_kb) { /* keyboard interrupt */ character = kb_int_handler(); if (character != KB_2BYTE_SCODE) kb_event_handler(character); } if (msg.NOTIFY_ARG & irq_set_mouse){ /* mouse interrupt */ mouse_int_handler(counter, packet); if(packet[0] != MOUSE_ACK && (packet[0] & BIT(3))) counter++; if(counter == 3){ counter = 0; mouse_event_handler(packet); } } if (msg.NOTIFY_ARG & irq_set_timer){ /* timer interrupt */ timer_int_handler(); } break; default: break; /* no other notifications expected: do nothing */ } } else { /* received a standard message, not a notification */ /* no standard messages expected: do nothing */ } } return 0; }
int main_menu() { continue_condition = true; fade_condition = false; function_to_call = NULL; darken = 127; int failure = 0; initialize_main_menu_buttons(); int ipc_status; message msg; int r; char * display_name = (char*)malloc(sizeof(char) * strlen("Player name: ")+MAX_PLAYER_NAME_LENGTH); strcpy(display_name, "Player name: "); display_name = strcat(display_name, get_program_playername()); if(!failure){ do{ /* Get a request message. */ if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.NOTIFY_ARG & get_kbd_irq_set()){ /* subscribed kbd interrupt */ kbd_int_handler(); } if (msg.NOTIFY_ARG & get_rtc_irq_set()) { /* subscribed timer interrupt */ if(rtc_ih()) failure = 1; getDateString(get_date_str_ptr()); } if(msg.NOTIFY_ARG & get_timer_irq_set()){ timer_int_handler(); main_menu_render(); } if (msg.NOTIFY_ARG & get_mouse_irq_set()) { /* subscribed timer interrupt */ mouse_int_handler(); } break; default: break; /* no other notifications expected: do nothing */ } } else {/* received a standard message, not a notification */ /* no standard messages expected: do nothing */ } if(mouse_is_updated()) { assign_mouse(get_previous_mouse(), get_mouse()); assign_mouse(get_mouse(), get_mouse_state()); main_menu_mouse_event(get_previous_mouse(), get_mouse()); move_cursor(get_cursor(), get_mouse()->coords); } } while(continue_condition); } return failure; }
void *test_init(unsigned short mode, unsigned short delay) { void* video_mem = NULL; if((video_mem=vg_init(mode)) == NULL) { printf("test_init(): vg_init() failed"); return NULL; } int failure=0; int timer_irq_set=0; if((timer_irq_set = timer_subscribe_int()) < 0){//subscribe timer 0 interrupts printf("test_move(): timer_subscribe_int() failed \n"); failure = 1; } int ipc_status; message msg; int r; //unsigned long ticks=0; if(!failure){ do{ /* Get a request message. */ if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if(msg.NOTIFY_ARG & timer_irq_set){ timer_int_handler(); } break; default: break; /* no other notifications expected: do nothing */ } } else {/* received a standard message, not a notification */ /* no standard messages expected: do nothing */ } } while(get_ticks() < delay * 60); } if(timer_unsubscribe_int()){//unsubscribe interrupts printf("test_move(): timer_unsubscribe_int() failed\n"); failure = 1; } if(vg_exit()){ printf("test_init(): vg_exit() failed"); return NULL; } printf("Physical Address of VRAM: 0x%X\n", get_vram_phys_addr()); return (void*)get_video_mem(); }
int timer_test_int(unsigned long time) { int irq_set = timer_subscribe_int(); //subscreve e inicia as interrupções do timer0 timer_set_square(0,60); //coloca a frequencia a 60 int ipc_status; int r; message msg; unsigned i = 0; while( i < time) { // enquando a contagem é menor que o valor passado no parametro /* Get a request message. */ if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.NOTIFY_ARG & irq_set) { /* subscribed interrupt */ timer_int_handler(); if (counter % 60 == 0) //a cada segundo (60 contagens a 60 de frequencia) { i++; printf("%d", i); // imprime o valor do segundo. printf("\n"); } } break; default: break; /* no other notifications expected: do nothing */ } } else { /* received a standard message, not a notification */ /* no standard messages expected: do nothing */ } } if (timer_unsubscribe_int() != OK) //termina a subscrição, caso dê erro retorna 1 return 1; return 0; }
int set_repetitive_task(unsigned long freq, void(*func)()) { if (doing_task) // Current task must be stopped first { return 1; } if (timer_subscribe_int()) { return 1; } if (timer_set_square(0, freq)) { return 1; } int r, ipc_status; message msg; time_counter = 0; doing_task = true; while (doing_task) { /* Get a request message. */ if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.NOTIFY_ARG & BIT(TIMER0_HOOK_BIT)) { /* subscribed interrupt */ timer_int_handler(); func(); } break; default: break; /* no other notifications expected: do nothing */ } } else { /* received a standard message, not a notification */ /* no standard messages expected: do nothing */ } } return 0; }
int kbd_test_timed_scan(unsigned short n) { int ipc_status; unsigned char result = 0; message msg; int kbd_interrupt = kbd_subscribe_int(); int timer_interrupt = timer_subscribe_int(); if (kbd_interrupt == ERROR || timer_interrupt == ERROR) { printf("kbd_test_timed_scan()::kernel call didn't return 0\n"); return 1; } while (result != KEY_ESC && timer_count < n * 60) { if (driver_receive(ANY, &msg, &ipc_status) != OK) { printf("kbd_test_timed_scan()::driver_receive failed\n"); continue; } if (is_ipc_notify(ipc_status)) { switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: if (msg.NOTIFY_ARG & timer_interrupt) { timer_int_handler(); if (timer_count >= n * 60) { printf("kbd_test_timed_scan()::timed out\n"); } } if (msg.NOTIFY_ARG & kbd_interrupt) { result = kbd_int_handler_C(); if (result == KEY_ESC) { printf( "kbd_test_timed_scan()::esc was pressed, press any key to continue\n"); } } break; } } } timer_unsubscribe_int(); kbd_unsubscribe_int(); return 0; }
int timer_test_int(unsigned long time) { if(time == 0){ printf("Error: Time interval must be positive.\n"); return EXIT_FAILURE; } counter = 0; // initialize "counter" global variable int irq_set = timer_subscribe_int(); int interrupt = BIT(irq_set); int r; int ipc_status; message msg; while( (time * 60) >= counter ) { /* You may want to use a different condition */ /* Get a request message. */ if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.NOTIFY_ARG & interrupt) { timer_int_handler(); if (counter % 60 == 0) printf("1 segundo decorrido, Interrupcoes totais (60/segundo): %d \n", counter); } break; default: break; /* no other notifications expected: do nothing */ } } else { /* received a standard message, not a notification */ /* no standard messages expected: do nothing */ } } timer_unsubscribe_int(); printf("Timer_test_int concluido.\n"); return EXIT_SUCCESS; }
int timer_test_int(unsigned long time) { int ipc_status; message msg; int r; if(timer_subscribe_int() < 0){ printf("ERROR SUBSCRIBING/ENABLING INTERRUPTS ON TIMER 0\n"); return 1; } while(timerInt.counter <= (time*60)){ r = driver_receive(ANY, &msg, &ipc_status); if( r != 0){ printf("driver_receive failed with: %d\n", r); continue; } if(is_ipc_notify(ipc_status)){ switch(_ENDPOINT_P(msg.m_source)){ case HARDWARE: if((msg.NOTIFY_ARG & TIMER_BIT_MASK) && (timerInt.counter % 60 == 0)){ timer_int_handler(); } break; default: break; } } else { } timerInt.counter++; } if(timer_unsubscribe_int()) return 1; else return 0; }
int mp_lobby() { int failure = 0; if((set_com1_irq_set(generic_ser_subscribe_int_tx_rx(COM1))) < 0) {//subscribe interrupts printf("mp_lobby(): generic_ser_subscribe_int_tx_rx() failed \n"); failure = 1; } if(SET_COMMUNICATION_PARAM()) { printf("ser_set() failed\n"); return 1; } initialize_lobby_buttons(); set_role(SEARCHING); lobby_state = NOT_READY; enable_per_second_alarm(on_rtc_alarm_int_transmission); if(im_message_log != NULL) { size_t del_i; for(del_i = 0 ; del_i < size(im_message_log); del_i++) { free(*(char**)at(im_message_log, del_i)); } delete_vector_t(im_message_log); } im_message_log = new_vector_t(sizeof(char*)); memset(current_im, 0, sizeof(char)*IM_MESSAGE_MAX); current_im_index = 0; draw_im_index = 0; int wait = 0; function_to_call = NULL; continue_condition = true; fade_condition = false; darken = FADE_MAX; int ipc_status; message msg; int r; set_program_player2name((char*)malloc(sizeof(char)*MAX_PLAYER_NAME_LENGTH+1)); if(get_program_playername2() == NULL) return 1; memset(get_program_playername2(),0,sizeof(char)*MAX_PLAYER_NAME_LENGTH+1); size_t next_char_index = 0; char last_char = 0; if(!failure){ do{ /* Get a request message. */ if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.NOTIFY_ARG & get_com1_irq_set()) { /* subscribed interrupt */ if(ser_ih(COM1)) failure = 1; } if (msg.NOTIFY_ARG & get_kbd_irq_set()){ /* subscribed kbd interrupt */ kbd_int_handler(); if(get_scancode() == ESC_BREAK) { lobby_state = NOT_READY; fade_condition = true; transmit_message(MES_BYE); } if(get_role() == SERVER || get_role() == CLIENT) { if(get_scancode() == ENTER_MAKE && current_im_index != 0) { current_im_index = 0; transmit_protocol(current_im, TYPE_STRING, strlen(current_im)+1); char* temp = (char*) calloc(IM_MESSAGE_MAX + 4,1); strcpy(temp, "me: "); strcat(temp,current_im); push_back(im_message_log, &temp); memset(current_im, 0, IM_MESSAGE_MAX); memset(transmission_message,0,IM_MESSAGE_MAX+4); } else { int action; action = process_input(get_scancode(), &last_char); if(action == CHAR_RECEIVED) { if(current_im_index < IM_MESSAGE_MAX -1) { current_im[current_im_index] = last_char; current_im_index++; } } else if(action == BACKSPACE_MAKE) { if(current_im_index > 0) { current_im_index--; current_im[current_im_index] = '\0'; } } } } } if (msg.NOTIFY_ARG & get_rtc_irq_set()) { /* subscribed timer interrupt */ if(rtc_ih()) failure = 1; getDateString(get_date_str_ptr()); } if(msg.NOTIFY_ARG & get_timer_irq_set()){ timer_int_handler(); mp_lobby_render(); //establish_connection_transmission(); } if (msg.NOTIFY_ARG & get_mouse_irq_set()) { /* subscribed timer interrupt */ mouse_int_handler(); } break; default: break; /* no other notifications expected: do nothing */ } } else { } establish_connection_reception(); if(mouse_is_updated()) { assign_mouse(get_previous_mouse(), get_mouse()); assign_mouse(get_mouse(), get_mouse_state()); if(get_role() == SERVER || get_role() == CLIENT) lobby_mouse_event(get_previous_mouse(), get_mouse()); move_cursor(get_cursor(), get_mouse()->coords); } } while(continue_condition); } if(im_message_log != NULL) { size_t del_i; for(del_i = 0 ; del_i < size(im_message_log); del_i++) { free(*(char**)at(im_message_log, del_i)); } delete_vector_t(im_message_log); im_message_log = NULL; } if(generic_ser_unsubscribe_int_tx_rx(COM1)) {//unsubscribe interrupts printf("ser_int_receive(): generic_ser_unsubscribe_int() failed \n"); failure = 1; } disable_per_second_alarm(); continue_condition = true; fade_condition = false; darken = FADE_MAX; return 0; }
int get_player_name() { continue_condition = true; fade_condition = false; darken = FADE_MAX; int failure = 0; int ipc_status; message msg; int r; if(get_program_playername() == NULL) { if(set_program_playername((char*)malloc(sizeof(char)*MAX_PLAYER_NAME_LENGTH)) ==NULL)return 1; memset(get_program_playername(),0,sizeof(char)*MAX_PLAYER_NAME_LENGTH); } size_t next_char_index = strlen(get_program_playername()); char last_char = 0; if(!failure){ do{ /* Get a request message. */ if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.NOTIFY_ARG & get_kbd_irq_set()){ /* subscribed kbd interrupt */ kbd_int_handler(); if(get_scancode() == ENTER_MAKE && next_char_index != 0) { fade_condition = true; } else { int action; action = process_input(get_scancode(), &last_char); if(action == CHAR_RECEIVED) { //printf("last_char: %c, %d\n", last_char,last_char); if(next_char_index < MAX_PLAYER_NAME_LENGTH-1) { get_program_playername()[next_char_index] = last_char; next_char_index++; } } else if(action == BACKSPACE_MAKE) { if(next_char_index > 0) { next_char_index--; get_program_playername()[next_char_index] = '\0'; } } } } if (msg.NOTIFY_ARG & get_rtc_irq_set()) { /* subscribed timer interrupt */ if(rtc_ih()) failure = 1; getDateString(get_date_str_ptr()); } if(msg.NOTIFY_ARG & get_timer_irq_set()){ timer_int_handler(); get_player_name_render(); } if (msg.NOTIFY_ARG & get_mouse_irq_set()) { /* subscribed timer interrupt */ mouse_int_handler(); } break; default: break; /* no other notifications expected: do nothing */ } } else {/* received a standard message, not a notification */ /* no standard messages expected: do nothing */ } } while(continue_condition); } get_program_playername()[next_char_index] = '\0'; continue_condition = true; fade_condition = false; darken = FADE_MAX; return 0; }
int test_move(unsigned short xi, unsigned short yi, char *xpm[], unsigned short hor, short delta, unsigned short time1) { if(xpm == NULL) return 1; char* video_mem = NULL; int failure =0; int width, height; char* pix = NULL; if((pix = (char*) read_xpm(xpm, &width, &height)) == NULL) return 1; if((video_mem=(char *)vg_init(VBE_MODE_RES_1024x768)) == NULL) { printf("test_move(): vg_init() failed"); return 1; } int kbd_irq_set = 0; int timer_irq_set=0; if((kbd_irq_set = kbd_subscribe_int(0)) < 0){//subscribe kbd interrupts printf("test_move(): kbd_subscribe_int() failed \n"); failure = 1; } if((timer_irq_set = timer_subscribe_int()) < 0){//subscribe timer 0 interrupts printf("test_move(): timer_subscribe_int() failed \n"); failure = 1; } int ipc_status; message msg; int r; double step = (double)delta/(time1*60); double xfp = xi; double yfp = yi; if(!failure){ do{ /* Get a request message. */ if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) { printf("driver_receive failed with: %d", r); continue; } if (is_ipc_notify(ipc_status)) { /* received notification */ switch (_ENDPOINT_P(msg.m_source)) { case HARDWARE: /* hardware interrupt notification */ if (msg.NOTIFY_ARG & kbd_irq_set) /* subscribed kbd interrupt */ if(kbd_int_handler()) failure = 1; if(msg.NOTIFY_ARG & timer_irq_set){ if(get_ticks() < time1 * 60){//enquanto tempo desjado nao passou timer_int_handler(); blank_secondary_buf();//clear buffer draw_pixmap(get_secondary_buf(),(int) xfp, (int) yfp, height, width, pix); commit_to_video_mem(); if(hor) xfp += step;//se movimento horizontal incrementar x else yfp +=step;//senao incrementar y } } break; default: break; /* no other notifications expected: do nothing */ } } else {/* received a standard message, not a notification */ /* no standard messages expected: do nothing */ } } while(get_scancode() != ESC_BREAK && !failure); } if(timer_unsubscribe_int()){//unsubscribe interrupts printf("test_move(): timer_unsubscribe_int() failed\n"); failure = 1; } if(kbd_unsubscribe_int()){//unsubscribe interrupts printf("test_move(): kbd_unsubscribe_int() failed\n"); failure = 1; } printf("Done\n"); if(vg_exit()){ printf("test_move(): vg_exit() failed"); return 1; } return failure; }