Demo Team / Mbed 2 deprecated Renard_YO

Dependencies:   BLE_API SharpLCD_LucidaFont mbed nRF51822

Fork of Renard_YO by Andrea Corrado

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 "SharpLCD.hpp"
00019 #include "BLEDevice.h"
00020 #include "icon.h"
00021 //#include "font.h"
00022 #include "dotfont.h"
00023 
00024 Serial pc (USBTX,USBRX);
00025 
00026 DigitalOut led1(P0_12);
00027 DigitalOut motor(P0_1);
00028 DigitalOut screen(P0_21);
00029 DigitalIn  button(P0_16);
00030 //a50,a60,a70,a80,a90,a100,a110,a120,a130,a140,a150,a160,a170
00031 
00032 char strn[50]; // incoming tx data
00033 char user[50]; // username to display
00034 uint8_t* rings[2] = {a0,a10};
00035 int set=0;
00036 int set2=0;
00037 int pairing =1;
00038 int anim=0;
00039 int elip=0;
00040 
00041 int timeUpdate = 1;
00042 time_t unixTime;
00043 char timeStr[50]; // time to display
00044 char dayStr[50]; // day to display
00045 char dateStr[50]; // date to display
00046 Timer mClock;
00047 int clockOffset = 0;
00048 int lastms = 0;
00049 
00050 //SharpLCD(PinName enable, PinName cs, PinName mosi, PinName miso_unused, PinName sclk, PinName _unused = NC)
00051 SharpLCD lcd(P0_0, P0_24, P0_20, P0_22, P0_25, P0_27);
00052 //SharpLCD lcd(P0_25, P0_24, P0_23, P0_13, P0_22, P0_27);
00053 uint8_t framebuffer[SharpLCD::SIZEOF_FRAMEBUFFER_FOR_ALLOC];
00054 SharpLCD::FrameBuffer fb(framebuffer);
00055 BLEDevice ble;
00056 //UARTService uart(ble);
00057 
00058 // The Nordic UART Service
00059 static const uint8_t uart_base_uuid[] = {0x6e, 0x40, 0x00, 0x01, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
00060 static const uint8_t uart_tx_uuid[]   = {0x6e, 0x40, 0x00, 0x02, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
00061 static const uint8_t uart_rx_uuid[]   = {0x6e, 0x40, 0x00, 0x03, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
00062 static const uint8_t uart_base_uuid_rev[] = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e};
00063 uint8_t txPayload[50] = {0};
00064 uint8_t rxPayload[50] = {0};
00065 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, 50,
00066                                       GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00067 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, 50,
00068                                       GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
00069 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
00070 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
00071 
00072 const FONT_INFO* mainFont;
00073 const FONT_INFO* exFont;
00074 const FONT_INFO* subFont;
00075 
00076 void screenSetup()
00077 {
00078     screen=1;
00079     wait_ms(200);
00080     lcd.enableDisplay();
00081     fb.clear();
00082     lcd.clear();
00083 
00084     mainFont = searchFontFace("Square Head Black", 20);
00085     subFont = searchFontFace("Century Gothic Black", 9);
00086     exFont = searchFontFace("Lucida", 8);
00087 }
00088 
00089 void showPairing()
00090 {
00091     fb.clear();
00092     fb.bitBlit(sScreen, 96, 96, 0, 0);
00093     fb.bitBlit(rings[anim], 32, 32, 2, 57); 
00094     char* dots[4] = {"", "..", "...", "..."};
00095     char str[15] = "Pairing";
00096     strcat(str, dots[elip]);
00097     
00098     fb.printString(exFont, 38, 80, str);
00099     
00100     lcd.drawFrameBuffer(fb);
00101     elip++;
00102     anim++;
00103     wait(0.15);
00104     
00105     if (anim>=2) {
00106         anim = 0;   
00107     }
00108     
00109     if (elip>=4) {
00110         elip = 0;   
00111     }
00112 }
00113 
00114 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00115 {
00116     ble.startAdvertising(); // restart advertising
00117 }
00118 
00119 void onDataWritten(const GattCharacteristicWriteCBParams *params)
00120 {
00121     uint16_t txHandle = txCharacteristic.getValueAttribute().getHandle();
00122     uint16_t bytesRead;
00123     
00124     if (params->charHandle == txHandle) {
00125         memset(strn, 0, sizeof(strn));
00126         ble.readCharacteristicValue(txHandle, txPayload, &bytesRead);
00127         strncpy(strn,(const char*)txPayload,bytesRead);
00128         set=1;
00129         pc.printf ("\n\r Payload length: %d", bytesRead);
00130         /*pc.printf("\n\r Payload: ");
00131         for (int i=0;i<10;i++) {
00132             pc.printf("%c", (char)txPayload[i]);
00133         }*/
00134         pc.printf ("\n\r Received: %s", strn);
00135     }
00136 }
00137 
00138 void bleSetup()
00139 {
00140     //Init ble
00141     ble.init();
00142     //and handlers...
00143     ble.onDisconnection((Gap::DisconnectionEventCallback_t)&disconnectionCallback);
00144     ble.onDataWritten(onDataWritten);
00145     // setup advertising
00146     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00147     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00148     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00149                                      (const uint8_t *)"YO!", sizeof("YO!") - 1);
00150     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00151                                      (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
00152     ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms.
00153     ble.startAdvertising();
00154     ble.addService(uartService);
00155 }
00156 
00157 void setTime(int newTime) {
00158     unixTime = newTime;
00159     struct tm* timeinfo = localtime (&unixTime);
00160     strftime (timeStr, 20, "%R", timeinfo);
00161     strftime (dayStr, 20, "%a", timeinfo);
00162     strftime (dateStr, 20, "%b %e", timeinfo);
00163 }
00164 
00165 // Main LCD display function, add all framebuffer updates here ONLY
00166 void updateLCD() {
00167     fb.clear();
00168     fb.bitBlit(img, 96, 96, 0,0);
00169     
00170     fb.bitBlit(battery, 16, 16, 75, 0);
00171     
00172     fb.bitBlit(BLE, 16, 16, 0, 0);
00173 
00174     fb.printString(mainFont, 60, 65, user);
00175     
00176     fb.printString(mainFont, 5, 61, timeStr);
00177                    
00178     fb.printString(subFont, 14, 78, dayStr);
00179     
00180     fb.printString(subFont, 43, 78, dateStr);
00181                    
00182     lcd.drawFrameBuffer(fb);
00183 }
00184 
00185 int main(void) {
00186 
00187     screenSetup();
00188     bleSetup();
00189     
00190     led1 = 0;
00191     set2 = 0;
00192 
00193     uint16_t rxHandle = rxCharacteristic.getValueAttribute().getHandle();
00194     
00195     // Initial time update
00196     pc.printf("\n\r Updating time");
00197     char cmd[5] = "time";
00198     ble.updateCharacteristicValue(rxHandle, (const uint8_t*)cmd, 4);
00199 
00200     while (true) {
00201         
00202         if (pairing){
00203             showPairing();
00204         }
00205     
00206         if(set){
00207             if (timeUpdate) {
00208                 setTime(atoi(strn));
00209                 pc.printf("\n\r New time: %s", timeStr);
00210                 timeUpdate = 0;
00211                 pairing=0;
00212                 mClock.start();
00213             } else {
00214                 strncpy(user, strn, sizeof(strn));
00215             }
00216             set = 0;
00217         }
00218         
00219         if(!button && !set2){
00220             set2 = 1;
00221             uint16_t bytesToSend = strlen(user);
00222             memcpy(rxPayload, user, bytesToSend);
00223             ble.updateCharacteristicValue(rxHandle, rxPayload, bytesToSend);
00224             pc.printf ("\n\r Sending %s", user);
00225         } else if (button){
00226             set2 = 0;
00227         }
00228 
00229         clockOffset = mClock.read_ms();
00230         if (clockOffset >= 1000) {
00231             clockOffset += lastms;
00232             int addTime = unixTime + (clockOffset/1000);
00233             lastms = clockOffset%1000; // e.g. 2564 - 2000 = 564
00234             mClock.reset();
00235             setTime(addTime);
00236             
00237             // do all the framebuffer updating here every second
00238             updateLCD();
00239         }
00240     }
00241 }