
USED IMAGE2GLCD
Dependencies: BLE_API SharpLCD_LucidaFont mbed nRF51822
Fork of Renard_YO by
main.cpp@6:9ea3943e9e24, 2015-04-20 (annotated)
- Committer:
- andcor02
- Date:
- Mon Apr 20 13:51:58 2015 +0000
- Revision:
- 6:9ea3943e9e24
- Parent:
- 5:ffa456512437
- Child:
- 7:8712aa56ee92
FIRST WORK ON YO!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andcor02 | 6:9ea3943e9e24 | 1 | /* mbed Microcontroller Library |
andcor02 | 6:9ea3943e9e24 | 2 | * Copyright (c) 2006-2013 ARM Limited |
andcor02 | 6:9ea3943e9e24 | 3 | * |
andcor02 | 6:9ea3943e9e24 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
andcor02 | 6:9ea3943e9e24 | 5 | * you may not use this file except in compliance with the License. |
andcor02 | 6:9ea3943e9e24 | 6 | * You may obtain a copy of the License at |
andcor02 | 6:9ea3943e9e24 | 7 | * |
andcor02 | 6:9ea3943e9e24 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
andcor02 | 6:9ea3943e9e24 | 9 | * |
andcor02 | 6:9ea3943e9e24 | 10 | * Unless required by applicable law or agreed to in writing, software |
andcor02 | 6:9ea3943e9e24 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
andcor02 | 6:9ea3943e9e24 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
andcor02 | 6:9ea3943e9e24 | 13 | * See the License for the specific language governing permissions and |
andcor02 | 6:9ea3943e9e24 | 14 | * limitations under the License. |
andcor02 | 6:9ea3943e9e24 | 15 | */ |
erigow01 | 0:9bea6067730f | 16 | |
andcor02 | 6:9ea3943e9e24 | 17 | #include "mbed.h" |
andcor02 | 6:9ea3943e9e24 | 18 | #include "SharpLCD.hpp" |
andcor02 | 6:9ea3943e9e24 | 19 | #include "BLEDevice.h" |
andcor02 | 6:9ea3943e9e24 | 20 | #include "icon.h" |
andcor02 | 6:9ea3943e9e24 | 21 | #include "font.h" |
andcor02 | 6:9ea3943e9e24 | 22 | |
andcor02 | 6:9ea3943e9e24 | 23 | Serial pc (USBTX,USBRX); |
andcor02 | 6:9ea3943e9e24 | 24 | |
erigow01 | 0:9bea6067730f | 25 | |
andcor02 | 6:9ea3943e9e24 | 26 | DigitalOut led1(P0_12); |
andcor02 | 6:9ea3943e9e24 | 27 | DigitalOut motor(P0_1); |
andcor02 | 6:9ea3943e9e24 | 28 | DigitalOut screen(P0_21); |
andcor02 | 6:9ea3943e9e24 | 29 | DigitalIn button(P0_16); |
andcor02 | 6:9ea3943e9e24 | 30 | char primary_text[50]; |
andcor02 | 6:9ea3943e9e24 | 31 | char strn[50]; |
andcor02 | 6:9ea3943e9e24 | 32 | int set=0; |
andcor02 | 6:9ea3943e9e24 | 33 | int set2=0; |
andcor02 | 6:9ea3943e9e24 | 34 | uint16_t bytesRead; |
andcor02 | 6:9ea3943e9e24 | 35 | |
andcor02 | 6:9ea3943e9e24 | 36 | |
andcor02 | 6:9ea3943e9e24 | 37 | |
andcor02 | 6:9ea3943e9e24 | 38 | //SharpLCD(PinName enable, PinName cs, PinName mosi, PinName miso_unused, PinName sclk, PinName _unused = NC) |
awatt196 | 3:e73cbdf58f5b | 39 | SharpLCD lcd(P0_0, P0_24, P0_20, P0_22, P0_25, P0_27); |
andcor02 | 6:9ea3943e9e24 | 40 | //SharpLCD lcd(P0_25, P0_24, P0_23, P0_13, P0_22, P0_27); |
awatt196 | 3:e73cbdf58f5b | 41 | uint8_t framebuffer[SharpLCD::SIZEOF_FRAMEBUFFER_FOR_ALLOC]; |
awatt196 | 3:e73cbdf58f5b | 42 | SharpLCD::FrameBuffer fb(framebuffer); |
andcor02 | 6:9ea3943e9e24 | 43 | BLEDevice ble; |
andcor02 | 6:9ea3943e9e24 | 44 | //UARTService uart(ble); |
erigow01 | 0:9bea6067730f | 45 | |
andcor02 | 6:9ea3943e9e24 | 46 | // The Nordic UART Service |
andcor02 | 6:9ea3943e9e24 | 47 | static const uint8_t uart_base_uuid[] = {0x6e, 0x40, 0x00, 0x01, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
andcor02 | 6:9ea3943e9e24 | 48 | static const uint8_t uart_tx_uuid[] = {0x6e, 0x40, 0x00, 0x02, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
andcor02 | 6:9ea3943e9e24 | 49 | static const uint8_t uart_rx_uuid[] = {0x6e, 0x40, 0x00, 0x03, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
andcor02 | 6:9ea3943e9e24 | 50 | static const uint8_t uart_base_uuid_rev[] = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e}; |
andcor02 | 6:9ea3943e9e24 | 51 | uint8_t txPayload[50] = {0}; |
andcor02 | 6:9ea3943e9e24 | 52 | uint8_t rxPayload[50] = {0}; |
andcor02 | 6:9ea3943e9e24 | 53 | GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, 50, |
andcor02 | 6:9ea3943e9e24 | 54 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
andcor02 | 6:9ea3943e9e24 | 55 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, 50, |
andcor02 | 6:9ea3943e9e24 | 56 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
andcor02 | 6:9ea3943e9e24 | 57 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
andcor02 | 6:9ea3943e9e24 | 58 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
erigow01 | 0:9bea6067730f | 59 | |
awatt196 | 3:e73cbdf58f5b | 60 | |
andcor02 | 6:9ea3943e9e24 | 61 | void screenSetup() |
andcor02 | 6:9ea3943e9e24 | 62 | { |
andcor02 | 6:9ea3943e9e24 | 63 | screen=1; |
andcor02 | 6:9ea3943e9e24 | 64 | wait_ms(200); |
andcor02 | 6:9ea3943e9e24 | 65 | lcd.enableDisplay(); |
andcor02 | 6:9ea3943e9e24 | 66 | fb.clear(); |
awatt196 | 3:e73cbdf58f5b | 67 | lcd.clear(); |
erigow01 | 0:9bea6067730f | 68 | } |
erigow01 | 0:9bea6067730f | 69 | |
erigow01 | 0:9bea6067730f | 70 | |
erigow01 | 0:9bea6067730f | 71 | |
andcor02 | 6:9ea3943e9e24 | 72 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
andcor02 | 6:9ea3943e9e24 | 73 | { |
andcor02 | 6:9ea3943e9e24 | 74 | ble.startAdvertising(); // restart advertising |
erigow01 | 0:9bea6067730f | 75 | } |
erigow01 | 0:9bea6067730f | 76 | |
andcor02 | 6:9ea3943e9e24 | 77 | void onDataWritten(const GattCharacteristicWriteCBParams *params) |
andcor02 | 6:9ea3943e9e24 | 78 | { |
andcor02 | 6:9ea3943e9e24 | 79 | uint16_t txHandle = txCharacteristic.getValueAttribute().getHandle(); |
andcor02 | 6:9ea3943e9e24 | 80 | |
andcor02 | 6:9ea3943e9e24 | 81 | |
andcor02 | 6:9ea3943e9e24 | 82 | if (params->charHandle == txHandle) { |
andcor02 | 6:9ea3943e9e24 | 83 | memset(strn, 0, sizeof(strn)); |
andcor02 | 6:9ea3943e9e24 | 84 | ble.readCharacteristicValue(txHandle, txPayload, &bytesRead); |
andcor02 | 6:9ea3943e9e24 | 85 | strncpy(strn,(const char*)txPayload,bytesRead); |
andcor02 | 6:9ea3943e9e24 | 86 | set=1; |
andcor02 | 6:9ea3943e9e24 | 87 | pc.printf ("\n\r Recieved: %s", strn); |
erigow01 | 0:9bea6067730f | 88 | } |
andcor02 | 6:9ea3943e9e24 | 89 | |
erigow01 | 0:9bea6067730f | 90 | } |
erigow01 | 0:9bea6067730f | 91 | |
andcor02 | 6:9ea3943e9e24 | 92 | void bleSetup() |
andcor02 | 6:9ea3943e9e24 | 93 | { |
andcor02 | 6:9ea3943e9e24 | 94 | //Init ble |
andcor02 | 6:9ea3943e9e24 | 95 | ble.init(); |
andcor02 | 6:9ea3943e9e24 | 96 | //and handlers... |
andcor02 | 6:9ea3943e9e24 | 97 | ble.onDisconnection((Gap::DisconnectionEventCallback_t)&disconnectionCallback); |
andcor02 | 6:9ea3943e9e24 | 98 | ble.onDataWritten(onDataWritten); |
andcor02 | 6:9ea3943e9e24 | 99 | // setup advertising |
andcor02 | 6:9ea3943e9e24 | 100 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
andcor02 | 6:9ea3943e9e24 | 101 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
andcor02 | 6:9ea3943e9e24 | 102 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
andcor02 | 6:9ea3943e9e24 | 103 | (const uint8_t *)"YO!", sizeof("YO2!") - 1); |
andcor02 | 6:9ea3943e9e24 | 104 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
andcor02 | 6:9ea3943e9e24 | 105 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
andcor02 | 6:9ea3943e9e24 | 106 | ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms. |
andcor02 | 6:9ea3943e9e24 | 107 | ble.startAdvertising(); |
andcor02 | 6:9ea3943e9e24 | 108 | ble.addService(uartService); |
erigow01 | 0:9bea6067730f | 109 | } |
erigow01 | 0:9bea6067730f | 110 | |
andcor02 | 6:9ea3943e9e24 | 111 | |
andcor02 | 6:9ea3943e9e24 | 112 | |
erigow01 | 0:9bea6067730f | 113 | |
andcor02 | 6:9ea3943e9e24 | 114 | int main(void) |
andcor02 | 6:9ea3943e9e24 | 115 | { time_t seconds = time(NULL); |
andcor02 | 6:9ea3943e9e24 | 116 | screenSetup(); |
andcor02 | 6:9ea3943e9e24 | 117 | bleSetup(); |
andcor02 | 6:9ea3943e9e24 | 118 | set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
andcor02 | 6:9ea3943e9e24 | 119 | pc.printf("Time as a basic string = %s", ctime(&seconds)); |
erigow01 | 1:f0635f12df8c | 120 | |
andcor02 | 6:9ea3943e9e24 | 121 | led1 = 0; |
andcor02 | 6:9ea3943e9e24 | 122 | set2=0; |
andcor02 | 6:9ea3943e9e24 | 123 | uint16_t rxHandle = rxCharacteristic.getValueAttribute().getHandle(); |
andcor02 | 6:9ea3943e9e24 | 124 | |
andcor02 | 6:9ea3943e9e24 | 125 | while (true) { |
andcor02 | 6:9ea3943e9e24 | 126 | |
andcor02 | 6:9ea3943e9e24 | 127 | if(set){ |
andcor02 | 6:9ea3943e9e24 | 128 | fb.clear(); |
andcor02 | 6:9ea3943e9e24 | 129 | lcd.clear(); |
erigow01 | 0:9bea6067730f | 130 | |
andcor02 | 6:9ea3943e9e24 | 131 | fb.printString(lookupFontFace("Lucida 8pt", 8), |
andcor02 | 6:9ea3943e9e24 | 132 | 20, |
andcor02 | 6:9ea3943e9e24 | 133 | 40, |
andcor02 | 6:9ea3943e9e24 | 134 | BLACK, |
andcor02 | 6:9ea3943e9e24 | 135 | strn); |
andcor02 | 6:9ea3943e9e24 | 136 | lcd.drawFrameBuffer(fb); |
andcor02 | 6:9ea3943e9e24 | 137 | set=0; |
erigow01 | 0:9bea6067730f | 138 | } |
andcor02 | 6:9ea3943e9e24 | 139 | |
andcor02 | 6:9ea3943e9e24 | 140 | if(!button && !set2){ |
andcor02 | 6:9ea3943e9e24 | 141 | set2=1; |
andcor02 | 6:9ea3943e9e24 | 142 | ble.updateCharacteristicValue(rxHandle, rxPayload, bytesRead); |
andcor02 | 6:9ea3943e9e24 | 143 | ble.updateCharacteristicValue(rxHandle, (const uint8_t *) strn, bytesRead); |
andcor02 | 6:9ea3943e9e24 | 144 | pc.printf ("\n\rSending %s", strn); |
andcor02 | 6:9ea3943e9e24 | 145 | } |
andcor02 | 6:9ea3943e9e24 | 146 | else if (button) |
andcor02 | 6:9ea3943e9e24 | 147 | set2=0; |
andcor02 | 6:9ea3943e9e24 | 148 | } |
andcor02 | 6:9ea3943e9e24 | 149 | |
erigow01 | 0:9bea6067730f | 150 | } |