USED IMAGE2GLCD

Dependencies:   BLE_API SharpLCD_LucidaFont mbed nRF51822

Fork of Renard_YO by Andrea Corrado

Revision:
7:8712aa56ee92
Parent:
6:9ea3943e9e24
Child:
8:bb11edbf5708
--- a/main.cpp	Mon Apr 20 13:51:58 2015 +0000
+++ b/main.cpp	Thu Apr 23 08:04:41 2015 +0000
@@ -22,25 +22,29 @@
 
 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];
+
+char strn[50]; // incoming tx data
+char user[50]; // username to display
 int set=0;
 int set2=0;
-uint16_t bytesRead;
 
-
+int timeUpdate = 1;
+time_t unixTime;
+char timeStr[50]; // time to display
+Timer mClock;
+int clockOffset = 0;
+int lastms = 0;
 
 //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);
-BLEDevice  ble;
+BLEDevice ble;
 //UARTService uart(ble);
 
 // The Nordic UART Service
@@ -53,7 +57,7 @@
 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::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
 
@@ -67,8 +71,6 @@
     lcd.clear();
 }
 
-
-
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
     ble.startAdvertising(); // restart advertising
@@ -77,16 +79,20 @@
 void onDataWritten(const GattCharacteristicWriteCBParams *params)
 {
     uint16_t txHandle = txCharacteristic.getValueAttribute().getHandle();
-
-
+    uint16_t bytesRead;
+    
     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);
+        pc.printf ("\n\r Payload length: %d", bytesRead);
+        /*pc.printf("\n\r Payload: ");
+        for (int i=0;i<10;i++) {
+            pc.printf("%c", (char)txPayload[i]);
+        }*/
+        pc.printf ("\n\r Received: %s", strn);
     }
-
 }
 
 void bleSetup()
@@ -100,7 +106,7 @@
     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);
+                                     (const uint8_t *)"YO!", sizeof("YO!") - 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.
@@ -108,43 +114,77 @@
     ble.addService(uartService);
 }
 
-
-
+void setTime(int newTime) {
+    unixTime = newTime;
+    struct tm* timeinfo = localtime (&unixTime);
+    strftime (timeStr, 20, "%I:%M:%S%p", timeinfo);
+}
 
-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));
-    
-    led1 = 0;
-    set2=0;
-    uint16_t rxHandle = rxCharacteristic.getValueAttribute().getHandle();
-
-    while (true) {
-            
-    if(set){
+// Main LCD display function, add all framebuffer updates here ONLY
+void updateLCD() {
     fb.clear();
-    lcd.clear();
-
     fb.printString(lookupFontFace("Lucida 8pt", 8),
                    20,
                    40,
                    BLACK,
-                   strn);
+                   user);
+    
+    fb.printString(lookupFontFace("Lucida 8pt", 8),
+                   20,
+                   80,
+                   BLACK,
+                   timeStr);
+    
     lcd.drawFrameBuffer(fb);
-    set=0;
-    }
+}
+
+int main(void) {
+
+    screenSetup();
+    bleSetup();
+    
+    led1 = 0;
+    set2 = 0;
+    uint16_t rxHandle = rxCharacteristic.getValueAttribute().getHandle();
+    
+    // Initial time update
+    pc.printf("\n\r Updating time");
+    char cmd[5] = "time";
+    ble.updateCharacteristicValue(rxHandle, (const uint8_t*)cmd, 4);
     
-if(!button && !set2){
-  set2=1;
-  ble.updateCharacteristicValue(rxHandle, rxPayload, bytesRead);
-    ble.updateCharacteristicValue(rxHandle, (const uint8_t *) strn, bytesRead);
-    pc.printf ("\n\rSending %s", strn);
+    while (true) {
+        if(set){
+            if (timeUpdate) {
+                setTime(atoi(strn));
+                pc.printf("\n\r New time: %s", timeStr);
+                timeUpdate = 0;
+                mClock.start();
+            } else {
+                strncpy(user, strn, sizeof(strn));
+            }
+            set = 0;
+        }
+        
+        if(!button && !set2){
+            set2 = 1;
+            uint16_t bytesToSend = strlen(user);
+            memcpy(rxPayload, user, bytesToSend);
+            ble.updateCharacteristicValue(rxHandle, rxPayload, bytesToSend);
+            pc.printf ("\n\r Sending %s", user);
+        } else if (button){
+            set2 = 0;
+        }
+
+        clockOffset = mClock.read_ms();
+        if (clockOffset >= 1000) {
+            clockOffset += lastms;
+            int addTime = unixTime + (clockOffset/1000);
+            lastms = clockOffset%1000; // e.g. 2564 - 2000 = 564
+            mClock.reset();
+            setTime(addTime);
+            
+            // do all the framebuffer updating here every second
+            updateLCD();
+        }
     }
-    else if (button)
-    set2=0;
- }
-
 }
\ No newline at end of file