Improved touchscreen calibration GUI & playground

This commit is contained in:
d0k3 2019-04-25 23:46:45 +02:00
parent 08b53f71e1
commit bc66cd0ccf
2 changed files with 39 additions and 35 deletions

View File

@ -149,6 +149,16 @@ void ClearScreenF(bool clear_main, bool clear_alt, int color)
if (clear_alt) ClearScreen(ALT_SCREEN, color); if (clear_alt) ClearScreen(ALT_SCREEN, color);
} }
void DrawPixel(u8* screen, int x, int y, int color)
{
int xDisplacement = (x * BYTES_PER_PIXEL * SCREEN_HEIGHT);
int yDisplacement = ((SCREEN_HEIGHT - y - 1) * BYTES_PER_PIXEL);
u8* screenPos = screen + xDisplacement + yDisplacement;
*(screenPos + 0) = color >> 16; // B
*(screenPos + 1) = color >> 8; // G
*(screenPos + 2) = color & 0xFF; // R
}
void DrawRectangle(u8* screen, int x, int y, int width, int height, int color) void DrawRectangle(u8* screen, int x, int y, int width, int height, int color)
{ {
for (int yy = 0; yy < height; yy++) { for (int yy = 0; yy < height; yy++) {
@ -1063,66 +1073,60 @@ bool ShowProgress(u64 current, u64 total, const char* opstr)
return !CheckButton(BUTTON_B); return !CheckButton(BUTTON_B);
} }
static void DrawSimpleCircle(unsigned char *screen, int x, int y, int color, int bgcolor)
{
x -= GetFontWidth() / 2;
y -= GetFontHeight() / 2;
DrawCharacter(screen, 'O', x, y, color, bgcolor);
}
bool ShowCalibrationDialog(void) bool ShowCalibrationDialog(void)
{ {
u32 current_dot;
static const u32 dot_positions[][2] = { static const u32 dot_positions[][2] = {
{16, 16}, {16, 16},
{320 - 16, 240 - 16}, {320 - 16, 240 - 16},
{16, 240 - 16}, {16, 240 - 16},
{320 - 16, 16}, {320 - 16, 16},
}; };
HID_CalibrationData calibrations[countof(dot_positions)];
ClearScreenF(true, true, COLOR_BLACK); HID_CalibrationData calibrations[countof(dot_positions)];
for (u32 i = 0; i < countof(dot_positions); i++) { for (u32 i = 0; i < countof(dot_positions); i++) {
calibrations[i].screen_x = dot_positions[i][0]; calibrations[i].screen_x = dot_positions[i][0];
calibrations[i].screen_y = dot_positions[i][1]; calibrations[i].screen_y = dot_positions[i][1];
} }
current_dot = 0; // clear screen, draw instructions
while(current_dot < countof(dot_positions)) { ClearScreen(BOT_SCREEN, COLOR_STD_BG);
for (u32 i = 0; i < current_dot; i++) DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG,
DrawSimpleCircle(BOT_SCREEN, dot_positions[i][0], dot_positions[i][1], COLOR_BRIGHTGREEN, COLOR_BLACK); "Touch the red crosshairs to\ncalibrate your touchscreen.\n \nUse the stylus for best\nresults!");
DrawSimpleCircle(BOT_SCREEN, dot_positions[current_dot][0], dot_positions[current_dot][1], COLOR_RED, COLOR_BLACK);
for (u32 i = current_dot+1; i < countof(dot_positions); i++)
DrawSimpleCircle(BOT_SCREEN, dot_positions[i][0], dot_positions[i][1], COLOR_WHITE, COLOR_BLACK);
while(HID_ReadState()); // actual calibration
for (u32 current_dot = 0; current_dot < countof(dot_positions); current_dot++) {
// draw four crosshairs
for (u32 i = 0; i < countof(dot_positions); i++) {
int color_cross = (i < current_dot) ? COLOR_BRIGHTGREEN : (i == current_dot) ? COLOR_RED : COLOR_STD_FONT;
for (u32 r = 2; r < 8; r++) {
DrawPixel(BOT_SCREEN, dot_positions[i][0] + 0, dot_positions[i][1] + r, color_cross);
DrawPixel(BOT_SCREEN, dot_positions[i][0] + r, dot_positions[i][1] + 0, color_cross);
DrawPixel(BOT_SCREEN, dot_positions[i][0] + 0, dot_positions[i][1] - r, color_cross);
DrawPixel(BOT_SCREEN, dot_positions[i][0] - r, dot_positions[i][1] + 0, color_cross);
}
}
while(1) { // wait until touchscreen released
while (HID_ReadState() & (BUTTON_B | BUTTON_TOUCH));
// wait for input, store calibration data
while (1) {
u32 pressed = HID_ReadState(); u32 pressed = HID_ReadState();
if (pressed & BUTTON_B) { if (pressed & BUTTON_B) {
if (current_dot == 0)
return false; return false;
current_dot--; } else if (pressed & BUTTON_TOUCH) {
break;
}
if (pressed & BUTTON_TOUCH) {
calibrations[current_dot].ts_raw = HID_ReadRawTouchState(); calibrations[current_dot].ts_raw = HID_ReadRawTouchState();
current_dot++;
if (current_dot == countof(dot_positions)) {
return HID_SetCalibrationData(calibrations, countof(dot_positions), 320, 240);
}
break; break;
} }
} }
} }
return true; return HID_SetCalibrationData(calibrations, countof(dot_positions), 320, 240);
} }
void ShowTouchPlayground(void) void ShowTouchPlayground(void)
{ {
ClearScreenF(true, true, COLOR_BLACK); ClearScreen(BOT_SCREEN, COLOR_STD_BG);
while(1) { while(1) {
u16 tx, ty; u16 tx, ty;
@ -1131,12 +1135,12 @@ void ShowTouchPlayground(void)
if (pressed & BUTTON_TOUCH) { if (pressed & BUTTON_TOUCH) {
HID_ReadTouchState(&tx, &ty); HID_ReadTouchState(&tx, &ty);
if (tx < 320 && ty < 240) if (tx < 320 && ty < 240)
DrawRectangle(BOT_SCREEN, tx, ty, 1, 1, COLOR_BRIGHTYELLOW); DrawPixel(BOT_SCREEN, tx, ty, COLOR_BRIGHTYELLOW);
} else { } else {
tx = ty = 0; tx = ty = 0;
} }
DrawStringF(TOP_SCREEN, 16, 16, COLOR_WHITE, COLOR_BLACK, "Current touchscreen coordinates: %d, %d", tx, ty); DrawStringF(BOT_SCREEN, 16, 16, COLOR_STD_FONT, COLOR_STD_BG, "Current touchscreen coordinates: %3.3d, %3.3d", tx, ty);
if (pressed & BUTTON_B) if (pressed & BUTTON_B)
return; return;
} }

View File

@ -2536,7 +2536,7 @@ u32 GodMode(int entrypoint) {
break; break;
} }
} else if (user_select == calib) { } else if (user_select == calib) {
ShowPrompt(true, "CALIB: %d", ShowCalibrationDialog()); ShowPrompt(false, "Touchscreen calibration %s!", (ShowCalibrationDialog()) ? "success" : "failed");
} else if (user_select == playground) { } else if (user_select == playground) {
ShowTouchPlayground(); ShowTouchPlayground();
} else if (user_select == payloads) { } else if (user_select == payloads) {