icalc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Simple calculator to integers
  2. #define word char
  3. #include "lib/math.c"
  4. #include "lib/stdlib.c"
  5. #include "lib/sys.c"
  6. #define CALC_BUFFER_MAX_LEN 32
  7. #define CALC_STATE_INPUTSTART 0
  8. #define CALC_STATE_INPUTA 1
  9. #define CALC_STATE_INPUTB 2
  10. #define CALC_STATE_INPUTOP 3
  11. // The current number that is being typed
  12. char CALC_buffer[CALC_BUFFER_MAX_LEN];
  13. word CALC_buffer_idx = 0;
  14. word CALC_a = 0;
  15. word CALC_b = 0;
  16. char CALC_state = CALC_STATE_INPUTSTART;
  17. word calcLoop()
  18. {
  19. if (CALC_state == CALC_STATE_INPUTSTART)
  20. {
  21. bdos_print("Input A: ");
  22. CALC_state = CALC_STATE_INPUTA;
  23. }
  24. if (hid_checkfifo())
  25. {
  26. word c = hid_fiforead();
  27. if (CALC_state == CALC_STATE_INPUTOP)
  28. {
  29. if (c == '+')
  30. {
  31. bdos_println("+");
  32. bdos_print("Result = ");
  33. bdos_printdec(CALC_a + CALC_b);
  34. CALC_state = CALC_STATE_INPUTA;
  35. bdos_print("\n\nInput A: ");
  36. }
  37. else if (c == '-')
  38. {
  39. bdos_println("-");
  40. bdos_print("Result = ");
  41. bdos_printdec(CALC_a - CALC_b);
  42. CALC_state = CALC_STATE_INPUTA;
  43. bdos_print("\n\nInput A: ");
  44. }
  45. else if (c == '*')
  46. {
  47. bdos_println("*");
  48. bdos_print("Result = ");
  49. bdos_printdec(CALC_a * CALC_b);
  50. CALC_state = CALC_STATE_INPUTA;
  51. bdos_print("\n\nInput A: ");
  52. }
  53. else if (c == '/')
  54. {
  55. bdos_println("/");
  56. bdos_print("Result = ");
  57. bdos_printdec( MATH_div(CALC_a, CALC_b) );
  58. CALC_state = CALC_STATE_INPUTA;
  59. bdos_print("\n\nInput A: ");
  60. }
  61. else if (c == '%')
  62. {
  63. bdos_println("%");
  64. bdos_print("Result = ");
  65. bdos_printdec( MATH_mod(CALC_a, CALC_b) );
  66. CALC_state = CALC_STATE_INPUTA;
  67. bdos_print("\n\nInput A: ");
  68. }
  69. else if (c == 'u')
  70. {
  71. bdos_println("u/");
  72. bdos_print("Result = ");
  73. bdos_printdec( MATH_divU(CALC_a, CALC_b) );
  74. CALC_state = CALC_STATE_INPUTA;
  75. bdos_print("\n\nInput A: ");
  76. }
  77. else if (c == 'w')
  78. {
  79. bdos_println("u%");
  80. bdos_print("Result = ");
  81. bdos_printdec( MATH_modU(CALC_a, CALC_b) );
  82. CALC_state = CALC_STATE_INPUTA;
  83. bdos_print("\n\nInput A: ");
  84. }
  85. else if (c == 0x1b) // escape
  86. {
  87. bdos_printc('\n');
  88. return 1;
  89. }
  90. }
  91. // number input, dot or sign
  92. else if ( (c >= '0' && c <= '9') || c == '-')
  93. {
  94. if (CALC_state == CALC_STATE_INPUTA || CALC_state == CALC_STATE_INPUTB)
  95. {
  96. // add to buffer and print character
  97. CALC_buffer[CALC_buffer_idx] = c;
  98. CALC_buffer_idx++;
  99. CALC_buffer[CALC_buffer_idx] = 0; // terminate
  100. bdos_printc(c);
  101. }
  102. }
  103. else if (c == 0x8) // backspace
  104. {
  105. if (CALC_state == CALC_STATE_INPUTA || CALC_state == CALC_STATE_INPUTB)
  106. {
  107. // replace last char in buffer by 0 (if not at start)
  108. if (CALC_buffer_idx != 0)
  109. {
  110. CALC_buffer_idx--;
  111. CALC_buffer[CALC_buffer_idx] = 0;
  112. bdos_printc(c);
  113. }
  114. }
  115. }
  116. else if (c == 0xa) // newline/enter
  117. {
  118. switch(CALC_state)
  119. {
  120. case CALC_STATE_INPUTA:
  121. if (CALC_buffer_idx > 0)
  122. {
  123. bdos_printc('\n');
  124. CALC_a = strToInt(CALC_buffer);
  125. CALC_buffer_idx = 0;
  126. bdos_print("\nInput B: ");
  127. CALC_state = CALC_STATE_INPUTB;
  128. }
  129. break;
  130. case CALC_STATE_INPUTB:
  131. if (CALC_buffer_idx > 0)
  132. {
  133. bdos_printc('\n');
  134. CALC_b = strToInt(CALC_buffer);
  135. CALC_buffer_idx = 0;
  136. bdos_print("Operation: ");
  137. CALC_state = CALC_STATE_INPUTOP;
  138. }
  139. break;
  140. }
  141. }
  142. else if (c == 0x1b) // escape
  143. {
  144. bdos_printc('\n');
  145. return 1;
  146. }
  147. }
  148. return 0;
  149. }
  150. int main()
  151. {
  152. bdos_println("Integer calculator test\n");
  153. word stop = 0;
  154. while (!stop)
  155. {
  156. stop = calcLoop();
  157. }
  158. return 'q';
  159. }
  160. void interrupt()
  161. {
  162. // Handle all interrupts
  163. word i = get_int_id();
  164. switch(i)
  165. {
  166. case INTID_TIMER1:
  167. timer1Value = 1; // Notify ending of timer1
  168. break;
  169. }
  170. }