You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
8.2 KiB

  1. /*
  2. This is the modified version of [calculator by MWWorks](https://github.com/MWWorks/mw_calc_numpad/blob/master/calc.c). Below is the quote from [MWWorks](https://github.com/MWWorks).
  3. Calculator for QMK-based keyboard by MWWorks, https://mwworks.uk
  4. This is free, usual disclaimers, don't use it to calculate megaton yields, surgery plans, etc
  5. I did not plan to reinvent the wheel for this - I figured surely somebody somewhere has working calculator code?
  6. Found lots but none that actually work like you expect a calculator to, hence DIYing it
  7. As such, this is probably a bit janky, especially as I am a bit of a hack at C
  8. Seems to be working well, with occasional glitchs, solved by clearing it
  9. And some occasional floating-point issues - eg get a long decimal rather than the whole number you were expecting
  10. Feel free to fix it! I think it needs to detect the precision of the two operands and then figure out what the precision of the result should be
  11. */
  12. #include "rubi.h"
  13. static uint8_t calc_current_operand = 0;
  14. static char calc_operand_0[CALC_DIGITS+1] = "";
  15. static char calc_operand_1[CALC_DIGITS+1] = "";
  16. char calc_result[CALC_DIGITS+1] = "";
  17. static char calc_status[CALC_DIGITS+1] = "";
  18. static char calc_operator = ' ';
  19. static bool calc_reset = false;
  20. void calcBegin(void){
  21. }
  22. //update display
  23. void calcUpdate(void){
  24. if (calc_display_lines == 2) {
  25. if((calc_current_operand == 1) || (calc_reset)){
  26. strcpy(calc_status, calc_operand_0);
  27. if((strlen(calc_operand_0)>0) || (strlen(calc_operand_1)>0)){
  28. uint8_t len = strlen(calc_status);
  29. if (!(calc_operator == 's' || calc_operator == 'r' || calc_operator == 'n')) {
  30. calc_status[len] = calc_operator;
  31. }
  32. calc_status[len+1] = 0;
  33. if(calc_reset
  34. && !(calc_operator == 's' || calc_operator == 'r' || calc_operator == 'n')){
  35. strncat(calc_status, calc_operand_1, CALC_DIGITS-strlen(calc_status));
  36. calc_operator = ' ';
  37. }
  38. }
  39. strcpy(calc_status_display, calc_status);
  40. }
  41. } else if (calc_display_lines == 1) {
  42. if(calc_reset
  43. && !(calc_operator == 's' || calc_operator == 'r' || calc_operator == 'n')){
  44. calc_operator = ' ';
  45. }
  46. }
  47. calc_operator_display = calc_operator;
  48. strcpy(calc_result_display, calc_result);
  49. }
  50. //perform calculation on the 2 operands
  51. void calcOperands(void){
  52. float result = 0;
  53. switch (calc_operator){
  54. //standard operators
  55. case '+':
  56. result = strtod(calc_operand_0, NULL) + strtod(calc_operand_1, NULL);
  57. break;
  58. case '-':
  59. result = strtod(calc_operand_0, NULL) - strtod(calc_operand_1, NULL);
  60. break;
  61. case '/':
  62. result = strtod(calc_operand_0, NULL) / strtod(calc_operand_1, NULL);
  63. break;
  64. case '*':
  65. result = strtod(calc_operand_0, NULL) * strtod(calc_operand_1, NULL);
  66. break;
  67. //single operand operators - these are all in 2
  68. case 's':
  69. result = sqrt(strtod(calc_operand_0, NULL));
  70. break;
  71. case 'r':
  72. result = 1/(strtod(calc_operand_0, NULL));
  73. break;
  74. }
  75. //now convert the float result into a string
  76. //we know the total string size but we need to find the size of the integer component to know how much we have for decimals
  77. uint8_t magnitude = ceil(log10(result));
  78. uint8_t max_decimals = CALC_DIGITS-magnitude-1;
  79. //but max it at 7 because that seems the useful limit of our floats
  80. if(max_decimals>7){
  81. max_decimals = 7;
  82. }
  83. dtostrf(result, CALC_DIGITS, max_decimals, calc_result);
  84. //now to clean up the result - we need it clean as it may be the input of next calculation
  85. //this seems a lot of code to format this string :| note that this c doesn't support float in sprintf
  86. uint8_t i;
  87. //first find if theres a dot
  88. uint8_t dotpos = CALC_DIGITS+1;
  89. for(i=0; i<strlen(calc_result); i++){
  90. if(calc_result[i] == '.'){
  91. dotpos = i;
  92. break;
  93. }
  94. }
  95. //if there is, work back to it and remove trailing 0 or .
  96. if(dotpos>=0){
  97. for(i=strlen(calc_result)-1; i>=dotpos; i--){
  98. if((calc_result[i] == '0') || (calc_result[i] == '.')){
  99. calc_result[i] = 0;
  100. }else{
  101. break;
  102. }
  103. }
  104. }
  105. //now find how many leading spaces
  106. uint8_t spaces = 0;
  107. for(i=0; i<strlen(calc_result); i++){
  108. if(calc_result[i] == ' '){
  109. spaces++;
  110. }else{
  111. break;
  112. }
  113. }
  114. //and shift the string
  115. for(i=0; i<strlen(calc_result)-spaces; i++){
  116. calc_result[i] = calc_result[i+spaces];
  117. }
  118. calc_result[strlen(calc_result)-spaces] = 0;
  119. calcUpdate();
  120. //the result is available as the first operand for another calculation
  121. strcpy(calc_operand_0, calc_result);
  122. calc_operand_1[0] = 0;
  123. }
  124. void calcInput(char input){
  125. char *operand = calc_operand_0;
  126. if(calc_current_operand == 1){
  127. operand = calc_operand_1;
  128. }
  129. uint8_t len = strlen(operand);
  130. if(
  131. ((input >= 48) && (input <= 57)) ||
  132. (input == '.')
  133. ){
  134. //if this is following an equals, then we start from scratch as if new calculation
  135. if(calc_reset == true){
  136. calc_reset = false;
  137. calc_current_operand = 0;
  138. calc_operand_0[0] = 0;
  139. calc_operand_1[0] = 0;
  140. operand = calc_operand_0;
  141. len = 0;
  142. }
  143. if(len<CALC_DIGITS){
  144. operand[len] = input;
  145. operand[len+1] = 0;
  146. strcpy(calc_result, operand);
  147. calcUpdate();
  148. }
  149. //special input to backspace
  150. }else if(input == 'x'){
  151. operand[len-1] = 0;
  152. strcpy(calc_result, operand);
  153. calcUpdate();
  154. //clear
  155. }else if(input == 'c'){
  156. operand[0] = 0;
  157. calc_operand_0[0] = 0;
  158. calc_operand_1[0] = 0;
  159. calc_operator = ' ';
  160. calc_reset = true;
  161. strcpy(calc_result, operand);
  162. calcUpdate();
  163. //special input switch neg/pos
  164. }else if((input == 'n') && (len>0)){
  165. uint8_t i;
  166. if(operand[0] == '-'){
  167. for(i=1; i<=len; i++){
  168. operand[i-1] = operand[i];
  169. }
  170. }else if(len<CALC_DIGITS){
  171. for(i=0; i<=len; i++){
  172. operand[len-i+1] = operand[len-i];
  173. }
  174. operand[0] = '-';
  175. }
  176. calc_operator = input;
  177. strcpy(calc_result, operand);
  178. calcUpdate();
  179. //standard 2 operand operators
  180. }else if((input == '+') || (input == '-') || (input == '*') || (input == '/')){
  181. //get ready for second operand
  182. if(calc_current_operand == 0){
  183. calc_operator = input;
  184. calc_current_operand = 1;
  185. calcUpdate();
  186. //we pressed = we now expect a new second operand
  187. }else if(calc_reset){
  188. calc_operator = input;
  189. calc_reset = false;
  190. calc_operand_1[0] = 0;
  191. calcUpdate();
  192. }else {
  193. //if we use this on the second operand, calculate first, then ready for a second operand again
  194. if (strlen(calc_operand_1)>0){
  195. calcOperands();
  196. }
  197. calc_operand_1[0] = 0;
  198. calc_operator = input;
  199. calcUpdate();
  200. }
  201. }else if(input == '='){
  202. //only accept = if we are on the second operand
  203. if(calc_current_operand == 1){
  204. //keep the second operand for a subsequent press of =; but flag to reset if start entry of new operand
  205. calc_reset = true;
  206. calcOperands();
  207. }
  208. //single operands - square root and reciprocal - needs to operate on 0 so it works after a previous = result
  209. }else if((input == 's') || (input == 'r')){
  210. //but maybe we started entering 1
  211. if(calc_current_operand == 1 && !calc_reset){
  212. strcpy(calc_operand_0, calc_operand_1);
  213. }
  214. calc_current_operand = 1;
  215. calc_operand_1[0] = 0;
  216. calc_operator = input;
  217. calc_reset = true; //simulate another =
  218. calcOperands();
  219. }
  220. }