Merge branch 'new-extension' of https://github.com/arduino/Arduino

This commit is contained in:
amcewen 2011-08-27 23:53:42 +01:00
commit f9d50a7930
11 changed files with 579 additions and 128 deletions

View File

@ -14,11 +14,11 @@ uno.bootloader.lock_bits=0x0F
uno.build.mcu=atmega328p uno.build.mcu=atmega328p
uno.build.f_cpu=16000000L uno.build.f_cpu=16000000L
uno.build.core=arduino uno.build.core=arduino
uno.build.pins=standard uno.build.variant=standard
############################################################## ##############################################################
atmega328.name=Arduino Duemilanove or Nano w/ ATmega328 atmega328.name=Arduino Duemilanove w/ ATmega328
atmega328.upload.protocol=stk500 atmega328.upload.protocol=stk500
atmega328.upload.maximum_size=30720 atmega328.upload.maximum_size=30720
@ -35,11 +35,11 @@ atmega328.bootloader.lock_bits=0x0F
atmega328.build.mcu=atmega328p atmega328.build.mcu=atmega328p
atmega328.build.f_cpu=16000000L atmega328.build.f_cpu=16000000L
atmega328.build.core=arduino atmega328.build.core=arduino
atmega328.build.pins=standard atmega328.build.variant=standard
############################################################## ##############################################################
diecimila.name=Arduino Diecimila, Duemilanove, or Nano w/ ATmega168 diecimila.name=Arduino Diecimila or Duemilanove w/ ATmega168
diecimila.upload.protocol=stk500 diecimila.upload.protocol=stk500
diecimila.upload.maximum_size=14336 diecimila.upload.maximum_size=14336
@ -56,7 +56,49 @@ diecimila.bootloader.lock_bits=0x0F
diecimila.build.mcu=atmega168 diecimila.build.mcu=atmega168
diecimila.build.f_cpu=16000000L diecimila.build.f_cpu=16000000L
diecimila.build.core=arduino diecimila.build.core=arduino
diecimila.build.pins=standard diecimila.build.variant=standard
##############################################################
nano328.name=Arduino Nano w/ ATmega328
nano328.upload.protocol=stk500
nano328.upload.maximum_size=30720
nano328.upload.speed=57600
nano328.bootloader.low_fuses=0xFF
nano328.bootloader.high_fuses=0xDA
nano328.bootloader.extended_fuses=0x05
nano328.bootloader.path=atmega
nano328.bootloader.file=ATmegaBOOT_168_atmega328.hex
nano328.bootloader.unlock_bits=0x3F
nano328.bootloader.lock_bits=0x0F
nano328.build.mcu=atmega328p
nano328.build.f_cpu=16000000L
nano328.build.core=arduino
nano328.build.variant=eightanaloginputs
##############################################################
nano.name=Arduino Nano w/ ATmega168
nano.upload.protocol=stk500
nano.upload.maximum_size=14336
nano.upload.speed=19200
nano.bootloader.low_fuses=0xff
nano.bootloader.high_fuses=0xdd
nano.bootloader.extended_fuses=0x00
nano.bootloader.path=atmega
nano.bootloader.file=ATmegaBOOT_168_diecimila.hex
nano.bootloader.unlock_bits=0x3F
nano.bootloader.lock_bits=0x0F
nano.build.mcu=atmega168
nano.build.f_cpu=16000000L
nano.build.core=arduino
nano.build.variant=eightanaloginputs
############################################################## ##############################################################
@ -77,7 +119,7 @@ mega2560.bootloader.lock_bits=0x0F
mega2560.build.mcu=atmega2560 mega2560.build.mcu=atmega2560
mega2560.build.f_cpu=16000000L mega2560.build.f_cpu=16000000L
mega2560.build.core=arduino mega2560.build.core=arduino
mega2560.build.pins=mega mega2560.build.variant=mega
############################################################## ##############################################################
@ -98,7 +140,7 @@ mega.bootloader.lock_bits=0x0F
mega.build.mcu=atmega1280 mega.build.mcu=atmega1280
mega.build.f_cpu=16000000L mega.build.f_cpu=16000000L
mega.build.core=arduino mega.build.core=arduino
mega.build.pins=mega mega.build.variant=mega
############################################################## ##############################################################
@ -119,7 +161,7 @@ mini.bootloader.lock_bits=0x0F
mini.build.mcu=atmega168 mini.build.mcu=atmega168
mini.build.f_cpu=16000000L mini.build.f_cpu=16000000L
mini.build.core=arduino mini.build.core=arduino
mini.build.pins=standard mini.build.variant=eightanaloginputs
############################################################## ##############################################################
@ -140,7 +182,7 @@ fio.bootloader.lock_bits=0x0F
fio.build.mcu=atmega328p fio.build.mcu=atmega328p
fio.build.f_cpu=8000000L fio.build.f_cpu=8000000L
fio.build.core=arduino fio.build.core=arduino
fio.build.pins=standard fio.build.variant=eightanaloginputs
############################################################## ##############################################################
@ -162,7 +204,7 @@ bt328.bootloader.lock_bits=0x0F
bt328.build.mcu=atmega328p bt328.build.mcu=atmega328p
bt328.build.f_cpu=16000000L bt328.build.f_cpu=16000000L
bt328.build.core=arduino bt328.build.core=arduino
bt328.build.pins=standard bt328.build.variant=eightanaloginputs
############################################################## ##############################################################
@ -184,7 +226,7 @@ bt.bootloader.lock_bits=0x0F
bt.build.mcu=atmega168 bt.build.mcu=atmega168
bt.build.f_cpu=16000000L bt.build.f_cpu=16000000L
bt.build.core=arduino bt.build.core=arduino
bt.build.pins=standard bt.build.variant=eightanaloginputs
############################################################## ##############################################################
@ -205,7 +247,7 @@ lilypad328.bootloader.lock_bits=0x0F
lilypad328.build.mcu=atmega328p lilypad328.build.mcu=atmega328p
lilypad328.build.f_cpu=8000000L lilypad328.build.f_cpu=8000000L
lilypad328.build.core=arduino lilypad328.build.core=arduino
lilypad328.build.pins=standard lilypad328.build.variant=standard
############################################################## ##############################################################
@ -226,7 +268,7 @@ lilypad.bootloader.lock_bits=0x0F
lilypad.build.mcu=atmega168 lilypad.build.mcu=atmega168
lilypad.build.f_cpu=8000000L lilypad.build.f_cpu=8000000L
lilypad.build.core=arduino lilypad.build.core=arduino
lilypad.build.pins=standard lilypad.build.variant=standard
############################################################## ##############################################################
@ -247,7 +289,7 @@ pro5v328.bootloader.lock_bits=0x0F
pro5v328.build.mcu=atmega328p pro5v328.build.mcu=atmega328p
pro5v328.build.f_cpu=16000000L pro5v328.build.f_cpu=16000000L
pro5v328.build.core=arduino pro5v328.build.core=arduino
pro5v328.build.pins=standard pro5v328.build.variant=standard
############################################################## ##############################################################
@ -268,7 +310,7 @@ pro5v.bootloader.lock_bits=0x0F
pro5v.build.mcu=atmega168 pro5v.build.mcu=atmega168
pro5v.build.f_cpu=16000000L pro5v.build.f_cpu=16000000L
pro5v.build.core=arduino pro5v.build.core=arduino
pro5v.build.pins=standard pro5v.build.variant=standard
############################################################## ##############################################################
@ -289,7 +331,7 @@ pro328.bootloader.lock_bits=0x0F
pro328.build.mcu=atmega328p pro328.build.mcu=atmega328p
pro328.build.f_cpu=8000000L pro328.build.f_cpu=8000000L
pro328.build.core=arduino pro328.build.core=arduino
pro328.build.pins=standard pro328.build.variant=standard
############################################################## ##############################################################
@ -310,7 +352,7 @@ pro.bootloader.lock_bits=0x0F
pro.build.mcu=atmega168 pro.build.mcu=atmega168
pro.build.f_cpu=8000000L pro.build.f_cpu=8000000L
pro.build.core=arduino pro.build.core=arduino
pro.build.pins=standard pro.build.variant=standard
############################################################## ##############################################################
@ -331,7 +373,7 @@ atmega168.bootloader.lock_bits=0x0F
atmega168.build.mcu=atmega168 atmega168.build.mcu=atmega168
atmega168.build.f_cpu=16000000L atmega168.build.f_cpu=16000000L
atmega168.build.core=arduino atmega168.build.core=arduino
atmega168.build.pins=standard atmega168.build.variant=standard
############################################################## ##############################################################
@ -351,4 +393,4 @@ atmega8.bootloader.lock_bits=0x0F
atmega8.build.mcu=atmega8 atmega8.build.mcu=atmega8
atmega8.build.f_cpu=16000000L atmega8.build.f_cpu=16000000L
atmega8.build.core=arduino atmega8.build.core=arduino
atmega8.build.pins=standard atmega8.build.variant=standard

