1 /*---------------------------------------------------------------------------*
2   Project:      MIDI adaptor sample demo
3   File:         main.c
4   Programmer:   HIRATSU Daisuke
5 
6   Copyright (C) 2007 Nintendo  All rights reserved.
7 
8   These coded instructions, statements, and computer programs contain
9   proprietary information of Nintendo of America Inc. and/or Nintendo
10   Company Ltd., and are protected by Federal copyright law.  They may
11   not be disclosed to third parties or copied or duplicated in any form,
12   in whole or in part, without the prior written consent of Nintendo.
13 
14   $Log: main.c,v $
15   Revision 1.2  2007/04/17 08:24:47  hiratsu
16   Refactoring.
17 
18   Revision 1.1  2007/04/13 08:39:53  hiratsu
19   Initial check-in.
20 
21  *---------------------------------------------------------------------------*/
22 
23 
24 #include "midiqueue.h"
25 #include "synth.h"
26 
27 #include <revolution.h>
28 #include <revolution/midi.h>
29 #include <demo.h>
30 
31 #include <string.h>
32 
33 
34 #define STACK_SIZE (0x200*8)
35 #define PRIO 10
36 #define SCREEN_WIDTH  320
37 #define SCREEN_HEIGHT 240
38 
39 #define LOG_SIZE 256
40 
41 static void* midiThread(void*);
42 
43 static u8 s_midiSequence[LOG_SIZE];
44 static u8 s_index = 0;
45 
46 
main(void)47 int main(void)
48 {
49     static u8 s_stack[STACK_SIZE] ATTRIBUTE_ALIGN(32);
50     OSThread th;
51 
52     DEMOInit(0);
53     DEMOInitCaption(DM_FT_XLU, SCREEN_WIDTH, SCREEN_HEIGHT);
54     GXSetZMode( GX_ENABLE, GX_ALWAYS, GX_ENABLE );
55     GXSetBlendMode( GX_BM_BLEND, GX_BL_ONE, GX_BL_ONE, GX_LO_CLEAR );
56 
57     initialize();
58     MIDIInit();
59 
60     OSCreateThread(&th, midiThread, NULL,
61                    s_stack + STACK_SIZE, STACK_SIZE,
62                    PRIO, OS_THREAD_ATTR_DETACH);
63     OSResumeThread(&th);
64 
65     programChange(0);
66     while(1)
67     {
68         int i = 0;
69         DEMOBeforeRender();
70         DEMOPrintf(60, 10, 0, "MIDI adaptor sample demo");
71 
72         for(i=0; i<LOG_SIZE; ++i)
73         {
74             char buf[16] = "";
75             sprintf(buf, "%02x", s_midiSequence[i]);
76             DEMOPrintf((s16)(i%16*18+14), (s16)(i/16*13+26), 0, buf);
77         }
78 
79         DEMODoneRender();
80     }
81 
82     return 0;
83 }
84 
85 
midiThread(void *)86 static void* midiThread(void*)
87 {
88     while(1)
89     {
90         const int CHAN=PAD_CHAN3;
91         u8 stat = 0;
92         u8 buf[4]={0x00, 0x00, 0x00, 0x00};
93         s32 ret = 0;
94         u8 count = 0;
95         int i = 0;
96 
97         ret = MIDIGetStatus(CHAN, &stat);
98         if(ret != MIDI_READY)
99         {
100             continue;
101         }
102         if(!(stat & MIDI_JSTAT_SEND))
103         {
104             continue;
105         }
106 
107         ret = MIDIRead(CHAN, buf, &stat);
108         if(ret != MIDI_READY)
109         {
110             continue;
111         }
112         if(!(stat & MIDI_JSTAT_SEND))
113         {
114             continue;
115         }
116 
117         count = stat;
118         count &= MIDI_JSTAT_FLAGS_MASK;
119         count >>= MIDI_JSTAT_FLAGS_SHIFT;
120         count++;
121         for(i=0; i<count; ++i)
122         {
123             push(buf[i]);
124             s_midiSequence[(s_index++)%LOG_SIZE] = buf[i];
125         }
126 
127         while(isReady())
128         {
129             u8 cmd[3] = {0x00, 0x00, 0x00};
130             if(front(cmd))
131             {
132                 pop();
133             }
134             else
135             {
136                 OSReport("Something is wrong...\n");
137             }
138 
139             if(isSupportedCommand(cmd))
140             {
141                 input(cmd);
142             }
143         }
144     }
145 
146     return 0;  // Never reach here.
147 }
148