* AM2321的取温度间隔得大于2s,否则,i2c会不工作了 * SimpleTimer有个bug,会导致两次快速的读温度,现在读温度函数里加了保护 * Blynk有个bug,会导致无法把数据传到服务器 * 现在可以正常工作了

Dependencies:   mbed

Committer:
lixianyu
Date:
Fri Jun 24 02:06:43 2016 +0000
Revision:
1:e34100dd6532
Parent:
0:740c1eb2df13
?Arduino??????????0~255??????LPC824????????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:740c1eb2df13 1 /*
lixianyu 0:740c1eb2df13 2 WString.h - String library for Wiring & Arduino
lixianyu 0:740c1eb2df13 3 ...mostly rewritten by Paul Stoffregen...
lixianyu 0:740c1eb2df13 4 Copyright (c) 2009-10 Hernando Barragan. All right reserved.
lixianyu 0:740c1eb2df13 5 Copyright 2011, Paul Stoffregen, paul@pjrc.com
lixianyu 0:740c1eb2df13 6
lixianyu 0:740c1eb2df13 7 This library is free software; you can redistribute it and/or
lixianyu 0:740c1eb2df13 8 modify it under the terms of the GNU Lesser General Public
lixianyu 0:740c1eb2df13 9 License as published by the Free Software Foundation; either
lixianyu 0:740c1eb2df13 10 version 2.1 of the License, or (at your option) any later version.
lixianyu 0:740c1eb2df13 11
lixianyu 0:740c1eb2df13 12 This library is distributed in the hope that it will be useful,
lixianyu 0:740c1eb2df13 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
lixianyu 0:740c1eb2df13 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lixianyu 0:740c1eb2df13 15 Lesser General Public License for more details.
lixianyu 0:740c1eb2df13 16
lixianyu 0:740c1eb2df13 17 You should have received a copy of the GNU Lesser General Public
lixianyu 0:740c1eb2df13 18 License along with this library; if not, write to the Free Software
lixianyu 0:740c1eb2df13 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lixianyu 0:740c1eb2df13 20 */
lixianyu 0:740c1eb2df13 21
lixianyu 0:740c1eb2df13 22 #ifndef String_class_h
lixianyu 0:740c1eb2df13 23 #define String_class_h
lixianyu 0:740c1eb2df13 24 #ifdef __cplusplus
lixianyu 0:740c1eb2df13 25
lixianyu 0:740c1eb2df13 26 #include <stdlib.h>
lixianyu 0:740c1eb2df13 27 #include <string.h>
lixianyu 0:740c1eb2df13 28 #include <ctype.h>
lixianyu 0:740c1eb2df13 29 //#include <avr/pgmspace.h>
lixianyu 0:740c1eb2df13 30
lixianyu 0:740c1eb2df13 31 // When compiling programs with this class, the following gcc parameters
lixianyu 0:740c1eb2df13 32 // dramatically increase performance and memory (RAM) efficiency, typically
lixianyu 0:740c1eb2df13 33 // with little or no increase in code size.
lixianyu 0:740c1eb2df13 34 // -felide-constructors
lixianyu 0:740c1eb2df13 35 // -std=c++0x
lixianyu 0:740c1eb2df13 36
lixianyu 0:740c1eb2df13 37 //class __FlashStringHelper;
lixianyu 0:740c1eb2df13 38 #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
lixianyu 0:740c1eb2df13 39
lixianyu 0:740c1eb2df13 40 // An inherited class for holding the result of a concatenation. These
lixianyu 0:740c1eb2df13 41 // result objects are assumed to be writable by subsequent concatenations.
lixianyu 0:740c1eb2df13 42 class StringSumHelper;
lixianyu 0:740c1eb2df13 43
lixianyu 0:740c1eb2df13 44 // The string class
lixianyu 0:740c1eb2df13 45 class String
lixianyu 0:740c1eb2df13 46 {
lixianyu 0:740c1eb2df13 47 // use a function pointer to allow for "if (s)" without the
lixianyu 0:740c1eb2df13 48 // complications of an operator bool(). for more information, see:
lixianyu 0:740c1eb2df13 49 // http://www.artima.com/cppsource/safebool.html
lixianyu 0:740c1eb2df13 50 typedef void (String::*StringIfHelperType)() const;
lixianyu 0:740c1eb2df13 51 void StringIfHelper() const {}
lixianyu 0:740c1eb2df13 52
lixianyu 0:740c1eb2df13 53 public:
lixianyu 0:740c1eb2df13 54 // constructors
lixianyu 0:740c1eb2df13 55 // creates a copy of the initial value.
lixianyu 0:740c1eb2df13 56 // if the initial value is null or invalid, or if memory allocation
lixianyu 0:740c1eb2df13 57 // fails, the string will be marked as invalid (i.e. "if (s)" will
lixianyu 0:740c1eb2df13 58 // be false).
lixianyu 0:740c1eb2df13 59 String(const char *cstr = "");
lixianyu 0:740c1eb2df13 60 String(const String &str);
lixianyu 0:740c1eb2df13 61 #if 0
lixianyu 0:740c1eb2df13 62 String(const __FlashStringHelper *str);
lixianyu 0:740c1eb2df13 63 #endif
lixianyu 0:740c1eb2df13 64 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
lixianyu 0:740c1eb2df13 65 String(String &&rval);
lixianyu 0:740c1eb2df13 66 String(StringSumHelper &&rval);
lixianyu 0:740c1eb2df13 67 #endif
lixianyu 0:740c1eb2df13 68 explicit String(char c);
lixianyu 0:740c1eb2df13 69 explicit String(unsigned char, unsigned char base=10);
lixianyu 0:740c1eb2df13 70 explicit String(int, unsigned char base=10);
lixianyu 0:740c1eb2df13 71 explicit String(unsigned int, unsigned char base=10);
lixianyu 0:740c1eb2df13 72 explicit String(long, unsigned char base=10);
lixianyu 0:740c1eb2df13 73 explicit String(unsigned long, unsigned char base=10);
lixianyu 0:740c1eb2df13 74 explicit String(float, unsigned char decimalPlaces=2);
lixianyu 0:740c1eb2df13 75 explicit String(double, unsigned char decimalPlaces=2);
lixianyu 0:740c1eb2df13 76 ~String(void);
lixianyu 0:740c1eb2df13 77
lixianyu 0:740c1eb2df13 78 // memory management
lixianyu 0:740c1eb2df13 79 // return true on success, false on failure (in which case, the string
lixianyu 0:740c1eb2df13 80 // is left unchanged). reserve(0), if successful, will validate an
lixianyu 0:740c1eb2df13 81 // invalid string (i.e., "if (s)" will be true afterwards)
lixianyu 0:740c1eb2df13 82 unsigned char reserve(unsigned int size);
lixianyu 0:740c1eb2df13 83 inline unsigned int length(void) const {
lixianyu 0:740c1eb2df13 84 return len;
lixianyu 0:740c1eb2df13 85 }
lixianyu 0:740c1eb2df13 86
lixianyu 0:740c1eb2df13 87 // creates a copy of the assigned value. if the value is null or
lixianyu 0:740c1eb2df13 88 // invalid, or if the memory allocation fails, the string will be
lixianyu 0:740c1eb2df13 89 // marked as invalid ("if (s)" will be false).
lixianyu 0:740c1eb2df13 90 String & operator = (const String &rhs);
lixianyu 0:740c1eb2df13 91 String & operator = (const char *cstr);
lixianyu 0:740c1eb2df13 92 #if 0
lixianyu 0:740c1eb2df13 93 String & operator = (const __FlashStringHelper *str);
lixianyu 0:740c1eb2df13 94 #endif
lixianyu 0:740c1eb2df13 95 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
lixianyu 0:740c1eb2df13 96 String & operator = (String &&rval);
lixianyu 0:740c1eb2df13 97 String & operator = (StringSumHelper &&rval);
lixianyu 0:740c1eb2df13 98 #endif
lixianyu 0:740c1eb2df13 99
lixianyu 0:740c1eb2df13 100 // concatenate (works w/ built-in types)
lixianyu 0:740c1eb2df13 101
lixianyu 0:740c1eb2df13 102 // returns true on success, false on failure (in which case, the string
lixianyu 0:740c1eb2df13 103 // is left unchanged). if the argument is null or invalid, the
lixianyu 0:740c1eb2df13 104 // concatenation is considered unsucessful.
lixianyu 0:740c1eb2df13 105 unsigned char concat(const String &str);
lixianyu 0:740c1eb2df13 106 unsigned char concat(const char *cstr);
lixianyu 0:740c1eb2df13 107 unsigned char concat(char c);
lixianyu 0:740c1eb2df13 108 unsigned char concat(unsigned char c);
lixianyu 0:740c1eb2df13 109 unsigned char concat(int num);
lixianyu 0:740c1eb2df13 110 unsigned char concat(unsigned int num);
lixianyu 0:740c1eb2df13 111 unsigned char concat(long num);
lixianyu 0:740c1eb2df13 112 unsigned char concat(unsigned long num);
lixianyu 0:740c1eb2df13 113 unsigned char concat(float num);
lixianyu 0:740c1eb2df13 114 unsigned char concat(double num);
lixianyu 0:740c1eb2df13 115 #if 0
lixianyu 0:740c1eb2df13 116 unsigned char concat(const __FlashStringHelper * str);
lixianyu 0:740c1eb2df13 117 #endif
lixianyu 0:740c1eb2df13 118 // if there's not enough memory for the concatenated value, the string
lixianyu 0:740c1eb2df13 119 // will be left unchanged (but this isn't signalled in any way)
lixianyu 0:740c1eb2df13 120 String & operator += (const String &rhs) {
lixianyu 0:740c1eb2df13 121 concat(rhs);
lixianyu 0:740c1eb2df13 122 return (*this);
lixianyu 0:740c1eb2df13 123 }
lixianyu 0:740c1eb2df13 124 String & operator += (const char *cstr) {
lixianyu 0:740c1eb2df13 125 concat(cstr);
lixianyu 0:740c1eb2df13 126 return (*this);
lixianyu 0:740c1eb2df13 127 }
lixianyu 0:740c1eb2df13 128 String & operator += (char c) {
lixianyu 0:740c1eb2df13 129 concat(c);
lixianyu 0:740c1eb2df13 130 return (*this);
lixianyu 0:740c1eb2df13 131 }
lixianyu 0:740c1eb2df13 132 String & operator += (unsigned char num) {
lixianyu 0:740c1eb2df13 133 concat(num);
lixianyu 0:740c1eb2df13 134 return (*this);
lixianyu 0:740c1eb2df13 135 }
lixianyu 0:740c1eb2df13 136 String & operator += (int num) {
lixianyu 0:740c1eb2df13 137 concat(num);
lixianyu 0:740c1eb2df13 138 return (*this);
lixianyu 0:740c1eb2df13 139 }
lixianyu 0:740c1eb2df13 140 String & operator += (unsigned int num) {
lixianyu 0:740c1eb2df13 141 concat(num);
lixianyu 0:740c1eb2df13 142 return (*this);
lixianyu 0:740c1eb2df13 143 }
lixianyu 0:740c1eb2df13 144 String & operator += (long num) {
lixianyu 0:740c1eb2df13 145 concat(num);
lixianyu 0:740c1eb2df13 146 return (*this);
lixianyu 0:740c1eb2df13 147 }
lixianyu 0:740c1eb2df13 148 String & operator += (unsigned long num) {
lixianyu 0:740c1eb2df13 149 concat(num);
lixianyu 0:740c1eb2df13 150 return (*this);
lixianyu 0:740c1eb2df13 151 }
lixianyu 0:740c1eb2df13 152 String & operator += (float num) {
lixianyu 0:740c1eb2df13 153 concat(num);
lixianyu 0:740c1eb2df13 154 return (*this);
lixianyu 0:740c1eb2df13 155 }
lixianyu 0:740c1eb2df13 156 String & operator += (double num) {
lixianyu 0:740c1eb2df13 157 concat(num);
lixianyu 0:740c1eb2df13 158 return (*this);
lixianyu 0:740c1eb2df13 159 }
lixianyu 0:740c1eb2df13 160 #if 0
lixianyu 0:740c1eb2df13 161 String & operator += (const __FlashStringHelper *str) {
lixianyu 0:740c1eb2df13 162 concat(str);
lixianyu 0:740c1eb2df13 163 return (*this);
lixianyu 0:740c1eb2df13 164 }
lixianyu 0:740c1eb2df13 165 #endif
lixianyu 0:740c1eb2df13 166
lixianyu 0:740c1eb2df13 167 friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
lixianyu 0:740c1eb2df13 168 friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
lixianyu 0:740c1eb2df13 169 friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
lixianyu 0:740c1eb2df13 170 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
lixianyu 0:740c1eb2df13 171 friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
lixianyu 0:740c1eb2df13 172 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
lixianyu 0:740c1eb2df13 173 friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
lixianyu 0:740c1eb2df13 174 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
lixianyu 0:740c1eb2df13 175 friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
lixianyu 0:740c1eb2df13 176 friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
lixianyu 0:740c1eb2df13 177 #if 0
lixianyu 0:740c1eb2df13 178 friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
lixianyu 0:740c1eb2df13 179 #endif
lixianyu 0:740c1eb2df13 180 // comparison (only works w/ Strings and "strings")
lixianyu 0:740c1eb2df13 181 operator StringIfHelperType() const {
lixianyu 0:740c1eb2df13 182 return buffer ? &String::StringIfHelper : 0;
lixianyu 0:740c1eb2df13 183 }
lixianyu 0:740c1eb2df13 184 int compareTo(const String &s) const;
lixianyu 0:740c1eb2df13 185 unsigned char equals(const String &s) const;
lixianyu 0:740c1eb2df13 186 unsigned char equals(const char *cstr) const;
lixianyu 0:740c1eb2df13 187 unsigned char operator == (const String &rhs) const {
lixianyu 0:740c1eb2df13 188 return equals(rhs);
lixianyu 0:740c1eb2df13 189 }
lixianyu 0:740c1eb2df13 190 unsigned char operator == (const char *cstr) const {
lixianyu 0:740c1eb2df13 191 return equals(cstr);
lixianyu 0:740c1eb2df13 192 }
lixianyu 0:740c1eb2df13 193 unsigned char operator != (const String &rhs) const {
lixianyu 0:740c1eb2df13 194 return !equals(rhs);
lixianyu 0:740c1eb2df13 195 }
lixianyu 0:740c1eb2df13 196 unsigned char operator != (const char *cstr) const {
lixianyu 0:740c1eb2df13 197 return !equals(cstr);
lixianyu 0:740c1eb2df13 198 }
lixianyu 0:740c1eb2df13 199 unsigned char operator < (const String &rhs) const;
lixianyu 0:740c1eb2df13 200 unsigned char operator > (const String &rhs) const;
lixianyu 0:740c1eb2df13 201 unsigned char operator <= (const String &rhs) const;
lixianyu 0:740c1eb2df13 202 unsigned char operator >= (const String &rhs) const;
lixianyu 0:740c1eb2df13 203 unsigned char equalsIgnoreCase(const String &s) const;
lixianyu 0:740c1eb2df13 204 unsigned char startsWith( const String &prefix) const;
lixianyu 0:740c1eb2df13 205 unsigned char startsWith(const String &prefix, unsigned int offset) const;
lixianyu 0:740c1eb2df13 206 unsigned char endsWith(const String &suffix) const;
lixianyu 0:740c1eb2df13 207
lixianyu 0:740c1eb2df13 208 // character acccess
lixianyu 0:740c1eb2df13 209 char charAt(unsigned int index) const;
lixianyu 0:740c1eb2df13 210 void setCharAt(unsigned int index, char c);
lixianyu 0:740c1eb2df13 211 char operator [] (unsigned int index) const;
lixianyu 0:740c1eb2df13 212 char& operator [] (unsigned int index);
lixianyu 0:740c1eb2df13 213 void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
lixianyu 0:740c1eb2df13 214 void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const {
lixianyu 0:740c1eb2df13 215 getBytes((unsigned char *)buf, bufsize, index);
lixianyu 0:740c1eb2df13 216 }
lixianyu 0:740c1eb2df13 217 const char * c_str() const {
lixianyu 0:740c1eb2df13 218 return buffer;
lixianyu 0:740c1eb2df13 219 }
lixianyu 0:740c1eb2df13 220 const char* begin() {
lixianyu 0:740c1eb2df13 221 return c_str();
lixianyu 0:740c1eb2df13 222 }
lixianyu 0:740c1eb2df13 223 const char* end() {
lixianyu 0:740c1eb2df13 224 return c_str() + length();
lixianyu 0:740c1eb2df13 225 }
lixianyu 0:740c1eb2df13 226
lixianyu 0:740c1eb2df13 227 // search
lixianyu 0:740c1eb2df13 228 int indexOf( char ch ) const;
lixianyu 0:740c1eb2df13 229 int indexOf( char ch, unsigned int fromIndex ) const;
lixianyu 0:740c1eb2df13 230 int indexOf( const String &str ) const;
lixianyu 0:740c1eb2df13 231 int indexOf( const String &str, unsigned int fromIndex ) const;
lixianyu 0:740c1eb2df13 232 int lastIndexOf( char ch ) const;
lixianyu 0:740c1eb2df13 233 int lastIndexOf( char ch, unsigned int fromIndex ) const;
lixianyu 0:740c1eb2df13 234 int lastIndexOf( const String &str ) const;
lixianyu 0:740c1eb2df13 235 int lastIndexOf( const String &str, unsigned int fromIndex ) const;
lixianyu 0:740c1eb2df13 236 String substring( unsigned int beginIndex ) const {
lixianyu 0:740c1eb2df13 237 return substring(beginIndex, len);
lixianyu 0:740c1eb2df13 238 };
lixianyu 0:740c1eb2df13 239 String substring( unsigned int beginIndex, unsigned int endIndex ) const;
lixianyu 0:740c1eb2df13 240
lixianyu 0:740c1eb2df13 241 // modification
lixianyu 0:740c1eb2df13 242 void replace(char find, char replace);
lixianyu 0:740c1eb2df13 243 void replace(const String& find, const String& replace);
lixianyu 0:740c1eb2df13 244 void remove(unsigned int index);
lixianyu 0:740c1eb2df13 245 void remove(unsigned int index, unsigned int count);
lixianyu 0:740c1eb2df13 246 void toLowerCase(void);
lixianyu 0:740c1eb2df13 247 void toUpperCase(void);
lixianyu 0:740c1eb2df13 248 void trim(void);
lixianyu 0:740c1eb2df13 249
lixianyu 0:740c1eb2df13 250 // parsing/conversion
lixianyu 0:740c1eb2df13 251 long toInt(void) const;
lixianyu 0:740c1eb2df13 252 float toFloat(void) const;
lixianyu 0:740c1eb2df13 253
lixianyu 0:740c1eb2df13 254 protected:
lixianyu 0:740c1eb2df13 255 char *buffer; // the actual char array
lixianyu 0:740c1eb2df13 256 unsigned int capacity; // the array length minus one (for the '\0')
lixianyu 0:740c1eb2df13 257 unsigned int len; // the String length (not counting the '\0')
lixianyu 0:740c1eb2df13 258 protected:
lixianyu 0:740c1eb2df13 259 void init(void);
lixianyu 0:740c1eb2df13 260 void invalidate(void);
lixianyu 0:740c1eb2df13 261 unsigned char changeBuffer(unsigned int maxStrLen);
lixianyu 0:740c1eb2df13 262 unsigned char concat(const char *cstr, unsigned int length);
lixianyu 0:740c1eb2df13 263
lixianyu 0:740c1eb2df13 264 // copy and move
lixianyu 0:740c1eb2df13 265 String & copy(const char *cstr, unsigned int length);
lixianyu 0:740c1eb2df13 266 #if 0
lixianyu 0:740c1eb2df13 267 String & copy(const __FlashStringHelper *pstr, unsigned int length);
lixianyu 0:740c1eb2df13 268 #endif
lixianyu 0:740c1eb2df13 269 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
lixianyu 0:740c1eb2df13 270 void move(String &rhs);
lixianyu 0:740c1eb2df13 271 #endif
lixianyu 0:740c1eb2df13 272 };
lixianyu 0:740c1eb2df13 273
lixianyu 0:740c1eb2df13 274 class StringSumHelper : public String
lixianyu 0:740c1eb2df13 275 {
lixianyu 0:740c1eb2df13 276 public:
lixianyu 0:740c1eb2df13 277 StringSumHelper(const String &s) : String(s) {}
lixianyu 0:740c1eb2df13 278 StringSumHelper(const char *p) : String(p) {}
lixianyu 0:740c1eb2df13 279 StringSumHelper(char c) : String(c) {}
lixianyu 0:740c1eb2df13 280 StringSumHelper(unsigned char num) : String(num) {}
lixianyu 0:740c1eb2df13 281 StringSumHelper(int num) : String(num) {}
lixianyu 0:740c1eb2df13 282 StringSumHelper(unsigned int num) : String(num) {}
lixianyu 0:740c1eb2df13 283 StringSumHelper(long num) : String(num) {}
lixianyu 0:740c1eb2df13 284 StringSumHelper(unsigned long num) : String(num) {}
lixianyu 0:740c1eb2df13 285 StringSumHelper(float num) : String(num) {}
lixianyu 0:740c1eb2df13 286 StringSumHelper(double num) : String(num) {}
lixianyu 0:740c1eb2df13 287 };
lixianyu 0:740c1eb2df13 288
lixianyu 0:740c1eb2df13 289 #endif // __cplusplus
lixianyu 0:740c1eb2df13 290 #endif // String_class_h
lixianyu 0:740c1eb2df13 291