Psi Swarm robot library version 0.9

Dependents:   PsiSwarm_V9_Blank

Fork of PsiSwarmV9 by James Hilder

Revision:
19:3e3b03d80ea3
Parent:
14:2f1ad77d281e
Child:
20:2b6ebe60929d
--- a/animations.cpp	Sun Jun 04 20:22:41 2017 +0000
+++ b/animations.cpp	Mon Jun 05 22:47:14 2017 +0000
@@ -27,13 +27,26 @@
 #include "psiswarm.h"
 
 char hold_colour = 1;
+char animation_running = 0;
 //
 void Animations::set_colour(char colour)
 {
     hold_colour = colour;
 }
 
-void Animations::led_run1()
+//Blink through LED pattern
+void Animations::led_run1(void)
+{
+    if(animation_running == 0){
+        animation_counter = 0;
+        animation_running = 1;
+        IF_led_run1();   
+    }else{
+        psi.debug("WARNING: Animation called before previous animation finished, ignoring\n");   
+    }
+}
+
+void Animations::IF_led_run1()
 {
     if(animation_counter == 0)led.save_led_states();
     char led_pattern = 0x0;
@@ -61,16 +74,28 @@
     led.set_leds(green_state,red_state);
     animation_counter++;
     if(animation_counter < 21) {
-        animation_timeout.attach(this, &Animations::led_run1, 0.05f);
+        animation_timeout.attach(this, &Animations::IF_led_run1, 0.05f);
     } else {
         animation_counter = 0;
         led.restore_led_states();
+        animation_running = 0;
     }
 }
 
 //Do a simple wiggle
 void Animations::vibrate(void)
 {
+    if(animation_running == 0){
+        animation_counter = 0;
+        animation_running = 1;
+        IF_vibrate();   
+    }else{
+        psi.debug("WARNING: Animation called before previous animation finished, ignoring\n");   
+    }
+}
+
+void Animations::IF_vibrate(void)
+{
     if(animation_counter == 0)led.save_led_states();
     if(animation_counter % 2 == 0) {
         led.set_leds(0xC7,0x00);
@@ -85,10 +110,11 @@
         float wiggle_timeout_period = 0.06;
         //Move less on first 'wiggle' so that we stay in roughly the same place!
         if(animation_counter == 0) wiggle_timeout_period = 0.03;
-        animation_timeout.attach(this, &Animations::vibrate, wiggle_timeout_period);
+        animation_timeout.attach(this, &Animations::IF_vibrate, wiggle_timeout_period);
     } else {
         animation_counter = 0;
         motors.brake();
         led.restore_led_states();
+        animation_running = 0;
     }
 }
\ No newline at end of file