USED IMAGE2GLCD

Dependencies:   BLE_API SharpLCD_LucidaFont mbed nRF51822

Fork of Renard_YO by Andrea Corrado

Committer:
andcor02
Date:
Wed Apr 29 07:41:08 2015 +0000
Revision:
12:6bd7c00a0a93
Parent:
11:7fe2639ee5f6
Extra changes;

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