Frank Duignan / Mbed 2 deprecated CJMCU-8223-Oled

Dependencies:   mbed BLE_API nRF51822

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 "mbed.h"
00018 #include "ble/BLE.h"
00019 #include "LEDService.h"
00020 #include "accelService.h"
00021 #include "oled.h"
00022 #define enable_interrupts() asm(" cpsie i ")
00023 #define disable_interrupts() asm(" cpsid i ")
00024 
00025 
00026 DigitalOut myled(P0_29);
00027 
00028 const static char     DEVICE_NAME[] = "CJMCU-8223";
00029 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID,accelService::ACCEL_SERVICE_UUID};
00030 accelService *AccelServicePtr;
00031 LEDService *ledServicePtr;
00032 
00033 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00034 {
00035     BLE::Instance().gap().startAdvertising();
00036 }
00037 
00038 /**
00039  * This callback allows the LEDService to receive updates to the ledState Characteristic.
00040  *
00041  * @param[in] params
00042  *     Information about the characterisitc being updated.
00043  */
00044 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
00045     if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
00046         myled = *(params->data);
00047     }
00048 }
00049 void onDataReadCallback(const GattReadCallbackParams *params) {
00050     
00051 }
00052 /**
00053  * This function is called when the ble initialization process has failed
00054  */
00055 void onBleInitError(BLE &ble, ble_error_t error)
00056 {
00057     /* Initialization error handling should go here */
00058 }
00059 
00060 /**
00061  * Callback triggered when the ble initialization process has finished
00062  */
00063 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00064 {
00065     BLE&        ble   = params->ble;
00066     ble_error_t error = params->error;
00067 
00068     if (error != BLE_ERROR_NONE) {
00069         /* In case of error, forward the error handling to onBleInitError */
00070         onBleInitError(ble, error);
00071         return;
00072     }
00073 
00074     /* Ensure that it is the default instance of BLE */
00075     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00076         return;
00077     }
00078  
00079     ble.gap().onDisconnection(disconnectionCallback);
00080     ble.gattServer().onDataWritten(onDataWrittenCallback);
00081     ble.gattServer().onDataRead(onDataReadCallback);
00082     bool initialValueForLEDCharacteristic = false;
00083     ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
00084     int16_t initialValue = 0;
00085     AccelServicePtr = new accelService(ble,initialValue);
00086 
00087     /* setup advertising */
00088     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00089     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00090     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00091     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00092     ble.gap().setAdvertisingInterval(100); /* 100ms. */
00093     ble.gap().startAdvertising();
00094 }
00095 oled Oled;
00096 int main(void)
00097 {
00098  
00099     int Count=0;
00100     Oled.begin();
00101     Lis3dh.begin();
00102     Oled.print(0,0  ,"Hello");
00103     
00104 
00105     BLE &ble = BLE::Instance();
00106     ble.init(bleInitComplete);
00107     
00108     while (ble.hasInitialized()  == false) { 
00109         Oled.print(0,0,"BLE init fail");
00110         myled = 1;
00111         wait(0.2);
00112         myled = 0;
00113         wait(0.1);
00114     
00115     /* spin loop */ }
00116     Oled.print(0,0,"BLE init done");
00117     while (true) {
00118         // update the values for the BLE interface
00119         AccelServicePtr->poll();
00120         int x,y,z;
00121         x = 1;
00122         y = 2;
00123         z = 3;        
00124         // show the values on the display
00125         if (Lis3dh.dataReady())        
00126         {
00127             Lis3dh.read(x,y,z);
00128             Oled.print(0,0,x/16);
00129             Oled.print(0,1,y/16);
00130             Oled.print(0,2,z/16);
00131         }
00132         ble.waitForEvent();
00133         
00134     }
00135 }