![](/media/cache/profiles/01e5405ccd7d3efa1fd033aa7b7489e9.jpg.50x50_q85.jpg)
Simple demo for SerialPlotter program found at following link https://developer.mbed.org/users/borislav/notebook/serial-port-plotter/
main.cpp@1:0951a80a60e2, 2017-09-07 (annotated)
- Committer:
- j3
- Date:
- Thu Sep 07 23:02:08 2017 +0000
- Revision:
- 1:0951a80a60e2
- Parent:
- 0:cdcbb66ba497
- Child:
- 2:7a66c77ff328
added comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 1:0951a80a60e2 | 1 | /****************************************************************************** |
j3 | 1:0951a80a60e2 | 2 | * MIT License |
j3 | 0:cdcbb66ba497 | 3 | * |
j3 | 1:0951a80a60e2 | 4 | * Copyright (c) 2017 Justin J. Jordan |
j3 | 0:cdcbb66ba497 | 5 | * |
j3 | 1:0951a80a60e2 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
j3 | 1:0951a80a60e2 | 7 | * of this software and associated documentation files (the "Software"), to deal |
j3 | 1:0951a80a60e2 | 8 | * in the Software without restriction, including without limitation the rights |
j3 | 1:0951a80a60e2 | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
j3 | 1:0951a80a60e2 | 10 | * copies of the Software, and to permit persons to whom the Software is |
j3 | 1:0951a80a60e2 | 11 | * furnished to do so, subject to the following conditions: |
j3 | 1:0951a80a60e2 | 12 | |
j3 | 1:0951a80a60e2 | 13 | * The above copyright notice and this permission notice shall be included in all |
j3 | 1:0951a80a60e2 | 14 | * copies or substantial portions of the Software. |
j3 | 1:0951a80a60e2 | 15 | |
j3 | 1:0951a80a60e2 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
j3 | 1:0951a80a60e2 | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
j3 | 1:0951a80a60e2 | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
j3 | 1:0951a80a60e2 | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
j3 | 1:0951a80a60e2 | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
j3 | 1:0951a80a60e2 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
j3 | 1:0951a80a60e2 | 22 | * SOFTWARE. |
j3 | 1:0951a80a60e2 | 23 | ******************************************************************************/ |
j3 | 0:cdcbb66ba497 | 24 | |
j3 | 0:cdcbb66ba497 | 25 | |
j3 | 0:cdcbb66ba497 | 26 | //https://developer.mbed.org/users/borislav/notebook/serial-port-plotter/ |
j3 | 0:cdcbb66ba497 | 27 | |
j3 | 1:0951a80a60e2 | 28 | |
j3 | 0:cdcbb66ba497 | 29 | #include "mbed.h" |
j3 | 0:cdcbb66ba497 | 30 | |
j3 | 0:cdcbb66ba497 | 31 | |
j3 | 0:cdcbb66ba497 | 32 | int32_t float_to_int(float f); |
j3 | 0:cdcbb66ba497 | 33 | |
j3 | 0:cdcbb66ba497 | 34 | |
j3 | 0:cdcbb66ba497 | 35 | int main() |
j3 | 0:cdcbb66ba497 | 36 | { |
j3 | 0:cdcbb66ba497 | 37 | DigitalOut rLed(LED_RED, 1); |
j3 | 0:cdcbb66ba497 | 38 | DigitalOut gLed(LED_GREEN, 1); |
j3 | 0:cdcbb66ba497 | 39 | DigitalOut bLed(LED_BLUE, 1); |
j3 | 0:cdcbb66ba497 | 40 | |
j3 | 0:cdcbb66ba497 | 41 | DigitalOut leds[] = {rLed, gLed, bLed}; |
j3 | 0:cdcbb66ba497 | 42 | |
j3 | 0:cdcbb66ba497 | 43 | Serial debug(USBTX, USBRX); |
j3 | 0:cdcbb66ba497 | 44 | debug.baud(115200); |
j3 | 0:cdcbb66ba497 | 45 | |
j3 | 0:cdcbb66ba497 | 46 | //const float PI = 3.14159265F; |
j3 | 0:cdcbb66ba497 | 47 | float angle[] = {0.0F, 120.0F, 240.0F}; |
j3 | 0:cdcbb66ba497 | 48 | float sinScaled; |
j3 | 0:cdcbb66ba497 | 49 | int32_t data[3]; |
j3 | 0:cdcbb66ba497 | 50 | uint32_t cnt = 0; |
j3 | 0:cdcbb66ba497 | 51 | |
j3 | 0:cdcbb66ba497 | 52 | while(1) |
j3 | 0:cdcbb66ba497 | 53 | { |
j3 | 0:cdcbb66ba497 | 54 | //get data |
j3 | 0:cdcbb66ba497 | 55 | for(uint32_t idx = 0; idx < 3; idx++) |
j3 | 0:cdcbb66ba497 | 56 | { |
j3 | 0:cdcbb66ba497 | 57 | //get sin, scaled up and offset above 0 |
j3 | 0:cdcbb66ba497 | 58 | sinScaled = (100 + (100.0F * sin(angle[idx]*(PI/180.0F)))); |
j3 | 0:cdcbb66ba497 | 59 | data[idx] = float_to_int(sinScaled); |
j3 | 0:cdcbb66ba497 | 60 | |
j3 | 0:cdcbb66ba497 | 61 | //inc angle, check for roll over |
j3 | 0:cdcbb66ba497 | 62 | angle[idx] += 1.0F; |
j3 | 0:cdcbb66ba497 | 63 | if(angle[idx] > 360.0F) |
j3 | 0:cdcbb66ba497 | 64 | { |
j3 | 0:cdcbb66ba497 | 65 | angle[idx] = 0.0F; |
j3 | 0:cdcbb66ba497 | 66 | } |
j3 | 0:cdcbb66ba497 | 67 | } |
j3 | 0:cdcbb66ba497 | 68 | |
j3 | 0:cdcbb66ba497 | 69 | //plot |
j3 | 0:cdcbb66ba497 | 70 | debug.printf("$%d %d %d;", data[0], data[1], data[2]); |
j3 | 0:cdcbb66ba497 | 71 | |
j3 | 0:cdcbb66ba497 | 72 | //blink some lights, just cause you have to |
j3 | 0:cdcbb66ba497 | 73 | leds[cnt % 3] = !leds[cnt % 3]; |
j3 | 0:cdcbb66ba497 | 74 | cnt++; |
j3 | 0:cdcbb66ba497 | 75 | |
j3 | 0:cdcbb66ba497 | 76 | //wait a bit |
j3 | 0:cdcbb66ba497 | 77 | wait(0.01F); |
j3 | 0:cdcbb66ba497 | 78 | } |
j3 | 0:cdcbb66ba497 | 79 | } |
j3 | 0:cdcbb66ba497 | 80 | |
j3 | 0:cdcbb66ba497 | 81 | |
j3 | 0:cdcbb66ba497 | 82 | int32_t float_to_int(float f) |
j3 | 0:cdcbb66ba497 | 83 | { |
j3 | 0:cdcbb66ba497 | 84 | int32_t i; |
j3 | 0:cdcbb66ba497 | 85 | |
j3 | 0:cdcbb66ba497 | 86 | if (f >= 0.0F) |
j3 | 0:cdcbb66ba497 | 87 | { |
j3 | 0:cdcbb66ba497 | 88 | i = static_cast<int32_t>(f + 0.5F); |
j3 | 0:cdcbb66ba497 | 89 | } |
j3 | 0:cdcbb66ba497 | 90 | else |
j3 | 0:cdcbb66ba497 | 91 | { |
j3 | 0:cdcbb66ba497 | 92 | i = static_cast<int32_t>(f - 0.5F); |
j3 | 0:cdcbb66ba497 | 93 | } |
j3 | 0:cdcbb66ba497 | 94 | |
j3 | 0:cdcbb66ba497 | 95 | return i; |
j3 | 0:cdcbb66ba497 | 96 | } |