utilities.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "utilities.h"
  2. #include "calculator.h"
  3. char getKeyAtPosition(short x, short y) {
  4. const char keys[6][5] = {
  5. {'M', ' ', ' ', ' ', ' '}, // Row 1 (MODE key)
  6. {'7', '8', '9', 'A', 'B'}, // Row 2
  7. {'4', '5', '6', 'C', 'D'}, // Row 3
  8. {'1', '2', '3', 'E', 'F'}, // Row 4
  9. {'<', '0', '=', 'R', ' '} // Row 5
  10. };
  11. return (y < 6 && x < 5) ? keys[y][x] : ' ';
  12. }
  13. short calculateStringWidth(const char* str, short length) {
  14. short width = 0;
  15. for(short i = 0; i < length; i++) {
  16. switch (str[i]) {
  17. case 'M':
  18. width += 5;
  19. break;
  20. case '0':
  21. width += 4;
  22. break;
  23. case '1':
  24. width += 3;
  25. break;
  26. case '2':
  27. case '3':
  28. case '4':
  29. width += 4;
  30. break;
  31. case '5':
  32. width += 4;
  33. break;
  34. case '6':
  35. width += 4;
  36. break;
  37. case '7':
  38. width += 4;
  39. break;
  40. case '8':
  41. width += 4;
  42. break;
  43. case '9':
  44. width += 4;
  45. break;
  46. case 'A':
  47. case 'B':
  48. case 'C':
  49. case 'D':
  50. case 'E':
  51. case 'F':
  52. case '<':
  53. width += 3;
  54. break;
  55. case '=':
  56. width += 4;
  57. break;
  58. case 'R':
  59. width += 9;
  60. break;
  61. default:
  62. width += 3;
  63. break;
  64. }
  65. width += 1;
  66. }
  67. return width;
  68. }