diff options
Diffstat (limited to 'cores')
-rw-r--r-- | cores/arduino/HardwareSerial.h | 14 | ||||
-rwxr-xr-x | cores/arduino/Print.cpp | 207 | ||||
-rw-r--r-- | cores/arduino/WString.h | 255 |
3 files changed, 168 insertions, 308 deletions
diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index 176abe1..9dc67c4 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -32,7 +32,6 @@ class HardwareSerial : public Stream { private: ring_buffer *_rx_buffer; - ring_buffer *_tx_buffer; volatile uint8_t *_ubrrh; volatile uint8_t *_ubrrl; volatile uint8_t *_ucsra; @@ -41,29 +40,28 @@ class HardwareSerial : public Stream uint8_t _rxen; uint8_t _txen; uint8_t _rxcie; - uint8_t _udrie; + uint8_t _udre; uint8_t _u2x; public: - HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer, + HardwareSerial(ring_buffer *rx_buffer, volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, volatile uint8_t *ucsra, volatile uint8_t *ucsrb, volatile uint8_t *udr, - uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x); + uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udre, uint8_t u2x); void begin(unsigned long); void end(); virtual int available(void); virtual int peek(void); virtual int read(void); virtual void flush(void); - virtual size_t write(uint8_t); + virtual void write(uint8_t); using Print::write; // pull in write(str) and write(buf, size) from Print }; #if defined(UBRRH) || defined(UBRR0H) extern HardwareSerial Serial; #elif defined(USBCON) - #include "USBAPI.h" -// extern HardwareSerial Serial_; + #include "usb_api.h" #endif #if defined(UBRR1H) extern HardwareSerial Serial1; @@ -75,6 +73,4 @@ class HardwareSerial : public Stream extern HardwareSerial Serial3; #endif -extern void serialEventRun(void) __attribute__((weak)); - #endif diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index ff9b154..fd68942 100755 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -23,213 +23,173 @@ #include <stdio.h> #include <string.h> #include <math.h> -#include "Arduino.h" +#include "wiring.h" #include "Print.h" // Public Methods ////////////////////////////////////////////////////////////// /* default implementation: may be overridden */ -size_t Print::write(const uint8_t *buffer, size_t size) +void Print::write(const char *str) { - size_t n = 0; - while (size--) { - n += write(*buffer++); - } - return n; + while (*str) + write(*str++); } -size_t Print::print(const __FlashStringHelper *ifsh) +/* default implementation: may be overridden */ +void Print::write(const uint8_t *buffer, size_t size) { - const prog_char *p = (const prog_char *)ifsh; - size_t n = 0; - while (1) { - unsigned char c = pgm_read_byte(p++); - if (c == 0) break; - n += write(c); - } - return n; + while (size--) + write(*buffer++); } -size_t Print::print(const String &s) +void Print::print(const String &s) { - size_t n = 0; - for (uint16_t i = 0; i < s.length(); i++) { - n += write(s[i]); + for (unsigned int i = 0; i < s.length(); i++) { + write(s[i]); } - return n; } -size_t Print::print(const char str[]) +void Print::print(const char str[]) { - return write(str); + write(str); } -size_t Print::print(char c) +void Print::print(char c, int base) { - return write(c); + print((long) c, base); } -size_t Print::print(unsigned char b, int base) +void Print::print(unsigned char b, int base) { - return print((unsigned long) b, base); + print((unsigned long) b, base); } -size_t Print::print(int n, int base) +void Print::print(int n, int base) { - return print((long) n, base); + print((long) n, base); } -size_t Print::print(unsigned int n, int base) +void Print::print(unsigned int n, int base) { - return print((unsigned long) n, base); + print((unsigned long) n, base); } -size_t Print::print(long n, int base) +void Print::print(long n, int base) { if (base == 0) { - return write(n); + write(n); } else if (base == 10) { if (n < 0) { - int t = print('-'); + print('-'); n = -n; - return printNumber(n, 10) + t; } - return printNumber(n, 10); + printNumber(n, 10); } else { - return printNumber(n, base); + printNumber(n, base); } } -size_t Print::print(unsigned long n, int base) -{ - if (base == 0) return write(n); - else return printNumber(n, base); -} - -size_t Print::print(double n, int digits) +void Print::print(unsigned long n, int base) { - return printFloat(n, digits); + if (base == 0) write(n); + else printNumber(n, base); } -size_t Print::println(const __FlashStringHelper *ifsh) +void Print::print(double n, int digits) { - size_t n = print(ifsh); - n += println(); - return n; + printFloat(n, digits); } -size_t Print::print(const Printable& x) +void Print::println(void) { - return x.printTo(*this); + print('\r'); + print('\n'); } -size_t Print::println(void) +void Print::println(const String &s) { - size_t n = print('\r'); - n += print('\n'); - return n; + print(s); + println(); } -size_t Print::println(const String &s) +void Print::println(const char c[]) { - size_t n = print(s); - n += println(); - return n; + print(c); + println(); } -size_t Print::println(const char c[]) +void Print::println(char c, int base) { - size_t n = print(c); - n += println(); - return n; + print(c, base); + println(); } -size_t Print::println(char c) +void Print::println(unsigned char b, int base) { - size_t n = print(c); - n += println(); - return n; + print(b, base); + println(); } -size_t Print::println(unsigned char b, int base) +void Print::println(int n, int base) { - size_t n = print(b, base); - n += println(); - return n; + print(n, base); + println(); } -size_t Print::println(int num, int base) +void Print::println(unsigned int n, int base) { - size_t n = print(num, base); - n += println(); - return n; + print(n, base); + println(); } -size_t Print::println(unsigned int num, int base) +void Print::println(long n, int base) { - size_t n = print(num, base); - n += println(); - return n; + print(n, base); + println(); } -size_t Print::println(long num, int base) +void Print::println(unsigned long n, int base) { - size_t n = print(num, base); - n += println(); - return n; + print(n, base); + println(); } -size_t Print::println(unsigned long num, int base) +void Print::println(double n, int digits) { - size_t n = print(num, base); - n += println(); - return n; -} - -size_t Print::println(double num, int digits) -{ - size_t n = print(num, digits); - n += println(); - return n; -} - -size_t Print::println(const Printable& x) -{ - size_t n = print(x); - n += println(); - return n; + print(n, digits); + println(); } // Private Methods ///////////////////////////////////////////////////////////// -size_t Print::printNumber(unsigned long n, uint8_t base) { - char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. - char *str = &buf[sizeof(buf) - 1]; - - *str = '\0'; +void Print::printNumber(unsigned long n, uint8_t base) +{ + unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. + unsigned long i = 0; - // prevent crash if called with base == 1 - if (base < 2) base = 10; + if (n == 0) { + print('0'); + return; + } - do { - unsigned long m = n; + while (n > 0) { + buf[i++] = n % base; n /= base; - char c = m - base * n; - *--str = c < 10 ? c + '0' : c + 'A' - 10; - } while(n); + } - return write(str); + for (; i > 0; i--) + print((char) (buf[i - 1] < 10 ? + '0' + buf[i - 1] : + 'A' + buf[i - 1] - 10)); } -size_t Print::printFloat(double number, uint8_t digits) +void Print::printFloat(double number, uint8_t digits) { - size_t n = 0; - // Handle negative numbers if (number < 0.0) { - n += print('-'); + print('-'); number = -number; } @@ -243,21 +203,18 @@ size_t Print::printFloat(double number, uint8_t digits) // Extract the integer part of the number and print it unsigned long int_part = (unsigned long)number; double remainder = number - (double)int_part; - n += print(int_part); + print(int_part); // Print the decimal point, but only if there are digits beyond - if (digits > 0) { - n += print("."); - } + if (digits > 0) + print("."); // Extract digits from the remainder one at a time while (digits-- > 0) { remainder *= 10.0; int toPrint = int(remainder); - n += print(toPrint); + print(toPrint); remainder -= toPrint; } - - return n; } diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index d76d2a3..56faf9a 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -1,8 +1,6 @@ /* WString.h - String library for Wiring & Arduino - ...mostly rewritten by Paul Stoffregen... Copyright (c) 2009-10 Hernando Barragan. All right reserved. - Copyright 2011, Paul Stoffregen, paul@pjrc.com This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -19,187 +17,96 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef String_class_h -#define String_class_h -#ifdef __cplusplus +#ifndef String_h +#define String_h +//#include "WProgram.h" #include <stdlib.h> #include <string.h> #include <ctype.h> -#include <avr/pgmspace.h> -// When compiling programs with this class, the following gcc parameters -// dramatically increase performance and memory (RAM) efficiency, typically -// with little or no increase in code size. -// -felide-constructors -// -std=c++0x - -class __FlashStringHelper; -#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal))) - -// An inherited class for holding the result of a concatenation. These -// result objects are assumed to be writable by subsequent concatenations. -class StringSumHelper; - -// The string class class String { - // use a function pointer to allow for "if (s)" without the - // complications of an operator bool(). for more information, see: - // http://www.artima.com/cppsource/safebool.html - typedef void (String::*StringIfHelperType)() const; - void StringIfHelper() const {} - -public: - // constructors - // creates a copy of the initial value. - // if the initial value is null or invalid, or if memory allocation - // fails, the string will be marked as invalid (i.e. "if (s)" will - // be false). - String(const char *cstr = ""); - String(const String &str); - #ifdef __GXX_EXPERIMENTAL_CXX0X__ - String(String &&rval); - String(StringSumHelper &&rval); - #endif - explicit String(char c); - explicit String(unsigned char, unsigned char base=10); - explicit String(int, unsigned char base=10); - explicit String(unsigned int, unsigned char base=10); - explicit String(long, unsigned char base=10); - explicit String(unsigned long, unsigned char base=10); - ~String(void); - - // memory management - // return true on success, false on failure (in which case, the string - // is left unchanged). reserve(0), if successful, will validate an - // invalid string (i.e., "if (s)" will be true afterwards) - unsigned char reserve(unsigned int size); - inline unsigned int length(void) const {return len;} - - // creates a copy of the assigned value. if the value is null or - // invalid, or if the memory allocation fails, the string will be - // marked as invalid ("if (s)" will be false). - String & operator = (const String &rhs); - String & operator = (const char *cstr); - #ifdef __GXX_EXPERIMENTAL_CXX0X__ - String & operator = (String &&rval); - String & operator = (StringSumHelper &&rval); - #endif - - // concatenate (works w/ built-in types) - - // returns true on success, false on failure (in which case, the string - // is left unchanged). if the argument is null or invalid, the - // concatenation is considered unsucessful. - unsigned char concat(const String &str); - unsigned char concat(const char *cstr); - unsigned char concat(char c); - unsigned char concat(unsigned char c); - unsigned char concat(int num); - unsigned char concat(unsigned int num); - unsigned char concat(long num); - unsigned char concat(unsigned long num); - - // if there's not enough memory for the concatenated value, the string - // will be left unchanged (but this isn't signalled in any way) - String & operator += (const String &rhs) {concat(rhs); return (*this);} - String & operator += (const char *cstr) {concat(cstr); return (*this);} - String & operator += (char c) {concat(c); return (*this);} - String & operator += (unsigned char num) {concat(num); return (*this);} - String & operator += (int num) {concat(num); return (*this);} - String & operator += (unsigned int num) {concat(num); return (*this);} - String & operator += (long num) {concat(num); return (*this);} - String & operator += (unsigned long num) {concat(num); return (*this);} - - friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); - friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); - friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); - friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num); - friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); - friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); - friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); - friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); - - // comparison (only works w/ Strings and "strings") - operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } - int compareTo(const String &s) const; - unsigned char equals(const String &s) const; - unsigned char equals(const char *cstr) const; - unsigned char operator == (const String &rhs) const {return equals(rhs);} - unsigned char operator == (const char *cstr) const {return equals(cstr);} - unsigned char operator != (const String &rhs) const {return !equals(rhs);} - unsigned char operator != (const char *cstr) const {return !equals(cstr);} - unsigned char operator < (const String &rhs) const; - unsigned char operator > (const String &rhs) const; - unsigned char operator <= (const String &rhs) const; - unsigned char operator >= (const String &rhs) const; - unsigned char equalsIgnoreCase(const String &s) const; - unsigned char startsWith( const String &prefix) const; - unsigned char startsWith(const String &prefix, unsigned int offset) const; - unsigned char endsWith(const String &suffix) const; + public: + // constructors + String( const char *value = "" ); + String( const String &value ); + String( const char ); + String( const unsigned char ); + String( const int, const int base=10); + String( const unsigned int, const int base=10 ); + String( const long, const int base=10 ); + String( const unsigned long, const int base=10 ); + ~String() { free(_buffer); _length = _capacity = 0;} //added _length = _capacity = 0; + + // operators + const String & operator = ( const String &rhs ); + const String & operator +=( const String &rhs ); + //const String & operator +=( const char ); + int operator ==( const String &rhs ) const; + int operator !=( const String &rhs ) const; + int operator < ( const String &rhs ) const; + int operator > ( const String &rhs ) const; + int operator <=( const String &rhs ) const; + int operator >=( const String &rhs ) const; + char operator []( unsigned int index ) const; + char& operator []( unsigned int index ); + //operator const char *() const { return _buffer; } + + // general methods + char charAt( unsigned int index ) const; + int compareTo( const String &anotherString ) const; + unsigned char endsWith( const String &suffix ) const; + unsigned char equals( const String &anObject ) const; + unsigned char equalsIgnoreCase( const String &anotherString ) const; + int indexOf( char ch ) const; + int indexOf( char ch, unsigned int fromIndex ) const; + int indexOf( const String &str ) const; + int indexOf( const String &str, unsigned int fromIndex ) const; + int lastIndexOf( char ch ) const; + int lastIndexOf( char ch, unsigned int fromIndex ) const; + int lastIndexOf( const String &str ) const; + int lastIndexOf( const String &str, unsigned int fromIndex ) const; + unsigned int length( ) const { return _length; } + void setCharAt(unsigned int index, const char ch); + unsigned char startsWith( const String &prefix ) const; + unsigned char startsWith( const String &prefix, unsigned int toffset ) const; + String substring( unsigned int beginIndex ) const; + String substring( unsigned int beginIndex, unsigned int endIndex ) const; + String toLowerCase( ) const; + String toUpperCase( ) const; + String trim( ) const; + void getBytes(unsigned char *buf, unsigned int bufsize); + void toCharArray(char *buf, unsigned int bufsize); + long toInt( ); + const String& concat( const String &str ); + String replace( char oldChar, char newChar ); + String replace( const String& match, const String& replace ); + friend String operator + ( String lhs, const String &rhs ); + + protected: + char *_buffer; // the actual char array + unsigned int _capacity; // the array length minus one (for the '\0') + unsigned int _length; // the String length (not counting the '\0') + + void getBuffer(unsigned int maxStrLen); + + private: - // character acccess - char charAt(unsigned int index) const; - void setCharAt(unsigned int index, char c); - char operator [] (unsigned int index) const; - char& operator [] (unsigned int index); - void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const; - void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const - {getBytes((unsigned char *)buf, bufsize, index);} - - // search - int indexOf( char ch ) const; - int indexOf( char ch, unsigned int fromIndex ) const; - int indexOf( const String &str ) const; - int indexOf( const String &str, unsigned int fromIndex ) const; - int lastIndexOf( char ch ) const; - int lastIndexOf( char ch, unsigned int fromIndex ) const; - int lastIndexOf( const String &str ) const; - int lastIndexOf( const String &str, unsigned int fromIndex ) const; - String substring( unsigned int beginIndex ) const; - String substring( unsigned int beginIndex, unsigned int endIndex ) const; - - // modification - void replace(char find, char replace); - void replace(const String& find, const String& replace); - void toLowerCase(void); - void toUpperCase(void); - void trim(void); - - // parsing/conversion - long toInt(void) const; - -protected: - char *buffer; // the actual char array - unsigned int capacity; // the array length minus one (for the '\0') - unsigned int len; // the String length (not counting the '\0') - unsigned char flags; // unused, for future features -protected: - void init(void); - void invalidate(void); - unsigned char changeBuffer(unsigned int maxStrLen); - unsigned char concat(const char *cstr, unsigned int length); - - // copy and move - String & copy(const char *cstr, unsigned int length); - #ifdef __GXX_EXPERIMENTAL_CXX0X__ - void move(String &rhs); - #endif }; -class StringSumHelper : public String +// allocate buffer space +inline void String::getBuffer(unsigned int maxStrLen) { -public: - StringSumHelper(const String &s) : String(s) {} - StringSumHelper(const char *p) : String(p) {} - StringSumHelper(char c) : String(c) {} - StringSumHelper(unsigned char num) : String(num) {} - StringSumHelper(int num) : String(num) {} - StringSumHelper(unsigned int num) : String(num) {} - StringSumHelper(long num) : String(num) {} - StringSumHelper(unsigned long num) : String(num) {} -}; + _capacity = maxStrLen; + _buffer = (char *) malloc(_capacity + 1); + if (_buffer == NULL) _length = _capacity = 0; +} + +inline String operator+( String lhs, const String &rhs ) +{ + return lhs += rhs; +} + -#endif // __cplusplus -#endif // String_class_h +#endif |