Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "keyboard.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- static u16 sizeMax;
- static u16 sizeOff;
- static char* string;
- static bool isShift;
- static const char* rows[] = {
- "1234567890-",
- "qwertyuiop",
- "asdfghjkl",
- "zxcvbnm",
- };
- static const char* rowsShift[] = {
- "1234567890_",
- "QWERTYUIOP",
- "ASDFGHJKL",
- "ZXCVBNM",
- };
- static inline bool KB_isInitialized()
- {
- return sizeMax != 0 && string != NULL;
- }
- static inline bool within(touchPosition p, s16 x, s16 y, s16 w, s16 h)
- {
- return (p.px >= x && p.px < x + w) && (p.py >= y && p.py < y + h);
- }
- static char* KB_appendChar(char c)
- {
- if (sizeOff >= sizeMax-1) return NULL;
- string[sizeOff++] = c;
- string[sizeOff] = '\0';
- printf("%s (%c)\n", string, c);
- return string;
- }
- static char* KB_popChar()
- {
- if (sizeOff == 0) return NULL;
- string[--sizeOff] = '\0';
- printf("%s\n", string);
- return string;
- }
- void KB_update()
- {
- static touchPosition kTouch;
- if ((hidKeysHeld() & KEY_TOUCH) || (hidKeysDown() & KEY_TOUCH))
- {
- hidTouchRead(&kTouch);
- }
- if (hidKeysUp() & KEY_TOUCH)
- {
- s16 x, y;
- // Line 0
- x = 64-16;
- y = 16;
- if (within(kTouch, (x+=16), y, 8, 8))
- {
- KB_appendChar('1');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- KB_appendChar('2');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- KB_appendChar('3');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- KB_appendChar('4');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- KB_appendChar('5');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- KB_appendChar('6');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- KB_appendChar('7');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- KB_appendChar('8');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- KB_appendChar('9');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- KB_appendChar('0');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('_');
- else
- KB_appendChar('-');
- }
- // Line 1
- x = 72-16;
- y = 32;
- if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('Q');
- else
- KB_appendChar('q');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('W');
- else
- KB_appendChar('w');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('E');
- else
- KB_appendChar('e');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('R');
- else
- KB_appendChar('r');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('T');
- else
- KB_appendChar('t');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('Y');
- else
- KB_appendChar('y');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('U');
- else
- KB_appendChar('u');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('I');
- else
- KB_appendChar('i');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('O');
- else
- KB_appendChar('o');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('P');
- else
- KB_appendChar('p');
- }
- // Line 2
- x = 80-16;
- y = 48;
- if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('A');
- else
- KB_appendChar('a');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('S');
- else
- KB_appendChar('s');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('D');
- else
- KB_appendChar('d');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('F');
- else
- KB_appendChar('f');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('G');
- else
- KB_appendChar('g');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('H');
- else
- KB_appendChar('h');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('J');
- else
- KB_appendChar('j');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('K');
- else
- KB_appendChar('k');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('L');
- else
- KB_appendChar('l');
- }
- // Line 3
- x = 88-16;
- y = 64;
- // Shift
- if (within(kTouch, 64, y, 16, 8))
- {
- isShift = !isShift;
- }
- // Del
- if (within(kTouch, 208, y, 24, 8))
- {
- KB_popChar();
- }
- if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('Z');
- else
- KB_appendChar('z');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('X');
- else
- KB_appendChar('x');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('C');
- else
- KB_appendChar('c');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('V');
- else
- KB_appendChar('v');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('B');
- else
- KB_appendChar('b');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('N');
- else
- KB_appendChar('n');
- }
- else if (within(kTouch, (x+=16), y, 8, 8))
- {
- if (isShift)
- KB_appendChar('M');
- else
- KB_appendChar('m');
- }
- }
- }
- void KB_draw()
- {
- printf("\x1B[30m\x1B[47m");
- const char** row = (isShift ? rowsShift : rows);
- for (u8 iR = 0, i, off; iR < 4; iR++)
- {
- i = 0;
- off = 8 + iR;
- while (row[iR][i])
- {
- printf("\x1B[%i;%iH%c", 2 * (iR + 1), off, row[iR][i]);
- off += 2;
- i++;
- }
- }
- printf("\x1B[8;8H^ ");
- printf("\x1B[8;26H<- %i", isShift);
- printf("\x1B[0m");
- }
- char* KB_getString()
- {
- return string;
- }
- void KB_init(u16 _sizeMax, char* _string)
- {
- printf("KB_init");
- sizeMax = _sizeMax;
- sizeOff = 0;
- string = malloc(sizeof(char) * sizeMax);
- memset(string, '\0', sizeMax);
- if (_string)
- strncpy(string, _string, sizeMax);
- }
- void KB_exit()
- {
- free(string);
- string = NULL;
- sizeMax = 0;
- sizeOff = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement