1 /*---------------------------------------------------------------------------*
2 Project: HIO2 demos - multi
3 File: multi-main.c
4
5 (C)2005 HUDSON SOFT
6
7 $Header: /home/cvsroot/SDK/build/demos/hio2demo/src/multi-main.c,v 1.3 2007/11/26 13:54:44 iwai_yuma Exp $
8
9 $NoKeywords: $
10 *---------------------------------------------------------------------------*/
11
12 #include <string.h>
13 #include <demo.h>
14 #include "Hio2If.h"
15 #include "multi.h"
16
17 //-----------------------------------------------------------------------------
18 // Define
19
20 // Coordinates for DEMORFPrintf()
21 #define POS_COMM_X 32
22 #define POS_TITLE_Y 64
23 #define POS_SENDDATA_Y 128
24 #define POS_RECVDATA_Y 192
25 #define POS_CHAN_Y 384
26 #define POS_STAT_X 512
27 #define POS_STAT_Y 96
28 #define POS_V_Y 416
29 #define POS_CONNECT_X 200
30 #define POS_CONNECT_Y 416
31 #define POS_SYNC_X POS_STAT_X
32 #define POS_SYNC_Y POS_CONNECT_Y
33
34 //-----------------------------------------------------------------------------
35 // type definitions
36
37 // Type for calling the HIO2IF API, based on the presence of a PROTOCOL_USED specification
38 typedef HIO2IF_RESULT (* MYREAD)(HIO2IF_ID id, u32 addr, void* buffer, s32 size, BOOL async);
39 typedef HIO2IF_RESULT (* MYWRITE)(HIO2IF_ID id, u32 addr, void* buffer, s32 size, BOOL async);
40
41 //-----------------------------------------------------------------------------
42 // Local symbol definition
43
44 // Host I/O API buffer
45 static MULTI_PACKET sendBuffer ATTRIBUTE_ALIGN(32);
46 static MULTI_PACKET recvBuffer ATTRIBUTE_ALIGN(32);
47
48 // Color data
49 static const GXColor gxFont = { 0xFF, 0xFF, 0xFF, 0 }, gxBg = { 0, 0, 0, 0 };
50
51 // HIO2IF ID
52 static HIO2IF_ID hioID = HIO2IF_INVALID_ID;
53
54 // Synchronous, asynchronous controls
55 static BOOL hioAsync = FALSE;
56
57 // Connection-related information
58 volatile const char* pStatus = "NOT FIND";
59 #ifdef PROTOCOL_USED
60 volatile BOOL bReceived = FALSE;
61 volatile BOOL bSendPossible = TRUE;
62 #else // PROTOCOL_USED
63 #define bReceived TRUE
64 #define bSendPossible TRUE
65 #endif // PROTOCOL_USED
66
67 // V counter
68 static u32 dwVCounter = 0;
69
70 #ifdef PROTOCOL_USED
71 static const char* strProtocol = "protocol used";
72 #else // PROTOCOL_USED
73 static const char* strProtocol = "non protocol";
74 #endif // PROTOCOL_USED
75
76 static char strBuffer[128];
77
78 //-----------------------------------------------------------------------------
79 // Local function definition
80
81 static void myAppInit(void);
82 static void myHioInit(void);
83 static void myHioExit(void);
84 static void myDispInfo(void);
85
86 static inline
myHalt(void)87 void myHalt(void)
88 {
89 OSFatal(gxFont, gxBg, HIO2IFGetErrorMessage());
90 }
91
92 ///////////////////////////////////////////////////////////////////////////////
93 //
94 // Event callback
95 //
96 static
myEventCallback(HIO2IF_ID id,HIO2IF_EVENT event)97 void myEventCallback(HIO2IF_ID id, HIO2IF_EVENT event)
98 {
99 switch ( event )
100 {
101 case HIO2IF_EVENT_CONNECT: // Establish connection
102 #ifdef HIO2IF_DEBUG
103 OSReport("EVENT CONNECT : id(%d)\n", id);
104 #endif
105 pStatus = "CONNECT";
106 break;
107 case HIO2IF_EVENT_DISCONNECT: // Connection released
108 #ifdef HIO2IF_DEBUG
109 OSReport("EVENT DISCONNECT : id(%d)", id);
110 #endif
111 pStatus = "DISCONNECT";
112 (void)HIO2IFClose(id);
113 break;
114 #ifdef PROTOCOL_USED
115 case HIO2IF_EVENT_RECEIVED: // Receive data
116 bReceived = TRUE;
117 break;
118 case HIO2IF_EVENT_SEND_POSSIBLE: // Send possible
119 bSendPossible = TRUE;
120 break;
121 #endif // PROTOCOL_USED
122 case HIO2IF_EVENT_READ_ASYNC_DONE:
123 case HIO2IF_EVENT_WRITE_ASYNC_DONE:
124 break;
125 case HIO2IF_EVENT_INTERRUPT:
126 myHalt();
127 break;
128 }
129 }
130
131 ///////////////////////////////////////////////////////////////////////////////
132 //
133 // Double-main function
134 //
135
136 //-----------------------------------------------------------------------------
main(void)137 void main(void)
138 {
139 MYREAD hioIfRead;
140 MYWRITE hioIfWrite;
141
142 // Initialization
143 myAppInit();
144
145 #ifdef PROTOCOL_USED
146 hioIfRead = HIO2IFRead;
147 hioIfWrite = HIO2IFWrite;
148 #else // PROTOCOL_USED
149 hioIfRead = HIO2IFReadFree;
150 hioIfWrite = HIO2IFWriteFree;
151 #endif // PROTOCOL_USED
152
153 // Host I/O initialization
154 myHioInit();
155
156 while ( 1 )
157 {
158 OSCalendarTime calender;
159 HIO2IF_RESULT result;
160 u32 stat = 0;
161
162 // Display communication state
163 (void)HIO2IFReadStatus(hioID, &stat);
164 (void)DEMORFPrintf(POS_STAT_X, POS_STAT_Y, 0, "ST:%04X", stat);
165
166 // Set calendar input results to PC send buffer
167 OSTicksToCalendarTime(OSGetTime(), &calender);
168 sendBuffer.mon = (u8)(calender.mon + 1);
169 sendBuffer.mday = (u8)calender.mday;
170 sendBuffer.hour = (u8)calender.hour;
171 sendBuffer.min = (u8)calender.min;
172 sendBuffer.sec = (u8)calender.sec;
173 sendBuffer.msec = (u16)calender.msec;
174
175 // Controller input
176 DEMOPadRead();
177
178 // Connect when A Button is pressed
179 // End connection when B Button is pressed
180 // Switch between SYNC and ASYNC modes when the X Button is pressed
181 if ( DEMOPadGetButtonDown(0) & PAD_BUTTON_A ) myHioInit();
182 else if ( DEMOPadGetButtonDown(0) & PAD_BUTTON_B ) myHioExit();
183 else if ( DEMOPadGetButtonDown(0) & PAD_BUTTON_X ) hioAsync=!hioAsync;
184
185 // Perform receive process when connected to PC
186 if ( bReceived && HIO2IFIsConnected(hioID) )
187 {
188 // Receive from PC
189 result = hioIfRead(hioID, MULTI_PC2NNGC_ADDR,
190 &recvBuffer, MULTI_BUFFER_SIZE, FALSE);
191 if ( HIO2IF_FAILED(result) ) myHalt();
192 #ifdef PROTOCOL_USED
193 bReceived = FALSE;
194 #endif // PROTOCOL_USED
195 }
196
197 // When the Y button is pressed and when there is a connection with the PC, current calendar information is sent to the PC.
198 //
199 if ( (DEMOPadGetButtonDown(0) & PAD_BUTTON_Y)
200 && bSendPossible && HIO2IFIsConnected(hioID) )
201 {
202 result = hioIfWrite(hioID, MULTI_NNGC2PC_ADDR,
203 &sendBuffer, MULTI_BUFFER_SIZE, hioAsync);
204 if ( HIO2IF_FAILED(result) ) myHalt();
205 #ifdef PROTOCOL_USED
206 bSendPossible = FALSE;
207 #endif // PROTOCOL_USED
208 }
209
210 DEMOBeforeRender();
211
212 // Display information update
213 myDispInfo();
214
215 DEMODoneRender();
216
217 dwVCounter++;
218
219 // Sync
220 HIO2IFSync();
221 }
222
223 }
224
225 //----------------------------------------------------------------------------
226 static
myAppInit(void)227 void myAppInit(void)
228 {
229 GXRenderModeObj* rmp;
230
231 DEMOInit(NULL);
232
233 // DEMORFPrintf() initialization
234 (void)DEMOInitROMFont();
235
236 rmp = DEMOGetRenderModeObj();
237 DEMOInitCaption(DM_FT_OPQ, (s16) rmp->fbWidth, (s16) rmp->efbHeight);
238 DEMOSetFontType(DM_FT_OPQ);
239
240 // Screen initialization
241 GXSetCopyClear(gxBg, 0x00ffffff);
242 GXCopyDisp(DEMOGetCurrentBuffer(), GX_TRUE);
243
244 // Controller initialization
245 DEMOPadInit();
246
247 // Host I/O API buffer initialization
248 (void)memset(&sendBuffer, 0, sizeof(sendBuffer));
249 (void)memset(&recvBuffer, 0, sizeof(recvBuffer));
250 (void)strcpy(sendBuffer.string,"NNGC TIME");
251 }
252
253 //----------------------------------------------------------------------------
254 static
myHioInit(void)255 void myHioInit(void)
256 {
257 if ( HIO2IFIsConnected(hioID) ) return ;
258
259 // Host I/O initialization
260 if ( HIO2IF_FAILED(HIO2IFInit()) ) myHalt();
261
262 // Channel open
263 if ( HIO2IFGetDeviceCount() > 0 )
264 {
265 if ( HIO2IF_FAILED(HIO2IFOpen(
266 HIO2IFGetDevice(0),
267 HIO2IF_MODE_RDWR,
268 myEventCallback,
269 &hioID)) )
270 myHalt();
271 pStatus = "DISCONNECT";
272 #ifdef PROTOCOL_USED
273 bReceived = FALSE;
274 bSendPossible = TRUE;
275 #endif
276 }
277 }
278
279 //----------------------------------------------------------------------------
280 static
myHioExit(void)281 void myHioExit(void)
282 {
283 if ( HIO2IFIsConnected(hioID) )
284 {
285 (void)HIO2IFClose(hioID);
286 pStatus = "DISCONNECT";
287 #ifdef PROTOCOL_USED
288 bReceived = FALSE;
289 bSendPossible = FALSE;
290 #endif
291 }
292 }
293
294 //----------------------------------------------------------------------------
295 static
myDispInfo(void)296 void myDispInfo(void)
297 {
298 static const char* devName[HIO2_CHAN_MAX + 2] =
299 { "UNKOWN", "EXI2USB0", "EXI2USB1", "MrEXI" };
300
301 // Title
302 (void)DEMORFPrintf(POS_COMM_X, POS_TITLE_Y, 0, "HIO2 DEMO - multi(%s)",
303 strProtocol);
304 // Send information
305 MultiPacketToString(strBuffer, &sendBuffer);
306 (void)DEMORFPrintf(POS_COMM_X, POS_SENDDATA_Y, 0, "%s", strBuffer);
307
308 // Receive information
309 MultiPacketToString(strBuffer, &recvBuffer);
310 (void)DEMORFPrintf(POS_COMM_X, POS_RECVDATA_Y, 0, "%s", strBuffer);
311
312 // Channel status
313 (void)DEMORFPrintf(POS_COMM_X, POS_CHAN_Y, 0,
314 "CHAN : %s, NNGC %s, PC%d",
315 pStatus, devName[HIO2IFGetDeviceType(hioID) + 1],
316 HIO2IFGetPcChan(hioID));
317
318 // V counter
319 (void)DEMORFPrintf(POS_COMM_X, POS_V_Y, 0, "V=%ld", dwVCounter);
320
321 // Connection state
322 (void)DEMORFPrintf(POS_CONNECT_X, POS_CONNECT_Y, 0,
323 HIO2IFIsConnected(hioID)
324 ? "PUSH B DISCONNECT" : "PUSH A CONNECT");
325
326 // SYNC/ASYNC status
327 (void)DEMORFPrintf(POS_SYNC_X, POS_SYNC_Y, 0, hioAsync ? "ASYNC" : "SYNC");
328 }
329
330 // End of multi-main.c
331