Snažím sa vytvoriť os v c a montáž v32-bitový chránený režim. Snažím sa vytvoriť funkciu typu scanf (), ktorá dostane vstup klávesnice, kým sa nestlačí tlačidlo Enter. Mám základnú klávesnicu pre nastavenie IRQ handler, ktorá vytlačí čokoľvek napísané a chcem implementovať funkciu scanf (), ale mám problémy s vrátením hodnoty z Handler Handler do hlavného jadra.
Tu je kód pre obsluhu klávesnice.
unsigned int shift_key = 0;
unsigned int caps_key = 0;
int counter = 0;
char *sbuf;
int scan = 1;
void keyboard_handler(registers_t regs)
{
// monitor_write("handler called");
unsigned char scancode;
scancode = get_scancode();
if(scancode == 0x2A )
{
shift_key = 1;//Shift key is pressed
//monitor_write("Shift + ");
}
else if(scancode == 0xAA)
{
shift_key= 0;//Shift Key is not pressed
// monitor_write("Unshift");
}
else if(scancode == 0x3A && caps_key == 1)
{
caps_key = 0;//Caps key is pressed
// monitor_write("Shift + ");
}
else if(scancode == 0x3A && caps_key == 0)
{
caps_key = 1;//Caps key is pressed
// monitor_write("Shift + ");
}
//Left arrow
else if(scancode == 0x4B)
{
move_cursor_LR(1,0);
}
//right Arrow
else if(scancode == 0x4D)
{
move_cursor_LR(0,1);
}
else
{
if (shift_key == 1 && caps_key == 0)
{
// int shiftaltctrl = 1;//Put anything to see what special keys were pressed
monitor_put(Kkbdus[scancode]);
if (scan == 1 && Kkbdus[scancode] != "n")
{
sbuf[counter] = Kkbdus[scancode];
counter++ ;
}
}
if (caps_key == 1 && shift_key == 0)
{
// int shiftaltctrl = 1;//Put anything to see what special keys were pressed
monitor_put(Kkbdus[scancode]);
if (scan == 1 && Kkbdus[scancode] != "n")
{
sbuf[counter] = Kkbdus[scancode] ;
counter++ ;
}
}
if(shift_key == 0 && caps_key == 0)
{
monitor_put(kbdus[scancode]); //Prints the character which was pressed
if (scan == 1 && kbdus[scancode] != "n")
{
sbuf[counter] = kbdus[scancode];
counter++ ;
}
}
if( caps_key == 1 && shift_key == 1)
{
monitor_put(kbdus[scancode]);
if (scan == 1 && kbdus[scancode] != "n")
{
sbuf[counter] = kbdus[scancode];
counter++ ;
}
}
if(scan == 1 && kbdus[scancode] == "n")
{
scan = 0;
sbuf[0] = " ";
}
if(kbdus[scancode] == "t")
{
monitor_write(sbuf);
}
}
}
Používam premennú scan ako bool na vloženie znakov do poľa pri vyvolaní irq. ale nemôžem získať spôsob, ako ho vrátiť do hlavného súboru, z ktorého ho volám.
odpovede:
0 pre odpoveď č. 1Premýšľal som o tom a skúšal som kód, ktorý fungoval. Čo som urobil, bola vykonaná funkcia v ovládači klávesnice, ktorá by udržala posledný char napísaný. a keď chcem získať znak, ktorý by som jednoducho zavolal getch();
ktorý by vrátil char. a urobil som hlavné scanf();
v hlavnom súbore, ktorý by zbieral znakya najprv ich nastavte na hodnotu 0, aby sa ten istý znak nezopakoval a potom prejde cez slučku, ktorá by overila stlačenie klávesu Enter. a pridal znak na a char*
ak nie je = 0 a ak buffch
premenná je nastavená na 1, ak je nastavená na 0, nebude tlačiť. To bola teória.
Tu je kód:
v ovládači klávesnice: pridal getch a getchter, ktorý by získal znak a nastavil ho na 0.
char getch()
{
char buf = sbuf; // clone the buffer
getchter(); // set the sbuf to 0 so that the same char is not repeated
return buf; // return buff.
}
void getchter()
{
sbuf = 0; // setting sbuf to 0
}
a tu je funkcia scanf (), ktorá sa nachádza v hlavnom súbore jadra.
char* scanf() //scanf() is called
{
char* buff; //make a buffer
char key = "/0"; // make a single char buffer
int i = 0; //counter
int n = 100; // counter the number is the limit of chars to input
int buffch = 1;
while(i < n) //until the max char limit is reached
{
buffch = 1; // buffch used as bool to buffer or not buffer the char we
key = getch(); // get the last char typed
if(key == "n") // if enter is pressed stop
{
break;
}
if(key == "b") if key is a backspace
{
//! dont buffer this char
buffch = 0;
if (i > 0) // if the count is more than 0 then
{
//! go back one char in buffer
i--;
}
}
//! only add the char if it is to be buffered
if (buffch == 1)
{
if (key != 0) // insure that it is not a 0 ie if it is repeated it will be = 0
{
buff[i++] = key; // add the char to the buffer
}
}
}
buff[i] = "/0"; in the last set the string to be null terminated
return buff; // return the buff
}
Dúfam, že to pomôže ostatným ľuďom :)