Simple demo for SerialPlotter program found at following link https://developer.mbed.org/users/borislav/notebook/serial-port-plotter/
main.cpp@2:7a66c77ff328, 2017-09-07 (annotated)
- Committer:
- j3
- Date:
- Thu Sep 07 23:37:31 2017 +0000
- Revision:
- 2:7a66c77ff328
- Parent:
- 1:0951a80a60e2
added notes on fixing negative val plotting for GUI
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 | int32_t float_to_int(float f); |
j3 | 0:cdcbb66ba497 | 32 | |
j3 | 0:cdcbb66ba497 | 33 | |
j3 | 0:cdcbb66ba497 | 34 | int main() |
j3 | 0:cdcbb66ba497 | 35 | { |
j3 | 0:cdcbb66ba497 | 36 | DigitalOut rLed(LED_RED, 1); |
j3 | 0:cdcbb66ba497 | 37 | DigitalOut gLed(LED_GREEN, 1); |
j3 | 0:cdcbb66ba497 | 38 | DigitalOut bLed(LED_BLUE, 1); |
j3 | 0:cdcbb66ba497 | 39 | |
j3 | 0:cdcbb66ba497 | 40 | DigitalOut leds[] = {rLed, gLed, bLed}; |
j3 | 0:cdcbb66ba497 | 41 | |
j3 | 0:cdcbb66ba497 | 42 | Serial debug(USBTX, USBRX); |
j3 | 0:cdcbb66ba497 | 43 | debug.baud(115200); |
j3 | 0:cdcbb66ba497 | 44 | |
j3 | 0:cdcbb66ba497 | 45 | //const float PI = 3.14159265F; |
j3 | 0:cdcbb66ba497 | 46 | float angle[] = {0.0F, 120.0F, 240.0F}; |
j3 | 0:cdcbb66ba497 | 47 | float sinScaled; |
j3 | 0:cdcbb66ba497 | 48 | int32_t data[3]; |
j3 | 0:cdcbb66ba497 | 49 | uint32_t cnt = 0; |
j3 | 0:cdcbb66ba497 | 50 | |
j3 | 0:cdcbb66ba497 | 51 | while(1) |
j3 | 0:cdcbb66ba497 | 52 | { |
j3 | 0:cdcbb66ba497 | 53 | //get data |
j3 | 0:cdcbb66ba497 | 54 | for(uint32_t idx = 0; idx < 3; idx++) |
j3 | 0:cdcbb66ba497 | 55 | { |
j3 | 2:7a66c77ff328 | 56 | /*If you have Qt creator installed, you can open the SerialPlotter |
j3 | 2:7a66c77ff328 | 57 | project and edit line 440 of mainwindow.cpp too the following |
j3 | 2:7a66c77ff328 | 58 | |
j3 | 2:7a66c77ff328 | 59 | //else if(isdigit(temp[i]) || isspace(temp[i]) || temp[i] == '-') |
j3 | 2:7a66c77ff328 | 60 | |
j3 | 2:7a66c77ff328 | 61 | without the comment, of course. |
j3 | 2:7a66c77ff328 | 62 | |
j3 | 2:7a66c77ff328 | 63 | If you dont, use the commented out version of sinScaled below and |
j3 | 2:7a66c77ff328 | 64 | comment the first one out. |
j3 | 2:7a66c77ff328 | 65 | */ |
j3 | 2:7a66c77ff328 | 66 | |
j3 | 2:7a66c77ff328 | 67 | //get sin, scaled up |
j3 | 2:7a66c77ff328 | 68 | sinScaled = (100.0F * sin(angle[idx]*(PI/180.0F))); |
j3 | 2:7a66c77ff328 | 69 | |
j3 | 2:7a66c77ff328 | 70 | //sinScaled = (100 + (100.0F * sin(angle[idx]*(PI/180.0F)))); |
j3 | 2:7a66c77ff328 | 71 | |
j3 | 0:cdcbb66ba497 | 72 | data[idx] = float_to_int(sinScaled); |
j3 | 0:cdcbb66ba497 | 73 | |
j3 | 0:cdcbb66ba497 | 74 | //inc angle, check for roll over |
j3 | 0:cdcbb66ba497 | 75 | angle[idx] += 1.0F; |
j3 | 0:cdcbb66ba497 | 76 | if(angle[idx] > 360.0F) |
j3 | 0:cdcbb66ba497 | 77 | { |
j3 | 0:cdcbb66ba497 | 78 | angle[idx] = 0.0F; |
j3 | 0:cdcbb66ba497 | 79 | } |
j3 | 0:cdcbb66ba497 | 80 | } |
j3 | 0:cdcbb66ba497 | 81 | |
j3 | 0:cdcbb66ba497 | 82 | //plot |
j3 | 0:cdcbb66ba497 | 83 | debug.printf("$%d %d %d;", data[0], data[1], data[2]); |
j3 | 0:cdcbb66ba497 | 84 | |
j3 | 0:cdcbb66ba497 | 85 | //blink some lights, just cause you have to |
j3 | 0:cdcbb66ba497 | 86 | leds[cnt % 3] = !leds[cnt % 3]; |
j3 | 0:cdcbb66ba497 | 87 | cnt++; |
j3 | 0:cdcbb66ba497 | 88 | |
j3 | 0:cdcbb66ba497 | 89 | //wait a bit |
j3 | 0:cdcbb66ba497 | 90 | wait(0.01F); |
j3 | 0:cdcbb66ba497 | 91 | } |
j3 | 0:cdcbb66ba497 | 92 | } |
j3 | 0:cdcbb66ba497 | 93 | |
j3 | 0:cdcbb66ba497 | 94 | |
j3 | 0:cdcbb66ba497 | 95 | int32_t float_to_int(float f) |
j3 | 0:cdcbb66ba497 | 96 | { |
j3 | 0:cdcbb66ba497 | 97 | int32_t i; |
j3 | 0:cdcbb66ba497 | 98 | |
j3 | 0:cdcbb66ba497 | 99 | if (f >= 0.0F) |
j3 | 0:cdcbb66ba497 | 100 | { |
j3 | 0:cdcbb66ba497 | 101 | i = static_cast<int32_t>(f + 0.5F); |
j3 | 0:cdcbb66ba497 | 102 | } |
j3 | 0:cdcbb66ba497 | 103 | else |
j3 | 0:cdcbb66ba497 | 104 | { |
j3 | 0:cdcbb66ba497 | 105 | i = static_cast<int32_t>(f - 0.5F); |
j3 | 0:cdcbb66ba497 | 106 | } |
j3 | 0:cdcbb66ba497 | 107 | |
j3 | 0:cdcbb66ba497 | 108 | return i; |
j3 | 0:cdcbb66ba497 | 109 | } |