Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usbkeyboard: fix double printing of input #20

Merged
merged 5 commits into from
Jan 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions devices/usbkeyboard/basic_stdin/source/basic_stdin.c
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <wiikeyboard/keyboard.h>
#include <wiiuse/wpad.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


static void *xfb = NULL;
static GXRModeObj *rmode = NULL;

bool quitapp=false;
bool quitapp = false;

void keyPress_cb( char sym) {

if (sym > 31 ) putchar(sym);
if (sym == 13) putchar('\n');

if ( sym == 0x1b) quitapp = true;
void keyPress_cb(char sym)
{
// Check for escape key to exit
if (key == 0x1b)
quitapp = true;
}


int main(int argc, char **argv) {
int main(int argc, char **argv)
{
// Initialise the video system
VIDEO_Init();

Expand All @@ -37,7 +35,8 @@ int main(int argc, char **argv) {
xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));

// Initialise the console, required for printf
console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
console_init(xfb, 20, 20, rmode->fbWidth, rmode->xfbHeight,
rmode->fbWidth * VI_DISPLAY_PIX_SZ);

// Set up the video registers with the chosen mode
VIDEO_Configure(rmode);
Expand All @@ -53,33 +52,48 @@ int main(int argc, char **argv) {

// Wait for Video setup to complete
VIDEO_WaitVSync();
if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();

if (rmode->viTVMode & VI_NON_INTERLACE)
VIDEO_WaitVSync();

// The console understands VT terminal escape codes
// This positions the cursor on row 2, column 0
// we can use variables for this with format codes too
// e.g. printf ("\x1b[%d;%dH", row, column );
printf("\x1b[2;0HHello World!\n");
if (KEYBOARD_Init(keyPress_cb) == 0)
printf("keyboard initialised\n");

if (KEYBOARD_Init(keyPress_cb) == 0) printf("keyboard initialised\n");

do {
do
{
// Call WPAD_ScanPads each loop, this reads the latest controller states
WPAD_ScanPads();

// WPAD_ButtonsDown tells us which buttons were pressed in this loop
// this is a "one shot" state which will not fire again until the button has been released
// this is a "one shot" state which will not fire again until the button
// has been released
u32 pressed = WPAD_ButtonsDown(0);

int key = getchar();
putchar(key);
// Check for keyboard input
int key;

if ((key = getchar()) != EOF)
{
// Display the pressed character
// Print readable characters (ASCII > 31)
if (key > 31)
putchar(sym);
// Convert Enter key (ASCII 13) to a newline
else if(key == 13)
putchar('\n');
}

// We return to the launcher application via exit
if ( pressed & WPAD_BUTTON_HOME ) quitapp=true;
if (pressed & WPAD_BUTTON_HOME)
quitapp = true;

// Wait for the next frame
VIDEO_WaitVSync();
} while(!quitapp);
} while (!quitapp);

return 0;
}