share for RS

Fork of rbVectorIQ by Mark @RRVA

Revision:
14:459081f9f70b
Parent:
13:047bbd59f351
--- a/main.cpp	Tue Apr 18 17:00:37 2017 +0000
+++ b/main.cpp	Wed Apr 19 21:20:31 2017 +0000
@@ -68,10 +68,35 @@
             pattern = buf[5];
         } else if(buf[0] == PIEZO_HEADER) {
             mPiezoOn = buf[1];
+            //mPiezoDur = buf[2];
+            mPiezoPattern = buf[2];
         } else if(buf[0] == HAPTIC_HEADER) {
             mHapticOn = buf[1];
             mHapticDur = buf[2];
             mHapticPattern = buf[3];
+        } else if(buf[0] == ALARM_HEADER) {
+            mAlarmOn = buf[1];
+            //mAlarmStart = buf[2];
+            mAlarmDur = buf[2];
+            mAlarmPiezoOn = buf[4];
+            mAlarmTone = buf[5];
+            mAlarmPattern = buf[6];
+            pattern = mAlarmPattern;
+            if (mAlarmOn == ALARM_ON) {
+                // Setup the timer
+                t_alarm.start();
+                mBeginAlarm = t_alarm.read();
+                mEndAlarm = mBeginAlarm + mAlarmDur;
+                mPiezoOn = mAlarmPiezoOn;
+                LEDS_ON_FLAG = 1; 
+            } else if (mAlarmOn == ALARM_OFF) {
+                // Stop the timer
+                t_alarm.stop();
+                // Kill all the effects
+                mHapticOn = HAPTIC_OFF;
+                mPiezoOn = PIEZO_OFF;
+                LEDS_ON_FLAG = 0;
+            }
         }   
         else
         {
@@ -84,9 +109,23 @@
     }
 }
 
+// pb Interrupt routine - is interrupt activated by a falling edge of pb input
+void pb_hit_interrupt (void) {
+    pattern++;
+    if (pattern >= PATTERN_MEM)
+        pattern = PATTERN_ON;
+}
+
 void m_status_check_handle(void)
 {
     //uint8_t buf[3];
+    
+    //if (BUTTON == 1) {
+        // Cycle through display patterns
+        pattern++;
+        if (pattern >= PATTERN_MEM)
+            pattern = PATTERN_NONE;
+    //}
 
     /* if (BUTTON == 1)
         LEDS_ON_FLAG = 0;
@@ -117,25 +156,35 @@
 void ble_on_radio_active_evt(bool radio_active)
 {
     if (radio_active == false) {
+        if (mAlarmOn == ALARM_ON) {
+            if (t_alarm.read() > mEndAlarm) {
+                // end alarm 
+                t_alarm.stop();
+                mHapticOn = HAPTIC_OFF;
+                mPiezoOn = PIEZO_OFF;
+                LEDS_ON_FLAG = 0;
+            }
+        } 
         if (mHapticOn == HAPTIC_ON) {
             BUZZER = 1;
-            //wait_ms(1000);
+            wait_ms(1000);
         } else {
             BUZZER = 0;
             //wait_ms(1000); 
         }
         if (mPiezoOn == PIEZO_ON) {
             for (int i = 0; i < (sizeof(freq_coll) / sizeof(int)); i++) {
+                // Keep checking for an "Off"
+                //if (mPiezoOn == PIEZO_OFF)
+                    //break;
                 PIEZO.period(1.0 / freq_coll[i]);
-                PIEZO.write(0.5);
+                PIEZO.write(0.9);
                 wait(1.0 / beat_coll[i]);
                 PIEZO.write(0);
                 wait(0.05);
             }
-        } else {
-            BUZZER = 0;
-            //wait_ms(1000); 
-        }
+            wait_ms(500);
+        } 
         if (LEDS_ON_FLAG == 1) {          
             if (pattern == PATTERN_ON) {
                 for (uint8_t i = 0; i <= NUM_LEDS; i++)
@@ -162,9 +211,15 @@
                 wait_ms(1000);
                 
             } else if (pattern == PATTERN_CHASE) {
-                for (uint8_t i = 0; i < NUM_LEDS; i++)
-                    neopixel_set_color(&m_strip, i, red, green, blue);
+                for (uint8_t i = 0; i <= NUM_LEDS; i++)
+                    neopixel_set_color(&m_strip, i, 0, 0, 0);
+                if (led_to_enable > NUM_LEDS)
+                    led_to_enable = 0;
+                for (uint8_t i = 0; i <= NUM_LEDS; i++)
+                    neopixel_set_color(&m_strip, led_to_enable, red, green, blue);
                 neopixel_show(&m_strip);
+                led_to_enable++;
+                wait_ms(100);
                 
             } else if (pattern == PATTERN_MRB) {
                 //neopixel_clear(&m_strip);
@@ -257,9 +312,18 @@
 int main(void)
 {
     //uint8_t led_error;
-    //BUTTON.mode(PullNone); 
-    Ticker ticker;
-    ticker.attach_us(m_status_check_handle, 200000);
+    //BUTTON.mode(PullDown); 
+    //Ticker ticker;
+    //ticker.attach_us(m_status_check_handle, 200000);
+    
+    // Use internal pullup for pushbutton
+    //pb.mode(PullDown);
+    // Delay for initial pullup to take effect
+    //wait(.01);
+    // Attach the address of the interrupt handler routine for pushbutton
+    //pb.fall(&pb_hit_interrupt);
+    
+    set_time(1256729737  );  // Set RTC time to Wed, 28 Oct 2009 11:35:37
     
     strcpy(mDeviceName, "VIQ");
     mLength = strlen(mDeviceName);