RadiantRVA / Mbed OS rbVectorIQ

Fork of rbVectorIQ by Mark @RRVA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 
00003 Copyright (c) 2017 Radiant RVA
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00006 and associated documentation files (the "Software"), to deal in the Software without restriction,
00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
00009 subject to the following conditions:
00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00011 
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 
00018 */
00019 #define DK_52
00020 
00021 #include <events/mbed_events.h>
00022 #include "vector_iq.h"
00023 
00024 #ifdef DK_52
00025     DigitalOut led1(LED1, 1);
00026 #endif
00027 
00028 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
00029 
00030 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
00031 {
00032 #ifdef VIQ
00033     neopixel_init(&m_strip, dig_pin_num, NUM_LEDS);
00034     neopixel_clear(&m_strip);
00035     LEDS_ON_FLAG = 0;
00036 #endif
00037 }
00038 
00039 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00040 {
00041     BLE::Instance().gap().startAdvertising(); // restart advertising
00042 }
00043 
00044 void WrittenHandler(const GattWriteCallbackParams *Handler)
00045 {
00046 #ifdef VIQ
00047     uint8_t buf[TXRX_BUF_LEN];
00048     uint16_t bytesRead;
00049 
00050     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) {
00051         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00052         memset(txPayload, 0, TXRX_BUF_LEN);
00053         memcpy(txPayload, buf, TXRX_BUF_LEN);
00054 
00055         //for(index=0; index<bytesRead; index++)
00056         //pc.putc(buf[index]);
00057 
00058         if(buf[0] == ONOFF_HEADER) {
00059             //pc.printf("LED value received \r\n");
00060             if(buf[1] == LEDS_ON) {
00061                 LEDS_ON_FLAG = 1;
00062             } else if(buf[1] == LEDS_OFF) {
00063                 LEDS_ON_FLAG = 0;
00064                 neopixel_init(&m_strip, dig_pin_num, NUM_LEDS);
00065                 neopixel_clear(&m_strip);
00066             }
00067         } else if(buf[0] == COLOR_HEADER) {
00068             red = buf[1];
00069             green = buf[2];
00070             blue = buf[3];
00071             pattern = buf[5];
00072         } else if(buf[0] == PIEZO_HEADER) {
00073             mPiezoOn = buf[1];
00074             //mPiezoDur = buf[2];
00075             mPiezoPattern = buf[2];
00076         } else if(buf[0] == HAPTIC_HEADER) {
00077             mHapticOn = buf[1];
00078             mHapticDur = buf[2];
00079             mHapticPattern = buf[3];
00080         } else if(buf[0] == ALARM_HEADER) {
00081             mAlarmOn = buf[1];
00082             //mAlarmStart = buf[2];
00083             mAlarmDur = buf[2];
00084             mAlarmPiezoOn = buf[4];
00085             mAlarmTone = buf[5];
00086             mAlarmPattern = buf[6];
00087             pattern = mAlarmPattern;
00088             if (mAlarmOn == ALARM_ON) {
00089                 // Setup the timer
00090                 t_alarm.start();
00091                 mBeginAlarm = t_alarm.read();
00092                 mEndAlarm = mBeginAlarm + mAlarmDur;
00093                 mPiezoOn = mAlarmPiezoOn;
00094                 LEDS_ON_FLAG = 1; 
00095             } else if (mAlarmOn == ALARM_OFF) {
00096                 // Stop the timer
00097                 t_alarm.stop();
00098                 // Kill all the effects
00099                 mHapticOn = HAPTIC_OFF;
00100                 mPiezoOn = PIEZO_OFF;
00101                 LEDS_ON_FLAG = 0;
00102             }
00103         }   
00104         else
00105         {
00106             LEDS_ON_FLAG = 1;
00107             red = buf[1];
00108             green = buf[2];
00109             blue = buf[3];
00110             pattern = buf[5];
00111         }
00112     }
00113 #endif
00114 }
00115 
00116 // pb Interrupt routine - is interrupt activated by a falling edge of pb input
00117 void pb_hit_interrupt (void) {
00118     pattern++;
00119     if (pattern >= PATTERN_MEM)
00120         pattern = PATTERN_ON;
00121 }
00122 
00123 void m_status_check_handle(void)
00124 {
00125     //uint8_t buf[3];
00126     
00127     //if (BUTTON == 1) {
00128         // Cycle through display patterns
00129         //pattern++;
00130         //if (pattern >= PATTERN_MEM)
00131            // pattern = PATTERN_NONE;
00132     //}
00133 }
00134 
00135 void updateLEDs()
00136 {
00137 #ifdef VIQ    
00138         if (mAlarmOn == ALARM_ON) {
00139             if (t_alarm.read() > mEndAlarm) {
00140                 // end alarm 
00141                 t_alarm.stop();
00142                 mHapticOn = HAPTIC_OFF;
00143                 mPiezoOn = PIEZO_OFF;
00144                 LEDS_ON_FLAG = 0;
00145             }
00146         } 
00147         if (mHapticOn == HAPTIC_ON) {
00148             BUZZER = 1;
00149             wait_ms(1000);
00150         } else {
00151             BUZZER = 0;
00152             //wait_ms(1000); 
00153         }
00154         if (mPiezoOn == PIEZO_ON) {
00155             for (int i = 0; i < (sizeof(freq_coll) / sizeof(int)); i++) {
00156                 // Keep checking for an "Off"
00157                 //if (mPiezoOn == PIEZO_OFF)
00158                     //break;
00159                 PIEZO.period(1.0 / freq_coll[i]);
00160                 PIEZO.write(0.9);
00161                 wait(1.0 / beat_coll[i]);
00162                 PIEZO.write(0);
00163                 wait(0.05);
00164             }
00165             wait_ms(500);
00166         } 
00167         if (LEDS_ON_FLAG == 1) {          
00168             if (pattern == PATTERN_ON) {
00169                 for (uint8_t i = 0; i <= NUM_LEDS; i++)
00170                     neopixel_set_color(&m_strip, i, red, green, blue);
00171                 neopixel_show(&m_strip);
00172                 
00173             } else if (pattern == PATTERN_FAST) {
00174                 //neopixel_clear(&m_strip);
00175                 for (uint8_t i = 0; i <= NUM_LEDS; i++)
00176                     neopixel_set_color(&m_strip, i, 0, 0, 0);
00177                 if (led_to_enable > NUM_LEDS)
00178                     led_to_enable = 0;
00179                 neopixel_set_color(&m_strip, led_to_enable, red, green, blue);
00180                 neopixel_show(&m_strip);
00181                 led_to_enable++;
00182             } else if (pattern == PATTERN_SLOW) {
00183                 for (uint8_t i = 0; i <= NUM_LEDS; i++)
00184                     neopixel_set_color(&m_strip, i, 0, 0, 0);
00185                 if (led_to_enable > NUM_LEDS)
00186                     led_to_enable = 0;
00187                 neopixel_set_color(&m_strip, led_to_enable, red, green, blue);
00188                 neopixel_show(&m_strip);
00189                 led_to_enable++;
00190                 wait_ms(1000);
00191                 
00192             } else if (pattern == PATTERN_CHASE) {
00193                 for (uint8_t i = 0; i <= NUM_LEDS; i++)
00194                     neopixel_set_color(&m_strip, i, 0, 0, 0);
00195                 if (led_to_enable > NUM_LEDS)
00196                     led_to_enable = 0;
00197                 for (uint8_t i = 0; i <= NUM_LEDS; i++)
00198                     neopixel_set_color(&m_strip, led_to_enable, red, green, blue);
00199                 neopixel_show(&m_strip);
00200                 led_to_enable++;
00201                 wait_ms(100);
00202                 
00203             } else if (pattern == PATTERN_MRB) {
00204                 //neopixel_clear(&m_strip);
00205                 for (uint8_t i = 0; i <= NUM_LEDS; i++)
00206                     neopixel_set_color(&m_strip, i, 0, 0, 0);
00207                 led_to_enable = rand()%NUM_LEDS;
00208                 neopixel_set_color(&m_strip, led_to_enable, red, green, blue);
00209                 neopixel_show(&m_strip);
00210             }
00211     
00212             else if (pattern == PATTERN_RAINBOW)
00213                 rainbowCycle(20, NUM_LEDS, m_strip);
00214     
00215             else if (pattern == PATTERN_CANDY)
00216                 candyChase(100, NUM_LEDS, m_strip);
00217     
00218             else if (pattern == PATTERN_SNOW)
00219                 snowflakes(250, NUM_LEDS, m_strip);
00220                 
00221             else if (pattern == PATTERN_ICE)
00222                 iceflakes(150, NUM_LEDS, m_strip);
00223                 
00224             else if (pattern == PATTERN_XMAS)
00225                 xmas(200, NUM_LEDS, m_strip);
00226                 
00227             else if (pattern == PATTERN_COL)
00228                 collegiate(100, NUM_LEDS, m_strip);
00229                 
00230             else if (pattern == PATTERN_IRON)
00231                 irondude(250, NUM_LEDS, m_strip);
00232                 
00233             else if (pattern == PATTERN_EASTER)
00234                 easter(250, NUM_LEDS, m_strip);
00235                 
00236             else if (pattern == PATTERN_SPRING)
00237                 spring(250, NUM_LEDS, m_strip);
00238                 
00239             else if (pattern == PATTERN_MEM)
00240                 memorial(250, NUM_LEDS, m_strip);
00241                 
00242             else
00243                 neopixel_clear(&m_strip);
00244         }
00245  #endif   
00246 }
00247 
00248 void periodicCallback(void)
00249 {
00250 #ifdef DK_52
00251     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00252 #endif
00253     if (BLE::Instance().getGapState().connected) {
00254         eventQueue.call(updateLEDs);
00255     }
00256 }
00257 
00258 void onBleInitError(BLE &ble, ble_error_t error)
00259 {
00260     (void)ble;
00261     (void)error;
00262    /* Initialization error handling should go here */
00263 }
00264 
00265 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00266 {
00267    
00268     BLE&        ble   = params->ble;
00269     ble_error_t error = params->error;
00270 
00271     if (error != BLE_ERROR_NONE) {
00272         onBleInitError(ble, error);
00273         return;
00274     }
00275 
00276     if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00277         return;
00278     }
00279 
00280     ble.gap().onDisconnection(disconnectionCallback);
00281     
00282     /* Setup primary (UART) service */
00283     ble.gattServer().addService(uartService); 
00284     
00285     /* Setup auxiliary service. */
00286     deviceInfo = new DeviceInformationService(ble, "RRVA", "VectorIQ", "SN1", "hw-rev1", "fw-rev1-1-0", "soft-rev1"); 
00287     //deviceInfo = new DeviceInformationService(ble, "RRVA", "VIQ", "", "", "1-0-0", ""); 
00288      
00289     // setup advertising
00290     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); // | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00291     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, 
00292                                      (uint8_t *)uuid16_list, sizeof(uuid16_list));
00293     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00294                                      (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
00295     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00296                                      (const uint8_t *)&mDeviceName, mLength);
00297     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00298     ble.gap().setAdvertisingInterval(500); /* ms */
00299     ble.gap().startAdvertising();
00300     //ble.startAdvertising();
00301     //pc.printf("Advertising Start \r\n");
00302 }
00303 
00304 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00305     BLE &ble = BLE::Instance();
00306     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00307 }
00308 
00309 int main(void)
00310 {    
00311     set_time(1256729737  );  // Set RTC time to Wed, 28 Oct 2009 11:35:37
00312     
00313     strcpy(mDeviceName, "VIQ");
00314     mLength = strlen(mDeviceName);
00315 
00316     //pc.printf("neoPixel Init \r\n");
00317 #ifdef VIQ
00318     neopixel_init(&m_strip, dig_pin_num, NUM_LEDS);
00319     neopixel_clear(&m_strip);
00320 #endif    
00321     eventQueue.call_every(500, periodicCallback);
00322 
00323     BLE &ble = BLE::Instance();
00324     ble.onEventsToProcess(scheduleBleEventsProcessing);
00325     ble.init(bleInitComplete);
00326 
00327     eventQueue.dispatch_forever();
00328 }