Code Sample for ERS4 GEII UNICE ERS4

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of DU4SmartCities by Philippe Bazot

Committer:
samdanbury
Date:
Wed Jul 16 12:03:54 2014 +0000
Revision:
0:cae064bcbe5e
Child:
1:1f187285667c
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samdanbury 0:cae064bcbe5e 1 /*******************************************************************************
samdanbury 0:cae064bcbe5e 2 * Copyright (c) 2014 IBM Corporation and other Contributors.
samdanbury 0:cae064bcbe5e 3 *
samdanbury 0:cae064bcbe5e 4 * All rights reserved. This program and the accompanying materials
samdanbury 0:cae064bcbe5e 5 * are made available under the terms of the Eclipse Public License v1.0
samdanbury 0:cae064bcbe5e 6 * which accompanies this distribution, and is available at
samdanbury 0:cae064bcbe5e 7 * http://www.eclipse.org/legal/epl-v10.html
samdanbury 0:cae064bcbe5e 8 *
samdanbury 0:cae064bcbe5e 9 * Contributors: Sam Danbury
samdanbury 0:cae064bcbe5e 10 * IBM - Initial Contribution
samdanbury 0:cae064bcbe5e 11 *******************************************************************************/
samdanbury 0:cae064bcbe5e 12
samdanbury 0:cae064bcbe5e 13 #include "stdio.h"
samdanbury 0:cae064bcbe5e 14 #include "mbed.h"
samdanbury 0:cae064bcbe5e 15 #include "rtos.h"
samdanbury 0:cae064bcbe5e 16 #include "C12832.h"
samdanbury 0:cae064bcbe5e 17 #include "LM75B.h"
samdanbury 0:cae064bcbe5e 18 #include "MMA7660.h"
samdanbury 0:cae064bcbe5e 19 #include "C12832.h"
samdanbury 0:cae064bcbe5e 20 #include "MQTTEthernetIoT.h"
samdanbury 0:cae064bcbe5e 21 #include "MQTTClient.h"
samdanbury 0:cae064bcbe5e 22 #include "ConfigFile.h"
samdanbury 0:cae064bcbe5e 23
samdanbury 0:cae064bcbe5e 24 #include <string>
samdanbury 0:cae064bcbe5e 25 #include <sstream>
samdanbury 0:cae064bcbe5e 26 #include <algorithm>
samdanbury 0:cae064bcbe5e 27
samdanbury 0:cae064bcbe5e 28 using namespace std;
samdanbury 0:cae064bcbe5e 29
samdanbury 0:cae064bcbe5e 30 #ifdef TARGET_LPC1768
samdanbury 0:cae064bcbe5e 31
samdanbury 0:cae064bcbe5e 32 #warning "Compiling for mbed LPC1768"
samdanbury 0:cae064bcbe5e 33
samdanbury 0:cae064bcbe5e 34 LocalFileSystem local("local");
samdanbury 0:cae064bcbe5e 35 C12832 lcd(p5, p7, p6, p8, p11);
samdanbury 0:cae064bcbe5e 36 DigitalOut led1(LED1);
samdanbury 0:cae064bcbe5e 37 DigitalOut led2(LED2);
samdanbury 0:cae064bcbe5e 38 DigitalOut led3(LED3);
samdanbury 0:cae064bcbe5e 39 MMA7660 MMA(p28, p27);
samdanbury 0:cae064bcbe5e 40 LM75B sensor(p28, p27);
samdanbury 0:cae064bcbe5e 41 DigitalIn Down(p12);
samdanbury 0:cae064bcbe5e 42 DigitalIn Left(p13);
samdanbury 0:cae064bcbe5e 43 DigitalIn Click(p14);
samdanbury 0:cae064bcbe5e 44 DigitalIn Up(p15);
samdanbury 0:cae064bcbe5e 45 DigitalIn Right(p16);
samdanbury 0:cae064bcbe5e 46 AnalogIn ain1(p19);
samdanbury 0:cae064bcbe5e 47 AnalogIn ain2(p20);
samdanbury 0:cae064bcbe5e 48
samdanbury 0:cae064bcbe5e 49 #elif TARGET_K64F
samdanbury 0:cae064bcbe5e 50
samdanbury 0:cae064bcbe5e 51 #warning "Compiling for mbed K64F"
samdanbury 0:cae064bcbe5e 52
samdanbury 0:cae064bcbe5e 53 //#define ORGANISATION "<org>";
samdanbury 0:cae064bcbe5e 54 //#define TYPE "<type>";
samdanbury 0:cae064bcbe5e 55 //#define ID "<id>";
samdanbury 0:cae064bcbe5e 56 //#define AUTHMETHOD "<auth-method>";
samdanbury 0:cae064bcbe5e 57 //#define AUTHTOKEN "<auth-token>";
samdanbury 0:cae064bcbe5e 58
samdanbury 0:cae064bcbe5e 59 C12832 lcd(D11, D13, D12, D7, D10);
samdanbury 0:cae064bcbe5e 60 DigitalOut led1(LED_RED);
samdanbury 0:cae064bcbe5e 61 DigitalOut led2(LED_GREEN);
samdanbury 0:cae064bcbe5e 62 DigitalOut led3(LED_BLUE);
samdanbury 0:cae064bcbe5e 63 MMA7660 MMA(PTE25, PTE24);
samdanbury 0:cae064bcbe5e 64 LM75B sensor(PTE25, PTE24);
samdanbury 0:cae064bcbe5e 65 DigitalIn Up(A2);
samdanbury 0:cae064bcbe5e 66 DigitalIn Down(A3);
samdanbury 0:cae064bcbe5e 67 DigitalIn Right(A4);
samdanbury 0:cae064bcbe5e 68 DigitalIn Left(A5);
samdanbury 0:cae064bcbe5e 69 DigitalIn Click(D4);
samdanbury 0:cae064bcbe5e 70 AnalogIn ain1(A0);
samdanbury 0:cae064bcbe5e 71 AnalogIn ain2(A1);
samdanbury 0:cae064bcbe5e 72
samdanbury 0:cae064bcbe5e 73 #else
samdanbury 0:cae064bcbe5e 74
samdanbury 0:cae064bcbe5e 75 LocalFileSystem local("local");
samdanbury 0:cae064bcbe5e 76 C12832 lcd(D11, D13, D12, D7, D10);
samdanbury 0:cae064bcbe5e 77 DigitalOut led1(LED1);
samdanbury 0:cae064bcbe5e 78 DigitalOut led2(LED2);
samdanbury 0:cae064bcbe5e 79 DigitalOut led3(LED3);
samdanbury 0:cae064bcbe5e 80 MMA7660 MMA(D14, D15);
samdanbury 0:cae064bcbe5e 81 LM75B sensor(D14,D15);
samdanbury 0:cae064bcbe5e 82 DigitalIn Up(A2);
samdanbury 0:cae064bcbe5e 83 DigitalIn Down(A3);
samdanbury 0:cae064bcbe5e 84 DigitalIn Left(A4);
samdanbury 0:cae064bcbe5e 85 DigitalIn Right(A5);
samdanbury 0:cae064bcbe5e 86 DigitalIn Click(D4);
samdanbury 0:cae064bcbe5e 87 AnalogIn ain1 (A0);
samdanbury 0:cae064bcbe5e 88 AnalogIn ain2 (A1);
samdanbury 0:cae064bcbe5e 89
samdanbury 0:cae064bcbe5e 90 #endif
samdanbury 0:cae064bcbe5e 91
samdanbury 0:cae064bcbe5e 92 //Joystick
samdanbury 0:cae064bcbe5e 93 string joystickPos;
samdanbury 0:cae064bcbe5e 94 void joystickThread(void const *args);
samdanbury 0:cae064bcbe5e 95
samdanbury 0:cae064bcbe5e 96 //Commands
samdanbury 0:cae064bcbe5e 97 enum command {
samdanbury 0:cae064bcbe5e 98 blink
samdanbury 0:cae064bcbe5e 99 };
samdanbury 0:cae064bcbe5e 100 command getCommand (std::string const& command);
samdanbury 0:cae064bcbe5e 101 void messageArrived(MQTT::MessageData& md);
samdanbury 0:cae064bcbe5e 102 int interval;
samdanbury 0:cae064bcbe5e 103 string getUUID48();
samdanbury 0:cae064bcbe5e 104
samdanbury 0:cae064bcbe5e 105 //Config
samdanbury 0:cae064bcbe5e 106 void parseConfig();
samdanbury 0:cae064bcbe5e 107 bool quickstartMode = true;
samdanbury 0:cae064bcbe5e 108 string org = "";
samdanbury 0:cae064bcbe5e 109 string type = "";
samdanbury 0:cae064bcbe5e 110 string id = "";
samdanbury 0:cae064bcbe5e 111 string auth_method = "";
samdanbury 0:cae064bcbe5e 112 string auth_token = "";
samdanbury 0:cae064bcbe5e 113 string mac = "";
samdanbury 0:cae064bcbe5e 114
samdanbury 0:cae064bcbe5e 115
samdanbury 0:cae064bcbe5e 116 void parseConfig() {
samdanbury 0:cae064bcbe5e 117
samdanbury 0:cae064bcbe5e 118 ConfigFile cfg;
samdanbury 0:cae064bcbe5e 119
samdanbury 0:cae064bcbe5e 120 char value[30];
samdanbury 0:cae064bcbe5e 121 char value1[30];
samdanbury 0:cae064bcbe5e 122 char value2[30];
samdanbury 0:cae064bcbe5e 123 char value3[30];
samdanbury 0:cae064bcbe5e 124
samdanbury 0:cae064bcbe5e 125 if (cfg.read("/local/device.cfg")) {
samdanbury 0:cae064bcbe5e 126 quickstartMode = false;
samdanbury 0:cae064bcbe5e 127
samdanbury 0:cae064bcbe5e 128 if (cfg.getValue("org", value, sizeof(value))) {
samdanbury 0:cae064bcbe5e 129 stringstream ss(value);
samdanbury 0:cae064bcbe5e 130 ss >> org;
samdanbury 0:cae064bcbe5e 131 }
samdanbury 0:cae064bcbe5e 132 if (cfg.getValue("type", value1, sizeof(value1))) {
samdanbury 0:cae064bcbe5e 133 stringstream ss(value1);
samdanbury 0:cae064bcbe5e 134 ss >> type;
samdanbury 0:cae064bcbe5e 135 }
samdanbury 0:cae064bcbe5e 136 if (cfg.getValue("id", value2, sizeof(value2))) {
samdanbury 0:cae064bcbe5e 137 stringstream ss(value2);
samdanbury 0:cae064bcbe5e 138 ss >> id;
samdanbury 0:cae064bcbe5e 139 }
samdanbury 0:cae064bcbe5e 140 if (cfg.getValue("auth-token", value3, sizeof(value3))) {
samdanbury 0:cae064bcbe5e 141 stringstream ss(value3);
samdanbury 0:cae064bcbe5e 142 ss >> auth_token;
samdanbury 0:cae064bcbe5e 143 }
samdanbury 0:cae064bcbe5e 144
samdanbury 0:cae064bcbe5e 145 } else {
samdanbury 0:cae064bcbe5e 146 quickstartMode = true;
samdanbury 0:cae064bcbe5e 147 org = "quickstart";
samdanbury 0:cae064bcbe5e 148 type = "iotsample-mbed-lpc1768";
samdanbury 0:cae064bcbe5e 149 id = mac;
samdanbury 0:cae064bcbe5e 150 }
samdanbury 0:cae064bcbe5e 151
samdanbury 0:cae064bcbe5e 152 #ifdef TARGET_K64F
samdanbury 0:cae064bcbe5e 153 #ifdef ORGANISATION
samdanbury 0:cae064bcbe5e 154 quickstartMode = false;
samdanbury 0:cae064bcbe5e 155 org = ORGANISATION;
samdanbury 0:cae064bcbe5e 156
samdanbury 0:cae064bcbe5e 157 #ifdef TYPE
samdanbury 0:cae064bcbe5e 158 type = TYPE;
samdanbury 0:cae064bcbe5e 159 #else
samdanbury 0:cae064bcbe5e 160 lcd.printf("Type is not defined");
samdanbury 0:cae064bcbe5e 161 #endif
samdanbury 0:cae064bcbe5e 162
samdanbury 0:cae064bcbe5e 163 #ifdef ID
samdanbury 0:cae064bcbe5e 164 id = ID;
samdanbury 0:cae064bcbe5e 165 #else
samdanbury 0:cae064bcbe5e 166 lcd.printf("ID is not defined");
samdanbury 0:cae064bcbe5e 167 #endif
samdanbury 0:cae064bcbe5e 168
samdanbury 0:cae064bcbe5e 169 #ifdef AUTHMETHOD
samdanbury 0:cae064bcbe5e 170 auth_method = AUTHMETHOD;
samdanbury 0:cae064bcbe5e 171 #else
samdanbury 0:cae064bcbe5e 172 lcd.printf("Auth method is not defined");
samdanbury 0:cae064bcbe5e 173 #endif
samdanbury 0:cae064bcbe5e 174
samdanbury 0:cae064bcbe5e 175 #ifdef AUTHTOKEN
samdanbury 0:cae064bcbe5e 176 auth_token = AUTHTOKEN;
samdanbury 0:cae064bcbe5e 177 #else
samdanbury 0:cae064bcbe5e 178 lcd.printf("Auth token is not defined");
samdanbury 0:cae064bcbe5e 179 #endif
samdanbury 0:cae064bcbe5e 180 #endif
samdanbury 0:cae064bcbe5e 181 #endif
samdanbury 0:cae064bcbe5e 182 }
samdanbury 0:cae064bcbe5e 183
samdanbury 0:cae064bcbe5e 184 int main()
samdanbury 0:cae064bcbe5e 185 {
samdanbury 0:cae064bcbe5e 186 lcd.locate(0,0);
samdanbury 0:cae064bcbe5e 187 lcd.printf("ARM IBM IoT\n");
samdanbury 0:cae064bcbe5e 188 lcd.printf("Network connecting\n");
samdanbury 0:cae064bcbe5e 189
samdanbury 0:cae064bcbe5e 190 //Connect to network
samdanbury 0:cae064bcbe5e 191 MQTTEthernetIoT ipstack = MQTTEthernetIoT();
samdanbury 0:cae064bcbe5e 192 MQTT::Client<MQTTEthernetIoT, Countdown, 250> client = MQTT::Client<MQTTEthernetIoT, Countdown, 250>(ipstack);
samdanbury 0:cae064bcbe5e 193
samdanbury 0:cae064bcbe5e 194 //Obtain mac address of mbed
samdanbury 0:cae064bcbe5e 195 #ifdef TARGET_K64F
samdanbury 0:cae064bcbe5e 196 mac = getUUID48();
samdanbury 0:cae064bcbe5e 197 #else
samdanbury 0:cae064bcbe5e 198 mac = ipstack.getMACAddress();
samdanbury 0:cae064bcbe5e 199
samdanbury 0:cae064bcbe5e 200 //Remove colons from mac address
samdanbury 0:cae064bcbe5e 201 mac.erase(remove(mac.begin(), mac.end(), ':'), mac.end());
samdanbury 0:cae064bcbe5e 202 #endif
samdanbury 0:cae064bcbe5e 203
samdanbury 0:cae064bcbe5e 204 //Parse config file if present
samdanbury 0:cae064bcbe5e 205 parseConfig();
samdanbury 0:cae064bcbe5e 206
samdanbury 0:cae064bcbe5e 207 //TCP Connect
samdanbury 0:cae064bcbe5e 208 lcd.printf("Attempting TCP connect");
samdanbury 0:cae064bcbe5e 209 string ip = org + ".messaging.internetofthings.ibmcloud.com";
samdanbury 0:cae064bcbe5e 210
samdanbury 0:cae064bcbe5e 211 char* hostname = new char[ip.length() + 1];
samdanbury 0:cae064bcbe5e 212 strcpy(hostname, ip.c_str());
samdanbury 0:cae064bcbe5e 213
samdanbury 0:cae064bcbe5e 214 int port = 1883;
samdanbury 0:cae064bcbe5e 215 int rc = ipstack.connect(hostname, port);
samdanbury 0:cae064bcbe5e 216 if (rc != 0) {
samdanbury 0:cae064bcbe5e 217 lcd.printf("TCP connect failed");
samdanbury 0:cae064bcbe5e 218 }
samdanbury 0:cae064bcbe5e 219
samdanbury 0:cae064bcbe5e 220 //Construct clientId based on config
samdanbury 0:cae064bcbe5e 221 string str = string("d:") + org + ":" + type + ":" + id;
samdanbury 0:cae064bcbe5e 222 char clientId[str.size()];
samdanbury 0:cae064bcbe5e 223 memcpy(clientId, str.c_str(), str.size() + 1);
samdanbury 0:cae064bcbe5e 224
samdanbury 0:cae064bcbe5e 225 //MQTT Connect
samdanbury 0:cae064bcbe5e 226 lcd.cls();
samdanbury 0:cae064bcbe5e 227 lcd.locate(0,0);
samdanbury 0:cae064bcbe5e 228 lcd.printf("Attempting MQTT Connect\n");
samdanbury 0:cae064bcbe5e 229 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
samdanbury 0:cae064bcbe5e 230 data.MQTTVersion = 3;
samdanbury 0:cae064bcbe5e 231 data.clientID.cstring = clientId;
samdanbury 0:cae064bcbe5e 232
samdanbury 0:cae064bcbe5e 233 if (!quickstartMode) {
samdanbury 0:cae064bcbe5e 234 char* password = new char[auth_token.length() + 1];
samdanbury 0:cae064bcbe5e 235 strcpy(password, auth_token.c_str());
samdanbury 0:cae064bcbe5e 236
samdanbury 0:cae064bcbe5e 237 data.username.cstring = "use-token-auth";
samdanbury 0:cae064bcbe5e 238 data.password.cstring = password;
samdanbury 0:cae064bcbe5e 239 }
samdanbury 0:cae064bcbe5e 240
samdanbury 0:cae064bcbe5e 241 if ((rc = client.connect(&data)) != 0)
samdanbury 0:cae064bcbe5e 242 lcd.printf("rc from MQTT connect is %d\n", rc);
samdanbury 0:cae064bcbe5e 243
samdanbury 0:cae064bcbe5e 244 if (!quickstartMode) {
samdanbury 0:cae064bcbe5e 245 lcd.printf("Attempting MQTT Subscribe\n");
samdanbury 0:cae064bcbe5e 246 char* subTopic = "iot-2/cmd/+/fmt/json";
samdanbury 0:cae064bcbe5e 247 if ((rc = client.subscribe(subTopic, MQTT::QOS1, messageArrived)) != 0)
samdanbury 0:cae064bcbe5e 248 lcd.printf("rc from MQTT subscribe is %d\n", rc);
samdanbury 0:cae064bcbe5e 249 }
samdanbury 0:cae064bcbe5e 250
samdanbury 0:cae064bcbe5e 251 //Initialize lcd
samdanbury 0:cae064bcbe5e 252 lcd.cls();
samdanbury 0:cae064bcbe5e 253 lcd.locate(0,0);
samdanbury 0:cae064bcbe5e 254 lcd.printf("http://ibm.biz/iotcloud\n");
samdanbury 0:cae064bcbe5e 255 lcd.printf("Mac address: %s\n", mac);
samdanbury 0:cae064bcbe5e 256
samdanbury 0:cae064bcbe5e 257 //Start thread to read data from joystick
samdanbury 0:cae064bcbe5e 258 joystickPos = "CENTRE";
samdanbury 0:cae064bcbe5e 259 Thread jThd(joystickThread);
samdanbury 0:cae064bcbe5e 260
samdanbury 0:cae064bcbe5e 261 interval = 0;
samdanbury 0:cae064bcbe5e 262 int i = 0;
samdanbury 0:cae064bcbe5e 263
samdanbury 0:cae064bcbe5e 264
samdanbury 0:cae064bcbe5e 265 while(1)
samdanbury 0:cae064bcbe5e 266 {
samdanbury 0:cae064bcbe5e 267 //Message published every second
samdanbury 0:cae064bcbe5e 268 if (i == 100) {
samdanbury 0:cae064bcbe5e 269 //MQTT Publish
samdanbury 0:cae064bcbe5e 270 MQTT::Message message;
samdanbury 0:cae064bcbe5e 271 char* pubTopic = "iot-2/evt/status/fmt/json";
samdanbury 0:cae064bcbe5e 272
samdanbury 0:cae064bcbe5e 273 char buf[250];
samdanbury 0:cae064bcbe5e 274 sprintf(buf,
samdanbury 0:cae064bcbe5e 275 "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}",
samdanbury 0:cae064bcbe5e 276 MMA.x(), MMA.y(), MMA.z(),
samdanbury 0:cae064bcbe5e 277 sensor.temp(),
samdanbury 0:cae064bcbe5e 278 joystickPos,
samdanbury 0:cae064bcbe5e 279 ain1.read(),
samdanbury 0:cae064bcbe5e 280 ain2.read());
samdanbury 0:cae064bcbe5e 281 message.qos = MQTT::QOS0;
samdanbury 0:cae064bcbe5e 282 message.retained = false;
samdanbury 0:cae064bcbe5e 283 message.dup = false;
samdanbury 0:cae064bcbe5e 284 message.payload = (void*)buf;
samdanbury 0:cae064bcbe5e 285 message.payloadlen = strlen(buf);
samdanbury 0:cae064bcbe5e 286
samdanbury 0:cae064bcbe5e 287 if ((rc = client.publish(pubTopic, &message)) != 0)
samdanbury 0:cae064bcbe5e 288 lcd.printf("MQTT publish failed");
samdanbury 0:cae064bcbe5e 289
samdanbury 0:cae064bcbe5e 290 i = 0;
samdanbury 0:cae064bcbe5e 291 led1 = !led1;
samdanbury 0:cae064bcbe5e 292 }
samdanbury 0:cae064bcbe5e 293
samdanbury 0:cae064bcbe5e 294 if (interval == 0) {
samdanbury 0:cae064bcbe5e 295 led2 = 0;
samdanbury 0:cae064bcbe5e 296 } else {
samdanbury 0:cae064bcbe5e 297 if (i%(interval)==0) {
samdanbury 0:cae064bcbe5e 298 led2 = !led2;
samdanbury 0:cae064bcbe5e 299 }
samdanbury 0:cae064bcbe5e 300 }
samdanbury 0:cae064bcbe5e 301
samdanbury 0:cae064bcbe5e 302 wait(0.01);
samdanbury 0:cae064bcbe5e 303 i++;
samdanbury 0:cae064bcbe5e 304 client.yield(1);
samdanbury 0:cae064bcbe5e 305 }
samdanbury 0:cae064bcbe5e 306 }
samdanbury 0:cae064bcbe5e 307
samdanbury 0:cae064bcbe5e 308 void messageArrived(MQTT::MessageData& md) {
samdanbury 0:cae064bcbe5e 309 MQTT::Message &message = md.message;
samdanbury 0:cae064bcbe5e 310
samdanbury 0:cae064bcbe5e 311 char* topic = new char[md.topicName.lenstring.len + 1];
samdanbury 0:cae064bcbe5e 312 sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data);
samdanbury 0:cae064bcbe5e 313
samdanbury 0:cae064bcbe5e 314 char* payload = new char[message.payloadlen + 1];
samdanbury 0:cae064bcbe5e 315 sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload);
samdanbury 0:cae064bcbe5e 316
samdanbury 0:cae064bcbe5e 317 string topicStr = topic;
samdanbury 0:cae064bcbe5e 318 string payloadStr = payload;
samdanbury 0:cae064bcbe5e 319
samdanbury 0:cae064bcbe5e 320 //Command topic: iot-2/cmd/blink/fmt/json
samdanbury 0:cae064bcbe5e 321 string cmd = topicStr.substr(10, topicStr.find("/fmt/") - 10);
samdanbury 0:cae064bcbe5e 322
samdanbury 0:cae064bcbe5e 323 switch(getCommand(cmd)) {
samdanbury 0:cae064bcbe5e 324 case blink: {
samdanbury 0:cae064bcbe5e 325 string str = payloadStr.substr(8, payloadStr.find("}") - 8);
samdanbury 0:cae064bcbe5e 326 int rate = atoi(str.c_str());
samdanbury 0:cae064bcbe5e 327
samdanbury 0:cae064bcbe5e 328 if (rate == 0) {
samdanbury 0:cae064bcbe5e 329 interval = 0;
samdanbury 0:cae064bcbe5e 330 } else if (rate > 50) {
samdanbury 0:cae064bcbe5e 331 interval = 1;
samdanbury 0:cae064bcbe5e 332 } else if (rate > 0) {
samdanbury 0:cae064bcbe5e 333 interval = 50/rate;
samdanbury 0:cae064bcbe5e 334 }
samdanbury 0:cae064bcbe5e 335
samdanbury 0:cae064bcbe5e 336 break;
samdanbury 0:cae064bcbe5e 337 }
samdanbury 0:cae064bcbe5e 338 default:
samdanbury 0:cae064bcbe5e 339 lcd.printf("Unsupported command: %s\n", cmd);
samdanbury 0:cae064bcbe5e 340 }
samdanbury 0:cae064bcbe5e 341
samdanbury 0:cae064bcbe5e 342 if (topic) {
samdanbury 0:cae064bcbe5e 343 delete[] topic;
samdanbury 0:cae064bcbe5e 344 }
samdanbury 0:cae064bcbe5e 345 if (payload) {
samdanbury 0:cae064bcbe5e 346 delete[] payload;
samdanbury 0:cae064bcbe5e 347 }
samdanbury 0:cae064bcbe5e 348
samdanbury 0:cae064bcbe5e 349 }
samdanbury 0:cae064bcbe5e 350
samdanbury 0:cae064bcbe5e 351 command getCommand (string const& command) {
samdanbury 0:cae064bcbe5e 352 if (command == "blink")
samdanbury 0:cae064bcbe5e 353 return blink;
samdanbury 0:cae064bcbe5e 354 }
samdanbury 0:cae064bcbe5e 355
samdanbury 0:cae064bcbe5e 356 void joystickThread(void const *args) {
samdanbury 0:cae064bcbe5e 357 while (true) {
samdanbury 0:cae064bcbe5e 358 if (Down)
samdanbury 0:cae064bcbe5e 359 joystickPos = "DOWN";
samdanbury 0:cae064bcbe5e 360 else if (Left)
samdanbury 0:cae064bcbe5e 361 joystickPos = "LEFT";
samdanbury 0:cae064bcbe5e 362 else if (Click)
samdanbury 0:cae064bcbe5e 363 joystickPos = "CLICK";
samdanbury 0:cae064bcbe5e 364 else if (Up)
samdanbury 0:cae064bcbe5e 365 joystickPos = "UP";
samdanbury 0:cae064bcbe5e 366 else if (Right)
samdanbury 0:cae064bcbe5e 367 joystickPos = "RIGHT";
samdanbury 0:cae064bcbe5e 368 else
samdanbury 0:cae064bcbe5e 369 joystickPos = "CENTRE";
samdanbury 0:cae064bcbe5e 370 }
samdanbury 0:cae064bcbe5e 371 }
samdanbury 0:cae064bcbe5e 372
samdanbury 0:cae064bcbe5e 373 string getUUID48 () {
samdanbury 0:cae064bcbe5e 374
samdanbury 0:cae064bcbe5e 375 unsigned int UUID_LOC_WORD0 = 0x40048060;
samdanbury 0:cae064bcbe5e 376 unsigned int UUID_LOC_WORD1 = 0x4004805C;
samdanbury 0:cae064bcbe5e 377
samdanbury 0:cae064bcbe5e 378
samdanbury 0:cae064bcbe5e 379 // Fetch word 0
samdanbury 0:cae064bcbe5e 380 uint32_t Word0 = *(uint32_t *)UUID_LOC_WORD0;
samdanbury 0:cae064bcbe5e 381
samdanbury 0:cae064bcbe5e 382 // Fetch word 1
samdanbury 0:cae064bcbe5e 383 // we only want bottom 16 bits of word1 (MAC bits 32-47)
samdanbury 0:cae064bcbe5e 384 // and bit 9 forced to 1, bit 8 forced to 0
samdanbury 0:cae064bcbe5e 385 // Locally administered MAC, reduced conflicts
samdanbury 0:cae064bcbe5e 386 // http://en.wikipedia.org/wiki/MAC_address
samdanbury 0:cae064bcbe5e 387 uint32_t Word1 = *(uint32_t *)UUID_LOC_WORD1;
samdanbury 0:cae064bcbe5e 388 Word1 |= 0x00000200;
samdanbury 0:cae064bcbe5e 389 Word1 &= 0x0000FEFF;
samdanbury 0:cae064bcbe5e 390
samdanbury 0:cae064bcbe5e 391 string sd;
samdanbury 0:cae064bcbe5e 392 char stemp[100] = "";
samdanbury 0:cae064bcbe5e 393 snprintf(stemp, 100, "%4X%08X", Word1,Word0); // I use the safer version of sprintf() -- snprintf()
samdanbury 0:cae064bcbe5e 394 sd = stemp; // the contents of sd are now "This is a string!"
samdanbury 0:cae064bcbe5e 395
samdanbury 0:cae064bcbe5e 396 return (sd);
samdanbury 0:cae064bcbe5e 397 }