USED IMAGE2GLCD

Dependencies:   BLE_API SharpLCD_LucidaFont mbed nRF51822

Fork of Renard_UI_Only by Demo Team

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