WonXSystem.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*****************************************************************************/
  2. /* ここから */
  3. /*****************************************************************************/
  4. #include "wonx_configure.h"
  5. #include "WonXSystemP.h"
  6. #include "WonX.h"
  7. /*****************************************************************************/
  8. /* メンバ関数の定義 */
  9. /*****************************************************************************/
  10. WWInterrupt WonXSystem_GetWWInterrupt(WonXSystem wonx_system)
  11. { return (wonx_system->ww_interrupt); }
  12. WWInterrupt WonXSystem_SetWWInterrupt(WonXSystem wonx_system,
  13. WWInterrupt ww_interrupt)
  14. { return (wonx_system->ww_interrupt = ww_interrupt); }
  15. WWTimer WonXSystem_GetWWTimer(WonXSystem wonx_system, int type)
  16. { return (wonx_system->ww_timer[type]); }
  17. WWTimer WonXSystem_SetWWTimer(WonXSystem wonx_system, int type, WWTimer t)
  18. { return (wonx_system->ww_timer[type] = t); }
  19. WWTimer WonXSystem_GetWWVBlankTimer(WonXSystem wonx_system)
  20. { return (WonXSystem_GetWWTimer(wonx_system, 0)); }
  21. WWTimer WonXSystem_SetWWVBlankTimer(WonXSystem wonx_system, WWTimer t)
  22. { return (WonXSystem_SetWWTimer(wonx_system, 0, t)); }
  23. WWTimer WonXSystem_GetWWVBlankCountUpTimer(WonXSystem wonx_system)
  24. { return (WonXSystem_GetWWTimer(wonx_system, 1)); }
  25. WWTimer WonXSystem_SetWWVBlankCountUpTimer(WonXSystem wonx_system, WWTimer t)
  26. { return (WonXSystem_SetWWTimer(wonx_system, 1, t)); }
  27. WWTimer WonXSystem_GetWWHBlankCountUpTimer(WonXSystem wonx_system)
  28. { return (WonXSystem_GetWWTimer(wonx_system, 2)); }
  29. WWTimer WonXSystem_SetWWHBlankCountUpTimer(WonXSystem wonx_system, WWTimer t)
  30. { return (WonXSystem_SetWWTimer(wonx_system, 2, t)); }
  31. UNIXTimer WonXSystem_GetUNIXTimer(WonXSystem wonx_system)
  32. { return (wonx_system->unix_timer); }
  33. UNIXTimer WonXSystem_SetUNIXTimer(WonXSystem wonx_system,
  34. UNIXTimer unix_timer)
  35. { return (wonx_system->unix_timer = unix_timer); }
  36. static int WonXTimer_Callback(WonXSystem wonx_system)
  37. {
  38. WWTimer ww_timer;
  39. WWInterrupt ww_interrupt;
  40. WonXDisplay wonx_display;
  41. XDisplay x_display;
  42. WonXSerialPort wonx_serial_port;
  43. WWSerialPort ww_serial_port;
  44. UNIXSerialPort unix_serial_port;
  45. unsigned int old_key;
  46. unsigned int new_key;
  47. ww_interrupt = WonXSystem_GetWWInterrupt(wonx_system);
  48. wonx_display = WonX_GetWonXDisplay();
  49. x_display = WonXDisplay_GetXDisplay(wonx_display);
  50. wonx_serial_port = WonX_GetWonXSerialPort();
  51. ww_serial_port = WonXSerialPort_GetWWSerialPort(wonx_serial_port);
  52. unix_serial_port = WonXSerialPort_GetUNIXSerialPort(wonx_serial_port);
  53. ww_timer = WonXSystem_GetWWVBlankTimer(wonx_system);
  54. if (WWTimer_IsON(ww_timer)) {
  55. if (WWTimer_Count(ww_timer))
  56. WWInterrupt_ExecuteVBlankCallback(ww_interrupt);
  57. }
  58. ww_timer = WonXSystem_GetWWVBlankCountUpTimer(wonx_system);
  59. if (WWTimer_IsON(ww_timer)) {
  60. if (WWTimer_Count(ww_timer))
  61. WWInterrupt_ExecuteTimerCountUpCallback(ww_interrupt);
  62. }
  63. ww_timer = WonXSystem_GetWWHBlankCountUpTimer(wonx_system);
  64. if (WWTimer_IsON(ww_timer)) {
  65. if (WWTimer_Count(ww_timer))
  66. WWInterrupt_ExecuteHBlankCountUpCallback(ww_interrupt);
  67. }
  68. /* キー入力割り込み */
  69. /*
  70. * XDisplay_Flush() が他のところから呼ばれた場合に割り込みをとり
  71. * こぼしてしまうので,XDisplay クラスのほうで割り込みの処理をするか,
  72. * フラグを立てるように改良する必要が有るかも.
  73. * (別の場所で頻繁に XDisplay_Flush() が呼ばれていると,キー割り込みが
  74. * 発生しなくなってしまう)
  75. * とりあえずは簡易的にこのような実装にしておいて問題無いだろう.
  76. */
  77. old_key = XDisplay_GetKeyPress(x_display);
  78. XDisplay_Flush(x_display);
  79. new_key = XDisplay_GetKeyPress(x_display);
  80. /* 新しいキーが押された場合 */
  81. /*
  82. * WonderWitch で実験したところ,キーを離した場合には割り込みは
  83. * かからないようだったので,押されたときのみコールバック関数を呼び出す.
  84. */
  85. if (new_key & (~old_key)) {
  86. WWInterrupt_ExecuteKeyCallback(ww_interrupt);
  87. }
  88. /* シリアル受信割り込み */
  89. /*
  90. * 上と同じ理由で割り込みをとりこぼす可能性が有るので,UNIXSerialPort の
  91. * ほうで割り込みの処理をするか,フラグを立てるように改良する必要が有るかも.
  92. * とりあえずは簡易的にこのような実装にしておいて問題無いだろう.
  93. */
  94. if (WWSerialPort_IsOpened(ww_serial_port) &&
  95. UNIXSerialPort_IsOpened(unix_serial_port)) {
  96. if (UNIXSerialPort_IsDataExisting(unix_serial_port, 0))
  97. WWInterrupt_ExecuteReceiveReadyCallback(ww_interrupt);
  98. }
  99. WonXDisplay_PrintData(wonx_display);
  100. return (0);
  101. }
  102. WonXSystem WonXSystem_Create()
  103. {
  104. WonXSystem wonx_system;
  105. WWInterrupt ww_interrupt;
  106. WWTimer ww_timer;
  107. UNIXTimer unix_timer;
  108. wonx_system = (WonXSystem)malloc(sizeof(_WonXSystem));
  109. if (wonx_system == NULL)
  110. WonX_Error("WonXSystem_Create", "Cannot allocate memory.");
  111. ww_interrupt = WWInterrupt_Create();
  112. if (ww_interrupt == NULL)
  113. WonX_Error("WonXSystem_Create", "Cannot create WonderWitch interrupt.");
  114. WonXSystem_SetWWInterrupt(wonx_system, ww_interrupt);
  115. ww_timer = WWTimer_Create(1, WONX_VBLANK_INTERVAL);
  116. if (ww_timer == NULL)
  117. WonX_Error("WonXSystem_Create", "Cannot create WonderWitch VBlank timer.");
  118. WonXSystem_SetWWVBlankTimer(wonx_system, ww_timer);
  119. ww_timer = WWTimer_Create(0, WONX_VBLANK_INTERVAL);
  120. if (ww_timer == NULL)
  121. WonX_Error("WonXSystem_Create",
  122. "Cannot create WonderWitch VBlank count up timer.");
  123. WonXSystem_SetWWVBlankCountUpTimer(wonx_system, ww_timer);
  124. ww_timer = WWTimer_Create(0, WONX_HBLANK_INTERVAL);
  125. if (ww_timer == NULL)
  126. WonX_Error("WonXSystem_Create",
  127. "Cannot create WonderWitch HBlank count up timer.");
  128. WonXSystem_SetWWHBlankCountUpTimer(wonx_system, ww_timer);
  129. WWTimer_Reset(WonXSystem_GetWWVBlankTimer( wonx_system));
  130. WWTimer_Reset(WonXSystem_GetWWVBlankCountUpTimer(wonx_system));
  131. WWTimer_Reset(WonXSystem_GetWWHBlankCountUpTimer(wonx_system));
  132. WWTimer_ON( WonXSystem_GetWWVBlankTimer( wonx_system));
  133. WWTimer_OFF(WonXSystem_GetWWVBlankCountUpTimer(wonx_system));
  134. WWTimer_OFF(WonXSystem_GetWWHBlankCountUpTimer(wonx_system));
  135. unix_timer = UNIXTimer_Create(1, WONX_TIMER_INTERVAL, wonx_system,
  136. (UNIXTimerCallBack)WonXTimer_Callback);
  137. if (unix_timer == NULL)
  138. WonX_Error("WonXSystem_Create", "Cannot create UNIX timer.");
  139. WonXSystem_SetUNIXTimer(wonx_system, unix_timer);
  140. UNIXTimer_ON(unix_timer);
  141. return (wonx_system);
  142. }
  143. WonXSystem WonXSystem_Destroy(WonXSystem wonx_system)
  144. {
  145. WWInterrupt wi;
  146. WWTimer wt;
  147. UNIXTimer unix_timer;
  148. if (wonx_system == NULL)
  149. WonX_Error("WonXSystem_Destroy", "Object is not created.");
  150. unix_timer = WonXSystem_GetUNIXTimer(wonx_system);
  151. if (unix_timer)
  152. UNIXTimer_OFF(unix_timer);
  153. wt = WonXSystem_GetWWVBlankTimer(wonx_system);
  154. if (wt) WWTimer_OFF(wt);
  155. wt = WonXSystem_GetWWVBlankCountUpTimer(wonx_system);
  156. if (wt) WWTimer_OFF(wt);
  157. wt = WonXSystem_GetWWHBlankCountUpTimer(wonx_system);
  158. if (wt) WWTimer_OFF(wt);
  159. if (unix_timer)
  160. WonXSystem_SetUNIXTimer(wonx_system, UNIXTimer_Destroy(unix_timer));
  161. wt = WonXSystem_GetWWVBlankTimer(wonx_system);
  162. if (wt) WonXSystem_SetWWVBlankTimer(wonx_system, WWTimer_Destroy(wt));
  163. wt = WonXSystem_GetWWVBlankCountUpTimer(wonx_system);
  164. if (wt) WonXSystem_SetWWVBlankCountUpTimer(wonx_system, WWTimer_Destroy(wt));
  165. wt = WonXSystem_GetWWHBlankCountUpTimer(wonx_system);
  166. if (wt) WonXSystem_SetWWHBlankCountUpTimer(wonx_system, WWTimer_Destroy(wt));
  167. wi = WonXSystem_GetWWInterrupt(wonx_system);
  168. if (wi) WonXSystem_SetWWInterrupt(wonx_system, WWInterrupt_Destroy(wi));
  169. free(wonx_system);
  170. return (NULL);
  171. }
  172. /*****************************************************************************/
  173. /* ここまで */
  174. /*****************************************************************************/
  175. /*****************************************************************************/
  176. /* End of File. */
  177. /*****************************************************************************/