UserInterface
UserInterface.cpp@0:61a21e393e36, 2017-11-12 (annotated)
- Committer:
- roger5641
- Date:
- Sun Nov 12 01:22:00 2017 +0000
- Revision:
- 0:61a21e393e36
ver1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roger5641 | 0:61a21e393e36 | 1 | //! @todo Review this file. |
roger5641 | 0:61a21e393e36 | 2 | /* |
roger5641 | 0:61a21e393e36 | 3 | UserInterface.cpp |
roger5641 | 0:61a21e393e36 | 4 | |
roger5641 | 0:61a21e393e36 | 5 | UserInterface routines are used to read and write user data through the Arduino's |
roger5641 | 0:61a21e393e36 | 6 | serial interface. |
roger5641 | 0:61a21e393e36 | 7 | |
roger5641 | 0:61a21e393e36 | 8 | Copyright (c) 2013, Linear Technology Corp.(LTC) |
roger5641 | 0:61a21e393e36 | 9 | All rights reserved. |
roger5641 | 0:61a21e393e36 | 10 | |
roger5641 | 0:61a21e393e36 | 11 | Redistribution and use in source and binary forms, with or without |
roger5641 | 0:61a21e393e36 | 12 | modification, are permitted provided that the following conditions are met: |
roger5641 | 0:61a21e393e36 | 13 | |
roger5641 | 0:61a21e393e36 | 14 | 1. Redistributions of source code must retain the above copyright notice, this |
roger5641 | 0:61a21e393e36 | 15 | list of conditions and the following disclaimer. |
roger5641 | 0:61a21e393e36 | 16 | 2. Redistributions in binary form must reproduce the above copyright notice, |
roger5641 | 0:61a21e393e36 | 17 | this list of conditions and the following disclaimer in the documentation |
roger5641 | 0:61a21e393e36 | 18 | and/or other materials provided with the distribution. |
roger5641 | 0:61a21e393e36 | 19 | |
roger5641 | 0:61a21e393e36 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
roger5641 | 0:61a21e393e36 | 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
roger5641 | 0:61a21e393e36 | 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
roger5641 | 0:61a21e393e36 | 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
roger5641 | 0:61a21e393e36 | 24 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
roger5641 | 0:61a21e393e36 | 25 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
roger5641 | 0:61a21e393e36 | 26 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
roger5641 | 0:61a21e393e36 | 27 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
roger5641 | 0:61a21e393e36 | 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
roger5641 | 0:61a21e393e36 | 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
roger5641 | 0:61a21e393e36 | 30 | |
roger5641 | 0:61a21e393e36 | 31 | The views and conclusions contained in the software and documentation are those |
roger5641 | 0:61a21e393e36 | 32 | of the authors and should not be interpreted as representing official policies, |
roger5641 | 0:61a21e393e36 | 33 | either expressed or implied, of Linear Technology Corp. |
roger5641 | 0:61a21e393e36 | 34 | |
roger5641 | 0:61a21e393e36 | 35 | The Linear Technology Linduino is not affiliated with the official Arduino team. |
roger5641 | 0:61a21e393e36 | 36 | However, the Linduino is only possible because of the Arduino team's commitment |
roger5641 | 0:61a21e393e36 | 37 | to the open-source community. Please, visit http://www.arduino.cc and |
roger5641 | 0:61a21e393e36 | 38 | http://store.arduino.cc , and consider a purchase that will help fund their |
roger5641 | 0:61a21e393e36 | 39 | ongoing work. |
roger5641 | 0:61a21e393e36 | 40 | */ |
roger5641 | 0:61a21e393e36 | 41 | #include "UserInterface.h" |
roger5641 | 0:61a21e393e36 | 42 | |
roger5641 | 0:61a21e393e36 | 43 | char ui_buffer[UI_BUFFER_SIZE]; |
roger5641 | 0:61a21e393e36 | 44 | |
roger5641 | 0:61a21e393e36 | 45 | // Read data from the serial interface into the ui_buffer |
roger5641 | 0:61a21e393e36 | 46 | uint8_t read_data() |
roger5641 | 0:61a21e393e36 | 47 | { |
roger5641 | 0:61a21e393e36 | 48 | uint8_t index = 0; //index to hold current location in ui_buffer |
roger5641 | 0:61a21e393e36 | 49 | int c; // single character used to store incoming keystrokes |
roger5641 | 0:61a21e393e36 | 50 | while (index < UI_BUFFER_SIZE-1) |
roger5641 | 0:61a21e393e36 | 51 | { |
roger5641 | 0:61a21e393e36 | 52 | c = pc.getc(); //read one character |
roger5641 | 0:61a21e393e36 | 53 | if (((char) c == '\r') || ((char) c == '\n')) break; // if carriage return or linefeed, stop and return data |
roger5641 | 0:61a21e393e36 | 54 | if ( ((char) c == '\x7F') || ((char) c == '\x08') ) // remove previous character (decrement index) if Backspace/Delete key pressed index--; |
roger5641 | 0:61a21e393e36 | 55 | { |
roger5641 | 0:61a21e393e36 | 56 | if (index > 0) index--; |
roger5641 | 0:61a21e393e36 | 57 | } |
roger5641 | 0:61a21e393e36 | 58 | else if (c >= 0) |
roger5641 | 0:61a21e393e36 | 59 | { |
roger5641 | 0:61a21e393e36 | 60 | ui_buffer[index++]=(char) c; // put character into ui_buffer |
roger5641 | 0:61a21e393e36 | 61 | } |
roger5641 | 0:61a21e393e36 | 62 | } |
roger5641 | 0:61a21e393e36 | 63 | ui_buffer[index]='\0'; // terminate string with NULL |
roger5641 | 0:61a21e393e36 | 64 | |
roger5641 | 0:61a21e393e36 | 65 | if ((char) c == '\r') // if the last character was a carriage return, also clear linefeed if it is next character |
roger5641 | 0:61a21e393e36 | 66 | { |
roger5641 | 0:61a21e393e36 | 67 | wait_ms(10); // allow 10ms for linefeed to appear on serial pins |
roger5641 | 0:61a21e393e36 | 68 | // if (pc.peek() == '\n') |
roger5641 | 0:61a21e393e36 | 69 | pc.getc(); // if linefeed appears, read it and throw it away |
roger5641 | 0:61a21e393e36 | 70 | } |
roger5641 | 0:61a21e393e36 | 71 | |
roger5641 | 0:61a21e393e36 | 72 | return index; // return number of characters, not including null terminator |
roger5641 | 0:61a21e393e36 | 73 | } |
roger5641 | 0:61a21e393e36 | 74 | |
roger5641 | 0:61a21e393e36 | 75 | // Read a float value from the serial interface |
roger5641 | 0:61a21e393e36 | 76 | float read_float() |
roger5641 | 0:61a21e393e36 | 77 | { |
roger5641 | 0:61a21e393e36 | 78 | float data; |
roger5641 | 0:61a21e393e36 | 79 | read_data(); |
roger5641 | 0:61a21e393e36 | 80 | data = atof(ui_buffer); |
roger5641 | 0:61a21e393e36 | 81 | return(data); |
roger5641 | 0:61a21e393e36 | 82 | } |
roger5641 | 0:61a21e393e36 | 83 | |
roger5641 | 0:61a21e393e36 | 84 | // Read an integer from the serial interface. |
roger5641 | 0:61a21e393e36 | 85 | // The routine can recognize Hex, Decimal, Octal, or Binary |
roger5641 | 0:61a21e393e36 | 86 | // Example: |
roger5641 | 0:61a21e393e36 | 87 | // Hex: 0x11 (0x prefix) |
roger5641 | 0:61a21e393e36 | 88 | // Decimal: 17 |
roger5641 | 0:61a21e393e36 | 89 | // Octal: 021 (leading zero prefix) |
roger5641 | 0:61a21e393e36 | 90 | // Binary: B10001 (leading B prefix) |
roger5641 | 0:61a21e393e36 | 91 | int32_t read_int() |
roger5641 | 0:61a21e393e36 | 92 | { |
roger5641 | 0:61a21e393e36 | 93 | int32_t data; |
roger5641 | 0:61a21e393e36 | 94 | read_data(); |
roger5641 | 0:61a21e393e36 | 95 | if (ui_buffer[0] == 'm') |
roger5641 | 0:61a21e393e36 | 96 | return('m'); |
roger5641 | 0:61a21e393e36 | 97 | if ((ui_buffer[0] == 'B') || (ui_buffer[0] == 'b')) |
roger5641 | 0:61a21e393e36 | 98 | { |
roger5641 | 0:61a21e393e36 | 99 | data = strtol(ui_buffer+1, NULL, 2); |
roger5641 | 0:61a21e393e36 | 100 | } |
roger5641 | 0:61a21e393e36 | 101 | else |
roger5641 | 0:61a21e393e36 | 102 | data = strtol(ui_buffer, NULL, 0); |
roger5641 | 0:61a21e393e36 | 103 | return(data); |
roger5641 | 0:61a21e393e36 | 104 | } |
roger5641 | 0:61a21e393e36 | 105 | |
roger5641 | 0:61a21e393e36 | 106 | // Read a string from the serial interface. Returns a pointer to the ui_buffer. |
roger5641 | 0:61a21e393e36 | 107 | char *read_string() |
roger5641 | 0:61a21e393e36 | 108 | { |
roger5641 | 0:61a21e393e36 | 109 | read_data(); |
roger5641 | 0:61a21e393e36 | 110 | return(ui_buffer); |
roger5641 | 0:61a21e393e36 | 111 | } |
roger5641 | 0:61a21e393e36 | 112 | |
roger5641 | 0:61a21e393e36 | 113 | // Read a character from the serial interface |
roger5641 | 0:61a21e393e36 | 114 | int8_t read_char() |
roger5641 | 0:61a21e393e36 | 115 | { |
roger5641 | 0:61a21e393e36 | 116 | read_data(); |
roger5641 | 0:61a21e393e36 | 117 | return(ui_buffer[0]); |
roger5641 | 0:61a21e393e36 | 118 | } |