Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
UserInterface.cpp
Go to the documentation of this file.
1 //! @todo Review this file.
2 /*
3 UserInterface.cpp
4 
5 UserInterface routines are used to read and write user data through the Arduino's
6 serial interface.
7 
8 Copyright 2018(c) Analog Devices, Inc.
9 
10 All rights reserved.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  - Redistributions of source code must retain the above copyright
15  notice, this list of conditions and the following disclaimer.
16  - Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in
18  the documentation and/or other materials provided with the
19  distribution.
20  - Neither the name of Analog Devices, Inc. nor the names of its
21  contributors may be used to endorse or promote products derived
22  from this software without specific prior written permission.
23  - The use of this software may or may not infringe the patent rights
24  of one or more patent holders. This license does not release you
25  from the requirement that you obtain separate licenses from these
26  patent holders to use this software.
27  - Use of the software either in source or binary form, must be run
28  on or directly connected to an Analog Devices Inc. component.
29 
30 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
31 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
32 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
34 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
36 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
38 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41 
42 #include <Arduino.h>
43 #include <stdint.h>
44 #include "UserInterface.h"
45 
47 
48 // Read data from the serial interface into the ui_buffer
49 uint8_t read_data()
50 {
51  uint8_t index = 0; //index to hold current location in ui_buffer
52  int c; // single character used to store incoming keystrokes
53  while (index < UI_BUFFER_SIZE-1)
54  {
55  c = Serial.read(); //read one character
56  if (((char) c == '\r') || ((char) c == '\n')) break; // if carriage return or linefeed, stop and return data
57  if ( ((char) c == '\x7F') || ((char) c == '\x08') ) // remove previous character (decrement index) if Backspace/Delete key pressed index--;
58  {
59  if (index > 0) index--;
60  }
61  else if (c >= 0)
62  {
63  ui_buffer[index++]=(char) c; // put character into ui_buffer
64  }
65  }
66  ui_buffer[index]='\0'; // terminate string with NULL
67 
68  if ((char) c == '\r') // if the last character was a carriage return, also clear linefeed if it is next character
69  {
70  delay(10); // allow 10ms for linefeed to appear on serial pins
71  if (Serial.peek() == '\n') Serial.read(); // if linefeed appears, read it and throw it away
72  }
73 
74  return index; // return number of characters, not including null terminator
75 }
76 
77 // Read a float value from the serial interface
78 float read_float()
79 {
80  float data;
81  read_data();
82  data = atof(ui_buffer);
83  return(data);
84 }
85 
86 // Read an integer from the serial interface.
87 // The routine can recognize Hex, Decimal, Octal, or Binary
88 // Example:
89 // Hex: 0x11 (0x prefix)
90 // Decimal: 17
91 // Octal: 021 (leading zero prefix)
92 // Binary: B10001 (leading B prefix)
93 int32_t read_int()
94 {
95  int32_t data;
96  read_data();
97  if (ui_buffer[0] == 'm')
98  return('m');
99  if ((ui_buffer[0] == 'B') || (ui_buffer[0] == 'b'))
100  {
101  data = strtol(ui_buffer+1, NULL, 2);
102  }
103  else
104  data = strtol(ui_buffer, NULL, 0);
105  return(data);
106 }
107 
108 // Read a string from the serial interface. Returns a pointer to the ui_buffer.
109 char *read_string()
110 {
111  read_data();
112  return(ui_buffer);
113 }
114 
115 // Read a character from the serial interface
116 int8_t read_char()
117 {
118  read_data();
119  return(ui_buffer[0]);
120 }
uint8_t read_data()
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
char * read_string()
int32_t read_int()
static int index
float read_float()
#define UI_BUFFER_SIZE
Definition: UserInterface.h:47
int8_t read_char()
char ui_buffer[UI_BUFFER_SIZE]