USED IMAGE2GLCD

Dependencies:   BLE_API SharpLCD_LucidaFont mbed nRF51822

Fork of Renard_YO by Andrea Corrado

Revision:
6:9ea3943e9e24
Parent:
5:ffa456512437
Child:
7:8712aa56ee92
--- a/main.cpp	Thu Mar 05 14:03:28 2015 +0000
+++ b/main.cpp	Mon Apr 20 13:51:58 2015 +0000
@@ -1,342 +1,150 @@
-#include "mbed.h"
-#include "DebouncedInterrupt.h"
-#include "font.h"
-#include "SharpLCD.hpp"
-#include "icon.h"
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
-//I/O Initialisation
-DigitalOut myled(LED1);
-DigitalOut motor(P0_23);
-#define BUTTON_INTERRUPT_DEBOUNCE_TIME_MS 150
-DebouncedInterrupt buttonOne (P0_16);
-DebouncedInterrupt buttonTwo (P0_17);
+#include "mbed.h"
+#include "SharpLCD.hpp"
+#include "BLEDevice.h"
+#include "icon.h"
+#include "font.h"
+
+Serial pc (USBTX,USBRX);
+
 
+DigitalOut led1(P0_12);
+DigitalOut motor(P0_1);
+DigitalOut screen(P0_21);
+DigitalIn  button(P0_16);
+char primary_text[50];
+char strn[50];
+int set=0;
+int set2=0;
+uint16_t bytesRead;
+
+
+
+//SharpLCD(PinName enable, PinName cs, PinName mosi, PinName miso_unused, PinName sclk, PinName _unused = NC)
 SharpLCD lcd(P0_0, P0_24, P0_20, P0_22, P0_25, P0_27);
-
+//SharpLCD lcd(P0_25, P0_24, P0_23, P0_13, P0_22, P0_27);
 uint8_t framebuffer[SharpLCD::SIZEOF_FRAMEBUFFER_FOR_ALLOC];
 SharpLCD::FrameBuffer fb(framebuffer);
-
-//Notification Data struct
-#define NOTIFICATION_TEXT_MAX_LENGTH 20
-#define NOTIFICATION_TYPE_ALARM    0x01
-#define NOTIFICATION_TYPE_CALL     0x02
-#define NOTIFICATION_TYPE_SMS      0x03
-#define NOTIFICATION_TYPE_EMAIL    0x04
-#define NOTIFICATION_TYPE_EVENT    0x05
-#define NOTIFICATION_STATE_DELETED 0x00
-#define NOTIFICATION_STATE_UNREAD  0x01
-#define NOTIFICATION_STATE_READ    0x02
-struct Notification{
-    uint8_t type;
-    uint8_t state;
-    char primary_text[NOTIFICATION_TEXT_MAX_LENGTH];
-    char secondary_text[NOTIFICATION_TEXT_MAX_LENGTH];
-    };
-
-//notification storage
-#define MAX_NOTIFICATIONS 20
-Notification notifications[MAX_NOTIFICATIONS];
-uint8_t notification_count = 0;
-int visible_notification_index = -1;
-
-//States
-uint8_t needs_display_update = 0;
-uint8_t has_unread = 0;
-
-//For LED
-#define LED_BLINK_TIME_MS 500
-uint8_t led_on = 0;
-Timer led_timer;
-
-//For Buzzer
-#define BUZZ_TIME_MS 200
-#define MAX_BUZZ_COUNT 3
-uint8_t buzz_count = MAX_BUZZ_COUNT;
-Timer buzz_timer;
-
-void setType(Notification* note, uint8_t type) {
-    note->type = type;
-}
-
-uint8_t isCall(Notification note) {
-    return note.type == NOTIFICATION_TYPE_CALL;
-}
-
-uint8_t isSMS(Notification note) {
-    return note.type == NOTIFICATION_TYPE_SMS;
-}
-
-uint8_t isEmail(Notification note) {
-    return note.type == NOTIFICATION_TYPE_EMAIL;
-}
-
-uint8_t isEvent(Notification note) {
-    return note.type == NOTIFICATION_TYPE_EVENT;
-}
-
-void setState(Notification* note, uint8_t state) {
-    note->state = state;
-}
-
-uint8_t isDeleted(Notification note) {
-    return note.state == NOTIFICATION_STATE_DELETED;
-}
-
-uint8_t isUnread(Notification note) {
-    return note.state == NOTIFICATION_STATE_UNREAD;
-}
+BLEDevice  ble;
+//UARTService uart(ble);
 
