doogle999.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #include "doogle999.h"
  2. static unsigned char inputLocation = 0; // Current index in text input
  3. static double calc(const char input[CALC_BUFFER_SIZE +1]) // Finds value of input char array, relatively small and fast I think
  4. {
  5. char inputToken[CALC_BUFFER_SIZE + 1]; // Input buffer, used when a single token (generally a number) takes up more
  6. unsigned char inputTokenLocation = 0, inputLocation = 0; // Keep track of indices
  7. struct Token tokens[CALC_BUFFER_SIZE + 1]; // Input, converted to tokens, one extra large to accomodate for possible negative sign then open parenthesis as first character
  8. unsigned char tokenCount = 0; // Keep track of index
  9. bool dashAsMinus = false; // Kind of a hacky solution to determining whether to treat a dash as a minus sign or a negative sign
  10. while(inputLocation < CALC_BUFFER_SIZE + 1)
  11. {
  12. char digit = input[inputLocation];
  13. if(inputLocation == 0 && input[inputLocation] == CALC_CHAR_SUB && input[inputLocation + 1] == CALC_CHAR_BEG)
  14. {
  15. tokens[tokenCount].raw.num = 0;
  16. tokens[tokenCount].isNum = true;
  17. tokenCount++;
  18. dashAsMinus = true;
  19. }
  20. if ((digit >= '0' && digit <= '9') || /* valid digit */
  21. (inputTokenLocation != 0 && input[inputLocation] == CALC_CHAR_DEC) || /* valid floating point */
  22. (!dashAsMinus && inputTokenLocation == 0 && input[inputLocation] == CALC_CHAR_SUB)) /* - is negative sign */
  23. {
  24. inputToken[inputTokenLocation] = input[inputLocation];
  25. inputTokenLocation++;
  26. inputLocation++;
  27. continue;
  28. }
  29. if(inputTokenLocation != 0)
  30. {
  31. // sscanf(inputToken, "%lf", &tokens[tokenCount].raw.num); // I would like to use sscanf here, but the small version of stdio.h on the chip doesn't allow sscanf or its sister functions to be used to process floats
  32. tokens[tokenCount].raw.num = atof(inputToken);
  33. tokens[tokenCount].isNum = true;
  34. for(unsigned char i = 0; i < inputTokenLocation + 1; i++)
  35. {
  36. inputToken[i] = '\0';
  37. }
  38. inputTokenLocation = 0;
  39. tokenCount++;
  40. dashAsMinus = true;
  41. continue;
  42. }
  43. /* inputTokenLocation == 0 */
  44. tokens[tokenCount].isNum = false;
  45. tokens[tokenCount].raw.op.c = input[inputLocation];
  46. tokens[tokenCount].raw.op.priority = 0;
  47. tokens[tokenCount].raw.op.ltr = true;
  48. dashAsMinus = false;
  49. switch(input[inputLocation])
  50. {
  51. case CALC_CHAR_BEG:
  52. break;
  53. case CALC_CHAR_END:
  54. dashAsMinus = true;
  55. break;
  56. case CALC_CHAR_ADD:
  57. tokens[tokenCount].raw.op.priority = CALC_PRIO_ADD;
  58. break;
  59. case CALC_CHAR_SUB:
  60. tokens[tokenCount].raw.op.priority = CALC_PRIO_SUB;
  61. break;
  62. case CALC_CHAR_MUL:
  63. tokens[tokenCount].raw.op.priority = CALC_PRIO_MUL;
  64. break;
  65. case CALC_CHAR_DIV:
  66. tokens[tokenCount].raw.op.priority = CALC_PRIO_DIV;
  67. break;
  68. case CALC_CHAR_EXP:
  69. tokens[tokenCount].raw.op.priority = CALC_PRIO_EXP;
  70. tokens[tokenCount].raw.op.ltr = false;
  71. break;
  72. case CALC_CHAR_SIN:
  73. case CALC_CHAR_COS:
  74. case CALC_CHAR_TAN:
  75. case CALC_CHAR_ASN:
  76. case CALC_CHAR_ACS:
  77. case CALC_CHAR_ATN:
  78. case CALC_CHAR_LGE:
  79. case CALC_CHAR_LOG:
  80. case CALC_CHAR_SQT:
  81. break;
  82. case CALC_CHAR_EUL:
  83. tokens[tokenCount].isNum = true;
  84. tokens[tokenCount].raw.num = CALC_VALU_EUL;
  85. dashAsMinus = true;
  86. break;
  87. case CALC_CHAR_PI:
  88. tokens[tokenCount].isNum = true;
  89. tokens[tokenCount].raw.num = CALC_VALU_PI;
  90. dashAsMinus = true;
  91. break;
  92. case '\0':
  93. tokenCount--;
  94. inputLocation = CALC_BUFFER_SIZE;
  95. break;
  96. default:
  97. tokenCount--;
  98. break;
  99. }
  100. tokenCount++;
  101. inputLocation++;
  102. }
  103. struct Token output[CALC_BUFFER_SIZE + 1]; // Final output tokens before evaluation
  104. struct Token opstack[CALC_BUFFER_SIZE + 1]; // Stack of operators
  105. unsigned char outputLocation = 0, opstackLocation = 0; // Keep track of indices
  106. unsigned char numBrackets = 0; // The number of parenthesis
  107. for(unsigned char i = 0; i < tokenCount; i++)
  108. {
  109. if(tokens[i].isNum)
  110. {
  111. output[outputLocation] = tokens[i];
  112. outputLocation++;
  113. }
  114. else if(tokens[i].raw.op.c == CALC_CHAR_BEG)
  115. {
  116. opstack[opstackLocation] = tokens[i];
  117. opstackLocation++;
  118. }
  119. else if(tokens[i].raw.op.c == CALC_CHAR_END)
  120. {
  121. while(opstack[opstackLocation - 1].raw.op.c != CALC_CHAR_BEG)
  122. {
  123. output[outputLocation] = opstack[opstackLocation - 1];
  124. outputLocation++;
  125. opstackLocation--;
  126. }
  127. opstackLocation--;
  128. numBrackets += 2;
  129. }
  130. else if(tokens[i].raw.op.priority == 0)
  131. {
  132. opstack[opstackLocation] = tokens[i];
  133. opstackLocation++;
  134. }
  135. else
  136. {
  137. while(opstackLocation != 0
  138. && (opstack[opstackLocation - 1].raw.op.priority == 0
  139. || tokens[i].raw.op.priority < opstack[opstackLocation - 1].raw.op.priority
  140. || (tokens[i].raw.op.priority == opstack[opstackLocation - 1].raw.op.priority && opstack[opstackLocation - 1].raw.op.ltr))
  141. && opstack[opstackLocation - 1].raw.op.c != CALC_CHAR_BEG)
  142. {
  143. output[outputLocation] = opstack[opstackLocation - 1];
  144. outputLocation++;
  145. opstackLocation--;
  146. }
  147. opstack[opstackLocation] = tokens[i];
  148. opstackLocation++;
  149. }
  150. }
  151. tokenCount -= numBrackets;
  152. for(signed char i = opstackLocation - 1; i >= 0; i--)
  153. {
  154. output[outputLocation] = opstack[i];
  155. outputLocation++;
  156. opstackLocation--;
  157. }
  158. double answer[CALC_BUFFER_SIZE];
  159. unsigned char answerLocation = 0;
  160. for(unsigned char i = 0; i < tokenCount; i++)
  161. {
  162. if(output[i].isNum)
  163. {
  164. answer[answerLocation] = output[i].raw.num;
  165. answerLocation++;
  166. continue;
  167. }
  168. if(output[i].raw.op.priority == 0)
  169. {
  170. if (answerLocation < 1) { /* not handled here -- ERROR? */ } else
  171. if(answerLocation >= 1)
  172. {
  173. double (*op)(double);
  174. switch(output[i].raw.op.c)
  175. {
  176. case CALC_CHAR_SIN:
  177. op = sin;
  178. break;
  179. case CALC_CHAR_COS:
  180. op = cos;
  181. break;
  182. case CALC_CHAR_TAN:
  183. op = tan;
  184. break;
  185. case CALC_CHAR_ASN:
  186. op = asin;
  187. break;
  188. case CALC_CHAR_ACS:
  189. op = acos;
  190. break;
  191. case CALC_CHAR_ATN:
  192. op = atan;
  193. break;
  194. case CALC_CHAR_LGE:
  195. op = log;
  196. break;
  197. case CALC_CHAR_LOG:
  198. op = log10;
  199. break;
  200. case CALC_CHAR_SQT:
  201. op = sqrt;
  202. break;
  203. default:
  204. continue; /* invalid input */
  205. }
  206. answer[answerLocation - 1] = op(answer[answerLocation - 1]);
  207. }
  208. }
  209. /* priority != 0 */
  210. else if(answerLocation >= 2)
  211. {
  212. switch(output[i].raw.op.c)
  213. {
  214. case CALC_CHAR_ADD:
  215. answer[answerLocation - 2] += answer[answerLocation - 1];
  216. break;
  217. case CALC_CHAR_SUB:
  218. answer[answerLocation - 2] -= answer[answerLocation - 1];
  219. break;
  220. case CALC_CHAR_MUL:
  221. answer[answerLocation - 2] *= answer[answerLocation - 1];
  222. break;
  223. case CALC_CHAR_DIV:
  224. answer[answerLocation - 2] /= answer[answerLocation - 1];
  225. break;
  226. case CALC_CHAR_EXP:
  227. answer[answerLocation - 2] = pow(answer[answerLocation - 2], answer[answerLocation - 1]);
  228. break;
  229. }
  230. answerLocation--;
  231. }
  232. }
  233. return answer[0];
  234. }
  235. /*
  236. * @returns 0 when nothing should happen and QMK should work as usual
  237. * @returns -1 when invalid input was given, QMK should ignore it
  238. * @returns -2 when BSP should be done
  239. * @returns -3 when CALC should be done
  240. * @returns -4 when ENDCALC should be done
  241. * @returns positive value of CALC_* when normal input was processed
  242. */
  243. static int process_input(const uint16_t keycode, const uint8_t mods, const keyevent_t event)
  244. {
  245. /* handle even when no key was pressed */
  246. if(!event.pressed)
  247. {
  248. switch(keycode)
  249. {
  250. /* QMK should handle those */
  251. case KC_RSFT:
  252. case KC_LSFT:
  253. return 0;
  254. break;
  255. }
  256. /* ??? ignore */
  257. return -1;
  258. }
  259. /* when shift key is pressed handle characters differently */
  260. char characterPressed;
  261. if((get_mods() & MODS_SHIFT_MASK))
  262. {
  263. switch(keycode)
  264. {
  265. case KC_9:
  266. characterPressed = CALC_CHAR_BEG;
  267. break;
  268. case KC_0:
  269. characterPressed = CALC_CHAR_END;
  270. break;
  271. case KC_EQUAL:
  272. characterPressed = CALC_CHAR_ADD;
  273. break;
  274. case KC_KP_PLUS:
  275. characterPressed = CALC_CHAR_ADD;
  276. break;
  277. case KC_6:
  278. characterPressed = CALC_CHAR_EXP;
  279. break;
  280. case KC_8:
  281. characterPressed = CALC_CHAR_MUL;
  282. break;
  283. case KC_KP_ASTERISK:
  284. characterPressed = CALC_CHAR_MUL;
  285. break;
  286. case KC_S:
  287. characterPressed = CALC_CHAR_ASN;
  288. break;
  289. case KC_C:
  290. characterPressed = CALC_CHAR_ACS;
  291. break;
  292. case KC_T:
  293. characterPressed = CALC_CHAR_ATN;
  294. break;
  295. case KC_L:
  296. characterPressed = CALC_CHAR_LOG;
  297. break;
  298. default:
  299. return -1;
  300. break;
  301. }
  302. return characterPressed;
  303. }
  304. /* normal key handling: shift not pressed */
  305. /* digits */
  306. if (keycode == KC_KP_0 || keycode == KC_0) {
  307. return '0';
  308. } else if (keycode >= KC_KP_1 && keycode <= KC_KP_9) {
  309. return keycode - KC_KP_1 +1 + '0';
  310. } else if (keycode >= KC_1 && keycode <= KC_9) {
  311. return keycode - KC_1 +1 + '0';
  312. }
  313. /* other tokens */
  314. switch (keycode) {
  315. case KC_MINUS:
  316. case KC_KP_MINUS:
  317. return characterPressed = CALC_CHAR_SUB;
  318. case KC_SLASH:
  319. case KC_KP_SLASH:
  320. return characterPressed = CALC_CHAR_DIV;
  321. case KC_S:
  322. return characterPressed = CALC_CHAR_SIN;
  323. case KC_C:
  324. return characterPressed = CALC_CHAR_COS;
  325. case KC_T:
  326. return characterPressed = CALC_CHAR_TAN;
  327. case KC_Q:
  328. return characterPressed = CALC_CHAR_SQT;
  329. case KC_L:
  330. return characterPressed = CALC_CHAR_LGE;
  331. case KC_DOT:
  332. case KC_KP_DOT:
  333. return characterPressed = CALC_CHAR_DEC;
  334. case KC_P:
  335. return characterPressed = CALC_CHAR_PI;
  336. case KC_E:
  337. return characterPressed = CALC_CHAR_EUL;
  338. case KC_BSPC:
  339. return -2;
  340. case KC_RSFT:
  341. return 0;
  342. case KC_LSFT:
  343. return 0;
  344. case CALC:
  345. return -3;
  346. case ENDCALC:
  347. return -4;
  348. default:
  349. return -1;
  350. }
  351. }
  352. bool process_record_user(uint16_t keycode, keyrecord_t* record)
  353. {
  354. static char text[CALC_BUFFER_SIZE + 1]; // Used to store input and then output when ready to print
  355. static char backspaceText[CALC_BUFFER_SIZE + 1]; // Pretty dumb waste of memory because only backspace characters, used with send_string to backspace and remove input
  356. if((biton32(layer_state) == CALC_LAYER && CALC_FORCE_NUM_LOCK_INSIDE_CALC) || (biton32(layer_state) != CALC_LAYER && CALC_FORCE_NUM_LOCK_OUTSIDE_CALC))
  357. {
  358. bool numpadKeyPressed = record->event.pressed &&
  359. !(get_mods() & MODS_SHIFT_MASK) &&
  360. /* KC_KP_1, KC_KP_2, ..., KC_KP_0, KC_KP_DOT */
  361. (keycode >= KC_KP_1 && keycode <= KC_KP_DOT);
  362. if(numpadKeyPressed && !(host_keyboard_leds() & (1 << USB_LED_NUM_LOCK)))
  363. {
  364. add_key(KC_NLCK);
  365. send_keyboard_report();
  366. del_key(KC_NLCK);
  367. }
  368. }
  369. if(biton32(layer_state) != CALC_LAYER) { return true; }
  370. int action = process_input(keycode, get_mods(), record->event);
  371. switch(action)
  372. {
  373. case 0:
  374. return true;
  375. case -1:
  376. return false;
  377. case -2:
  378. if(inputLocation > 0)
  379. {
  380. inputLocation--;
  381. text[inputLocation] = '\0';
  382. backspaceText[0] = (char)8;
  383. backspaceText[1] = '\0';
  384. send_string(backspaceText);
  385. }
  386. return false;
  387. case -3:
  388. for(int i = 0; i < inputLocation; i++)
  389. {
  390. backspaceText[i] = (char)8;
  391. }
  392. send_string(backspaceText);
  393. dtostrf(calc(text), CALC_PRINT_SIZE, CALC_PRINT_SIZE, text);
  394. send_string(text);
  395. for(unsigned char i = 0; i < CALC_BUFFER_SIZE; i++)
  396. {
  397. text[i] = '\0';
  398. backspaceText[i] = '\0';
  399. }
  400. inputLocation = 0;
  401. return false;
  402. case -4:
  403. for(unsigned char i = 0; i < CALC_BUFFER_SIZE; i++)
  404. {
  405. text[i] = '\0';
  406. backspaceText[i] = '\0';
  407. }
  408. inputLocation = 0;
  409. layer_off(CALC_LAYER);
  410. return false;
  411. default:
  412. break;
  413. }
  414. char characterPressed = (char)action;
  415. if(inputLocation < CALC_BUFFER_SIZE)
  416. {
  417. text[inputLocation] = characterPressed;
  418. inputLocation++;
  419. char characterToSend[2];
  420. characterToSend[0] = characterPressed;
  421. characterToSend[1] = '\0';
  422. send_string(characterToSend);
  423. }
  424. return false;
  425. }