Changeset 447:2335fced942a
- Timestamp:
- 12/08/07 20:56:01 (13 months ago)
- Author:
- David Anderson <dave@…>
- Branch:
- default
- Message:
-
Implement a demonstration of semaphores in Marvin's main().
This demo implements a producer/consumer beeping task pair. One task "produces"
beep tokens by incrementing a semaphore, and the other "consumes" the tokens
by emitting a single beep for each time it successfully decremented the
semaphore. The result is a rythmic 2s beep, but produced by the synchronized
cooperation of two tasks.
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r442
|
r447
|
|
| 13 | 13 | #include "base/drivers/sound.h" |
| 14 | 14 | #include "_scheduler.h" |
| | 15 | #include "semaphore.h" |
| 15 | 16 | |
| 16 | | static void test_beep() { |
| 17 | | nx_sound_freq(820, 500); |
| | 17 | mv_sem_t *beep_res; |
| | 18 | |
| | 19 | static void beep_consumer() { |
| | 20 | while(1) { |
| | 21 | mv_semaphore_dec(beep_res); |
| | 22 | nx_sound_freq(820, 500); |
| | 23 | } |
| 18 | 24 | } |
| 19 | 25 | |
| 20 | | static void test_display() { |
| 21 | | static U32 counter = 0; |
| 22 | | counter++; |
| 23 | | nx_display_clear(); |
| 24 | | nx_display_cursor_set_pos(0,0); |
| 25 | | nx_display_uint(counter); |
| 26 | | nx_display_end_line(); |
| 27 | | } |
| 28 | | |
| 29 | | static void task_spawner() { |
| 30 | | int i; |
| 31 | | for (i=0; i<10; i++) { |
| 32 | | mv_scheduler_create_task(test_beep, 512); |
| 33 | | mv_scheduler_create_task(test_display, 512); |
| | 26 | static void beep_producer() { |
| | 27 | while(1) { |
| 34 | 28 | nx_systick_wait_ms(2000); |
| | 29 | mv_semaphore_inc(beep_res); |
| 35 | 30 | } |
| 36 | 31 | } |
| … |
… |
|
| 39 | 34 | nx_memalloc_init(); |
| 40 | 35 | mv__scheduler_init(); |
| 41 | | mv_scheduler_create_task(task_spawner, 512); |
| | 36 | beep_res = mv_semaphore_create(0); |
| | 37 | mv_scheduler_create_task(beep_consumer, 512); |
| | 38 | mv_scheduler_create_task(beep_producer, 512); |
| 42 | 39 | mv__scheduler_run(); |
| 43 | 40 | } |