-uint8_t isRead(Notification note) {
-    return note.state == NOTIFICATION_STATE_READ;
-}
+// The Nordic UART Service
+static const uint8_t uart_base_uuid[] = {0x6e, 0x40, 0x00, 0x01, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
+static const uint8_t uart_tx_uuid[]   = {0x6e, 0x40, 0x00, 0x02, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
+static const uint8_t uart_rx_uuid[]   = {0x6e, 0x40, 0x00, 0x03, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
+static const uint8_t uart_base_uuid_rev[] = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e};
+uint8_t txPayload[50] = {0};
+uint8_t rxPayload[50] = {0};
+GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, 50,
+                                      GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
+GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, 50,
+                                      GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
+GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
 
-//Layout Coordinates
-#define TYPE_ICON_X         0 //5
-#define TYPE_ICON_Y         0 //10
-#define UNREAD_ICON_X       60 //96
-#define UNREAD_ICON_Y       60 //14
-#define PRIMARY_TEXT_X      10 //5
-#define PRIMARY_TEXT_Y      30 //64
-#define SECONDARY_TEXT_X    10 //5
-#define SECONDARY_TEXT_Y    50 //82
-//Update Display to show current notification...
 
-//Serial  pc(USBTX, USBRX);
-
-void doDisplayUpdate() {
-    //pc.printf("display update"); //Debug
+void screenSetup()
+{
+    screen=1;
+    wait_ms(200);
+    lcd.enableDisplay();
+    fb.clear();
     lcd.clear();
-    if(visible_notification_index >= 0) {
-        //Write current notification...        
-        //Draw type icon...
-        switch(notifications[visible_notification_index].type) {
-            case NOTIFICATION_TYPE_ALARM:
-                fb.bitBlit(Alarm, 96, 96, TYPE_ICON_X, TYPE_ICON_Y);
-                break;
-                
-            case NOTIFICATION_TYPE_CALL:
-                fb.bitBlit(Call, 32, 32, TYPE_ICON_X, TYPE_ICON_Y);
-                break;
-                
-            case NOTIFICATION_TYPE_SMS:
-                fb.bitBlit(SMS, 32, 32, TYPE_ICON_X, TYPE_ICON_Y);
-                break;
-                
-            case NOTIFICATION_TYPE_EMAIL:
-                fb.bitBlit(Email, 32, 32, TYPE_ICON_X, TYPE_ICON_Y);
-                break;
-                
-            case NOTIFICATION_TYPE_EVENT:
-                fb.bitBlit(Calendar, 32, 32, TYPE_ICON_X, TYPE_ICON_Y);
-                break;
-        }
-                
-        //Unread notification
-        if(isUnread(notifications[visible_notification_index])){
-//            fb.bitBlit(Unread, 16, 24, UNREAD_ICON_X, UNREAD_ICON_Y);
-        }
-        //Text fields...
-        fb.printString(lookupFontFace("Lucida 8pt", 8), PRIMARY_TEXT_X, PRIMARY_TEXT_Y, BLACK, notifications[visible_notification_index].primary_text);
-        fb.printString(lookupFontFace("Lucida 8pt", 8), SECONDARY_TEXT_X, SECONDARY_TEXT_Y, BLACK, notifications[visible_notification_index].secondary_text);
-    } else {
-        //no notifications...
-        //Write current notification...
-        //fb.printString(lookupFontFace("Lucida 8pt", 8), PRIMARY_TEXT_X, PRIMARY_TEXT_Y, BLACK, "No Notifications");
-    }
-    //epaper.write_disp();
-    lcd.drawFrameBuffer(fb);
-    needs_display_update = 0;
-}
-
-//Request a display update..
-void requestDisplayUpdate() {
-    needs_display_update = 1;
-}
-
-//Starts buzz pattern...
-void startBuzz() {
-    //Buzz
-    buzz_count = 0;
-    buzz_timer.reset();
-    buzz_timer.start();
 }
 
 
-//Add Notification...
-int addNotification(Notification note) {
-    //Find insertion point...
-    uint8_t index = 0;
-    for(index = 0; index < MAX_NOTIFICATIONS; index++) {
-        if(isDeleted(notifications[index])) {
-            //Here...
-            break;
-        }
-    }
-    //If here, didn't find insertion point... wrap to beginning.
-    if(index >= MAX_NOTIFICATIONS) index = 0;
-    notifications[index] = note;
-    //Set buzzer
-    startBuzz();
-    //Set unread
-    if(isUnread(note)) has_unread = 1;
-    return index;
-}
 
-void deleteNotification(int index) {
-    setState(&notifications[index], NOTIFICATION_STATE_DELETED);
-    //Shift array elements left...
-    int i = 0;
-    for (i = index + 1; i < MAX_NOTIFICATIONS; i++) {
-        notifications[i - 1] = notifications[i];
-    }
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+{
+    ble.startAdvertising(); // restart advertising
 }
 
-void checkUnread(){
-    uint8_t i = 0;
-    led_timer.stop();
-    has_unread = 0;
-    for(i = 0; i < MAX_NOTIFICATIONS; i++) {
-        if(isUnread(notifications[i])) {
-            has_unread = 1;
-            led_timer.reset();
-            led_timer.start();
-            break;
-        }
+void onDataWritten(const GattCharacteristicWriteCBParams *params)
+{
+    uint16_t txHandle = txCharacteristic.getValueAttribute().getHandle();
+
+
+    if (params->charHandle == txHandle) {
+        memset(strn, 0, sizeof(strn));
+        ble.readCharacteristicValue(txHandle, txPayload, &bytesRead);
+        strncpy(strn,(const char*)txPayload,bytesRead);
+        set=1;
+        pc.printf ("\n\r Recieved: %s", strn);
     }
+
 }
 
-//Button One Handler
-void buttonOnePressed(){
-    if(visible_notification_index >= 0) {
-        //Increment index, wrap to beginning if last.
-        visible_notification_index++;
-        if(visible_notification_index >= MAX_NOTIFICATIONS || isDeleted(notifications[visible_notification_index])) {
-            visible_notification_index = 0;
-            if(isDeleted(notifications[visible_notification_index])) {
-                //Still deleted... none...
-                visible_notification_index = -1;
-            }
-        }
-        //Trigger display update...
-        requestDisplayUpdate();
-    }
+void bleSetup()
+{
+    //Init ble
+    ble.init();
+    //and handlers...
+    ble.onDisconnection((Gap::DisconnectionEventCallback_t)&disconnectionCallback);
+    ble.onDataWritten(onDataWritten);
+    // setup advertising
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                     (const uint8_t *)"YO!", sizeof("YO2!") - 1);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
+    ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms.
+    ble.startAdvertising();
+    ble.addService(uartService);
 }
 
-//Button Two handler
-void buttonTwoPressed(){
-    if(visible_notification_index >= 0) {
-        if(!isDeleted(notifications[visible_notification_index])) {
-            //Exists.
-            if(isUnread(notifications[visible_notification_index])) {
-                //Toggle to 'read'
-                setState(&notifications[visible_notification_index], NOTIFICATION_STATE_READ);
-                checkUnread();
-            } else if (isRead(notifications[visible_notification_index])) {
-                //Already 'read'... delete, this also shifts remaining notifications down...
-                deleteNotification(visible_notification_index);
-                //If current is deleted...
-                if(isDeleted(notifications[visible_notification_index])) {
-                    //We're at end, so wrap...
-                    visible_notification_index = 0;
-                    if(isDeleted(notifications[visible_notification_index])) {
-                        //Still deleted... so there are none...
-                        visible_notification_index = -1;
-                    }
-                }
-                //Otherwise, we've got one, so we should be ok...
-            }
-            
-            //Trigger display update...
-            requestDisplayUpdate();
-        }
-    }
-}
+
+
 
-//Initialise notification data...
-void initNotificationData() {
-    //For debug purposes...
-    
-    notifications[0].type = NOTIFICATION_TYPE_ALARM;
-    notifications[0].state = NOTIFICATION_STATE_UNREAD;
-    //strcpy(notifications[0].primary_text,"Wake Up");
-    //strcpy(notifications[0].secondary_text, "07:00");
-//    strcpy(notifications[0].primary_text,"Test");
-//    strcpy(notifications[0].secondary_text, "1");
-//    
-    notifications[1].type = NOTIFICATION_TYPE_CALL;
-    notifications[1].state = NOTIFICATION_STATE_UNREAD;
-    //strcpy(notifications[1].primary_text,"Eric Gowland");
-    //strcpy(notifications[1].secondary_text, "07770909177");
-    strcpy(notifications[1].primary_text,"Test");
-    strcpy(notifications[1].secondary_text, "2");
-    
-    notifications[2].type = NOTIFICATION_TYPE_SMS;
-    notifications[2].state = NOTIFICATION_STATE_UNREAD;
-    //strcpy(notifications[2].primary_text,"Hi, txt me...");
-    //strcpy(notifications[2].secondary_text, "07770909177");
-    strcpy(notifications[2].primary_text,"Test");
-    strcpy(notifications[2].secondary_text, "3");
+int main(void)
+{   time_t seconds = time(NULL);
+    screenSetup();
+    bleSetup();
+    set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
+    pc.printf("Time as a basic string = %s", ctime(&seconds));
     
-    notifications[3].type = NOTIFICATION_TYPE_EMAIL;
-    notifications[3].state = NOTIFICATION_STATE_UNREAD;
-    //strcpy(notifications[3].primary_text, "Dear Sir I have $US");
-    //strcpy(notifications[3].secondary_text, "not@scam.net");
-    strcpy(notifications[3].primary_text, "Test");
-    strcpy(notifications[3].secondary_text, "4");
-    
-    notifications[4].type = NOTIFICATION_TYPE_EVENT;
-    notifications[4].state = NOTIFICATION_STATE_UNREAD;
-    //strcpy(notifications[4].primary_text,"Review Meeting");
-    //strcpy(notifications[4].secondary_text, "10:00 - 10:30");
-    strcpy(notifications[4].primary_text,"Test");
-    strcpy(notifications[4].secondary_text, "5");
-    
-    //Set location, etc.
-    visible_notification_index = 0;
-    checkUnread();
-    requestDisplayUpdate();
-    startBuzz();
-}
+    led1 = 0;
+    set2=0;
+    uint16_t rxHandle = rxCharacteristic.getValueAttribute().getHandle();
+
+    while (true) {
+            
+    if(set){
+    fb.clear();
+    lcd.clear();
 
-//Main Program Function
-int main() {
-    lcd.enableDisplay();
-    //Init Data
-    initNotificationData();
-    //Attach interrupt handlers...
-//    buttonOne.attach(buttonOnePressed, IRQ_RISE, BUTTON_INTERRUPT_DEBOUNCE_TIME_MS);
-//    buttonTwo.attach(buttonTwoPressed, IRQ_RISE, BUTTON_INTERRUPT_DEBOUNCE_TIME_MS);
-    //Request display update...
-    requestDisplayUpdate();
-    while(1) {
-        if(needs_display_update){
-            doDisplayUpdate();
-        } else {
-            //If state hasn't changed, just action timers...
-            //Unread LED
-            if(has_unread) {
-                //LED flashing...
-                if(led_timer.read_ms() > LED_BLINK_TIME_MS) {
-                    myled = !myled;
-                    led_timer.reset();
-                }
-            } else {
-                myled = 0;
-                led_timer.stop();
-            }
-            
-            //Buzz
-            if(buzz_count < MAX_BUZZ_COUNT) {
-                //Buzzing...
-                if(buzz_timer.read_ms() > BUZZ_TIME_MS) {
-                    motor = !motor;
-                    if(!motor) buzz_count++;
-                    buzz_timer.reset();
-                }
-            } else {
-                motor = 0;
-                buzz_timer.stop();
-            }
-        }
+    fb.printString(lookupFontFace("Lucida 8pt", 8),
+                   20,
+                   40,
+                   BLACK,
+                   strn);
+    lcd.drawFrameBuffer(fb);
+    set=0;
     }
+    
+if(!button && !set2){
+  set2=1;
+  ble.updateCharacteristicValue(rxHandle, rxPayload, bytesRead);
+    ble.updateCharacteristicValue(rxHandle, (const uint8_t *) strn, bytesRead);
+    pc.printf ("\n\rSending %s", strn);
+    }
+    else if (button)
+    set2=0;
+ }
+
 }
\ No newline at end of file