View File

@ -352,12 +352,13 @@ void HardwareSerial::flush()
; ;
} }
void HardwareSerial::write(uint8_t c) size_t HardwareSerial::write(uint8_t c)
{ {
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE; int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
// If the output buffer is full, there's nothing for it other than to // If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit // wait for the interrupt handler to empty it a bit
// ???: return 0 here instead?
while (i == _tx_buffer->tail) while (i == _tx_buffer->tail)
; ;
@ -365,6 +366,8 @@ void HardwareSerial::write(uint8_t c)
_tx_buffer->head = i; _tx_buffer->head = i;
sbi(*_ucsrb, _udrie); sbi(*_ucsrb, _udrie);
return 1;
} }
// Preinstantiate Objects ////////////////////////////////////////////////////// // Preinstantiate Objects //////////////////////////////////////////////////////

View File

@ -55,7 +55,7 @@ class HardwareSerial : public Stream
virtual int peek(void); virtual int peek(void);
virtual int read(void); virtual int read(void);
virtual void flush(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 using Print::write; // pull in write(str) and write(buf, size) from Print
}; };

View File

@ -30,167 +30,190 @@
// Public Methods ////////////////////////////////////////////////////////////// // Public Methods //////////////////////////////////////////////////////////////
/* default implementation: may be overridden */ /* default implementation: may be overridden */
void Print::write(const char *str) size_t Print::write(const char *str)
{ {
while (*str) size_t n = 0;
write(*str++); while (*str) {
n += write(*str++);
}
return n;
} }
/* default implementation: may be overridden */ /* default implementation: may be overridden */
void Print::write(const uint8_t *buffer, size_t size) size_t Print::write(const uint8_t *buffer, size_t size)
{ {
while (size--) size_t n = 0;
write(*buffer++); while (size--) {
n += write(*buffer++);
}
return n;
} }
void Print::print(const __FlashStringHelper *ifsh) size_t Print::print(const __FlashStringHelper *ifsh)
{ {
const prog_char *p = (const prog_char *)ifsh; const prog_char *p = (const prog_char *)ifsh;
size_t n = 0;
while (1) { while (1) {
unsigned char c = pgm_read_byte(p++); unsigned char c = pgm_read_byte(p++);
if (c == 0) return; if (c == 0) break;
write(c); n += write(c);
} }
return n;
} }
void Print::print(const String &s) size_t Print::print(const String &s)
{ {
size_t n = 0;
for (int i = 0; i < s.length(); i++) { for (int i = 0; i < s.length(); i++) {
write(s[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) size_t Print::print(char c)
{ {
write(c); 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) { if (base == 0) {
write(n); return write(n);
} else if (base == 10) { } else if (base == 10) {
if (n < 0) { if (n < 0) {
print('-'); int t = print('-');
n = -n; n = -n;
return printNumber(n, 10) + t;
} }
printNumber(n, 10); return printNumber(n, 10);
} else { } 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); if (base == 0) return write(n);
else printNumber(n, base); 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(const __FlashStringHelper *ifsh) size_t Print::println(const __FlashStringHelper *ifsh)
{ {
print(ifsh); size_t n = print(ifsh);
println(); n += println();
return n;
} }
void Print::print(const Printable& x) size_t Print::print(const Printable& x)
{ {
x.printTo(*this); return x.printTo(*this);
} }
void Print::println(void) size_t Print::println(void)
{ {
print('\r'); size_t n = print('\r');
print('\n'); n += print('\n');
return n;
} }
void Print::println(const String &s) size_t Print::println(const String &s)
{ {
print(s); size_t n = print(s);
println(); n += println();
return n;
} }
void Print::println(const char c[]) size_t Print::println(const char c[])
{ {
print(c); size_t n = print(c);
println(); n += println();
return n;
} }
void Print::println(char c) size_t Print::println(char c)
{ {
print(c); size_t n = print(c);
println(); n += println();
return n;
} }
void Print::println(unsigned char b, int base) size_t Print::println(unsigned char b, int base)
{ {
print(b, base); size_t n = print(b, base);
println(); n += println();
return n;
} }
void Print::println(int n, int base) size_t Print::println(int num, int base)
{ {
print(n, base); size_t n = print(num, base);
println(); n += println();
return n;
} }
void Print::println(unsigned int n, int base) size_t Print::println(unsigned int num, int base)
{ {
print(n, base); size_t n = print(num, base);
println(); n += println();
return n;
} }
void Print::println(long n, int base) size_t Print::println(long num, int base)
{ {
print(n, base); size_t n = print(num, base);
println(); n += println();
return n;
} }
void Print::println(unsigned long n, int base) size_t Print::println(unsigned long num, int base)
{ {
print(n, base); size_t n = print(num, base);
println(); n += println();
return n;
} }
void Print::println(double n, int digits) size_t Print::println(double num, int digits)
{ {
print(n, digits); size_t n = print(num, digits);
println(); n += println();
return n;
} }
void Print::println(const Printable& x) size_t Print::println(const Printable& x)
{ {
print(x); size_t n = print(x);
println(); n += println();
return n;
} }
// Private Methods ///////////////////////////////////////////////////////////// // Private Methods /////////////////////////////////////////////////////////////
void Print::printNumber(unsigned long n, uint8_t base) { size_t Print::printNumber(unsigned long n, uint8_t base) {
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1]; char *str = &buf[sizeof(buf) - 1];
@ -206,15 +229,17 @@ void Print::printNumber(unsigned long n, uint8_t base) {
*--str = c < 10 ? c + '0' : c + 'A' - 10; *--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n); } while(n);
write(str); 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 // Handle negative numbers
if (number < 0.0) if (number < 0.0)
{ {
print('-'); n += print('-');
number = -number; number = -number;
} }
@ -228,18 +253,21 @@ void Print::printFloat(double number, uint8_t digits)
// Extract the integer part of the number and print it // Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number; unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part; double remainder = number - (double)int_part;
print(int_part); n += print(int_part);
// Print the decimal point, but only if there are digits beyond // Print the decimal point, but only if there are digits beyond
if (digits > 0) if (digits > 0) {
print("."); n += print(".");
}
// Extract digits from the remainder one at a time // Extract digits from the remainder one at a time
while (digits-- > 0) while (digits-- > 0)
{ {
remainder *= 10.0; remainder *= 10.0;
int toPrint = int(remainder); int toPrint = int(remainder);
print(toPrint); n += print(toPrint);
remainder -= toPrint; remainder -= toPrint;
} }
return n;
} }

View File

@ -34,37 +34,45 @@
class Print class Print
{ {
private: private:
void printNumber(unsigned long, uint8_t); int write_error;
void printFloat(double, uint8_t); size_t printNumber(unsigned long, uint8_t);
size_t printFloat(double, uint8_t);
protected:
void setWriteError(int err = 1) { write_error = err; }
public: public:
virtual void write(uint8_t) = 0; Print() : write_error(0) {}
virtual void write(const char *str);
virtual void write(const uint8_t *buffer, size_t size); int writeError() { return write_error; }
void clearWriteError() { setWriteError(0); }
virtual size_t write(uint8_t) = 0;
virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buffer, size_t size);
void print(const __FlashStringHelper *); size_t print(const __FlashStringHelper *);
void print(const String &); size_t print(const String &);
void print(const char[]); size_t print(const char[]);
void print(char); size_t print(char);
void print(unsigned char, int = DEC); size_t print(unsigned char, int = DEC);
void print(int, int = DEC); size_t print(int, int = DEC);
void print(unsigned int, int = DEC); size_t print(unsigned int, int = DEC);
void print(long, int = DEC); size_t print(long, int = DEC);
void print(unsigned long, int = DEC); size_t print(unsigned long, int = DEC);
void print(double, int = 2); size_t print(double, int = 2);
void print(const Printable&); size_t print(const Printable&);
void println(const __FlashStringHelper *); size_t println(const __FlashStringHelper *);
void println(const String &s); size_t println(const String &s);
void println(const char[]); size_t println(const char[]);
void println(char); size_t println(char);
void println(unsigned char, int = DEC); size_t println(unsigned char, int = DEC);
void println(int, int = DEC); size_t println(int, int = DEC);
void println(unsigned int, int = DEC); size_t println(unsigned int, int = DEC);
void println(long, int = DEC); size_t println(long, int = DEC);
void println(unsigned long, int = DEC); size_t println(unsigned long, int = DEC);
void println(double, int = 2); size_t println(double, int = 2);
void println(const Printable&); size_t println(const Printable&);
void println(void); size_t println(void);
}; };
#endif #endif

View File

@ -33,8 +33,7 @@ class Print;
class Printable class Printable
{ {
public: public:
virtual ~Printable() {}; virtual size_t printTo(Print& p) const = 0;
virtual void printTo(Print& p) const =0;
}; };
#endif #endif

233
cores/arduino/Stream.cpp Normal file
View File

@ -0,0 +1,233 @@
/*
Stream.cpp - adds parsing methods to Stream class
Copyright (c) 2008 David A. Mellis. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Created July 2011
parsing functions based on TextFinder library by Michael Margolis
*/
#include "Arduino.h"
#include "Stream.h"
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
// private method to read stream with timeout
int Stream::timedRead()
{
//Serial.println(_timeout);
this->_startMillis = millis();
while(millis() - this->_startMillis < this->_timeout)
{
if (this->available() > 0) {
return this->read();
}
}
return -1; // -1 indicates timeout
}
// returns the next digit in the stream or -1 if timeout
// discards non-numeric characters
int Stream::getNextDigit()
{
int c;
do{
c = timedRead();
if( c < 0)
return c; // timeout
}
while( c != '-' && (c < '0' || c > '9') ) ;
return c;
}
// Public Methods
//////////////////////////////////////////////////////////////
void Stream::setTimeout( long timeout) // sets the maximum number of milliseconds to wait
{
this->_timeout = timeout;
}
// find returns true if the target string is found
bool Stream::find(char *target)
{
return findUntil(target, NULL);
}
// reads data from the stream until the target string of given length is found
// returns true if target string is found, false if timed out
bool Stream::find(char *target, size_t length)
{
return findUntil(target, length, NULL, 0);
}
// as find but search ends if the terminator string is found
bool Stream::findUntil(char *target, char *terminator)
{
return findUntil(target, strlen(target), terminator, strlen(terminator));
}
// reads data from the stream until the target string of the given length is found
// search terminated if the terminator string is found
// returns true if target string is found, false if terminated or timed out
bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
{
size_t index = 0; // maximum target string length is 64k bytes!
size_t termIndex = 0;
int c;
if( *target == 0)
return true; // return true if target is a null string
while( (c = timedRead()) > 0){
if( c == target[index]){
//////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
if(++index >= targetLen){ // return true if all chars in the target match
return true;
}
}
else{
index = 0; // reset index if any char does not match
}
if(termLen > 0 && c == terminator[termIndex]){
if(++termIndex >= termLen)
return false; // return false if terminate string found before target string
}
else
termIndex = 0;
}
return false;
}
// returns the first valid (long) integer value from the current position.
// initial characters that are not digits (or the minus sign) are skipped
// function is terminated by the first character that is not a digit.
long Stream::parseInt()
{
return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
}
// as above but a given skipChar is ignored
// this allows format characters (typically commas) in values to be ignored
long Stream::parseInt(char skipChar)
{
boolean isNegative = false;
long value = 0;
int c;
c = getNextDigit();
// ignore non numeric leading characters
if(c < 0)
return 0; // zero returned if timeout
do{
if(c == skipChar)
; // ignore this charactor
else if(c == '-')
isNegative = true;
else if(c >= '0' && c <= '9') // is c a digit?
value = value * 10 + c - '0';
c = timedRead();
}
while( (c >= '0' && c <= '9') || c == skipChar );
if(isNegative)
value = -value;
return value;
}
// as parseInt but returns a floating point value
float Stream::parseFloat()
{
parseFloat(NO_SKIP_CHAR);
}
// as above but the given skipChar is ignored
// this allows format characters (typically commas) in values to be ignored
float Stream::parseFloat(char skipChar){
boolean isNegative = false;
boolean isFraction = false;
long value = 0;
float fValue;
char c;
float fraction = 1.0;
c = getNextDigit();
// ignore non numeric leading characters
if(c < 0)
return 0; // zero returned if timeout
do{
if(c == skipChar)
; // ignore
else if(c == '-')
isNegative = true;
else if (c == '.')
isFraction = true;
else if(c >= '0' && c <= '9') { // is c a digit?
value = value * 10 + c - '0';
if(isFraction)
fraction *= 0.1;
}
c = timedRead();
}
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
if(isNegative)
value = -value;
if(isFraction)
return value * fraction;
else
return value;
}
// read characters from stream into buffer
// terminates if length characters have been read, null is detected or timeout (see setTimeout)
// returns the number of characters placed in the buffer (0 means no valid data found)
int Stream::readBytes( char *buffer, size_t length)
{
return readBytesUntil( 0, buffer, length);
}
// as readBytes with terminator character
// terminates if length characters have been read, timeout, or if the terminator character detected
// returns the number of characters placed in the buffer (0 means no valid data found)
int Stream::readBytesUntil( char terminator, char *buffer, size_t length)
{
int index = 0;
*buffer = 0;
while(index < length-1 ){
int c = timedRead();
if( c <= 0 ){
return 0; // timeout returns 0 !
}
else if( c == terminator){
buffer[index] = 0; // terminate the string
return index; // data got successfully
}
else{
buffer[index++] = (char)c;
}
}
buffer[index] = 0;
return index; // here if buffer is full before detecting the terminator
}

View File

@ -15,6 +15,8 @@
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
parsing functions based on TextFinder library by Michael Margolis
*/ */
#ifndef Stream_h #ifndef Stream_h
@ -23,13 +25,69 @@
#include <inttypes.h> #include <inttypes.h>
#include "Print.h" #include "Print.h"
// compatability macros for testing
/*
#define getInt() parseInt()
#define getInt(skipChar) parseInt(skipchar)
#define getFloat() parseFloat()
#define getFloat(skipChar) parseFloat(skipChar)
#define getString( pre_string, post_string, buffer, length)
readBytesBetween( pre_string, terminator, buffer, length)
*/
class Stream : public Print class Stream : public Print
{ {
private:
long _timeout; // number of milliseconds to wait for the next char before aborting timed read
long _startMillis; // used for timeout measurement
int timedRead(); // private method to read stream with timeout
int getNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
public: public:
virtual int available() = 0; virtual int available() = 0;
virtual int read() = 0; virtual int read() = 0;
virtual int peek() = 0; virtual int peek() = 0;
virtual void flush() = 0; virtual void flush() = 0;
Stream() {_timeout=1000;}
// parsing methods
void setTimeout(long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
bool find(char *target); // reads data from the stream until the target string is found
// returns true if target string is found, false if timed out (see setTimeout)
bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
// returns true if target string is found, false if timed out
bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
long parseInt(); // returns the first valid (long) integer value from the current position.
// initial characters that are not digits (or the minus sign) are skipped
// integer is terminated by the first character that is not a digit.
long parseInt(char skipChar); // as above but the given skipChar is ignored
// as above but the given skipChar is ignored
// this allows format characters (typically commas) in values to be ignored
float parseFloat(); // float version of parseInt
float parseFloat(char skipChar); // as above but the given skipChar is ignored
int readBytes( char *buffer, size_t length); // read chars from stream into buffer
// terminates if length characters have been read or timeout (see setTimeout)
// returns the number of characters placed in the buffer (0 means no valid data found)
int readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
// terminates if length characters have been read, timeout, or if the terminator character detected
// returns the number of characters placed in the buffer (0 means no valid data found)
// Arduino String functions to be added here
}; };
#endif #endif

View File

@ -0,0 +1,27 @@
/*
pins_arduino.h - Pin definition functions for Arduino
Part of Arduino - http://www.arduino.cc/
Copyright (c) 2007 David A. Mellis
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
$Id: wiring.h 249 2007-02-03 16:52:51Z mellis $
*/
#include "../standard/pins_arduino.h"
#undef NUM_ANALOG_INPUTS
#define NUM_ANALOG_INPUTS 8

View File

@ -27,11 +27,20 @@
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#define NUM_DIGITAL_PINS 70
#define NUM_ANALOG_INPUTS 16
#define analogInputToDigitalPin(p) ((p < 16) ? (p) + 54 : -1)
#define digitalPinHasPWM(p) (((p) >= 2 && (p) <= 13) || ((p) >= 44 && (p)<= 46))
const static uint8_t SS = 53; const static uint8_t SS = 53;
const static uint8_t MOSI = 51; const static uint8_t MOSI = 51;
const static uint8_t MISO = 50; const static uint8_t MISO = 50;
const static uint8_t SCK = 52; const static uint8_t SCK = 52;
const static uint8_t SDA = 20;
const static uint8_t SCL = 21;
const static uint8_t LED = 13;
const static uint8_t A0 = 54; const static uint8_t A0 = 54;
const static uint8_t A1 = 55; const static uint8_t A1 = 55;
const static uint8_t A2 = 56; const static uint8_t A2 = 56;
@ -49,6 +58,31 @@ const static uint8_t A13 = 67;
const static uint8_t A14 = 68; const static uint8_t A14 = 68;
const static uint8_t A15 = 69; const static uint8_t A15 = 69;
// A majority of the pins are NOT PCINTs, SO BE WARNED (i.e. you cannot use them as receive pins)
// Only pins available for RECEIVE (TRANSMIT can be on any pin):
// (I've deliberately left out pin mapping to the Hardware USARTs - seems senseless to me)
// Pins: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
#define digitalPinToPCICR(p) ( (((p) >= 10) && ((p) <= 13)) || \
(((p) >= 50) && ((p) <= 53)) || \
(((p) >= 62) && ((p) <= 69)) ? (&PCICR) : ((uint8_t *)0) )
#define digitalPinToPCICRbit(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? 0 : \
( (((p) >= 62) && ((p) <= 69)) ? 2 : \
0 ) )
#define digitalPinToPCMSK(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? (&PCMSK0) : \
( (((p) >= 62) && ((p) <= 69)) ? (&PCMSK2) : \
((uint8_t *)0) ) )
#define digitalPinToPCMSKbit(p) ( (((p) >= 10) && ((p) <= 13)) ? ((p) - 6) : \
( ((p) == 50) ? 3 : \
( ((p) == 51) ? 2 : \
( ((p) == 52) ? 1 : \
( ((p) == 53) ? 0 : \
( (((p) >= 62) && ((p) <= 69)) ? ((p) - 62) : \
0 ) ) ) ) ) )
#ifdef ARDUINO_MAIN #ifdef ARDUINO_MAIN
const uint16_t PROGMEM port_to_mode_PGM[] = { const uint16_t PROGMEM port_to_mode_PGM[] = {

View File

@ -27,11 +27,25 @@
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#define NUM_DIGITAL_PINS 20
#define NUM_ANALOG_INPUTS 6
#define analogInputToDigitalPin(p) ((p < 6) ? (p) + 14 : -1)
#if defined(__AVR_ATmega8__)
#define digitalPinHasPWM(p) ((p) == 9 || (p) == 10 || (p) == 11)
#else
#define digitalPinHasPWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11)
#endif
const static uint8_t SS = 10; const static uint8_t SS = 10;
const static uint8_t MOSI = 11; const static uint8_t MOSI = 11;
const static uint8_t MISO = 12; const static uint8_t MISO = 12;
const static uint8_t SCK = 13; const static uint8_t SCK = 13;
const static uint8_t SDA = 18;
const static uint8_t SCL = 19;
const static uint8_t LED = 13;
const static uint8_t A0 = 14; const static uint8_t A0 = 14;
const static uint8_t A1 = 15; const static uint8_t A1 = 15;
const static uint8_t A2 = 16; const static uint8_t A2 = 16;
@ -41,6 +55,11 @@ const static uint8_t A5 = 19;
const static uint8_t A6 = 20; const static uint8_t A6 = 20;
const static uint8_t A7 = 21; const static uint8_t A7 = 21;
#define digitalPinToPCICR(p) (((p) >= 0 && (p) <= 21) ? (&PCICR) : ((uint8_t *)0))
#define digitalPinToPCICRbit(p) (((p) <= 7) ? 2 : (((p) <= 13) ? 0 : 1))
#define digitalPinToPCMSK(p) (((p) <= 7) ? (&PCMSK2) : (((p) <= 13) ? (&PCMSK0) : (((p) <= 21) ? (&PCMSK1) : ((uint8_t *)0))))
#define digitalPinToPCMSKbit(p) (((p) <= 7) ? (p) : (((p) <= 13) ? ((p) - 8) : ((p) - 14)))
#ifdef ARDUINO_MAIN #ifdef ARDUINO_MAIN
// On the Arduino board, digital pins are also used // On the Arduino board, digital pins are also used