WonX.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "WonXP.h"
  2. #include "etc.h"
  3. #include "wonx/disp.h"
  4. #include "wonx/system.h"
  5. #include "wonx/comm.h"
  6. static WonX wonx = NULL;
  7. int WonX_IsCreated(void)
  8. {
  9. return (wonx != NULL);
  10. }
  11. void WonX_Create(void)
  12. {
  13. if (WonX_IsCreated())
  14. WonX_Error("WonX_Create", "WonX has been already created.");
  15. wonx = (WonX)malloc(sizeof(_WonX));
  16. if (wonx == NULL) WonX_Error("WonX_Create", "Cannot allocate memory.");
  17. /*
  18. * WonX のタイマからはディスプレイのリフレッシュなどの関数を呼び出すため,
  19. * WonXSystem_Create() でタイマをスタートさせる前に WonXDisplay_Create() で
  20. * WonXDisplay オブジェクトを作成しておく必要がある.このため
  21. * WonXDisplay_Create() を WonXSystem_Create() の後に持っていってはいけない.
  22. */
  23. wonx->wonx_display =
  24. WonXDisplay_Create(LCD_PIXEL_WIDTH * 2, LCD_PIXEL_HEIGHT * 2,
  25. LCD_PIXEL_WIDTH, LCD_PIXEL_HEIGHT,
  26. SCREEN_CHAR_WIDTH, SCREEN_CHAR_HEIGHT);
  27. /*
  28. * WonXDisplay と同様のことが WonXSerialPort でも将来的に起きる可能性が
  29. * あるかもしれないので, WonXSerialPort_Create() もいちおう
  30. * WonXSystem_Create() の前に置いておく.
  31. */
  32. wonx->wonx_serial_port = WonXSerialPort_Create();
  33. /*
  34. * WonXSystem_Create() ではタイマを作成するが,タイマからはいろいろな
  35. * オブジェクトが呼ばれる可能性があるため,タイマの作成は一番最後に行う
  36. * べきである.このため,WonXSystem_Create() は一番最後に行う.
  37. * (他のオブジェクトの作成中等にタイマがかかった場合,おかしなことに
  38. * なってしまう)
  39. */
  40. wonx->wonx_system = WonXSystem_Create();
  41. return;
  42. }
  43. void WonX_Destroy(void)
  44. {
  45. if (!WonX_IsCreated())
  46. WonX_Error("WonX_Destroy", "WonX has not been created yet.");
  47. /*
  48. * WonXSystem はタイマを持つが,タイマからはいろいろなオブジェクトが
  49. * 呼ばれる可能性があるため,一番最初にタイマを停止する必要がある.
  50. * このため,WonXSystem_Destroy() は一番最初に行う.
  51. * (他のオブジェクトの削除中等にタイマがかかった場合,おかしなことに
  52. * なってしまう)
  53. */
  54. if (wonx->wonx_system)
  55. wonx->wonx_system = WonXSystem_Destroy(wonx->wonx_system);
  56. /*
  57. * WonXDisplay と同様のことが WonXSerialPort でも将来的に起きる可能性が
  58. * あるかもしれないので, WonXSerialPort_Destroy() もいちおう
  59. * WonXSystem_Destroy() の後に置いておく.
  60. */
  61. if (wonx->wonx_serial_port)
  62. wonx->wonx_serial_port = WonXSerialPort_Destroy(wonx->wonx_serial_port);
  63. /*
  64. * WonX のタイマからはディスプレイのリフレッシュなどの関数を呼び出すため,
  65. * WonXDisplay を消去する前に WonXSystem を消去して,タイマを停止する
  66. * 必要がある.(でないと WonXDisplay_Destroy() の実行中にタイマがかかった
  67. * ときに,おかしなことになる)
  68. * このため,WonXDisplay_Destroy() を WonXSystem_Destroy() の前に
  69. * 持っていってはいけない.
  70. */
  71. if (wonx->wonx_display)
  72. wonx->wonx_display = WonXDisplay_Destroy(wonx->wonx_display);
  73. free(wonx);
  74. wonx = NULL;
  75. return;
  76. }
  77. WonXDisplay WonX_GetWonXDisplay(void)
  78. {
  79. return (wonx->wonx_display);
  80. }
  81. WonXSystem WonX_GetWonXSystem(void)
  82. {
  83. return (wonx->wonx_system);
  84. }
  85. WonXSerialPort WonX_GetWonXSerialPort(void)
  86. {
  87. return (wonx->wonx_serial_port);
  88. }
  89. /*===========================================================================*/
  90. /* エラー処理 */
  91. /*===========================================================================*/
  92. int WonX_Error(char * funcname, char * message)
  93. {
  94. fprintf(stderr, "error : %s(): %s\n", funcname, message);
  95. exit (1);
  96. }
  97. int WonX_Warning(char * funcname, char * message)
  98. {
  99. fprintf(stderr, "warning : %s(): %s\n", funcname, message);
  100. return (0);
  101. }
  102. /*****************************************************************************/
  103. /* End of File. */
  104. /*****************************************************************************/