
USED IMAGE2GLCD
Dependencies: BLE_API SharpLCD_LucidaFont mbed nRF51822
Fork of Renard_YO by
main.cpp@7:8712aa56ee92, 2015-04-23 (annotated)
- Committer:
- andcor02
- Date:
- Thu Apr 23 08:04:41 2015 +0000
- Revision:
- 7:8712aa56ee92
- Parent:
- 6:9ea3943e9e24
- Child:
- 8:bb11edbf5708
ADDED SYNCED TIME, YO PUSHED FROM API
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 | |
andcor02 | 6:9ea3943e9e24 | 25 | DigitalOut led1(P0_12); |
andcor02 | 6:9ea3943e9e24 | 26 | DigitalOut motor(P0_1); |
andcor02 | 6:9ea3943e9e24 | 27 | DigitalOut screen(P0_21); |
andcor02 | 6:9ea3943e9e24 | 28 | DigitalIn button(P0_16); |
andcor02 | 7:8712aa56ee92 | 29 | |
andcor02 | 7:8712aa56ee92 | 30 | char strn[50]; // incoming tx data |
andcor02 | 7:8712aa56ee92 | 31 | char user[50]; // username to display |
andcor02 | 6:9ea3943e9e24 | 32 | int set=0; |
andcor02 | 6:9ea3943e9e24 | 33 | int set2=0; |
andcor02 | 6:9ea3943e9e24 | 34 | |
andcor02 | 7:8712aa56ee92 | 35 | int timeUpdate = 1; |
andcor02 | 7:8712aa56ee92 | 36 | time_t unixTime; |
andcor02 | 7:8712aa56ee92 | 37 | char timeStr[50]; // time to display |
andcor02 | 7:8712aa56ee92 | 38 | Timer mClock; |
andcor02 | 7:8712aa56ee92 | 39 | int clockOffset = 0; |
andcor02 | 7:8712aa56ee92 | 40 | int lastms = 0; |
andcor02 | 6:9ea3943e9e24 | 41 | |
andcor02 | 6:9ea3943e9e24 | 42 | //SharpLCD(PinName enable, PinName cs, PinName mosi, PinName miso_unused, PinName sclk, PinName _unused = NC) |
awatt196 | 3:e73cbdf58f5b | 43 | SharpLCD lcd(P0_0, P0_24, P0_20, P0_22, P0_25, P0_27); |
andcor02 | 6:9ea3943e9e24 | 44 | //SharpLCD lcd(P0_25, P0_24, P0_23, P0_13, P0_22, P0_27); |
awatt196 | 3:e73cbdf58f5b | 45 | uint8_t framebuffer[SharpLCD::SIZEOF_FRAMEBUFFER_FOR_ALLOC]; |
awatt196 | 3:e73cbdf58f5b | 46 | SharpLCD::FrameBuffer fb(framebuffer); |
andcor02 | 7:8712aa56ee92 | 47 | BLEDevice ble; |
andcor02 | 6:9ea3943e9e24 | 48 | //UARTService uart(ble); |
erigow01 | 0:9bea6067730f | 49 | |
andcor02 | 6:9ea3943e9e24 | 50 | // The Nordic UART Service |
andcor02 | 6:9ea3943e9e24 | 51 | 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 | 52 | 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 | 53 | 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 | 54 | 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 | 55 | uint8_t txPayload[50] = {0}; |
andcor02 | 6:9ea3943e9e24 | 56 | uint8_t rxPayload[50] = {0}; |
andcor02 | 6:9ea3943e9e24 | 57 | GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, 50, |
andcor02 | 6:9ea3943e9e24 | 58 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
andcor02 | 6:9ea3943e9e24 | 59 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, 50, |
andcor02 | 7:8712aa56ee92 | 60 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
andcor02 | 6:9ea3943e9e24 | 61 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
andcor02 | 6:9ea3943e9e24 | 62 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
erigow01 | 0:9bea6067730f | 63 | |
awatt196 | 3:e73cbdf58f5b | 64 | |
andcor02 | 6:9ea3943e9e24 | 65 | void screenSetup() |
andcor02 | 6:9ea3943e9e24 | 66 | { |
andcor02 | 6:9ea3943e9e24 | 67 | screen=1; |
andcor02 | 6:9ea3943e9e24 | 68 | wait_ms(200); |
andcor02 | 6:9ea3943e9e24 | 69 | lcd.enableDisplay(); |
andcor02 | 6:9ea3943e9e24 | 70 | fb.clear(); |
awatt196 | 3:e73cbdf58f5b | 71 | lcd.clear(); |
erigow01 | 0:9bea6067730f | 72 | } |
erigow01 | 0:9bea6067730f | 73 | |
andcor02 | 6:9ea3943e9e24 | 74 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
andcor02 | 6:9ea3943e9e24 | 75 | { |
andcor02 | 6:9ea3943e9e24 | 76 | ble.startAdvertising(); // restart advertising |
erigow01 | 0:9bea6067730f | 77 | } |
erigow01 | 0:9bea6067730f | 78 | |
andcor02 | 6:9ea3943e9e24 | 79 | void onDataWritten(const GattCharacteristicWriteCBParams *params) |
andcor02 | 6:9ea3943e9e24 | 80 | { |
andcor02 | 6:9ea3943e9e24 | 81 | uint16_t txHandle = txCharacteristic.getValueAttribute().getHandle(); |
andcor02 | 7:8712aa56ee92 | 82 | uint16_t bytesRead; |
andcor02 | 7:8712aa56ee92 | 83 | |
andcor02 | 6:9ea3943e9e24 | 84 | if (params->charHandle == txHandle) { |
andcor02 | 6:9ea3943e9e24 | 85 | memset(strn, 0, sizeof(strn)); |
andcor02 | 6:9ea3943e9e24 | 86 | ble.readCharacteristicValue(txHandle, txPayload, &bytesRead); |
andcor02 | 6:9ea3943e9e24 | 87 | strncpy(strn,(const char*)txPayload,bytesRead); |
andcor02 | 6:9ea3943e9e24 | 88 | set=1; |
andcor02 | 7:8712aa56ee92 | 89 | pc.printf ("\n\r Payload length: %d", bytesRead); |
andcor02 | 7:8712aa56ee92 | 90 | /*pc.printf("\n\r Payload: "); |
andcor02 | 7:8712aa56ee92 | 91 | for (int i=0;i<10;i++) { |
andcor02 | 7:8712aa56ee92 | 92 | pc.printf("%c", (char)txPayload[i]); |
andcor02 | 7:8712aa56ee92 | 93 | }*/ |
andcor02 | 7:8712aa56ee92 | 94 | pc.printf ("\n\r Received: %s", strn); |
erigow01 | 0:9bea6067730f | 95 | } |
erigow01 | 0:9bea6067730f | 96 | } |
erigow01 | 0:9bea6067730f | 97 | |
andcor02 | 6:9ea3943e9e24 | 98 | void bleSetup() |
andcor02 | 6:9ea3943e9e24 | 99 | { |
andcor02 | 6:9ea3943e9e24 | 100 | //Init ble |
andcor02 | 6:9ea3943e9e24 | 101 | ble.init(); |
andcor02 | 6:9ea3943e9e24 | 102 | //and handlers... |
andcor02 | 6:9ea3943e9e24 | 103 | ble.onDisconnection((Gap::DisconnectionEventCallback_t)&disconnectionCallback); |
andcor02 | 6:9ea3943e9e24 | 104 | ble.onDataWritten(onDataWritten); |
andcor02 | 6:9ea3943e9e24 | 105 | // setup advertising |
andcor02 | 6:9ea3943e9e24 | 106 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
andcor02 | 6:9ea3943e9e24 | 107 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
andcor02 | 6:9ea3943e9e24 | 108 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
andcor02 | 7:8712aa56ee92 | 109 | (const uint8_t *)"YO!", sizeof("YO!") - 1); |
andcor02 | 6:9ea3943e9e24 | 110 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
andcor02 | 6:9ea3943e9e24 | 111 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
andcor02 | 6:9ea3943e9e24 | 112 | ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms. |
andcor02 | 6:9ea3943e9e24 | 113 | ble.startAdvertising(); |
andcor02 | 6:9ea3943e9e24 | 114 | ble.addService(uartService); |
erigow01 | 0:9bea6067730f | 115 | } |
erigow01 | 0:9bea6067730f | 116 | |
andcor02 | 7:8712aa56ee92 | 117 | void setTime(int newTime) { |
andcor02 | 7:8712aa56ee92 | 118 | unixTime = newTime; |
andcor02 | 7:8712aa56ee92 | 119 | struct tm* timeinfo = localtime (&unixTime); |
andcor02 | 7:8712aa56ee92 | 120 | strftime (timeStr, 20, "%I:%M:%S%p", timeinfo); |
andcor02 | 7:8712aa56ee92 | 121 | } |
erigow01 | 0:9bea6067730f | 122 | |
andcor02 | 7:8712aa56ee92 | 123 | // Main LCD display function, add all framebuffer updates here ONLY |
andcor02 | 7:8712aa56ee92 | 124 | void updateLCD() { |
andcor02 | 6:9ea3943e9e24 | 125 | fb.clear(); |
andcor02 | 6:9ea3943e9e24 | 126 | fb.printString(lookupFontFace("Lucida 8pt", 8), |
andcor02 | 6:9ea3943e9e24 | 127 | 20, |
andcor02 | 6:9ea3943e9e24 | 128 | 40, |
andcor02 | 6:9ea3943e9e24 | 129 | BLACK, |
andcor02 | 7:8712aa56ee92 | 130 | user); |
andcor02 | 7:8712aa56ee92 | 131 | |
andcor02 | 7:8712aa56ee92 | 132 | fb.printString(lookupFontFace("Lucida 8pt", 8), |
andcor02 | 7:8712aa56ee92 | 133 | 20, |
andcor02 | 7:8712aa56ee92 | 134 | 80, |
andcor02 | 7:8712aa56ee92 | 135 | BLACK, |
andcor02 | 7:8712aa56ee92 | 136 | timeStr); |
andcor02 | 7:8712aa56ee92 | 137 | |
andcor02 | 6:9ea3943e9e24 | 138 | lcd.drawFrameBuffer(fb); |
andcor02 | 7:8712aa56ee92 | 139 | } |
andcor02 | 7:8712aa56ee92 | 140 | |
andcor02 | 7:8712aa56ee92 | 141 | int main(void) { |
andcor02 | 7:8712aa56ee92 | 142 | |
andcor02 | 7:8712aa56ee92 | 143 | screenSetup(); |
andcor02 | 7:8712aa56ee92 | 144 | bleSetup(); |
andcor02 | 7:8712aa56ee92 | 145 | |
andcor02 | 7:8712aa56ee92 | 146 | led1 = 0; |
andcor02 | 7:8712aa56ee92 | 147 | set2 = 0; |
andcor02 | 7:8712aa56ee92 | 148 | uint16_t rxHandle = rxCharacteristic.getValueAttribute().getHandle(); |
andcor02 | 7:8712aa56ee92 | 149 | |
andcor02 | 7:8712aa56ee92 | 150 | // Initial time update |
andcor02 | 7:8712aa56ee92 | 151 | pc.printf("\n\r Updating time"); |
andcor02 | 7:8712aa56ee92 | 152 | char cmd[5] = "time"; |
andcor02 | 7:8712aa56ee92 | 153 | ble.updateCharacteristicValue(rxHandle, (const uint8_t*)cmd, 4); |
andcor02 | 6:9ea3943e9e24 | 154 | |
andcor02 | 7:8712aa56ee92 | 155 | while (true) { |
andcor02 | 7:8712aa56ee92 | 156 | if(set){ |
andcor02 | 7:8712aa56ee92 | 157 | if (timeUpdate) { |
andcor02 | 7:8712aa56ee92 | 158 | setTime(atoi(strn)); |
andcor02 | 7:8712aa56ee92 | 159 | pc.printf("\n\r New time: %s", timeStr); |
andcor02 | 7:8712aa56ee92 | 160 | timeUpdate = 0; |
andcor02 | 7:8712aa56ee92 | 161 | mClock.start(); |
andcor02 | 7:8712aa56ee92 | 162 | } else { |
andcor02 | 7:8712aa56ee92 | 163 | strncpy(user, strn, sizeof(strn)); |
andcor02 | 7:8712aa56ee92 | 164 | } |
andcor02 | 7:8712aa56ee92 | 165 | set = 0; |
andcor02 | 7:8712aa56ee92 | 166 | } |
andcor02 | 7:8712aa56ee92 | 167 | |
andcor02 | 7:8712aa56ee92 | 168 | if(!button && !set2){ |
andcor02 | 7:8712aa56ee92 | 169 | set2 = 1; |
andcor02 | 7:8712aa56ee92 | 170 | uint16_t bytesToSend = strlen(user); |
andcor02 | 7:8712aa56ee92 | 171 | memcpy(rxPayload, user, bytesToSend); |
andcor02 | 7:8712aa56ee92 | 172 | ble.updateCharacteristicValue(rxHandle, rxPayload, bytesToSend); |
andcor02 | 7:8712aa56ee92 | 173 | pc.printf ("\n\r Sending %s", user); |
andcor02 | 7:8712aa56ee92 | 174 | } else if (button){ |
andcor02 | 7:8712aa56ee92 | 175 | set2 = 0; |
andcor02 | 7:8712aa56ee92 | 176 | } |
andcor02 | 7:8712aa56ee92 | 177 | |
andcor02 | 7:8712aa56ee92 | 178 | clockOffset = mClock.read_ms(); |
andcor02 | 7:8712aa56ee92 | 179 | if (clockOffset >= 1000) { |
andcor02 | 7:8712aa56ee92 | 180 | clockOffset += lastms; |
andcor02 | 7:8712aa56ee92 | 181 | int addTime = unixTime + (clockOffset/1000); |
andcor02 | 7:8712aa56ee92 | 182 | lastms = clockOffset%1000; // e.g. 2564 - 2000 = 564 |
andcor02 | 7:8712aa56ee92 | 183 | mClock.reset(); |
andcor02 | 7:8712aa56ee92 | 184 | setTime(addTime); |
andcor02 | 7:8712aa56ee92 | 185 | |
andcor02 | 7:8712aa56ee92 | 186 | // do all the framebuffer updating here every second |
andcor02 | 7:8712aa56ee92 | 187 | updateLCD(); |
andcor02 | 7:8712aa56ee92 | 188 | } |
andcor02 | 6:9ea3943e9e24 | 189 | } |
erigow01 | 0:9bea6067730f | 190 | } |