Add FS #10214. Initial commit of the original PDa code for the GSoC Pure Data plugin...
[kugel-rb.git] / apps / plugins / pdbox / pdbox.c
blob940784935248d8ef7e90e43656fc99c024dd0d60
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Wincent Balin
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
23 #include "pdbox.h"
25 /* Welcome to the PDBox plugin */
26 PLUGIN_HEADER
27 PLUGIN_IRAM_DECLARE
29 /* Quit flag. */
30 bool quit = false;
32 /* Thread IDs. */
33 unsigned int core_thread_id;
34 unsigned int gui_thread_id;
36 /* Stacks for threads. */
37 #define STACK_SIZE 16384
38 uint32_t core_stack[STACK_SIZE / sizeof(uint32_t)];
39 uint32_t gui_stack[STACK_SIZE / sizeof(uint32_t)];
42 /* Core thread */
43 void core_thread(void)
45 struct datagram ping;
47 /* Main loop */
48 while(!quit)
50 /* Wait for request. */
51 while(!RECEIVE_TO_CORE(&ping))
52 rb->yield();
54 if(memcmp("Ping!", ping.data, ping.size) == 0)
56 SEND_FROM_CORE("Pong!");
57 break;
60 rb->yield();
63 rb->thread_exit();
66 /* GUI thread */
67 void gui_thread(void)
69 struct datagram pong;
71 /* GUI loop */
72 while(!quit)
74 /* Send ping to the core. */
75 SEND_TO_CORE("Ping!");
76 rb->splash(HZ, "Sent ping.");
78 /* Wait for answer. */
79 while(!RECEIVE_FROM_CORE(&pong))
80 rb->yield();
82 /* If got a pong -- everything allright. */
83 if(memcmp("Pong!", pong.data, pong.size) == 0)
85 rb->splash(HZ, "Got pong!");
86 quit = true;
87 break;
90 rb->yield();
93 rb->thread_exit();
97 /* Plug-in entry point */
98 enum plugin_status plugin_start(const void* parameter)
100 PLUGIN_IRAM_INIT(rb)
102 size_t mem_size;
103 void* mem_pool;
105 /* Get the file name. */
106 const char* filename = (const char*) parameter;
108 #if 0
109 /* Allocate memory; check it's size; add to the pool. */
110 mem_pool = rb->plugin_get_audio_buffer(&mem_size);
111 if(mem_size < MIN_MEM_SIZE)
113 rb->splash(HZ, "Not enough memory!");
114 return PLUGIN_ERROR;
116 add_pool(mem_pool, mem_size);
117 #endif
119 /* Initialze net. */
120 net_init();
122 /* Start threads. */
123 core_thread_id =
124 rb->create_thread(&core_thread,
125 core_stack,
126 sizeof(core_stack),
127 0, /* FIXME Which flags? */
128 "PD core"
129 IF_PRIO(, MAX(PRIORITY_USER_INTERFACE / 2,
130 PRIORITY_REALTIME + 1))
131 IF_COP(, COP));
132 gui_thread_id =
133 rb->create_thread(&gui_thread,
134 gui_stack,
135 sizeof(gui_stack),
136 0, /* FIXME Which flags? */
137 "PD GUI"
138 IF_PRIO(, PRIORITY_USER_INTERFACE)
139 IF_COP(, CPU));
141 /* If having an error creating threads, bail out. */
142 if(core_thread_id == 0 || gui_thread_id == 0)
143 return PLUGIN_ERROR;
145 /* Wait for quit flag. */
146 while(!quit)
147 rb->yield();
149 /* Wait for threads to complete. */
150 rb->thread_wait(gui_thread_id);
151 rb->thread_wait(core_thread_id);
153 /* Destroy net. */
154 net_destroy();
156 return PLUGIN_OK;
158 /***************************************************************************
159 * __________ __ ___.
160 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
161 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
162 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
163 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
164 * \/ \/ \/ \/ \/
165 * $Id$
167 * Copyright (C) 2009 Wincent Balin
169 * This program is free software; you can redistribute it and/or
170 * modify it under the terms of the GNU General Public License
171 * as published by the Free Software Foundation; either version 2
172 * of the License, or (at your option) any later version.
174 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
175 * KIND, either express or implied.
177 ****************************************************************************/
179 #include "plugin.h"
180 #include "pdbox.h"
182 /* Welcome to the PDBox plugin */
183 PLUGIN_HEADER
184 PLUGIN_IRAM_DECLARE
186 /* Quit flag. */
187 bool quit = false;
189 /* Thread IDs. */
190 unsigned int core_thread_id;
191 unsigned int gui_thread_id;
193 /* Stacks for threads. */
194 #define STACK_SIZE 16384
195 uint32_t core_stack[STACK_SIZE / sizeof(uint32_t)];
196 uint32_t gui_stack[STACK_SIZE / sizeof(uint32_t)];
199 /* Core thread */
200 void core_thread(void)
202 struct datagram ping;
204 /* Main loop */
205 while(!quit)
207 /* Wait for request. */
208 while(!RECEIVE_TO_CORE(&ping))
209 rb->yield();
211 if(memcmp("Ping!", ping.data, ping.size) == 0)
213 SEND_FROM_CORE("Pong!");
214 break;
217 rb->yield();
220 rb->thread_exit();
223 /* GUI thread */
224 void gui_thread(void)
226 struct datagram pong;
228 /* GUI loop */
229 while(!quit)
231 /* Send ping to the core. */
232 SEND_TO_CORE("Ping!");
233 rb->splash(HZ, "Sent ping.");
235 /* Wait for answer. */
236 while(!RECEIVE_FROM_CORE(&pong))
237 rb->yield();
239 /* If got a pong -- everything allright. */
240 if(memcmp("Pong!", pong.data, pong.size) == 0)
242 rb->splash(HZ, "Got pong!");
243 quit = true;
244 break;
247 rb->yield();
250 rb->thread_exit();
254 /* Plug-in entry point */
255 enum plugin_status plugin_start(const void* parameter)
257 PLUGIN_IRAM_INIT(rb)
259 size_t mem_size;
260 void* mem_pool;
262 /* Get the file name. */
263 const char* filename = (const char*) parameter;
265 #if 0
266 /* Allocate memory; check it's size; add to the pool. */
267 mem_pool = rb->plugin_get_audio_buffer(&mem_size);
268 if(mem_size < MIN_MEM_SIZE)
270 rb->splash(HZ, "Not enough memory!");
271 return PLUGIN_ERROR;
273 add_pool(mem_pool, mem_size);
274 #endif
276 /* Initialze net. */
277 net_init();
279 /* Start threads. */
280 core_thread_id =
281 rb->create_thread(&core_thread,
282 core_stack,
283 sizeof(core_stack),
284 0, /* FIXME Which flags? */
285 "PD core"
286 IF_PRIO(, MAX(PRIORITY_USER_INTERFACE / 2,
287 PRIORITY_REALTIME + 1))
288 IF_COP(, COP));
289 gui_thread_id =
290 rb->create_thread(&gui_thread,
291 gui_stack,
292 sizeof(gui_stack),
293 0, /* FIXME Which flags? */
294 "PD GUI"
295 IF_PRIO(, PRIORITY_USER_INTERFACE)
296 IF_COP(, CPU));
298 /* If having an error creating threads, bail out. */
299 if(core_thread_id == 0 || gui_thread_id == 0)
300 return PLUGIN_ERROR;
302 /* Wait for quit flag. */
303 while(!quit)
304 rb->yield();
306 /* Wait for threads to complete. */
307 rb->thread_wait(gui_thread_id);
308 rb->thread_wait(core_thread_id);
310 /* Destroy net. */
311 net_destroy();
313 return PLUGIN_OK;