iv123 lin
/
connected-lights
sample_pir-lights_rgb
docs/3_software.md@0:7a352727249b, 2017-06-18 (annotated)
- Committer:
- iv123
- Date:
- Sun Jun 18 10:14:56 2017 +0000
- Revision:
- 0:7a352727249b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iv123 | 0:7a352727249b | 1 | # Writing the software |
iv123 | 0:7a352727249b | 2 | |
iv123 | 0:7a352727249b | 3 | Now we can write some software to verify that the circuit works. We'll enable the LED whenever we see movement. We can either build locally - using mbed CLI - or in the cloud by using the mbed Online Compiler. We'll first show how to use the online compiler, and at the end of this section we'll show the corresponding commands in mbed CLI. |
iv123 | 0:7a352727249b | 4 | |
iv123 | 0:7a352727249b | 5 | ## Setting up the online compiler |
iv123 | 0:7a352727249b | 6 | |
iv123 | 0:7a352727249b | 7 | Part of the ARM mbed IoT Device Platform is an online compiler, which we will use to write and compile software right in the browser. To get started: |
iv123 | 0:7a352727249b | 8 | |
iv123 | 0:7a352727249b | 9 | 1. Find your development board's [platform page](https://developer.mbed.org/platforms/). |
iv123 | 0:7a352727249b | 10 | 1. In the right hand panel, choose **Add to your mbed Compiler**. |
iv123 | 0:7a352727249b | 11 | 1. When prompted sign up (or sign in). You are redirected to the platform page. |
iv123 | 0:7a352727249b | 12 | 1. Choose **Open mbed Compiler**. The online compiler opens. |
iv123 | 0:7a352727249b | 13 | 1. Click the **Import** button. |
iv123 | 0:7a352727249b | 14 | |
iv123 | 0:7a352727249b | 15 | <span class="images">![Import button](assets/lights17.png)</span> |
iv123 | 0:7a352727249b | 16 | |
iv123 | 0:7a352727249b | 17 | 1. Click the "Click here to import from URL" link: |
iv123 | 0:7a352727249b | 18 | |
iv123 | 0:7a352727249b | 19 | <span class="images">![Import from URL](assets/lights18.png)</span> |
iv123 | 0:7a352727249b | 20 | |
iv123 | 0:7a352727249b | 21 | 1. Under **Source URL** enter `https://github.com/armmbed/connected-lights`. |
iv123 | 0:7a352727249b | 22 | |
iv123 | 0:7a352727249b | 23 | Do not check the **Update all libraries to the latest revision** box. |
iv123 | 0:7a352727249b | 24 | |
iv123 | 0:7a352727249b | 25 | <span class="images">![Creating a program in the online compiler](assets/lights6.png)</span> |
iv123 | 0:7a352727249b | 26 | |
iv123 | 0:7a352727249b | 27 | The program you just imported already contains some boilerplate, including mbed OS and a configuration file. We'll use this configuration file to configure the pins our software uses, then start writing some code. |
iv123 | 0:7a352727249b | 28 | |
iv123 | 0:7a352727249b | 29 | ## Adding the code |
iv123 | 0:7a352727249b | 30 | |
iv123 | 0:7a352727249b | 31 | mbed OS comes with a powerful [configuration system](https://docs.mbed.com/docs/mbedmicro-api/en/latest/api/md_docs_config_system.html) that makes it easy to separate configuration and application code. In this application we'll separate the configuration of the LED (cathode, anode or a Grove LED), pins, and connectivity method (next section). |
iv123 | 0:7a352727249b | 32 | |
iv123 | 0:7a352727249b | 33 | From the mbed Online Compiler's tree, open ``mbed_app.json``. Edit the file to reflect your LED choice, and the pins you used to connect the LED and the PIR sensor: |
iv123 | 0:7a352727249b | 34 | |
iv123 | 0:7a352727249b | 35 | 1. If you have a common cathode LED, set the `value` of `led-type` to `TRICOLOR_CATHODE`. |
iv123 | 0:7a352727249b | 36 | 1. If you have a Grove Chainable LED, set the `value` of `led-type` to `TRICOLOR_ANODE`. |
iv123 | 0:7a352727249b | 37 | 1. Replace the pins D2, D5, D6 and D7 with the pins you used when building the circuit. |
iv123 | 0:7a352727249b | 38 | |
iv123 | 0:7a352727249b | 39 | |
iv123 | 0:7a352727249b | 40 | ```js |
iv123 | 0:7a352727249b | 41 | /* mbed_app.json */ |
iv123 | 0:7a352727249b | 42 | |
iv123 | 0:7a352727249b | 43 | /* snip */ |
iv123 | 0:7a352727249b | 44 | |
iv123 | 0:7a352727249b | 45 | "pir-pin": { |
iv123 | 0:7a352727249b | 46 | "help": "Pin to which the PIR sensor is connected", |
iv123 | 0:7a352727249b | 47 | "macro_name": "PIR_PIN", |
iv123 | 0:7a352727249b | 48 | "value": "D2" |
iv123 | 0:7a352727249b | 49 | }, |
iv123 | 0:7a352727249b | 50 | |
iv123 | 0:7a352727249b | 51 | "led-type": { |
iv123 | 0:7a352727249b | 52 | "help": "options are TRICOLOR_ANODE,TRICOLOR_CATHODE,GROVE_CHAINABLE", |
iv123 | 0:7a352727249b | 53 | "value": "TRICOLOR_ANODE" |
iv123 | 0:7a352727249b | 54 | }, |
iv123 | 0:7a352727249b | 55 | |
iv123 | 0:7a352727249b | 56 | "led-pin-red": { |
iv123 | 0:7a352727249b | 57 | "help": "Only used for TRICOLOR_* LED types", |
iv123 | 0:7a352727249b | 58 | "value": "D5" |
iv123 | 0:7a352727249b | 59 | }, |
iv123 | 0:7a352727249b | 60 | "led-pin-green": { |
iv123 | 0:7a352727249b | 61 | "help": "Only used for TRICOLOR_* LED types", |
iv123 | 0:7a352727249b | 62 | "value": "D6" |
iv123 | 0:7a352727249b | 63 | }, |
iv123 | 0:7a352727249b | 64 | "led-pin-blue": { |
iv123 | 0:7a352727249b | 65 | "help": "Only used for TRICOLOR_* LED types", |
iv123 | 0:7a352727249b | 66 | "value": "D7" |
iv123 | 0:7a352727249b | 67 | }, |
iv123 | 0:7a352727249b | 68 | |
iv123 | 0:7a352727249b | 69 | "grove-clock-pin": { |
iv123 | 0:7a352727249b | 70 | "help": "Only used for GROVE_CHAINABLE LED types", |
iv123 | 0:7a352727249b | 71 | "value": "D5" |
iv123 | 0:7a352727249b | 72 | }, |
iv123 | 0:7a352727249b | 73 | "grove-data-pin": { |
iv123 | 0:7a352727249b | 74 | "help": "Only used for GROVE_CHAINABLE LED types", |
iv123 | 0:7a352727249b | 75 | "value": "D6" |
iv123 | 0:7a352727249b | 76 | }, |
iv123 | 0:7a352727249b | 77 | |
iv123 | 0:7a352727249b | 78 | /* snip */ |
iv123 | 0:7a352727249b | 79 | ``` |
iv123 | 0:7a352727249b | 80 | |
iv123 | 0:7a352727249b | 81 | Next, create a file called ``main.cpp`` in the `source` directory: |
iv123 | 0:7a352727249b | 82 | |
iv123 | 0:7a352727249b | 83 | ```cpp |
iv123 | 0:7a352727249b | 84 | /* lighting-system-firmware/source/main.cpp */ |
iv123 | 0:7a352727249b | 85 | |
iv123 | 0:7a352727249b | 86 | #include "mbed.h" |
iv123 | 0:7a352727249b | 87 | #include "led.h" // Abstracts away the differens between the LED types |
iv123 | 0:7a352727249b | 88 | |
iv123 | 0:7a352727249b | 89 | // PIR sensor acts as an interrupt - signals us whenever it goes high (or low) |
iv123 | 0:7a352727249b | 90 | InterruptIn pir(PIR_PIN); // This pin value comes out mbed_app.json |
iv123 | 0:7a352727249b | 91 | |
iv123 | 0:7a352727249b | 92 | // Whenever movement is not detected |
iv123 | 0:7a352727249b | 93 | void pir_fall() { |
iv123 | 0:7a352727249b | 94 | setRgbColor(0.0f, 0.0f, 0.0f); |
iv123 | 0:7a352727249b | 95 | } |
iv123 | 0:7a352727249b | 96 | |
iv123 | 0:7a352727249b | 97 | // Whenever movement is detected |
iv123 | 0:7a352727249b | 98 | void pir_rise() { |
iv123 | 0:7a352727249b | 99 | // set the color to red |
iv123 | 0:7a352727249b | 100 | setRgbColor(1.0f, 0.0f, 0.0f); |
iv123 | 0:7a352727249b | 101 | } |
iv123 | 0:7a352727249b | 102 | |
iv123 | 0:7a352727249b | 103 | int main(int, char**) { |
iv123 | 0:7a352727249b | 104 | // Blink the LED when the application starts |
iv123 | 0:7a352727249b | 105 | setRgbColor(0.0f, 1.0f, 0.0f); |
iv123 | 0:7a352727249b | 106 | Thread::wait(500); |
iv123 | 0:7a352727249b | 107 | setRgbColor(0.0f, 0.0f, 0.0f); |
iv123 | 0:7a352727249b | 108 | |
iv123 | 0:7a352727249b | 109 | // The PIR sensor uses interrupts, no need to poll |
iv123 | 0:7a352727249b | 110 | pir.fall(&pir_fall); |
iv123 | 0:7a352727249b | 111 | pir.rise(&pir_rise); |
iv123 | 0:7a352727249b | 112 | } |
iv123 | 0:7a352727249b | 113 | ``` |
iv123 | 0:7a352727249b | 114 | |
iv123 | 0:7a352727249b | 115 | ## Compiling and flashing |
iv123 | 0:7a352727249b | 116 | |
iv123 | 0:7a352727249b | 117 | ### Compiling |
iv123 | 0:7a352727249b | 118 | |
iv123 | 0:7a352727249b | 119 | Compile the code by clicking the *Compile* button at the top of the screen: |
iv123 | 0:7a352727249b | 120 | |
iv123 | 0:7a352727249b | 121 | <span class="images">![The compile button](assets/lights7.png)</span> |
iv123 | 0:7a352727249b | 122 | |
iv123 | 0:7a352727249b | 123 | A successful compilation downloads a `bin` file to your computer. This is the compiled firmware for your development board. |
iv123 | 0:7a352727249b | 124 | |
iv123 | 0:7a352727249b | 125 | ### Flashing |
iv123 | 0:7a352727249b | 126 | |
iv123 | 0:7a352727249b | 127 | When you connect your board to your computer, it mounts as a USB mass storage device, like a USB drive. To flash the new application onto the board, drag and drop the firmware file onto the mass storage device: |
iv123 | 0:7a352727249b | 128 | |
iv123 | 0:7a352727249b | 129 | <span class="images">![Flashing the application on Windows](assets/lights8.png)<span>Drag the firmware file onto the mass storage device to flash the application.</span></span> |
iv123 | 0:7a352727249b | 130 | |
iv123 | 0:7a352727249b | 131 | <span class="notes">**Note:** On some boards you might need to press the *Reset* button to load the program.</span> |
iv123 | 0:7a352727249b | 132 | |
iv123 | 0:7a352727249b | 133 | ### Testing the application |
iv123 | 0:7a352727249b | 134 | |
iv123 | 0:7a352727249b | 135 | After flashing the application, you can test it by waving your hand in front of the PIR sensor; the red LED should light up. |
iv123 | 0:7a352727249b | 136 | |
iv123 | 0:7a352727249b | 137 | ## Developing using mbed CLI |
iv123 | 0:7a352727249b | 138 | |
iv123 | 0:7a352727249b | 139 | You can also develop locally using [mbed CLI](http://github.com/armmbed/mbed-cli), a command line tool for mbed OS. First follow [the installation steps](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli), then use the following commands to recreate the flow above: |
iv123 | 0:7a352727249b | 140 | |
iv123 | 0:7a352727249b | 141 | ```bash |
iv123 | 0:7a352727249b | 142 | # import the connected-lights project |
iv123 | 0:7a352727249b | 143 | $ mbed import connected-lights |
iv123 | 0:7a352727249b | 144 | |
iv123 | 0:7a352727249b | 145 | # go into the folder |
iv123 | 0:7a352727249b | 146 | cd connected-lights/ |
iv123 | 0:7a352727249b | 147 | |
iv123 | 0:7a352727249b | 148 | # now edit the mbed_app.json file, and create the main.cpp file |
iv123 | 0:7a352727249b | 149 | |
iv123 | 0:7a352727249b | 150 | # detect which board you are using |
iv123 | 0:7a352727249b | 151 | $ mbed detect |
iv123 | 0:7a352727249b | 152 | |
iv123 | 0:7a352727249b | 153 | # build the project, you'll need the GCC ARM cross-compilation toolchain installed |
iv123 | 0:7a352727249b | 154 | # optionally, you can also build with ARMCC or IAR |
iv123 | 0:7a352727249b | 155 | $ mbed compile -t GCC_ARM -m YOUR_BOARD_NAME |
iv123 | 0:7a352727249b | 156 | |
iv123 | 0:7a352727249b | 157 | # … building … |
iv123 | 0:7a352727249b | 158 | # ends with something like: |
iv123 | 0:7a352727249b | 159 | # Image: ./.build/K64F/GCC_ARM/simple-mbed-client-example.bin |
iv123 | 0:7a352727249b | 160 | ``` |
iv123 | 0:7a352727249b | 161 | |
iv123 | 0:7a352727249b | 162 | Copy the binary file that was generated to your board using drag-and-drop programming (as shown under [Compiling and flashing](#compiling-and-flashing)). |
iv123 | 0:7a352727249b | 163 |