USED IMAGE2GLCD

Dependencies:   BLE_API SharpLCD_LucidaFont mbed nRF51822

Fork of Renard_YO by Andrea Corrado

main.cpp

Committer:
andcor02
Date:
2015-04-20
Revision:
6:9ea3943e9e24
Parent:
5:ffa456512437
Child:
7:8712aa56ee92

File content as of revision 6:9ea3943e9e24:

/* 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.
 */

#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);
BLEDevice  ble;
//UARTService uart(ble);

// 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 *));


void screenSetup()
{
    screen=1;
    wait_ms(200);
    lcd.enableDisplay();
    fb.clear();
    lcd.clear();
}



void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    ble.startAdvertising(); // restart advertising
}

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);
    }

}

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);
}




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){
    fb.clear();
    lcd.clear();

    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;
 }

}