Ran Katz / Mbed 2 deprecated LightSaber

Dependencies:   L152RE_USBDevice STM32_USB48MHz Watchdog mbed

Files at this revision

API Documentation at this revision

Comitter:
nightmechanic
Date:
Sat Mar 26 21:03:35 2016 +0000
Parent:
2:59a7d4677474
Child:
4:7e4bb0c29d3b
Commit message:
Moved sound player into main loop (from 10msec ticker)

Changed in this revision

LightSaber.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/LightSaber.h	Thu Mar 24 22:42:59 2016 +0000
+++ b/LightSaber.h	Sat Mar 26 21:03:35 2016 +0000
@@ -32,3 +32,4 @@
 void mpu_calc_avg(MPU_data_type * MPU_data,int mpu_pointer, MPU_data_type * MPU_avg_data, int avg_len);
 void init_color(int *new_color, int pulsewidth);
 void change_color(color_type new_color, int new_pulsewidth);
+bool sound_player(const int sound_table[][6], int table_lines);
--- a/main.cpp	Thu Mar 24 22:42:59 2016 +0000
+++ b/main.cpp	Sat Mar 26 21:03:35 2016 +0000
@@ -38,17 +38,15 @@
 volatile int Color_Button_Count = 0;
 
 //sounds
-int sound_to_play;
 volatile int sound_count = 1;
 volatile int sound_line = 0;
 volatile int next_sound_time = 0;
-volatile bool sound_is_playing_flag = FALSE;
 
 //Battery voltage monitor
 AnalogIn V_bat(PA_2);
 
 //Ticker
-Ticker button_ticker, sound_ticker;
+Ticker button_ticker;
 
 //Set up I2C, (SDA,SCL)
 //I2C MPU_i2c(PB_9, PB_8); //defined in MPU6050.h...
@@ -79,57 +77,6 @@
     
 }
 
-bool sound_player(const int sound_table[][6], int table_lines)
-{
-    int sound_period;
-    int sound_pulse_width_us;
-    if (sound_count > sound_table[sound_line][NUM_STEPS_IDX])
-    {
-        sound_count = 1;
-        sound_line++;
-        if (sound_line >= table_lines)
-        {
-            return FALSE;
-        }
-    }
-    
-    sound_period = sound_table[sound_line][INIT_PERIOD_IDX] + ((sound_count-1) * sound_table[sound_line][STEP_PERIOD_IDX]);
-    sound_pulse_width_us = ( sound_period * (sound_table[sound_line][INIT_VOL_IDX] + ((sound_count-1) * sound_table[sound_line][STEP_VOL_IDX])) )/ 200;
-    
-    // there are no checks for 0/negative values of the above - need to make sure that tables are valid.
-    // set PWM parameters for current step
-    
-    Speaker_OUT.period_us(sound_period);
-    Speaker_OUT.pulsewidth_us(sound_pulse_width_us);
-        
-    //update next_sound_time, step count        
-    
-    next_sound_time = t.read_ms() + sound_table[sound_line][STEP_DUR_IDX];
-    sound_count++;
-    
-    return TRUE;
-}
-
-void sound_inth()
-{
-    if ((t.read_ms() >= next_sound_time) && sound_is_playing_flag )
-    {
-        switch (sound_to_play) {
-        
-            case STARTUP_SOUND:
-                sound_is_playing_flag = sound_player(startup_sound_table, STARTUP_TBL_N_LINES);
-                break;
-            case MOVEMENT_SOUND:
-                sound_is_playing_flag = sound_player(movement_sound_table, MOVEMENT_TBL_N_LINES);
-                break;
-            case CLASH_SOUND:
-                sound_is_playing_flag = sound_player(clash_sound_table, CLASH_TBL_N_LINES);
-                break;
-            default:
-                break;
-        }       
-    }
-}
 int main()
 {
     bool motion_is_init = FALSE;
@@ -154,6 +101,9 @@
     bool ypr_thr_crossed = FALSE;
     int count = 0;
     
+    int sound_to_play;
+    bool sound_is_playing_flag = FALSE;
+    
     int temp_diff;
     
     int i = 0;
@@ -178,8 +128,6 @@
 // start "clock"
     t.start();
     
-// attach and set sound interrupt handler (every 10msec)   
-    sound_ticker.attach(&sound_inth, 0.01);
 // play startup sound    
     sound_to_play = STARTUP_SOUND;
     sound_line = 0;
@@ -192,7 +140,7 @@
     
     button_ticker.attach(&button_inth, 0.025);
         
-    wait_ms(1000); //from MPU example
+    wait_ms(100); //from MPU example (1000)
     
 //Initialize watchdog with a 2 second interval
  //   wd.Configure(2.0);       
@@ -397,6 +345,24 @@
             
             
         }
+// Play sound
+        if ((t.read_ms() >= next_sound_time) && sound_is_playing_flag ) 
+        {
+            switch (sound_to_play) {
+
+                case STARTUP_SOUND:
+                    sound_is_playing_flag = sound_player(startup_sound_table, STARTUP_TBL_N_LINES);
+                    break;
+                case MOVEMENT_SOUND:
+                    sound_is_playing_flag = sound_player(movement_sound_table, MOVEMENT_TBL_N_LINES);
+                    break;
+                case CLASH_SOUND:
+                    sound_is_playing_flag = sound_player(clash_sound_table, CLASH_TBL_N_LINES);
+                    break;
+                default:
+                    break;
+            }
+        }
 //handle timer overflow - even if a sound is playing there are 5 minutes before overflow
         if ((t.read_ms() > (30*60*1000)) && !sound_is_playing_flag)
         {
@@ -505,4 +471,35 @@
             break;
     }
 }
+
+bool sound_player(const int sound_table[][6], int table_lines)
+{
+    int sound_period;
+    int sound_pulse_width_us;
+    if (sound_count > sound_table[sound_line][NUM_STEPS_IDX])
+    {
+        sound_count = 1;
+        sound_line++;
+        if (sound_line >= table_lines)
+        {
+            return FALSE;
+        }
+    }
+    
+    sound_period = sound_table[sound_line][INIT_PERIOD_IDX] + ((sound_count-1) * sound_table[sound_line][STEP_PERIOD_IDX]);
+    sound_pulse_width_us = ( sound_period * (sound_table[sound_line][INIT_VOL_IDX] + ((sound_count-1) * sound_table[sound_line][STEP_VOL_IDX])) )/ 200;
+    
+    // there are no checks for 0/negative values of the above - need to make sure that tables are valid.
+    // set PWM parameters for current step
+    
+    Speaker_OUT.period_us(sound_period);
+    Speaker_OUT.pulsewidth_us(sound_pulse_width_us);
+        
+    //update next_sound_time, step count        
+    
+    next_sound_time = t.read_ms() + sound_table[sound_line][STEP_DUR_IDX];
+    sound_count++;
+    
+    return TRUE;
+}
     
\ No newline at end of file