hw.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * TamaLIB - A hardware agnostic Tamagotchi P1 emulation library
  3. *
  4. * Copyright (C) 2021 Jean-Christophe Rona <jc@rona.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "hw.h"
  21. #include "cpu.h"
  22. #include "hal.h"
  23. /* SEG -> LCD mapping */
  24. static u8_t seg_pos[40] = {0, 1, 2, 3, 4, 5, 6, 7, 32, 8, 9, 10, 11, 12,
  25. 13, 14, 15, 33, 34, 35, 31, 30, 29, 28, 27, 26, 25, 24,
  26. 36, 23, 22, 21, 20, 19, 18, 17, 16, 37, 38, 39};
  27. bool_t hw_init(void) {
  28. /* Buttons are active LOW */
  29. cpu_set_input_pin(PIN_K00, PIN_STATE_HIGH);
  30. cpu_set_input_pin(PIN_K01, PIN_STATE_HIGH);
  31. cpu_set_input_pin(PIN_K02, PIN_STATE_HIGH);
  32. return 0;
  33. }
  34. void hw_release(void) {
  35. }
  36. void hw_set_lcd_pin(u8_t seg, u8_t com, u8_t val) {
  37. if(seg_pos[seg] < LCD_WIDTH) {
  38. g_hal->set_lcd_matrix(seg_pos[seg], com, val);
  39. } else {
  40. /*
  41. * IC n -> seg-com|...
  42. * IC 0 -> 8-0 |18-3 |19-2
  43. * IC 1 -> 8-1 |17-0 |19-3
  44. * IC 2 -> 8-2 |17-1 |37-12|38-13|39-14
  45. * IC 3 -> 8-3 |17-2 |18-1 |19-0
  46. * IC 4 -> 28-12|37-13|38-14|39-15
  47. * IC 5 -> 28-13|37-14|38-15
  48. * IC 6 -> 28-14|37-15|39-12
  49. * IC 7 -> 28-15|38-12|39-13
  50. */
  51. if(seg == 8 && com < 4) {
  52. g_hal->set_lcd_icon(com, val);
  53. } else if(seg == 28 && com >= 12) {
  54. g_hal->set_lcd_icon(com - 8, val);
  55. }
  56. }
  57. }
  58. void hw_set_button(button_t btn, btn_state_t state) {
  59. pin_state_t pin_state = (state == BTN_STATE_PRESSED) ? PIN_STATE_LOW : PIN_STATE_HIGH;
  60. switch(btn) {
  61. case BTN_LEFT:
  62. cpu_set_input_pin(PIN_K02, pin_state);
  63. break;
  64. case BTN_MIDDLE:
  65. cpu_set_input_pin(PIN_K01, pin_state);
  66. break;
  67. case BTN_RIGHT:
  68. cpu_set_input_pin(PIN_K00, pin_state);
  69. break;
  70. }
  71. }
  72. void hw_set_buzzer_freq(u4_t freq) {
  73. u32_t snd_freq = 0;
  74. switch(freq) {
  75. case 0:
  76. /* 4096.0 Hz */
  77. snd_freq = 40960;
  78. break;
  79. case 1:
  80. /* 3276.8 Hz */
  81. snd_freq = 32768;
  82. break;
  83. case 2:
  84. /* 2730.7 Hz */
  85. snd_freq = 27307;
  86. break;
  87. case 3:
  88. /* 2340.6 Hz */
  89. snd_freq = 23406;
  90. break;
  91. case 4:
  92. /* 2048.0 Hz */
  93. snd_freq = 20480;
  94. break;
  95. case 5:
  96. /* 1638.4 Hz */
  97. snd_freq = 16384;
  98. break;
  99. case 6:
  100. /* 1365.3 Hz */
  101. snd_freq = 13653;
  102. break;
  103. case 7:
  104. /* 1170.3 Hz */
  105. snd_freq = 11703;
  106. break;
  107. }
  108. if(snd_freq != 0) {
  109. g_hal->set_frequency(snd_freq);
  110. }
  111. }
  112. void hw_enable_buzzer(bool_t en) {
  113. g_hal->play_frequency(en);
  114. }