USED IMAGE2GLCD

Dependencies:   BLE_API SharpLCD_LucidaFont mbed nRF51822

Fork of Renard_UI_Only by Demo Team

Committer:
pwright01
Date:
Wed May 13 08:15:01 2015 +0000
Revision:
13:6def6e1fb69e
Parent:
12:6bd7c00a0a93
Second screen

Who changed what in which revision?

UserRevisionLine numberNew 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"
pwright01 9:bae7cfbe8d85 21 //#include "font.h"
pwright01 9:bae7cfbe8d85 22 #include "dotfont.h"
andcor02 6:9ea3943e9e24 23
andcor02 6:9ea3943e9e24 24 Serial pc (USBTX,USBRX);
andcor02 6:9ea3943e9e24 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);
pwright01 13:6def6e1fb69e 30 DigitalIn button2(P0_17);
andcor02 8:bb11edbf5708 31 //a50,a60,a70,a80,a90,a100,a110,a120,a130,a140,a150,a160,a170
andcor02 7:8712aa56ee92 32
andcor02 7:8712aa56ee92 33 char strn[50]; // incoming tx data
andcor02 7:8712aa56ee92 34 char user[50]; // username to display
andcor02 8:bb11edbf5708 35 uint8_t* rings[2] = {a0,a10};
andcor02 6:9ea3943e9e24 36 int set=0;
andcor02 6:9ea3943e9e24 37 int set2=0;
pwright01 13:6def6e1fb69e 38 int set3=0;
andcor02 8:bb11edbf5708 39 int pairing =1;
andcor02 8:bb11edbf5708 40 int anim=0;
andcor02 8:bb11edbf5708 41 int elip=0;
pwright01 13:6def6e1fb69e 42 int flash=0;
pwright01 13:6def6e1fb69e 43 int screenIndex = 0;
andcor02 6:9ea3943e9e24 44
andcor02 7:8712aa56ee92 45 int timeUpdate = 1;
andcor02 7:8712aa56ee92 46 time_t unixTime;
andcor02 7:8712aa56ee92 47 char timeStr[50]; // time to display
andcor02 8:bb11edbf5708 48 char dayStr[50]; // day to display
andcor02 8:bb11edbf5708 49 char dateStr[50]; // date to display
andcor02 7:8712aa56ee92 50 Timer mClock;
andcor02 7:8712aa56ee92 51 int clockOffset = 0;
andcor02 7:8712aa56ee92 52 int lastms = 0;
andcor02 6:9ea3943e9e24 53
andcor02 6:9ea3943e9e24 54 //SharpLCD(PinName enable, PinName cs, PinName mosi, PinName miso_unused, PinName sclk, PinName _unused = NC)
awatt196 3:e73cbdf58f5b 55 SharpLCD lcd(P0_0, P0_24, P0_20, P0_22, P0_25, P0_27);
andcor02 6:9ea3943e9e24 56 //SharpLCD lcd(P0_25, P0_24, P0_23, P0_13, P0_22, P0_27);
awatt196 3:e73cbdf58f5b 57 uint8_t framebuffer[SharpLCD::SIZEOF_FRAMEBUFFER_FOR_ALLOC];
awatt196 3:e73cbdf58f5b 58 SharpLCD::FrameBuffer fb(framebuffer);
andcor02 7:8712aa56ee92 59 BLEDevice ble;
andcor02 6:9ea3943e9e24 60 //UARTService uart(ble);
erigow01 0:9bea6067730f 61
andcor02 6:9ea3943e9e24 62 // The Nordic UART Service
andcor02 6:9ea3943e9e24 63 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 64 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 65 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 66 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 67 uint8_t txPayload[50] = {0};
andcor02 6:9ea3943e9e24 68 uint8_t rxPayload[50] = {0};
andcor02 6:9ea3943e9e24 69 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, 50,
andcor02 6:9ea3943e9e24 70 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
andcor02 6:9ea3943e9e24 71 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, 50,
andcor02 7:8712aa56ee92 72 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
andcor02 6:9ea3943e9e24 73 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
andcor02 6:9ea3943e9e24 74 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
erigow01 0:9bea6067730f 75
pwright01 9:bae7cfbe8d85 76 const FONT_INFO* mainFont;
pwright01 9:bae7cfbe8d85 77 const FONT_INFO* exFont;
andcor02 10:56ef6053ffc7 78 const FONT_INFO* subFont;
awatt196 3:e73cbdf58f5b 79
andcor02 6:9ea3943e9e24 80 void screenSetup()
andcor02 6:9ea3943e9e24 81 {
andcor02 6:9ea3943e9e24 82 screen=1;
andcor02 6:9ea3943e9e24 83 wait_ms(200);
andcor02 6:9ea3943e9e24 84 lcd.enableDisplay();
andcor02 6:9ea3943e9e24 85 fb.clear();
awatt196 3:e73cbdf58f5b 86 lcd.clear();
andcor02 8:bb11edbf5708 87
pwright01 11:7fe2639ee5f6 88 mainFont = searchFontFace("Square Head Black", 20);
pwright01 13:6def6e1fb69e 89 subFont = searchFontFace("Droid Sans Mono Black", 9);
andcor02 10:56ef6053ffc7 90 exFont = searchFontFace("Lucida", 8);
andcor02 8:bb11edbf5708 91 }
andcor02 8:bb11edbf5708 92
andcor02 8:bb11edbf5708 93 void showPairing()
andcor02 8:bb11edbf5708 94 {
andcor02 8:bb11edbf5708 95 fb.clear();
pwright01 9:bae7cfbe8d85 96 fb.bitBlit(sScreen, 96, 96, 0, 0);
pwright01 9:bae7cfbe8d85 97 fb.bitBlit(rings[anim], 32, 32, 2, 57);
pwright01 9:bae7cfbe8d85 98 char* dots[4] = {"", "..", "...", "..."};
pwright01 9:bae7cfbe8d85 99 char str[15] = "Pairing";
pwright01 9:bae7cfbe8d85 100 strcat(str, dots[elip]);
andcor02 8:bb11edbf5708 101
pwright01 9:bae7cfbe8d85 102 fb.printString(exFont, 38, 80, str);
pwright01 9:bae7cfbe8d85 103
andcor02 8:bb11edbf5708 104 lcd.drawFrameBuffer(fb);
andcor02 8:bb11edbf5708 105 elip++;
andcor02 8:bb11edbf5708 106 anim++;
andcor02 8:bb11edbf5708 107 wait(0.15);
andcor02 8:bb11edbf5708 108
andcor02 8:bb11edbf5708 109 if (anim>=2) {
pwright01 13:6def6e1fb69e 110 anim = 0;
andcor02 8:bb11edbf5708 111 }
andcor02 8:bb11edbf5708 112
andcor02 8:bb11edbf5708 113 if (elip>=4) {
pwright01 13:6def6e1fb69e 114 elip = 0;
andcor02 8:bb11edbf5708 115 }
erigow01 0:9bea6067730f 116 }
erigow01 0:9bea6067730f 117
andcor02 6:9ea3943e9e24 118 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
andcor02 6:9ea3943e9e24 119 {
andcor02 6:9ea3943e9e24 120 ble.startAdvertising(); // restart advertising
erigow01 0:9bea6067730f 121 }
erigow01 0:9bea6067730f 122
andcor02 6:9ea3943e9e24 123 void onDataWritten(const GattCharacteristicWriteCBParams *params)
andcor02 6:9ea3943e9e24 124 {
andcor02 6:9ea3943e9e24 125 uint16_t txHandle = txCharacteristic.getValueAttribute().getHandle();
andcor02 7:8712aa56ee92 126 uint16_t bytesRead;
andcor02 7:8712aa56ee92 127
andcor02 6:9ea3943e9e24 128 if (params->charHandle == txHandle) {
andcor02 6:9ea3943e9e24 129 memset(strn, 0, sizeof(strn));
andcor02 6:9ea3943e9e24 130 ble.readCharacteristicValue(txHandle, txPayload, &bytesRead);
andcor02 6:9ea3943e9e24 131 strncpy(strn,(const char*)txPayload,bytesRead);
andcor02 6:9ea3943e9e24 132 set=1;
andcor02 7:8712aa56ee92 133 pc.printf ("\n\r Payload length: %d", bytesRead);
andcor02 7:8712aa56ee92 134 /*pc.printf("\n\r Payload: ");
andcor02 7:8712aa56ee92 135 for (int i=0;i<10;i++) {
andcor02 7:8712aa56ee92 136 pc.printf("%c", (char)txPayload[i]);
andcor02 7:8712aa56ee92 137 }*/
andcor02 7:8712aa56ee92 138 pc.printf ("\n\r Received: %s", strn);
erigow01 0:9bea6067730f 139 }
erigow01 0:9bea6067730f 140 }
erigow01 0:9bea6067730f 141
andcor02 6:9ea3943e9e24 142 void bleSetup()
andcor02 6:9ea3943e9e24 143 {
andcor02 6:9ea3943e9e24 144 //Init ble
andcor02 6:9ea3943e9e24 145 ble.init();
andcor02 6:9ea3943e9e24 146 //and handlers...
andcor02 6:9ea3943e9e24 147 ble.onDisconnection((Gap::DisconnectionEventCallback_t)&disconnectionCallback);
andcor02 6:9ea3943e9e24 148 ble.onDataWritten(onDataWritten);
andcor02 6:9ea3943e9e24 149 // setup advertising
andcor02 6:9ea3943e9e24 150 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
andcor02 6:9ea3943e9e24 151 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
andcor02 6:9ea3943e9e24 152 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
andcor02 7:8712aa56ee92 153 (const uint8_t *)"YO!", sizeof("YO!") - 1);
andcor02 6:9ea3943e9e24 154 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
andcor02 6:9ea3943e9e24 155 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
andcor02 6:9ea3943e9e24 156 ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms.
andcor02 6:9ea3943e9e24 157 ble.startAdvertising();
andcor02 6:9ea3943e9e24 158 ble.addService(uartService);
erigow01 0:9bea6067730f 159 }
erigow01 0:9bea6067730f 160
andcor02 7:8712aa56ee92 161 void setTime(int newTime) {
andcor02 7:8712aa56ee92 162 unixTime = newTime;
andcor02 7:8712aa56ee92 163 struct tm* timeinfo = localtime (&unixTime);
andcor02 8:bb11edbf5708 164 strftime (timeStr, 20, "%R", timeinfo);
pwright01 9:bae7cfbe8d85 165 strftime (dayStr, 20, "%a", timeinfo);
pwright01 9:bae7cfbe8d85 166 strftime (dateStr, 20, "%b %e", timeinfo);
andcor02 7:8712aa56ee92 167 }
erigow01 0:9bea6067730f 168
andcor02 7:8712aa56ee92 169 // Main LCD display function, add all framebuffer updates here ONLY
andcor02 7:8712aa56ee92 170 void updateLCD() {
andcor02 6:9ea3943e9e24 171 fb.clear();
andcor02 10:56ef6053ffc7 172 fb.bitBlit(img, 96, 96, 0,0);
andcor02 12:6bd7c00a0a93 173
andcor02 12:6bd7c00a0a93 174 fb.bitBlit(battery, 16, 16, 75, 0);
pwright01 13:6def6e1fb69e 175 //fb.bitBlit(BLE, 16, 16, 0, 0);
pwright01 13:6def6e1fb69e 176 if (screenIndex == 0) {
pwright01 13:6def6e1fb69e 177 fb.printString(mainFont, 5, 61, timeStr);
pwright01 13:6def6e1fb69e 178
pwright01 13:6def6e1fb69e 179 fb.printString(subFont, 14, 78, dayStr);
pwright01 13:6def6e1fb69e 180
pwright01 13:6def6e1fb69e 181 fb.printString(subFont, 43, 78, dateStr);
pwright01 13:6def6e1fb69e 182 } else if (screenIndex == 1) {
pwright01 13:6def6e1fb69e 183 if (flash % 2 == 0) {
pwright01 13:6def6e1fb69e 184 fb.printString(subFont, 10, 30, "Yo Received!");
pwright01 13:6def6e1fb69e 185 flash++;
pwright01 13:6def6e1fb69e 186 } else {
pwright01 13:6def6e1fb69e 187 flash = 0;
pwright01 13:6def6e1fb69e 188 }
pwright01 13:6def6e1fb69e 189
pwright01 13:6def6e1fb69e 190 fb.printString(subFont, 10, 60, "User:");
pwright01 13:6def6e1fb69e 191 fb.printString(subFont, 10, 80, user);
pwright01 13:6def6e1fb69e 192 }
andcor02 8:bb11edbf5708 193
andcor02 6:9ea3943e9e24 194 lcd.drawFrameBuffer(fb);
andcor02 7:8712aa56ee92 195 }
andcor02 7:8712aa56ee92 196
andcor02 7:8712aa56ee92 197 int main(void) {
andcor02 7:8712aa56ee92 198
andcor02 7:8712aa56ee92 199 screenSetup();
andcor02 7:8712aa56ee92 200 bleSetup();
andcor02 7:8712aa56ee92 201
andcor02 7:8712aa56ee92 202 led1 = 0;
andcor02 7:8712aa56ee92 203 set2 = 0;
andcor02 8:bb11edbf5708 204
andcor02 7:8712aa56ee92 205 uint16_t rxHandle = rxCharacteristic.getValueAttribute().getHandle();
andcor02 7:8712aa56ee92 206
andcor02 7:8712aa56ee92 207 // Initial time update
andcor02 7:8712aa56ee92 208 pc.printf("\n\r Updating time");
andcor02 7:8712aa56ee92 209 char cmd[5] = "time";
andcor02 7:8712aa56ee92 210 ble.updateCharacteristicValue(rxHandle, (const uint8_t*)cmd, 4);
andcor02 8:bb11edbf5708 211
andcor02 8:bb11edbf5708 212 while (true) {
andcor02 8:bb11edbf5708 213
andcor02 8:bb11edbf5708 214 if (pairing){
andcor02 8:bb11edbf5708 215 showPairing();
andcor02 8:bb11edbf5708 216 }
andcor02 6:9ea3943e9e24 217
andcor02 7:8712aa56ee92 218 if(set){
andcor02 7:8712aa56ee92 219 if (timeUpdate) {
andcor02 7:8712aa56ee92 220 setTime(atoi(strn));
andcor02 7:8712aa56ee92 221 pc.printf("\n\r New time: %s", timeStr);
andcor02 7:8712aa56ee92 222 timeUpdate = 0;
andcor02 8:bb11edbf5708 223 pairing=0;
andcor02 7:8712aa56ee92 224 mClock.start();
andcor02 7:8712aa56ee92 225 } else {
andcor02 7:8712aa56ee92 226 strncpy(user, strn, sizeof(strn));
pwright01 13:6def6e1fb69e 227 screenIndex = 1; // force second screen
andcor02 7:8712aa56ee92 228 }
andcor02 7:8712aa56ee92 229 set = 0;
andcor02 7:8712aa56ee92 230 }
andcor02 7:8712aa56ee92 231
pwright01 13:6def6e1fb69e 232 if(button && set2){
pwright01 13:6def6e1fb69e 233 set2 = 0;
andcor02 7:8712aa56ee92 234 uint16_t bytesToSend = strlen(user);
andcor02 7:8712aa56ee92 235 memcpy(rxPayload, user, bytesToSend);
andcor02 7:8712aa56ee92 236 ble.updateCharacteristicValue(rxHandle, rxPayload, bytesToSend);
andcor02 7:8712aa56ee92 237 pc.printf ("\n\r Sending %s", user);
pwright01 13:6def6e1fb69e 238 } else if (!button){
pwright01 13:6def6e1fb69e 239 set2 = 1;
pwright01 13:6def6e1fb69e 240 }
pwright01 13:6def6e1fb69e 241
pwright01 13:6def6e1fb69e 242 if (button2 && set3) {
pwright01 13:6def6e1fb69e 243
pwright01 13:6def6e1fb69e 244 set3 = 0;
pwright01 13:6def6e1fb69e 245 screenIndex = screenIndex == 0 ? 1 : 0; // if 0 switch to 1, if 1 switch to 0
pwright01 13:6def6e1fb69e 246 } else if (!button2) {
pwright01 13:6def6e1fb69e 247 set3 = 1;
andcor02 7:8712aa56ee92 248 }
andcor02 7:8712aa56ee92 249
andcor02 7:8712aa56ee92 250 clockOffset = mClock.read_ms();
andcor02 7:8712aa56ee92 251 if (clockOffset >= 1000) {
andcor02 7:8712aa56ee92 252 clockOffset += lastms;
andcor02 7:8712aa56ee92 253 int addTime = unixTime + (clockOffset/1000);
andcor02 7:8712aa56ee92 254 lastms = clockOffset%1000; // e.g. 2564 - 2000 = 564
andcor02 7:8712aa56ee92 255 mClock.reset();
andcor02 7:8712aa56ee92 256 setTime(addTime);
andcor02 7:8712aa56ee92 257
andcor02 7:8712aa56ee92 258 // do all the framebuffer updating here every second
andcor02 7:8712aa56ee92 259 updateLCD();
andcor02 7:8712aa56ee92 260 }
andcor02 6:9ea3943e9e24 261 }
erigow01 0:9bea6067730f 262 }