Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. #include "keyboard.h"
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. static u16 sizeMax;
  8. static u16 sizeOff;
  9. static char* string;
  10. static bool isShift;
  11.  
  12. static const char* rows[] = {
  13. "1234567890-",
  14. "qwertyuiop",
  15. "asdfghjkl",
  16. "zxcvbnm",
  17. };
  18.  
  19. static const char* rowsShift[] = {
  20. "1234567890_",
  21. "QWERTYUIOP",
  22. "ASDFGHJKL",
  23. "ZXCVBNM",
  24. };
  25.  
  26. static inline bool KB_isInitialized()
  27. {
  28. return sizeMax != 0 && string != NULL;
  29. }
  30.  
  31. static inline bool within(touchPosition p, s16 x, s16 y, s16 w, s16 h)
  32. {
  33. return (p.px >= x && p.px < x + w) && (p.py >= y && p.py < y + h);
  34. }
  35.  
  36. static char* KB_appendChar(char c)
  37. {
  38. if (sizeOff >= sizeMax-1) return NULL;
  39.  
  40. string[sizeOff++] = c;
  41. string[sizeOff] = '\0';
  42.  
  43. printf("%s (%c)\n", string, c);
  44.  
  45. return string;
  46. }
  47.  
  48. static char* KB_popChar()
  49. {
  50. if (sizeOff == 0) return NULL;
  51.  
  52. string[--sizeOff] = '\0';
  53.  
  54. printf("%s\n", string);
  55.  
  56. return string;
  57. }
  58.  
  59. void KB_update()
  60. {
  61. static touchPosition kTouch;
  62.  
  63. if ((hidKeysHeld() & KEY_TOUCH) || (hidKeysDown() & KEY_TOUCH))
  64. {
  65. hidTouchRead(&kTouch);
  66. }
  67.  
  68. if (hidKeysUp() & KEY_TOUCH)
  69. {
  70. s16 x, y;
  71.  
  72. // Line 0
  73. x = 64-16;
  74. y = 16;
  75.  
  76. if (within(kTouch, (x+=16), y, 8, 8))
  77. {
  78. KB_appendChar('1');
  79. }
  80. else if (within(kTouch, (x+=16), y, 8, 8))
  81. {
  82. KB_appendChar('2');
  83. }
  84. else if (within(kTouch, (x+=16), y, 8, 8))
  85. {
  86. KB_appendChar('3');
  87. }
  88. else if (within(kTouch, (x+=16), y, 8, 8))
  89. {
  90. KB_appendChar('4');
  91. }
  92. else if (within(kTouch, (x+=16), y, 8, 8))
  93. {
  94. KB_appendChar('5');
  95. }
  96. else if (within(kTouch, (x+=16), y, 8, 8))
  97. {
  98. KB_appendChar('6');
  99. }
  100. else if (within(kTouch, (x+=16), y, 8, 8))
  101. {
  102. KB_appendChar('7');
  103. }
  104. else if (within(kTouch, (x+=16), y, 8, 8))
  105. {
  106. KB_appendChar('8');
  107. }
  108. else if (within(kTouch, (x+=16), y, 8, 8))
  109. {
  110. KB_appendChar('9');
  111. }
  112. else if (within(kTouch, (x+=16), y, 8, 8))
  113. {
  114. KB_appendChar('0');
  115. }
  116. else if (within(kTouch, (x+=16), y, 8, 8))
  117. {
  118. if (isShift)
  119. KB_appendChar('_');
  120. else
  121. KB_appendChar('-');
  122. }
  123.  
  124. // Line 1
  125. x = 72-16;
  126. y = 32;
  127.  
  128. if (within(kTouch, (x+=16), y, 8, 8))
  129. {
  130. if (isShift)
  131. KB_appendChar('Q');
  132. else
  133. KB_appendChar('q');
  134. }
  135. else if (within(kTouch, (x+=16), y, 8, 8))
  136. {
  137. if (isShift)
  138. KB_appendChar('W');
  139. else
  140. KB_appendChar('w');
  141. }
  142. else if (within(kTouch, (x+=16), y, 8, 8))
  143. {
  144. if (isShift)
  145. KB_appendChar('E');
  146. else
  147. KB_appendChar('e');
  148. }
  149. else if (within(kTouch, (x+=16), y, 8, 8))
  150. {
  151. if (isShift)
  152. KB_appendChar('R');
  153. else
  154. KB_appendChar('r');
  155. }
  156. else if (within(kTouch, (x+=16), y, 8, 8))
  157. {
  158. if (isShift)
  159. KB_appendChar('T');
  160. else
  161. KB_appendChar('t');
  162. }
  163. else if (within(kTouch, (x+=16), y, 8, 8))
  164. {
  165. if (isShift)
  166. KB_appendChar('Y');
  167. else
  168. KB_appendChar('y');
  169. }
  170. else if (within(kTouch, (x+=16), y, 8, 8))
  171. {
  172. if (isShift)
  173. KB_appendChar('U');
  174. else
  175. KB_appendChar('u');
  176. }
  177. else if (within(kTouch, (x+=16), y, 8, 8))
  178. {
  179. if (isShift)
  180. KB_appendChar('I');
  181. else
  182. KB_appendChar('i');
  183. }
  184. else if (within(kTouch, (x+=16), y, 8, 8))
  185. {
  186. if (isShift)
  187. KB_appendChar('O');
  188. else
  189. KB_appendChar('o');
  190. }
  191. else if (within(kTouch, (x+=16), y, 8, 8))
  192. {
  193. if (isShift)
  194. KB_appendChar('P');
  195. else
  196. KB_appendChar('p');
  197. }
  198.  
  199. // Line 2
  200. x = 80-16;
  201. y = 48;
  202.  
  203. if (within(kTouch, (x+=16), y, 8, 8))
  204. {
  205. if (isShift)
  206. KB_appendChar('A');
  207. else
  208. KB_appendChar('a');
  209. }
  210. else if (within(kTouch, (x+=16), y, 8, 8))
  211. {
  212. if (isShift)
  213. KB_appendChar('S');
  214. else
  215. KB_appendChar('s');
  216. }
  217. else if (within(kTouch, (x+=16), y, 8, 8))
  218. {
  219. if (isShift)
  220. KB_appendChar('D');
  221. else
  222. KB_appendChar('d');
  223. }
  224. else if (within(kTouch, (x+=16), y, 8, 8))
  225. {
  226. if (isShift)
  227. KB_appendChar('F');
  228. else
  229. KB_appendChar('f');
  230. }
  231. else if (within(kTouch, (x+=16), y, 8, 8))
  232. {
  233. if (isShift)
  234. KB_appendChar('G');
  235. else
  236. KB_appendChar('g');
  237. }
  238. else if (within(kTouch, (x+=16), y, 8, 8))
  239. {
  240. if (isShift)
  241. KB_appendChar('H');
  242. else
  243. KB_appendChar('h');
  244. }
  245. else if (within(kTouch, (x+=16), y, 8, 8))
  246. {
  247. if (isShift)
  248. KB_appendChar('J');
  249. else
  250. KB_appendChar('j');
  251. }
  252. else if (within(kTouch, (x+=16), y, 8, 8))
  253. {
  254. if (isShift)
  255. KB_appendChar('K');
  256. else
  257. KB_appendChar('k');
  258. }
  259. else if (within(kTouch, (x+=16), y, 8, 8))
  260. {
  261. if (isShift)
  262. KB_appendChar('L');
  263. else
  264. KB_appendChar('l');
  265. }
  266.  
  267.  
  268. // Line 3
  269. x = 88-16;
  270. y = 64;
  271.  
  272. // Shift
  273. if (within(kTouch, 64, y, 16, 8))
  274. {
  275. isShift = !isShift;
  276. }
  277.  
  278. // Del
  279. if (within(kTouch, 208, y, 24, 8))
  280. {
  281. KB_popChar();
  282. }
  283.  
  284. if (within(kTouch, (x+=16), y, 8, 8))
  285. {
  286. if (isShift)
  287. KB_appendChar('Z');
  288. else
  289. KB_appendChar('z');
  290. }
  291. else if (within(kTouch, (x+=16), y, 8, 8))
  292. {
  293. if (isShift)
  294. KB_appendChar('X');
  295. else
  296. KB_appendChar('x');
  297. }
  298. else if (within(kTouch, (x+=16), y, 8, 8))
  299. {
  300. if (isShift)
  301. KB_appendChar('C');
  302. else
  303. KB_appendChar('c');
  304. }
  305. else if (within(kTouch, (x+=16), y, 8, 8))
  306. {
  307. if (isShift)
  308. KB_appendChar('V');
  309. else
  310. KB_appendChar('v');
  311. }
  312. else if (within(kTouch, (x+=16), y, 8, 8))
  313. {
  314. if (isShift)
  315. KB_appendChar('B');
  316. else
  317. KB_appendChar('b');
  318. }
  319. else if (within(kTouch, (x+=16), y, 8, 8))
  320. {
  321. if (isShift)
  322. KB_appendChar('N');
  323. else
  324. KB_appendChar('n');
  325. }
  326. else if (within(kTouch, (x+=16), y, 8, 8))
  327. {
  328. if (isShift)
  329. KB_appendChar('M');
  330. else
  331. KB_appendChar('m');
  332. }
  333. }
  334. }
  335.  
  336. void KB_draw()
  337. {
  338. printf("\x1B[30m\x1B[47m");
  339.  
  340. const char** row = (isShift ? rowsShift : rows);
  341. for (u8 iR = 0, i, off; iR < 4; iR++)
  342. {
  343. i = 0;
  344. off = 8 + iR;
  345. while (row[iR][i])
  346. {
  347. printf("\x1B[%i;%iH%c", 2 * (iR + 1), off, row[iR][i]);
  348. off += 2;
  349. i++;
  350. }
  351. }
  352.  
  353. printf("\x1B[8;8H^ ");
  354.  
  355. printf("\x1B[8;26H<- %i", isShift);
  356.  
  357. printf("\x1B[0m");
  358. }
  359.  
  360. char* KB_getString()
  361. {
  362. return string;
  363. }
  364.  
  365. void KB_init(u16 _sizeMax, char* _string)
  366. {
  367. printf("KB_init");
  368. sizeMax = _sizeMax;
  369. sizeOff = 0;
  370.  
  371. string = malloc(sizeof(char) * sizeMax);
  372. memset(string, '\0', sizeMax);
  373.  
  374. if (_string)
  375. strncpy(string, _string, sizeMax);
  376. }
  377.  
  378. void KB_exit()
  379. {
  380. free(string);
  381. string = NULL;
  382. sizeMax = 0;
  383. sizeOff = 0;
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement