Changeset 447:2335fced942a

Show
Ignore:
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:
1 modified

Legend:

Unmodified
Added
Removed
  • nxos/systems/marvin/main.c

    r442 r447  
    1313#include "base/drivers/sound.h" 
    1414#include "_scheduler.h" 
     15#include "semaphore.h" 
    1516 
    16 static void test_beep() { 
    17   nx_sound_freq(820, 500); 
     17mv_sem_t *beep_res; 
     18 
     19static void beep_consumer() { 
     20  while(1) { 
     21    mv_semaphore_dec(beep_res); 
     22    nx_sound_freq(820, 500); 
     23  } 
    1824} 
    1925 
    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); 
     26static void beep_producer() { 
     27  while(1) { 
    3428    nx_systick_wait_ms(2000); 
     29    mv_semaphore_inc(beep_res); 
    3530  } 
    3631} 
     
    3934  nx_memalloc_init(); 
    4035  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); 
    4239  mv__scheduler_run(); 
    4340}