Keyboard break handle when using global variable c

I have a space-like project and am trying to handle keyboard interruption. My problem is that I do not want to use global variable(ship) in my_keyboard_interrupt_handler . But I send the ship as a parameter for this function, I do not know how to organize setvect(0x09,my_keyboard_interrupt_handler); . If there is any way to use the setvect function, please give me some recommendations.

 int main() { void interrupt (*old_keyboard_interrupt_handler)(); ship = (space_ship*)malloc(sizeof(space_ship)); old_keyboard_interrupt_handler = getvect(0x09); ... setvect(0x09,my_keyboard_interrupt_handler); return 0; } int handle_key() { int key; asm{ sti in al,60H xor ah,ah mov key,ax in al,61h or al,82h out 61h,al and al,7fh out 61h,al mov al,20h out 20h,al } return key; } 

my keyboard interception handler:

 void interrupt my_keyboard_interrupt_handler() { int key = handle_key(); if(key == SPACE){ }else if(key == RIGHT){ ship->column++; }else if(key == LEFT){ ship->column--; }else if(key == UP){ ship->row_start--; ship->row_end--; } else if(key == DOWN){ ship->row_start++; ship->row_end++; }else if(key == ESC){ } clrscr(); print_space_ship(ship); } 

In short, I want to do void interrupt my_keyboard_interrupt_handler(space_ship* ship){..} . But I do not know how to handle the setvect function in this situation.

+6
source share
1 answer

Well, if you use ship only in ISR, then you can also declare it static inside this function:

 void interrupt my_keyboard_interrupt_handler() { static space_ship ship = {0}; ... print_space_ship(ship); } 

But if you use it in other threads or ISRs, then you should declare it as a shared (global) variable and protect it with a standard OS resource (for example, a semaphore or, rather, Mutex), where necessary.

If this is true, then passing it as an argument to the ISR makes no difference.

+4
source

All Articles