diff options
author | Zach Eveland <zeveland@blacklabel-development.com> | 2011-10-27 11:45:13 -0400 |
---|---|---|
committer | Zach Eveland <zeveland@blacklabel-development.com> | 2011-10-27 11:45:13 -0400 |
commit | 3524f2ff0b07338cd0939d9c695a6bcbb2af39dd (patch) | |
tree | 3a1ced5ef4198e9e40e06bc15a4cca5e106f5226 /cores | |
parent | ec93cd74a187a58f0e2bbe496c38edd7d9f33f30 (diff) |
Revert "Merge branch 'master' of github.com:arduino/Arduino into diskloader_reboot"
This reverts commit df9835efafd13685251749bc210c0b96a18a96a5, reversing
changes made to ec45af8bfa9222a807c075dd1db4b5aa798bba03.
Conflicts:
hardware/arduino/variants/mega/pins_arduino.h
libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino
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, 308 insertions, 168 deletions
diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index 9dc67c4..176abe1 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -32,6 +32,7 @@ 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; @@ -40,28 +41,29 @@ class HardwareSerial : public Stream uint8_t _rxen; uint8_t _txen; uint8_t _rxcie; - uint8_t _udre; + uint8_t _udrie; uint8_t _u2x; public: - HardwareSerial(ring_buffer *rx_buffer, + HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_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 udre, uint8_t u2x); + uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, 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 void write(uint8_t); + virtual size_t 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 "usb_api.h" + #include "USBAPI.h" +// extern HardwareSerial Serial_; #endif #if defined(UBRR1H) extern HardwareSerial Serial1; @@ -73,4 +75,6 @@ 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 fd68942..ff9b154 100755 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -23,173 +23,213 @@ #include <stdio.h> #include <string.h> #include <math.h> -#include "wiring.h" +#include "Arduino.h" #include "Print.h" // Public Methods ////////////////////////////////////////////////////////////// /* default implementation: may be overridden */ -void Print::write(const char *str) +size_t Print::write(const uint8_t *buffer, size_t size) { - while (*str) - write(*str++); + size_t n = 0; + while (size--) { + n += write(*buffer++); + } + return n; } -/* default implementation: may be overridden */ -void Print::write(const uint8_t *buffer, size_t size) +size_t Print::print(const __FlashStringHelper *ifsh) { - while (size--) - write(*buffer++); + 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; } -void Print::print(const String &s) +size_t Print::print(const String &s) { - for (unsigned int i = 0; i < s.length(); i++) { - write(s[i]); + size_t n = 0; + for (uint16_t i = 0; i < s.length(); i++) { + n += write(s[i]); } + return n; } -void Print::print(const char str[]) +size_t Print::print(const char str[]) { - write(str); + return write(str); } -void Print::print(char c, int base) +size_t Print::print(char c) { - print((long) c, base); + return write(c); } -void Print::print(unsigned char b, int base) +size_t Print::print(unsigned char b, int base) { - print((unsigned long) b, base); + return print((unsigned long) b, base); } -void Print::print(int n, int base) +size_t Print::print(int n, int base) { - print((long) n, base); + return print((long) n, base); } -void Print::print(unsigned int n, int base) +size_t Print::print(unsigned int n, int base) { - print((unsigned long) n, base); + return print((unsigned long) n, base); } -void Print::print(long n, int base) +size_t Print::print(long n, int base) { if (base == 0) { - write(n); + return write(n); } else if (base == 10) { if (n < 0) { - print('-'); + int t = print('-'); n = -n; + return printNumber(n, 10) + t; } - printNumber(n, 10); + return printNumber(n, 10); } else { - printNumber(n, base); + return printNumber(n, base); } } -void Print::print(unsigned long n, int base) +size_t Print::print(unsigned long n, int base) { - if (base == 0) write(n); - else printNumber(n, base); + if (base == 0) return write(n); + else return printNumber(n, base); } -void Print::print(double n, int digits) +size_t Print::print(double n, int digits) { - printFloat(n, digits); + return printFloat(n, digits); } -void Print::println(void) +size_t Print::println(const __FlashStringHelper *ifsh) { - print('\r'); - print('\n'); + size_t n = print(ifsh); + n += println(); + return n; } -void Print::println(const String &s) +size_t Print::print(const Printable& x) { - print(s); - println(); + return x.printTo(*this); } -void Print::println(const char c[]) +size_t Print::println(void) { - print(c); - println(); + size_t n = print('\r'); + n += print('\n'); + return n; } -void Print::println(char c, int base) +size_t Print::println(const String &s) { - print(c, base); - println(); + size_t n = print(s); + n += println(); + return n; } -void Print::println(unsigned char b, int base) +size_t Print::println(const char c[]) { - print(b, base); - println(); + size_t n = print(c); + n += println(); + return n; } -void Print::println(int n, int base) +size_t Print::println(char c) { - print(n, base); - println(); + size_t n = print(c); + n += println(); + return n; } -void Print::println(unsigned int n, int base) +size_t Print::println(unsigned char b, int base) { - print(n, base); - println(); + size_t n = print(b, base); + n += println(); + return n; } -void Print::println(long n, int base) +size_t Print::println(int num, int base) { - print(n, base); - println(); + size_t n = print(num, base); + n += println(); + return n; } -void Print::println(unsigned long n, int base) +size_t Print::println(unsigned int num, int base) { - print(n, base); - println(); + size_t n = print(num, base); + n += println(); + return n; } -void Print::println(double n, int digits) +size_t Print::println(long num, int base) { - print(n, digits); - println(); + size_t n = print(num, base); + n += println(); + return n; } -// Private Methods ///////////////////////////////////////////////////////////// +size_t Print::println(unsigned long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} -void Print::printNumber(unsigned long n, uint8_t base) +size_t Print::println(double num, int digits) { - unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. - unsigned long i = 0; + size_t n = print(num, digits); + n += println(); + return n; +} - if (n == 0) { - print('0'); - return; - } +size_t Print::println(const Printable& x) +{ + size_t n = print(x); + n += println(); + return n; +} - while (n > 0) { - buf[i++] = n % base; +// 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'; + + // prevent crash if called with base == 1 + if (base < 2) base = 10; + + do { + unsigned long m = n; n /= base; - } + char c = m - base * n; + *--str = c < 10 ? c + '0' : c + 'A' - 10; + } while(n); - for (; i > 0; i--) - print((char) (buf[i - 1] < 10 ? - '0' + buf[i - 1] : - 'A' + buf[i - 1] - 10)); + return write(str); } -void Print::printFloat(double number, uint8_t digits) +size_t Print::printFloat(double number, uint8_t digits) { + size_t n = 0; + // Handle negative numbers if (number < 0.0) { - print('-'); + n += print('-'); number = -number; } @@ -203,18 +243,21 @@ void 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; - print(int_part); + n += print(int_part); // Print the decimal point, but only if there are digits beyond - if (digits > 0) - print("."); + if (digits > 0) { + n += print("."); + } // Extract digits from the remainder one at a time while (digits-- > 0) { remainder *= 10.0; int toPrint = int(remainder); - print(toPrint); + n += print(toPrint); remainder -= toPrint; } + + return n; } diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 56faf9a..d76d2a3 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -1,6 +1,8 @@ /* 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 @@ -17,96 +19,187 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef String_h -#define String_h +#ifndef String_class_h +#define String_class_h +#ifdef __cplusplus -//#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 { - 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: + // 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); -// allocate buffer space -inline void String::getBuffer(unsigned int maxStrLen) -{ - _capacity = maxStrLen; - _buffer = (char *) malloc(_capacity + 1); - if (_buffer == NULL) _length = _capacity = 0; -} + // 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;} -inline String operator+( String lhs, const String &rhs ) -{ - return lhs += rhs; -} + // 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; + // 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 +{ +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) {} +}; -#endif +#endif // __cplusplus +#endif // String_class_h |