fpcalc.c 3.9 KB

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