Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:4e40266a9d41, 2013-11-19 (annotated)
- Committer:
- nickludlam
- Date:
- Tue Nov 19 16:19:24 2013 +0000
- Revision:
- 0:4e40266a9d41
Example mbed code for the Little Counter tutorial
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nickludlam | 0:4e40266a9d41 | 1 | /* |
| nickludlam | 0:4e40266a9d41 | 2 | LittleCounter - A simple demonstration of how you use the BERG Cloud mbed |
| nickludlam | 0:4e40266a9d41 | 3 | client libraries to fetch and send data to BERG Cloud. For |
| nickludlam | 0:4e40266a9d41 | 4 | more info see http://bergcloud.com/ |
| nickludlam | 0:4e40266a9d41 | 5 | |
| nickludlam | 0:4e40266a9d41 | 6 | This example code is in the public domain. |
| nickludlam | 0:4e40266a9d41 | 7 | |
| nickludlam | 0:4e40266a9d41 | 8 | https://github.com/bergcloud/devshield-mbed |
| nickludlam | 0:4e40266a9d41 | 9 | */ |
| nickludlam | 0:4e40266a9d41 | 10 | |
| nickludlam | 0:4e40266a9d41 | 11 | #include "mbed.h" |
| nickludlam | 0:4e40266a9d41 | 12 | #include "BERGCloudMbed.h" |
| nickludlam | 0:4e40266a9d41 | 13 | #include <sstream> // std::stringstream |
| nickludlam | 0:4e40266a9d41 | 14 | |
| nickludlam | 0:4e40266a9d41 | 15 | // Select the pins to be used for the SPI interface |
| nickludlam | 0:4e40266a9d41 | 16 | #if defined(TARGET_LPC176X) || defined(TARGET_LPC11UXX) |
| nickludlam | 0:4e40266a9d41 | 17 | // mbed DIP40 pinout |
| nickludlam | 0:4e40266a9d41 | 18 | #define MOSI_PIN p5 |
| nickludlam | 0:4e40266a9d41 | 19 | #define MISO_PIN p6 |
| nickludlam | 0:4e40266a9d41 | 20 | #define SCLK_PIN p7 |
| nickludlam | 0:4e40266a9d41 | 21 | #define nSSEL_PIN p8 |
| nickludlam | 0:4e40266a9d41 | 22 | #endif |
| nickludlam | 0:4e40266a9d41 | 23 | |
| nickludlam | 0:4e40266a9d41 | 24 | #if defined(TARGET_KL25Z) |
| nickludlam | 0:4e40266a9d41 | 25 | // Arduino compatible pinout |
| nickludlam | 0:4e40266a9d41 | 26 | #define MOSI_PIN D11 |
| nickludlam | 0:4e40266a9d41 | 27 | #define MISO_PIN D12 |
| nickludlam | 0:4e40266a9d41 | 28 | #define SCLK_PIN D13 |
| nickludlam | 0:4e40266a9d41 | 29 | #define nSSEL_PIN D10 |
| nickludlam | 0:4e40266a9d41 | 30 | #endif |
| nickludlam | 0:4e40266a9d41 | 31 | |
| nickludlam | 0:4e40266a9d41 | 32 | #if !defined(MOSI_PIN) || !defined(MISO_PIN) || !defined(SCLK_PIN) || !defined(nSSEL_PIN) |
| nickludlam | 0:4e40266a9d41 | 33 | #error Please add the pin names for your microcontroller. |
| nickludlam | 0:4e40266a9d41 | 34 | #endif |
| nickludlam | 0:4e40266a9d41 | 35 | |
| nickludlam | 0:4e40266a9d41 | 36 | // The Project Key ties this code into a Project on developer.bergcloud.com |
| nickludlam | 0:4e40266a9d41 | 37 | const uint8_t PROJECT_KEY[BC_KEY_SIZE_BYTES] = \ |
| nickludlam | 0:4e40266a9d41 | 38 | {0x8B,0x05,0xF7,0x25,0x10,0x54,0x0A,0xE4,0x7C,0x35,0xEE,0xE7,0x26,0xDC,0xD5,0xA8}; |
| nickludlam | 0:4e40266a9d41 | 39 | |
| nickludlam | 0:4e40266a9d41 | 40 | // The version of your code |
| nickludlam | 0:4e40266a9d41 | 41 | #define VERSION 0x0001 |
| nickludlam | 0:4e40266a9d41 | 42 | |
| nickludlam | 0:4e40266a9d41 | 43 | // Define your commands and events here, according to the schema from bergcloud.com |
| nickludlam | 0:4e40266a9d41 | 44 | #define COMMAND_SET_COUNTER 0x01 |
| nickludlam | 0:4e40266a9d41 | 45 | #define COMMAND_DISPLAY_TEXT 0x02 |
| nickludlam | 0:4e40266a9d41 | 46 | #define EVENT_COUNTER_CHANGED 0x01 |
| nickludlam | 0:4e40266a9d41 | 47 | |
| nickludlam | 0:4e40266a9d41 | 48 | // Forward declarations |
| nickludlam | 0:4e40266a9d41 | 49 | void loop(void); |
| nickludlam | 0:4e40266a9d41 | 50 | |
| nickludlam | 0:4e40266a9d41 | 51 | // These two methods correspond to the two commands defined within the Little Counter |
| nickludlam | 0:4e40266a9d41 | 52 | // project on the BERG Cloud developer site |
| nickludlam | 0:4e40266a9d41 | 53 | void handleSetCounter(BERGCloudMessage&); |
| nickludlam | 0:4e40266a9d41 | 54 | void handleGreet(BERGCloudMessage&); |
| nickludlam | 0:4e40266a9d41 | 55 | |
| nickludlam | 0:4e40266a9d41 | 56 | // Create an instance of the BERGCloudMbed class |
| nickludlam | 0:4e40266a9d41 | 57 | BERGCloudMbed BERGCloud; |
| nickludlam | 0:4e40266a9d41 | 58 | |
| nickludlam | 0:4e40266a9d41 | 59 | // The counter we will increment and send up to the cloud |
| nickludlam | 0:4e40266a9d41 | 60 | int32_t counter; |
| nickludlam | 0:4e40266a9d41 | 61 | |
| nickludlam | 0:4e40266a9d41 | 62 | int main(void) |
| nickludlam | 0:4e40266a9d41 | 63 | { |
| nickludlam | 0:4e40266a9d41 | 64 | BERGCloud.begin(MOSI_PIN, MISO_PIN, SCLK_PIN, nSSEL_PIN); |
| nickludlam | 0:4e40266a9d41 | 65 | printf("--- mbed reset ---\r\n"); |
| nickludlam | 0:4e40266a9d41 | 66 | |
| nickludlam | 0:4e40266a9d41 | 67 | counter = 0; // Initialise our counter |
| nickludlam | 0:4e40266a9d41 | 68 | |
| nickludlam | 0:4e40266a9d41 | 69 | // Attempt to connect with our project key and build version |
| nickludlam | 0:4e40266a9d41 | 70 | if (BERGCloud.connect(PROJECT_KEY, VERSION)) { |
| nickludlam | 0:4e40266a9d41 | 71 | printf("Connected to network"); |
| nickludlam | 0:4e40266a9d41 | 72 | } else { |
| nickludlam | 0:4e40266a9d41 | 73 | printf("BERGCloud.connect() returned false."); |
| nickludlam | 0:4e40266a9d41 | 74 | } |
| nickludlam | 0:4e40266a9d41 | 75 | |
| nickludlam | 0:4e40266a9d41 | 76 | // Run continuously |
| nickludlam | 0:4e40266a9d41 | 77 | while (1) |
| nickludlam | 0:4e40266a9d41 | 78 | { |
| nickludlam | 0:4e40266a9d41 | 79 | // The loop() function handles commands and events |
| nickludlam | 0:4e40266a9d41 | 80 | loop(); |
| nickludlam | 0:4e40266a9d41 | 81 | } |
| nickludlam | 0:4e40266a9d41 | 82 | |
| nickludlam | 0:4e40266a9d41 | 83 | } |
| nickludlam | 0:4e40266a9d41 | 84 | |
| nickludlam | 0:4e40266a9d41 | 85 | void loop(void) |
| nickludlam | 0:4e40266a9d41 | 86 | { |
| nickludlam | 0:4e40266a9d41 | 87 | // The ID of the command we've received. |
| nickludlam | 0:4e40266a9d41 | 88 | uint8_t commandID; |
| nickludlam | 0:4e40266a9d41 | 89 | |
| nickludlam | 0:4e40266a9d41 | 90 | // The command and event objects we use below |
| nickludlam | 0:4e40266a9d41 | 91 | BERGCloudMessage command, event; |
| nickludlam | 0:4e40266a9d41 | 92 | |
| nickludlam | 0:4e40266a9d41 | 93 | // Some simple string manipulation |
| nickludlam | 0:4e40266a9d41 | 94 | |
| nickludlam | 0:4e40266a9d41 | 95 | /////////////////////////////////////////////////////////////// |
| nickludlam | 0:4e40266a9d41 | 96 | // Fetching commands // |
| nickludlam | 0:4e40266a9d41 | 97 | /////////////////////////////////////////////////////////////// |
| nickludlam | 0:4e40266a9d41 | 98 | |
| nickludlam | 0:4e40266a9d41 | 99 | printf("Poll for command... "); |
| nickludlam | 0:4e40266a9d41 | 100 | if (BERGCloud.pollForCommand(command, commandID)) { |
| nickludlam | 0:4e40266a9d41 | 101 | |
| nickludlam | 0:4e40266a9d41 | 102 | // Print the |
| nickludlam | 0:4e40266a9d41 | 103 | printf("got command with ID: %u\r\n", commandID); |
| nickludlam | 0:4e40266a9d41 | 104 | |
| nickludlam | 0:4e40266a9d41 | 105 | // Here we can map the command IDs to method calls within our code |
| nickludlam | 0:4e40266a9d41 | 106 | switch (commandID) { |
| nickludlam | 0:4e40266a9d41 | 107 | case COMMAND_SET_COUNTER: |
| nickludlam | 0:4e40266a9d41 | 108 | // Command one contains an integer |
| nickludlam | 0:4e40266a9d41 | 109 | handleSetCounter(command); |
| nickludlam | 0:4e40266a9d41 | 110 | break; |
| nickludlam | 0:4e40266a9d41 | 111 | case COMMAND_DISPLAY_TEXT: |
| nickludlam | 0:4e40266a9d41 | 112 | // Command two is a simple message containing a string |
| nickludlam | 0:4e40266a9d41 | 113 | handleGreet(command); |
| nickludlam | 0:4e40266a9d41 | 114 | break; |
| nickludlam | 0:4e40266a9d41 | 115 | // Default case, we don't have a match for the Command ID |
| nickludlam | 0:4e40266a9d41 | 116 | default: |
| nickludlam | 0:4e40266a9d41 | 117 | printf("WARNING: Unknown command\r\n"); |
| nickludlam | 0:4e40266a9d41 | 118 | } |
| nickludlam | 0:4e40266a9d41 | 119 | } else { |
| nickludlam | 0:4e40266a9d41 | 120 | // No command! |
| nickludlam | 0:4e40266a9d41 | 121 | printf("none.\r\n"); |
| nickludlam | 0:4e40266a9d41 | 122 | } |
| nickludlam | 0:4e40266a9d41 | 123 | |
| nickludlam | 0:4e40266a9d41 | 124 | /////////////////////////////////////////////////////////////// |
| nickludlam | 0:4e40266a9d41 | 125 | // Sending events // |
| nickludlam | 0:4e40266a9d41 | 126 | /////////////////////////////////////////////////////////////// |
| nickludlam | 0:4e40266a9d41 | 127 | |
| nickludlam | 0:4e40266a9d41 | 128 | printf("Sending an event... "); |
| nickludlam | 0:4e40266a9d41 | 129 | |
| nickludlam | 0:4e40266a9d41 | 130 | // In this Little Counter example we send up a string and a counter |
| nickludlam | 0:4e40266a9d41 | 131 | // |
| nickludlam | 0:4e40266a9d41 | 132 | // Packing is very straight forward. Just define your |
| nickludlam | 0:4e40266a9d41 | 133 | // BERGCloudMessage object and call pack() passing in each type |
| nickludlam | 0:4e40266a9d41 | 134 | // you wish to encode. |
| nickludlam | 0:4e40266a9d41 | 135 | |
| nickludlam | 0:4e40266a9d41 | 136 | // There is currently a 64 byte limit on the size of events, so be |
| nickludlam | 0:4e40266a9d41 | 137 | // careful with how much data you're sending up! |
| nickludlam | 0:4e40266a9d41 | 138 | |
| nickludlam | 0:4e40266a9d41 | 139 | event.pack("BERG"); // Pack a string |
| nickludlam | 0:4e40266a9d41 | 140 | event.pack(counter); // Pack an unsigned int32 |
| nickludlam | 0:4e40266a9d41 | 141 | |
| nickludlam | 0:4e40266a9d41 | 142 | // Send the event object |
| nickludlam | 0:4e40266a9d41 | 143 | if (BERGCloud.sendEvent(EVENT_COUNTER_CHANGED, event)) { |
| nickludlam | 0:4e40266a9d41 | 144 | printf("ok\r\n"); |
| nickludlam | 0:4e40266a9d41 | 145 | } else { |
| nickludlam | 0:4e40266a9d41 | 146 | printf("failed/busy\r\n"); |
| nickludlam | 0:4e40266a9d41 | 147 | } |
| nickludlam | 0:4e40266a9d41 | 148 | |
| nickludlam | 0:4e40266a9d41 | 149 | counter++; // Increment our counter each time we loop around |
| nickludlam | 0:4e40266a9d41 | 150 | |
| nickludlam | 0:4e40266a9d41 | 151 | // A simple delay to rate limit what we send up to the cloud |
| nickludlam | 0:4e40266a9d41 | 152 | wait_ms(5000); |
| nickludlam | 0:4e40266a9d41 | 153 | } |
| nickludlam | 0:4e40266a9d41 | 154 | |
| nickludlam | 0:4e40266a9d41 | 155 | void handleSetCounter(BERGCloudMessage &command) { |
| nickludlam | 0:4e40266a9d41 | 156 | string prefixText; |
| nickludlam | 0:4e40266a9d41 | 157 | int32_t newCounterVal; |
| nickludlam | 0:4e40266a9d41 | 158 | stringstream counterText; |
| nickludlam | 0:4e40266a9d41 | 159 | |
| nickludlam | 0:4e40266a9d41 | 160 | prefixText = "Counter set to"; |
| nickludlam | 0:4e40266a9d41 | 161 | |
| nickludlam | 0:4e40266a9d41 | 162 | // We're expecting an integer value for the counter |
| nickludlam | 0:4e40266a9d41 | 163 | if (command.unpack(newCounterVal)) { |
| nickludlam | 0:4e40266a9d41 | 164 | |
| nickludlam | 0:4e40266a9d41 | 165 | printf("Decoded newCounterVal as: %i\r\n", newCounterVal); |
| nickludlam | 0:4e40266a9d41 | 166 | |
| nickludlam | 0:4e40266a9d41 | 167 | // Set the global to our new value |
| nickludlam | 0:4e40266a9d41 | 168 | counter = newCounterVal; |
| nickludlam | 0:4e40266a9d41 | 169 | |
| nickludlam | 0:4e40266a9d41 | 170 | // Convert to string (see std::stringstream) |
| nickludlam | 0:4e40266a9d41 | 171 | counterText << newCounterVal; |
| nickludlam | 0:4e40266a9d41 | 172 | |
| nickludlam | 0:4e40266a9d41 | 173 | // Show the string on the OLED screen with display() |
| nickludlam | 0:4e40266a9d41 | 174 | BERGCloud.clearDisplay(); |
| nickludlam | 0:4e40266a9d41 | 175 | |
| nickludlam | 0:4e40266a9d41 | 176 | // Print in reverse order |
| nickludlam | 0:4e40266a9d41 | 177 | BERGCloud.display(counterText.str().c_str()); |
| nickludlam | 0:4e40266a9d41 | 178 | BERGCloud.display(prefixText.c_str()); |
| nickludlam | 0:4e40266a9d41 | 179 | } else { |
| nickludlam | 0:4e40266a9d41 | 180 | printf("WARNING: unpacking the new counter value failed\r\n"); |
| nickludlam | 0:4e40266a9d41 | 181 | } |
| nickludlam | 0:4e40266a9d41 | 182 | } |
| nickludlam | 0:4e40266a9d41 | 183 | |
| nickludlam | 0:4e40266a9d41 | 184 | void handleGreet(BERGCloudMessage &command) { |
| nickludlam | 0:4e40266a9d41 | 185 | string prefixText, suffixText, finalText; |
| nickludlam | 0:4e40266a9d41 | 186 | int32_t number; |
| nickludlam | 0:4e40266a9d41 | 187 | |
| nickludlam | 0:4e40266a9d41 | 188 | prefixText = "Hello, "; |
| nickludlam | 0:4e40266a9d41 | 189 | |
| nickludlam | 0:4e40266a9d41 | 190 | // We're expecting a string and a number for display-text, so |
| nickludlam | 0:4e40266a9d41 | 191 | // we attempt to decode these types in turn |
| nickludlam | 0:4e40266a9d41 | 192 | |
| nickludlam | 0:4e40266a9d41 | 193 | if (command.unpack(suffixText)) { |
| nickludlam | 0:4e40266a9d41 | 194 | printf("Decoded text: '%s'\r\n", suffixText.c_str()); |
| nickludlam | 0:4e40266a9d41 | 195 | |
| nickludlam | 0:4e40266a9d41 | 196 | // Concatenate our strings |
| nickludlam | 0:4e40266a9d41 | 197 | finalText = prefixText + suffixText; |
| nickludlam | 0:4e40266a9d41 | 198 | |
| nickludlam | 0:4e40266a9d41 | 199 | // Show the string on the OLED screen with display() |
| nickludlam | 0:4e40266a9d41 | 200 | BERGCloud.clearDisplay(); |
| nickludlam | 0:4e40266a9d41 | 201 | BERGCloud.display(finalText.c_str()); |
| nickludlam | 0:4e40266a9d41 | 202 | } else { |
| nickludlam | 0:4e40266a9d41 | 203 | printf("WARNING: unpacking text failed\r\n"); |
| nickludlam | 0:4e40266a9d41 | 204 | } |
| nickludlam | 0:4e40266a9d41 | 205 | |
| nickludlam | 0:4e40266a9d41 | 206 | // For this command, we can optionally be passed an integer |
| nickludlam | 0:4e40266a9d41 | 207 | // and we log this out on the serial console. |
| nickludlam | 0:4e40266a9d41 | 208 | // |
| nickludlam | 0:4e40266a9d41 | 209 | // This is to demonstrate serializing multiple values within |
| nickludlam | 0:4e40266a9d41 | 210 | // one command. |
| nickludlam | 0:4e40266a9d41 | 211 | |
| nickludlam | 0:4e40266a9d41 | 212 | if (command.unpack(number)) { |
| nickludlam | 0:4e40266a9d41 | 213 | printf("Decoded number: %i\r\n", number); |
| nickludlam | 0:4e40266a9d41 | 214 | } else { |
| nickludlam | 0:4e40266a9d41 | 215 | printf("No additional number given\r\n"); |
| nickludlam | 0:4e40266a9d41 | 216 | } |
| nickludlam | 0:4e40266a9d41 | 217 | } |