Pulsating LEDs

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_BADGE by David Adkins

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <string.h>
00018 #include "mbed.h"
00019 #include "BLE.h"
00020 
00021 #include "UARTService.h"
00022 
00023 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
00024                                * it will have an impact on code-size and power consumption. */
00025                                
00026 #define TXRX_BUF_LEN                     20
00027 
00028 // sinetable is loaded with a progressive tick time
00029 // designed to continuesly vary the PWM period
00030 // for a 5 secound time frame.
00031 extern float sinetable[256];
00032                                
00033 // LED defines and variables
00034 #define GREEN_LED_PIN                   P0_7
00035 #define RED_LED_PIN                     P0_3
00036 
00037 #define LED_ON                          0x00
00038 #define LED_OFF                         0x01
00039 #define MAX_LED_LEVEL                   0x0A
00040 
00041 // Command set
00042 #define CMD_GREEN                       0x01
00043 #define CMD_RED                         0x02
00044 #define CMD_OFF                         0x03
00045 
00046 #define TICK_FRAME          0.02
00047 
00048 DigitalOut      LED_SET_GREEN(GREEN_LED_PIN);
00049 DigitalOut      LED_SET_RED(RED_LED_PIN);
00050 Ticker led_pwm_tick;    // for LED PWM
00051 
00052 uint8_t next_angle = 0;         // Counter for 5 secound pulsating
00053 float next_tick;                // Current PWM pulse width
00054 uint8_t led_brightness = 0;     // Used to scale the PWM period
00055                                 // 0 is off, 10 is always on
00056 
00057 #if NEED_CONSOLE_OUTPUT
00058 #define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); }
00059 #else
00060 #define DEBUG(...) /* nothing */
00061 #endif /* #if NEED_CONSOLE_OUTPUT */
00062 
00063 BLEDevice  ble;
00064 UARTService *uart;
00065 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
00066 //static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
00067 
00068 //GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00069 
00070 // Local prototypes
00071 void green_pwm_tick_off(void);
00072 void green_pwm_tick_on(void);
00073 void red_pwm_tick_off(void);
00074 void red_pwm_tick_on(void);
00075 void pwm_tick_min(void);
00076 
00077 
00078 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00079 {
00080     //DEBUG("Disconnected!\n\r");
00081     //DEBUG("Restarting the advertising process\n\r");
00082     ble.startAdvertising();
00083 }
00084 
00085 void WrittenHandler(const GattWriteCallbackParams *Handler)
00086 {   
00087     uint8_t buf[TXRX_BUF_LEN];
00088     //uint16_t bytesRead;
00089     //uint16_t index;
00090     //if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
00091     {
00092         //ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
00093         //memset(txPayload, 0, TXRX_BUF_LEN);
00094         //memcpy(txPayload, buf, TXRX_BUF_LEN);  
00095         buf[0] = uart->_getc();
00096         
00097         
00098         if(CMD_GREEN == buf[0])
00099         {
00100             led_brightness = uart->_getc();
00101             
00102             if(led_brightness > MAX_LED_LEVEL)
00103             {
00104                 led_brightness = MAX_LED_LEVEL;
00105             }
00106             
00107             if(MAX_LED_LEVEL == led_brightness)
00108             {
00109                 LED_SET_GREEN = LED_ON;
00110                 led_pwm_tick.attach(pwm_tick_min, 5.0);
00111             }
00112             else if(0 == led_brightness)
00113             {
00114                 LED_SET_GREEN = LED_OFF;
00115                 led_pwm_tick.attach(pwm_tick_min, 5.0);
00116             }
00117             else
00118             {
00119                 led_pwm_tick.attach(green_pwm_tick_on, .001);
00120                 next_angle = 0;
00121             }
00122 
00123             LED_SET_RED = LED_OFF;
00124         }
00125         else if(CMD_RED == buf[0])
00126         {
00127             led_brightness = uart->_getc();
00128             
00129             if(led_brightness > MAX_LED_LEVEL)
00130             {
00131                 led_brightness = MAX_LED_LEVEL;
00132             }
00133             
00134             if(MAX_LED_LEVEL == led_brightness)
00135             {
00136                 LED_SET_RED = LED_ON;
00137                 led_pwm_tick.attach(pwm_tick_min, 5.0);
00138             }
00139             else if(0 == led_brightness)
00140             {
00141                 LED_SET_RED = LED_OFF;
00142                 led_pwm_tick.attach(pwm_tick_min, 5.0);
00143            }
00144             else
00145             {
00146                 LED_SET_RED = LED_ON;
00147                 led_pwm_tick.attach(red_pwm_tick_on, .001);
00148             }
00149 
00150             LED_SET_GREEN = LED_OFF;
00151         }
00152         else if(CMD_OFF == buf[0])
00153         {
00154             LED_SET_GREEN = LED_OFF;
00155             LED_SET_RED = LED_OFF;
00156             led_pwm_tick.attach(pwm_tick_min, 5.0);
00157         }
00158     }
00159 }
00160 
00161 void pwm_tick_min()
00162 {
00163     // do nothing in this tick while LED's are off
00164     // 5 second tick to allow processor max sleep time
00165 }
00166 
00167 void green_pwm_tick_on(void)
00168 {
00169     next_tick = sinetable[next_angle] * ((float)led_brightness / (MAX_LED_LEVEL - 1));
00170     
00171     LED_SET_GREEN = LED_ON;
00172     led_pwm_tick.attach(green_pwm_tick_off, next_tick);     
00173 }
00174 
00175 void green_pwm_tick_off()
00176 {
00177     next_tick = TICK_FRAME - next_tick;
00178     
00179     LED_SET_GREEN = LED_OFF;
00180     led_pwm_tick.attach(green_pwm_tick_on, next_tick);
00181     
00182     ++next_angle;    
00183 }
00184 
00185 void red_pwm_tick_on(void)
00186 {
00187     next_tick = sinetable[next_angle] * ((float)led_brightness / (MAX_LED_LEVEL - 1));
00188     
00189     LED_SET_RED = LED_ON;
00190     led_pwm_tick.attach(red_pwm_tick_off, next_tick);     
00191 }
00192 
00193 void red_pwm_tick_off()
00194 {
00195     next_tick = TICK_FRAME - next_tick;
00196     
00197     LED_SET_RED = LED_OFF;
00198     led_pwm_tick.attach(red_pwm_tick_on, next_tick);
00199     
00200     ++next_angle;    
00201 }
00202 
00203 int main(void)
00204 {
00205     LED_SET_GREEN = LED_OFF;
00206     LED_SET_RED = LED_OFF;
00207     
00208     ble.init();
00209     ble.onDisconnection(disconnectionCallback);
00210     ble.onDataWritten(WrittenHandler);
00211 
00212     //led_brightness = 5;
00213     led_pwm_tick.attach(pwm_tick_min, 5.0);  // 
00214     
00215     uart = new UARTService(ble);
00216 
00217     /* setup advertising */
00218     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00219     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00220     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00221                                      (const uint8_t *)"Smart Badge", sizeof("Smart Badge") - 1);
00222     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00223                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00224 
00225     ble.gap().setAdvertisingInterval(2000); /* in multiples of 1 ms. */
00226     ble.gap().setTxPower(-40);
00227 
00228     ble.gap().startAdvertising();
00229 
00230     while (true)
00231     {
00232         ble.waitForEvent();
00233     }
00234 }