Updated copy of Arduino1 files related to the USB AVRs to RC2.

This commit is contained in:
opendous
2011-10-31 04:29:51 +00:00
parent 7e40191391
commit 7e15f4bdd8
20 changed files with 3113 additions and 3099 deletions

View File

@@ -1 +0,0 @@
This directory contains the core firmware of Arduino1-RC1 related to the USB AVRs.

View File

@@ -0,0 +1 @@
This directory contains the core firmware of Arduino1-RC2 related to the USB AVRs.

View File

@@ -40,6 +40,11 @@ extern "C"{
#define FALLING 2 #define FALLING 2
#define RISING 3 #define RISING 3
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
#define DEFAULT 0
#define EXTERNAL 1
#define INTERNAL 2
#else
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define INTERNAL1V1 2 #define INTERNAL1V1 2
#define INTERNAL2V56 3 #define INTERNAL2V56 3
@@ -48,6 +53,7 @@ extern "C"{
#endif #endif
#define DEFAULT 1 #define DEFAULT 1
#define EXTERNAL 0 #define EXTERNAL 0
#endif
// undefine stdlib's abs if encountered // undefine stdlib's abs if encountered
#ifdef abs #ifdef abs
@@ -142,6 +148,7 @@ extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
#define NOT_A_PIN 0 #define NOT_A_PIN 0
#define NOT_A_PORT 0 #define NOT_A_PORT 0
#ifdef ARDUINO_MAIN
#define PA 1 #define PA 1
#define PB 2 #define PB 2
#define PC 3 #define PC 3
@@ -153,6 +160,7 @@ extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
#define PJ 10 #define PJ 10
#define PK 11 #define PK 11
#define PL 12 #define PL 12
#endif
#define NOT_ON_TIMER 0 #define NOT_ON_TIMER 0
#define TIMER0A 1 #define TIMER0A 1

View File

@@ -91,7 +91,7 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
#if !defined(USART_RX_vect) && !defined(SIG_USART0_RECV) && \ #if !defined(USART_RX_vect) && !defined(SIG_USART0_RECV) && \
!defined(SIG_UART0_RECV) && !defined(USART0_RX_vect) && \ !defined(SIG_UART0_RECV) && !defined(USART0_RX_vect) && \
!defined(SIG_UART_RECV) !defined(SIG_UART_RECV)
#error Don't know what the Data Received vector is called for the first UART #error "Don't know what the Data Received vector is called for the first UART"
#else #else
void serialEvent() __attribute__((weak)); void serialEvent() __attribute__((weak));
void serialEvent() {} void serialEvent() {}
@@ -180,7 +180,7 @@ void serialEventRun(void)
// do nothing - on the 32u4 the first USART is USART1 // do nothing - on the 32u4 the first USART is USART1
#else #else
#if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect) #if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
#error Don't know what the Data Register Empty vector is called for the first UART #error "Don't know what the Data Register Empty vector is called for the first UART"
#else #else
#if defined(UART0_UDRE_vect) #if defined(UART0_UDRE_vect)
ISR(UART0_UDRE_vect) ISR(UART0_UDRE_vect)

View File

@@ -54,7 +54,7 @@ size_t Print::print(const __FlashStringHelper *ifsh)
size_t Print::print(const String &s) size_t Print::print(const String &s)
{ {
size_t n = 0; size_t n = 0;
for (int i = 0; i < s.length(); i++) { for (uint16_t i = 0; i < s.length(); i++) {
n += write(s[i]); n += write(s[i]);
} }
return n; return n;

View File

@@ -67,9 +67,9 @@ int Stream::peekNextDigit()
// Public Methods // Public Methods
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
void Stream::setTimeout( long timeout) // sets the maximum number of milliseconds to wait void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
{ {
this->_timeout = timeout; _timeout = timeout;
} }
// find returns true if the target string is found // find returns true if the target string is found
@@ -165,7 +165,7 @@ long Stream::parseInt(char skipChar)
// as parseInt but returns a floating point value // as parseInt but returns a floating point value
float Stream::parseFloat() float Stream::parseFloat()
{ {
parseFloat(NO_SKIP_CHAR); return parseFloat(NO_SKIP_CHAR);
} }
// as above but the given skipChar is ignored // as above but the given skipChar is ignored
@@ -174,7 +174,6 @@ float Stream::parseFloat(char skipChar){
boolean isNegative = false; boolean isNegative = false;
boolean isFraction = false; boolean isFraction = false;
long value = 0; long value = 0;
float fValue;
char c; char c;
float fraction = 1.0; float fraction = 1.0;
@@ -223,7 +222,7 @@ int Stream::readBytes( char *buffer, size_t length)
int Stream::readBytesUntil( char terminator, char *buffer, size_t length) int Stream::readBytesUntil( char terminator, char *buffer, size_t length)
{ {
int index = 0; unsigned int index = 0;
*buffer = 0; *buffer = 0;
while(index < length-1 ){ while(index < length-1 ){
int c = timedRead(); int c = timedRead();

View File

@@ -38,8 +38,8 @@ readBytesBetween( pre_string, terminator, buffer, length)
class Stream : public Print class Stream : public Print
{ {
private: private:
long _timeout; // number of milliseconds to wait for the next char before aborting timed read unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
long _startMillis; // used for timeout measurement unsigned long _startMillis; // used for timeout measurement
int timedRead(); // private method to read stream with timeout int timedRead(); // private method to read stream with timeout
int timedPeek(); // private method to peek stream with timeout int timedPeek(); // private method to peek stream with timeout
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
@@ -54,7 +54,7 @@ class Stream : public Print
// parsing methods // parsing methods
void setTimeout(long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second void setTimeout(unsigned 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 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) // returns true if target string is found, false if timed out (see setTimeout)

View File

File diff suppressed because it is too large Load Diff

View File

@@ -1,168 +1,168 @@
/* /*
WCharacter.h - Character utility functions for Wiring & Arduino WCharacter.h - Character utility functions for Wiring & Arduino
Copyright (c) 2010 Hernando Barragan. All right reserved. Copyright (c) 2010 Hernando Barragan. All right reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. 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, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
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
*/ */
#ifndef Character_h #ifndef Character_h
#define Character_h #define Character_h
#include <ctype.h> #include <ctype.h>
// WCharacter.h prototypes // WCharacter.h prototypes
inline boolean isAlphaNumeric(int c) __attribute__((always_inline)); inline boolean isAlphaNumeric(int c) __attribute__((always_inline));
inline boolean isAlpha(int c) __attribute__((always_inline)); inline boolean isAlpha(int c) __attribute__((always_inline));
inline boolean isAscii(int c) __attribute__((always_inline)); inline boolean isAscii(int c) __attribute__((always_inline));
inline boolean isWhitespace(int c) __attribute__((always_inline)); inline boolean isWhitespace(int c) __attribute__((always_inline));
inline boolean isControl(int c) __attribute__((always_inline)); inline boolean isControl(int c) __attribute__((always_inline));
inline boolean isDigit(int c) __attribute__((always_inline)); inline boolean isDigit(int c) __attribute__((always_inline));
inline boolean isGraph(int c) __attribute__((always_inline)); inline boolean isGraph(int c) __attribute__((always_inline));
inline boolean isLowerCase(int c) __attribute__((always_inline)); inline boolean isLowerCase(int c) __attribute__((always_inline));
inline boolean isPrintable(int c) __attribute__((always_inline)); inline boolean isPrintable(int c) __attribute__((always_inline));
inline boolean isPunct(int c) __attribute__((always_inline)); inline boolean isPunct(int c) __attribute__((always_inline));
inline boolean isSpace(int c) __attribute__((always_inline)); inline boolean isSpace(int c) __attribute__((always_inline));
inline boolean isUpperCase(int c) __attribute__((always_inline)); inline boolean isUpperCase(int c) __attribute__((always_inline));
inline boolean isHexadecimalDigit(int c) __attribute__((always_inline)); inline boolean isHexadecimalDigit(int c) __attribute__((always_inline));
inline int toAscii(int c) __attribute__((always_inline)); inline int toAscii(int c) __attribute__((always_inline));
inline int toLowerCase(int c) __attribute__((always_inline)); inline int toLowerCase(int c) __attribute__((always_inline));
inline int toUpperCase(int c)__attribute__((always_inline)); inline int toUpperCase(int c)__attribute__((always_inline));
// Checks for an alphanumeric character. // Checks for an alphanumeric character.
// It is equivalent to (isalpha(c) || isdigit(c)). // It is equivalent to (isalpha(c) || isdigit(c)).
inline boolean isAlphaNumeric(int c) inline boolean isAlphaNumeric(int c)
{ {
return ( isalnum(c) == 0 ? false : true); return ( isalnum(c) == 0 ? false : true);
} }
// Checks for an alphabetic character. // Checks for an alphabetic character.
// It is equivalent to (isupper(c) || islower(c)). // It is equivalent to (isupper(c) || islower(c)).
inline boolean isAlpha(int c) inline boolean isAlpha(int c)
{ {
return ( isalpha(c) == 0 ? false : true); return ( isalpha(c) == 0 ? false : true);
} }
// Checks whether c is a 7-bit unsigned char value // Checks whether c is a 7-bit unsigned char value
// that fits into the ASCII character set. // that fits into the ASCII character set.
inline boolean isAscii(int c) inline boolean isAscii(int c)
{ {
return ( isascii (c) == 0 ? false : true); return ( isascii (c) == 0 ? false : true);
} }
// Checks for a blank character, that is, a space or a tab. // Checks for a blank character, that is, a space or a tab.
inline boolean isWhitespace(int c) inline boolean isWhitespace(int c)
{ {
return ( isblank (c) == 0 ? false : true); return ( isblank (c) == 0 ? false : true);
} }
// Checks for a control character. // Checks for a control character.
inline boolean isControl(int c) inline boolean isControl(int c)
{ {
return ( iscntrl (c) == 0 ? false : true); return ( iscntrl (c) == 0 ? false : true);
} }
// Checks for a digit (0 through 9). // Checks for a digit (0 through 9).
inline boolean isDigit(int c) inline boolean isDigit(int c)
{ {
return ( isdigit (c) == 0 ? false : true); return ( isdigit (c) == 0 ? false : true);
} }
// Checks for any printable character except space. // Checks for any printable character except space.
inline boolean isGraph(int c) inline boolean isGraph(int c)
{ {
return ( isgraph (c) == 0 ? false : true); return ( isgraph (c) == 0 ? false : true);
} }
// Checks for a lower-case character. // Checks for a lower-case character.
inline boolean isLowerCase(int c) inline boolean isLowerCase(int c)
{ {
return (islower (c) == 0 ? false : true); return (islower (c) == 0 ? false : true);
} }
// Checks for any printable character including space. // Checks for any printable character including space.
inline boolean isPrintable(int c) inline boolean isPrintable(int c)
{ {
return ( isprint (c) == 0 ? false : true); return ( isprint (c) == 0 ? false : true);
} }
// Checks for any printable character which is not a space // Checks for any printable character which is not a space
// or an alphanumeric character. // or an alphanumeric character.
inline boolean isPunct(int c) inline boolean isPunct(int c)
{ {
return ( ispunct (c) == 0 ? false : true); return ( ispunct (c) == 0 ? false : true);
} }
// Checks for white-space characters. For the avr-libc library, // Checks for white-space characters. For the avr-libc library,
// these are: space, formfeed ('\f'), newline ('\n'), carriage // these are: space, formfeed ('\f'), newline ('\n'), carriage
// return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). // return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
inline boolean isSpace(int c) inline boolean isSpace(int c)
{ {
return ( isspace (c) == 0 ? false : true); return ( isspace (c) == 0 ? false : true);
} }
// Checks for an uppercase letter. // Checks for an uppercase letter.
inline boolean isUpperCase(int c) inline boolean isUpperCase(int c)
{ {
return ( isupper (c) == 0 ? false : true); return ( isupper (c) == 0 ? false : true);
} }
// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 // Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7
// 8 9 a b c d e f A B C D E F. // 8 9 a b c d e f A B C D E F.
inline boolean isHexadecimalDigit(int c) inline boolean isHexadecimalDigit(int c)
{ {
return ( isxdigit (c) == 0 ? false : true); return ( isxdigit (c) == 0 ? false : true);
} }
// Converts c to a 7-bit unsigned char value that fits into the // Converts c to a 7-bit unsigned char value that fits into the
// ASCII character set, by clearing the high-order bits. // ASCII character set, by clearing the high-order bits.
inline int toAscii(int c) inline int toAscii(int c)
{ {
return toascii (c); return toascii (c);
} }
// Warning: // Warning:
// Many people will be unhappy if you use this function. // Many people will be unhappy if you use this function.
// This function will convert accented letters into random // This function will convert accented letters into random
// characters. // characters.
// Converts the letter c to lower case, if possible. // Converts the letter c to lower case, if possible.
inline int toLowerCase(int c) inline int toLowerCase(int c)
{ {
return tolower (c); return tolower (c);
} }
// Converts the letter c to upper case, if possible. // Converts the letter c to upper case, if possible.
inline int toUpperCase(int c) inline int toUpperCase(int c)
{ {
return toupper (c); return toupper (c);
} }
#endif #endif

View File

@@ -1,248 +1,248 @@
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/* /*
Part of the Wiring project - http://wiring.uniandes.edu.co Part of the Wiring project - http://wiring.uniandes.edu.co
Copyright (c) 2004-05 Hernando Barragan Copyright (c) 2004-05 Hernando Barragan
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. 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, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA Boston, MA 02111-1307 USA
Modified 24 November 2006 by David A. Mellis Modified 24 November 2006 by David A. Mellis
Modified 1 August 2010 by Mark Sproul Modified 1 August 2010 by Mark Sproul
*/ */
#include <inttypes.h> #include <inttypes.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include <stdio.h> #include <stdio.h>
#include "wiring_private.h" #include "wiring_private.h"
volatile static voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS]; volatile static voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
// volatile static voidFuncPtr twiIntFunc; // volatile static voidFuncPtr twiIntFunc;
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) { void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) { if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
intFunc[interruptNum] = userFunc; intFunc[interruptNum] = userFunc;
// Configure the interrupt mode (trigger on low input, any change, rising // Configure the interrupt mode (trigger on low input, any change, rising
// edge, or falling edge). The mode constants were chosen to correspond // edge, or falling edge). The mode constants were chosen to correspond
// to the configuration bits in the hardware register, so we simply shift // to the configuration bits in the hardware register, so we simply shift
// the mode into place. // the mode into place.
// Enable the interrupt. // Enable the interrupt.
switch (interruptNum) { switch (interruptNum) {
#if defined(EICRA) && defined(EICRB) && defined(EIMSK) #if defined(EICRA) && defined(EICRB) && defined(EIMSK)
case 2: case 2:
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00); EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
EIMSK |= (1 << INT0); EIMSK |= (1 << INT0);
break; break;
case 3: case 3:
EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10); EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
EIMSK |= (1 << INT1); EIMSK |= (1 << INT1);
break; break;
case 4: case 4:
EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20); EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
EIMSK |= (1 << INT2); EIMSK |= (1 << INT2);
break; break;
case 5: case 5:
EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30); EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
EIMSK |= (1 << INT3); EIMSK |= (1 << INT3);
break; break;
case 0: case 0:
EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40); EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
EIMSK |= (1 << INT4); EIMSK |= (1 << INT4);
break; break;
case 1: case 1:
EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50); EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
EIMSK |= (1 << INT5); EIMSK |= (1 << INT5);
break; break;
case 6: case 6:
EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60); EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
EIMSK |= (1 << INT6); EIMSK |= (1 << INT6);
break; break;
case 7: case 7:
EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70); EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
EIMSK |= (1 << INT7); EIMSK |= (1 << INT7);
break; break;
#else #else
case 0: case 0:
#if defined(EICRA) && defined(ISC00) && defined(EIMSK) #if defined(EICRA) && defined(ISC00) && defined(EIMSK)
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00); EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
EIMSK |= (1 << INT0); EIMSK |= (1 << INT0);
#elif defined(MCUCR) && defined(ISC00) && defined(GICR) #elif defined(MCUCR) && defined(ISC00) && defined(GICR)
MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00); MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
GICR |= (1 << INT0); GICR |= (1 << INT0);
#elif defined(MCUCR) && defined(ISC00) && defined(GIMSK) #elif defined(MCUCR) && defined(ISC00) && defined(GIMSK)
MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00); MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
GIMSK |= (1 << INT0); GIMSK |= (1 << INT0);
#else #else
#error attachInterrupt not finished for this CPU (case 0) #error attachInterrupt not finished for this CPU (case 0)
#endif #endif
break; break;
case 1: case 1:
#if defined(EICRA) && defined(ISC10) && defined(ISC11) && defined(EIMSK) #if defined(EICRA) && defined(ISC10) && defined(ISC11) && defined(EIMSK)
EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10); EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
EIMSK |= (1 << INT1); EIMSK |= (1 << INT1);
#elif defined(MCUCR) && defined(ISC10) && defined(ISC11) && defined(GICR) #elif defined(MCUCR) && defined(ISC10) && defined(ISC11) && defined(GICR)
MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10); MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
GICR |= (1 << INT1); GICR |= (1 << INT1);
#elif defined(MCUCR) && defined(ISC10) && defined(GIMSK) && defined(GIMSK) #elif defined(MCUCR) && defined(ISC10) && defined(GIMSK) && defined(GIMSK)
MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10); MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
GIMSK |= (1 << INT1); GIMSK |= (1 << INT1);
#else #else
#warning attachInterrupt may need some more work for this cpu (case 1) #warning attachInterrupt may need some more work for this cpu (case 1)
#endif #endif
break; break;
#endif #endif
} }
} }
} }
void detachInterrupt(uint8_t interruptNum) { void detachInterrupt(uint8_t interruptNum) {
if(interruptNum < EXTERNAL_NUM_INTERRUPTS) { if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
// Disable the interrupt. (We can't assume that interruptNum is equal // Disable the interrupt. (We can't assume that interruptNum is equal
// to the number of the EIMSK bit to clear, as this isn't true on the // to the number of the EIMSK bit to clear, as this isn't true on the
// ATmega8. There, INT0 is 6 and INT1 is 7.) // ATmega8. There, INT0 is 6 and INT1 is 7.)
switch (interruptNum) { switch (interruptNum) {
#if defined(EICRA) && defined(EICRB) && defined(EIMSK) #if defined(EICRA) && defined(EICRB) && defined(EIMSK)
case 2: case 2:
EIMSK &= ~(1 << INT0); EIMSK &= ~(1 << INT0);
break; break;
case 3: case 3:
EIMSK &= ~(1 << INT1); EIMSK &= ~(1 << INT1);
break; break;
case 4: case 4:
EIMSK &= ~(1 << INT2); EIMSK &= ~(1 << INT2);
break; break;
case 5: case 5:
EIMSK &= ~(1 << INT3); EIMSK &= ~(1 << INT3);
break; break;
case 0: case 0:
EIMSK &= ~(1 << INT4); EIMSK &= ~(1 << INT4);
break; break;
case 1: case 1:
EIMSK &= ~(1 << INT5); EIMSK &= ~(1 << INT5);
break; break;
case 6: case 6:
EIMSK &= ~(1 << INT6); EIMSK &= ~(1 << INT6);
break; break;
case 7: case 7:
EIMSK &= ~(1 << INT7); EIMSK &= ~(1 << INT7);
break; break;
#else #else
case 0: case 0:
#if defined(EIMSK) && defined(INT0) #if defined(EIMSK) && defined(INT0)
EIMSK &= ~(1 << INT0); EIMSK &= ~(1 << INT0);
#elif defined(GICR) && defined(ISC00) #elif defined(GICR) && defined(ISC00)
GICR &= ~(1 << INT0); // atmega32 GICR &= ~(1 << INT0); // atmega32
#elif defined(GIMSK) && defined(INT0) #elif defined(GIMSK) && defined(INT0)
GIMSK &= ~(1 << INT0); GIMSK &= ~(1 << INT0);
#else #else
#error detachInterrupt not finished for this cpu #error detachInterrupt not finished for this cpu
#endif #endif
break; break;
case 1: case 1:
#if defined(EIMSK) && defined(INT1) #if defined(EIMSK) && defined(INT1)
EIMSK &= ~(1 << INT1); EIMSK &= ~(1 << INT1);
#elif defined(GICR) && defined(INT1) #elif defined(GICR) && defined(INT1)
GICR &= ~(1 << INT1); // atmega32 GICR &= ~(1 << INT1); // atmega32
#elif defined(GIMSK) && defined(INT1) #elif defined(GIMSK) && defined(INT1)
GIMSK &= ~(1 << INT1); GIMSK &= ~(1 << INT1);
#else #else
#warning detachInterrupt may need some more work for this cpu (case 1) #warning detachInterrupt may need some more work for this cpu (case 1)
#endif #endif
break; break;
#endif #endif
} }
intFunc[interruptNum] = 0; intFunc[interruptNum] = 0;
} }
} }
/* /*
void attachInterruptTwi(void (*userFunc)(void) ) { void attachInterruptTwi(void (*userFunc)(void) ) {
twiIntFunc = userFunc; twiIntFunc = userFunc;
} }
*/ */
#if defined(EICRA) && defined(EICRB) #if defined(EICRA) && defined(EICRB)
SIGNAL(INT0_vect) { SIGNAL(INT0_vect) {
if(intFunc[EXTERNAL_INT_2]) if(intFunc[EXTERNAL_INT_2])
intFunc[EXTERNAL_INT_2](); intFunc[EXTERNAL_INT_2]();
} }
SIGNAL(INT1_vect) { SIGNAL(INT1_vect) {
if(intFunc[EXTERNAL_INT_3]) if(intFunc[EXTERNAL_INT_3])
intFunc[EXTERNAL_INT_3](); intFunc[EXTERNAL_INT_3]();
} }
SIGNAL(INT2_vect) { SIGNAL(INT2_vect) {
if(intFunc[EXTERNAL_INT_4]) if(intFunc[EXTERNAL_INT_4])
intFunc[EXTERNAL_INT_4](); intFunc[EXTERNAL_INT_4]();
} }
SIGNAL(INT3_vect) { SIGNAL(INT3_vect) {
if(intFunc[EXTERNAL_INT_5]) if(intFunc[EXTERNAL_INT_5])
intFunc[EXTERNAL_INT_5](); intFunc[EXTERNAL_INT_5]();
} }
SIGNAL(INT4_vect) { SIGNAL(INT4_vect) {
if(intFunc[EXTERNAL_INT_0]) if(intFunc[EXTERNAL_INT_0])
intFunc[EXTERNAL_INT_0](); intFunc[EXTERNAL_INT_0]();
} }
SIGNAL(INT5_vect) { SIGNAL(INT5_vect) {
if(intFunc[EXTERNAL_INT_1]) if(intFunc[EXTERNAL_INT_1])
intFunc[EXTERNAL_INT_1](); intFunc[EXTERNAL_INT_1]();
} }
SIGNAL(INT6_vect) { SIGNAL(INT6_vect) {
if(intFunc[EXTERNAL_INT_6]) if(intFunc[EXTERNAL_INT_6])
intFunc[EXTERNAL_INT_6](); intFunc[EXTERNAL_INT_6]();
} }
SIGNAL(INT7_vect) { SIGNAL(INT7_vect) {
if(intFunc[EXTERNAL_INT_7]) if(intFunc[EXTERNAL_INT_7])
intFunc[EXTERNAL_INT_7](); intFunc[EXTERNAL_INT_7]();
} }
#else #else
SIGNAL(INT0_vect) { SIGNAL(INT0_vect) {
if(intFunc[EXTERNAL_INT_0]) if(intFunc[EXTERNAL_INT_0])
intFunc[EXTERNAL_INT_0](); intFunc[EXTERNAL_INT_0]();
} }
SIGNAL(INT1_vect) { SIGNAL(INT1_vect) {
if(intFunc[EXTERNAL_INT_1]) if(intFunc[EXTERNAL_INT_1])
intFunc[EXTERNAL_INT_1](); intFunc[EXTERNAL_INT_1]();
} }
#endif #endif
/* /*
SIGNAL(SIG_2WIRE_SERIAL) { SIGNAL(SIG_2WIRE_SERIAL) {
if(twiIntFunc) if(twiIntFunc)
twiIntFunc(); twiIntFunc();
} }
*/ */

View File

@@ -1,60 +1,60 @@
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/* /*
Part of the Wiring project - http://wiring.org.co Part of the Wiring project - http://wiring.org.co
Copyright (c) 2004-06 Hernando Barragan Copyright (c) 2004-06 Hernando Barragan
Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/ Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. 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, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA Boston, MA 02111-1307 USA
$Id$ $Id$
*/ */
extern "C" { extern "C" {
#include "stdlib.h" #include "stdlib.h"
} }
void randomSeed(unsigned int seed) void randomSeed(unsigned int seed)
{ {
if (seed != 0) { if (seed != 0) {
srandom(seed); srandom(seed);
} }
} }
long random(long howbig) long random(long howbig)
{ {
if (howbig == 0) { if (howbig == 0) {
return 0; return 0;
} }
return random() % howbig; return random() % howbig;
} }
long random(long howsmall, long howbig) long random(long howsmall, long howbig)
{ {
if (howsmall >= howbig) { if (howsmall >= howbig) {
return howsmall; return howsmall;
} }
long diff = howbig - howsmall; long diff = howbig - howsmall;
return random(diff) + howsmall; return random(diff) + howsmall;
} }
long map(long x, long in_min, long in_max, long out_min, long out_max) long map(long x, long in_min, long in_max, long out_min, long out_max)
{ {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
} }
unsigned int makeWord(unsigned int w) { return w; } unsigned int makeWord(unsigned int w) { return w; }
unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; } unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }

View File

File diff suppressed because it is too large Load Diff

View File

@@ -1,205 +1,205 @@
/* /*
WString.h - String library for Wiring & Arduino WString.h - String library for Wiring & Arduino
...mostly rewritten by Paul Stoffregen... ...mostly rewritten by Paul Stoffregen...
Copyright (c) 2009-10 Hernando Barragan. All right reserved. Copyright (c) 2009-10 Hernando Barragan. All right reserved.
Copyright 2011, Paul Stoffregen, paul@pjrc.com Copyright 2011, Paul Stoffregen, paul@pjrc.com
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. 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, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
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
*/ */
#ifndef String_class_h #ifndef String_class_h
#define String_class_h #define String_class_h
#ifdef __cplusplus #ifdef __cplusplus
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
// When compiling programs with this class, the following gcc parameters // When compiling programs with this class, the following gcc parameters
// dramatically increase performance and memory (RAM) efficiency, typically // dramatically increase performance and memory (RAM) efficiency, typically
// with little or no increase in code size. // with little or no increase in code size.
// -felide-constructors // -felide-constructors
// -std=c++0x // -std=c++0x
class __FlashStringHelper; class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal))) #define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal)))
// An inherited class for holding the result of a concatenation. These // An inherited class for holding the result of a concatenation. These
// result objects are assumed to be writable by subsequent concatenations. // result objects are assumed to be writable by subsequent concatenations.
class StringSumHelper; class StringSumHelper;
// The string class // The string class
class String class String
{ {
// use a function pointer to allow for "if (s)" without the // use a function pointer to allow for "if (s)" without the
// complications of an operator bool(). for more information, see: // complications of an operator bool(). for more information, see:
// http://www.artima.com/cppsource/safebool.html // http://www.artima.com/cppsource/safebool.html
typedef void (String::*StringIfHelperType)() const; typedef void (String::*StringIfHelperType)() const;
void StringIfHelper() const {} void StringIfHelper() const {}
public: public:
// constructors // constructors
// creates a copy of the initial value. // creates a copy of the initial value.
// if the initial value is null or invalid, or if memory allocation // 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 // fails, the string will be marked as invalid (i.e. "if (s)" will
// be false). // be false).
String(const char *cstr = ""); String(const char *cstr = "");
String(const String &str); String(const String &str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
String(String &&rval); String(String &&rval);
String(StringSumHelper &&rval); String(StringSumHelper &&rval);
#endif #endif
explicit String(char c); explicit String(char c);
explicit String(unsigned char, unsigned char base=10); explicit String(unsigned char, unsigned char base=10);
explicit String(int, unsigned char base=10); explicit String(int, unsigned char base=10);
explicit String(unsigned int, unsigned char base=10); explicit String(unsigned int, unsigned char base=10);
explicit String(long, unsigned char base=10); explicit String(long, unsigned char base=10);
explicit String(unsigned long, unsigned char base=10); explicit String(unsigned long, unsigned char base=10);
~String(void); ~String(void);
// memory management // memory management
// return true on success, false on failure (in which case, the string // return true on success, false on failure (in which case, the string
// is left unchanged). reserve(0), if successful, will validate an // is left unchanged). reserve(0), if successful, will validate an
// invalid string (i.e., "if (s)" will be true afterwards) // invalid string (i.e., "if (s)" will be true afterwards)
unsigned char reserve(unsigned int size); unsigned char reserve(unsigned int size);
inline unsigned int length(void) const {return len;} inline unsigned int length(void) const {return len;}
// creates a copy of the assigned value. if the value is null or // creates a copy of the assigned value. if the value is null or
// invalid, or if the memory allocation fails, the string will be // invalid, or if the memory allocation fails, the string will be
// marked as invalid ("if (s)" will be false). // marked as invalid ("if (s)" will be false).
String & operator = (const String &rhs); String & operator = (const String &rhs);
String & operator = (const char *cstr); String & operator = (const char *cstr);
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
String & operator = (String &&rval); String & operator = (String &&rval);
String & operator = (StringSumHelper &&rval); String & operator = (StringSumHelper &&rval);
#endif #endif
// concatenate (works w/ built-in types) // concatenate (works w/ built-in types)
// returns true on success, false on failure (in which case, the string // returns true on success, false on failure (in which case, the string
// is left unchanged). if the argument is null or invalid, the // is left unchanged). if the argument is null or invalid, the
// concatenation is considered unsucessful. // concatenation is considered unsucessful.
unsigned char concat(const String &str); unsigned char concat(const String &str);
unsigned char concat(const char *cstr); unsigned char concat(const char *cstr);
unsigned char concat(char c); unsigned char concat(char c);
unsigned char concat(unsigned char c); unsigned char concat(unsigned char c);
unsigned char concat(int num); unsigned char concat(int num);
unsigned char concat(unsigned int num); unsigned char concat(unsigned int num);
unsigned char concat(long num); unsigned char concat(long num);
unsigned char concat(unsigned long num); unsigned char concat(unsigned long num);
// if there's not enough memory for the concatenated value, the string // if there's not enough memory for the concatenated value, the string
// will be left unchanged (but this isn't signalled in any way) // will be left unchanged (but this isn't signalled in any way)
String & operator += (const String &rhs) {concat(rhs); return (*this);} String & operator += (const String &rhs) {concat(rhs); return (*this);}
String & operator += (const char *cstr) {concat(cstr); return (*this);} String & operator += (const char *cstr) {concat(cstr); return (*this);}
String & operator += (char c) {concat(c); return (*this);} String & operator += (char c) {concat(c); return (*this);}
String & operator += (unsigned char num) {concat(num); return (*this);} String & operator += (unsigned char num) {concat(num); return (*this);}
String & operator += (int num) {concat(num); return (*this);} String & operator += (int num) {concat(num); return (*this);}
String & operator += (unsigned 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 += (long num) {concat(num); return (*this);}
String & operator += (unsigned 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 String &rhs);
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num); friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned 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, long num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
// comparison (only works w/ Strings and "strings") // comparison (only works w/ Strings and "strings")
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
int compareTo(const String &s) const; int compareTo(const String &s) const;
unsigned char equals(const String &s) const; unsigned char equals(const String &s) const;
unsigned char equals(const char *cstr) const; unsigned char equals(const char *cstr) const;
unsigned char operator == (const String &rhs) const {return equals(rhs);} unsigned char operator == (const String &rhs) const {return equals(rhs);}
unsigned char operator == (const char *cstr) const {return equals(cstr);} unsigned char operator == (const char *cstr) const {return equals(cstr);}
unsigned char operator != (const String &rhs) const {return !equals(rhs);} unsigned char operator != (const String &rhs) const {return !equals(rhs);}
unsigned char operator != (const char *cstr) const {return !equals(cstr);} 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 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 equalsIgnoreCase(const String &s) const;
unsigned char startsWith( const String &prefix) const; unsigned char startsWith( const String &prefix) const;
unsigned char startsWith(const String &prefix, unsigned int offset) const; unsigned char startsWith(const String &prefix, unsigned int offset) const;
unsigned char endsWith(const String &suffix) const; unsigned char endsWith(const String &suffix) const;
// character acccess // character acccess
char charAt(unsigned int index) const; char charAt(unsigned int index) const;
void setCharAt(unsigned int index, char c); void setCharAt(unsigned int index, char c);
char operator [] (unsigned int index) const; char operator [] (unsigned int index) const;
char& operator [] (unsigned int index); char& operator [] (unsigned int index);
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const; 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 void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
{getBytes((unsigned char *)buf, bufsize, index);} {getBytes((unsigned char *)buf, bufsize, index);}
// search // search
int indexOf( char ch ) const; int indexOf( char ch ) const;
int indexOf( char ch, unsigned int fromIndex ) const; int indexOf( char ch, unsigned int fromIndex ) const;
int indexOf( const String &str ) const; int indexOf( const String &str ) const;
int indexOf( const String &str, unsigned int fromIndex ) const; int indexOf( const String &str, unsigned int fromIndex ) const;
int lastIndexOf( char ch ) const; int lastIndexOf( char ch ) const;
int lastIndexOf( char ch, int fromIndex ) const; int lastIndexOf( char ch, unsigned int fromIndex ) const;
int lastIndexOf( const String &str ) const; int lastIndexOf( const String &str ) const;
int lastIndexOf( const String &str, int fromIndex ) const; int lastIndexOf( const String &str, unsigned int fromIndex ) const;
String substring( unsigned int beginIndex ) const; String substring( unsigned int beginIndex ) const;
String substring( unsigned int beginIndex, unsigned int endIndex ) const; String substring( unsigned int beginIndex, unsigned int endIndex ) const;
// modification // modification
void replace(char find, char replace); void replace(char find, char replace);
void replace(const String& find, const String& replace); void replace(const String& find, const String& replace);
void toLowerCase(void); void toLowerCase(void);
void toUpperCase(void); void toUpperCase(void);
void trim(void); void trim(void);
// parsing/conversion // parsing/conversion
long toInt(void) const; long toInt(void) const;
protected: protected:
char *buffer; // the actual char array char *buffer; // the actual char array
unsigned int capacity; // the array length minus one (for the '\0') unsigned int capacity; // the array length minus one (for the '\0')
unsigned int len; // the String length (not counting the '\0') unsigned int len; // the String length (not counting the '\0')
unsigned char flags; // unused, for future features unsigned char flags; // unused, for future features
protected: protected:
void init(void); void init(void);
void invalidate(void); void invalidate(void);
unsigned char changeBuffer(unsigned int maxStrLen); unsigned char changeBuffer(unsigned int maxStrLen);
unsigned char concat(const char *cstr, unsigned int length); unsigned char concat(const char *cstr, unsigned int length);
// copy and move // copy and move
String & copy(const char *cstr, unsigned int length); String & copy(const char *cstr, unsigned int length);
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
void move(String &rhs); void move(String &rhs);
#endif #endif
}; };
class StringSumHelper : public String class StringSumHelper : public String
{ {
public: public:
StringSumHelper(const String &s) : String(s) {} StringSumHelper(const String &s) : String(s) {}
StringSumHelper(const char *p) : String(p) {} StringSumHelper(const char *p) : String(p) {}
StringSumHelper(char c) : String(c) {} StringSumHelper(char c) : String(c) {}
StringSumHelper(unsigned char num) : String(num) {} StringSumHelper(unsigned char num) : String(num) {}
StringSumHelper(int num) : String(num) {} StringSumHelper(int num) : String(num) {}
StringSumHelper(unsigned int num) : String(num) {} StringSumHelper(unsigned int num) : String(num) {}
StringSumHelper(long num) : String(num) {} StringSumHelper(long num) : String(num) {}
StringSumHelper(unsigned long num) : String(num) {} StringSumHelper(unsigned long num) : String(num) {}
}; };
#endif // __cplusplus #endif // __cplusplus
#endif // String_class_h #endif // String_class_h

View File

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,3 @@
#define ARDUINO_MAIN
#include <Arduino.h> #include <Arduino.h>
int main(void) int main(void)

View File

@@ -1,289 +1,297 @@
/* /*
wiring.c - Partial implementation of the Wiring API for the ATmega8. wiring.c - Partial implementation of the Wiring API for the ATmega8.
Part of Arduino - http://www.arduino.cc/ Part of Arduino - http://www.arduino.cc/
Copyright (c) 2005-2006 David A. Mellis Copyright (c) 2005-2006 David A. Mellis
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. 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, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA Boston, MA 02111-1307 USA
$Id$ $Id$
*/ */
#include "wiring_private.h" #include "wiring_private.h"
// the prescaler is set so that timer0 ticks every 64 clock cycles, and the // the prescaler is set so that timer0 ticks every 64 clock cycles, and the
// the overflow handler is called every 256 ticks. // the overflow handler is called every 256 ticks.
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256)) #define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
// the whole number of milliseconds per timer0 overflow // the whole number of milliseconds per timer0 overflow
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000) #define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
// the fractional number of milliseconds per timer0 overflow. we shift right // the fractional number of milliseconds per timer0 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care // by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.) // about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3) #define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3) #define FRACT_MAX (1000 >> 3)
volatile unsigned long timer0_overflow_count = 0; volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_millis = 0; volatile unsigned long timer0_millis = 0;
static unsigned char timer0_fract = 0; static unsigned char timer0_fract = 0;
SIGNAL(TIMER0_OVF_vect) #if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
{ SIGNAL(TIM0_OVF_vect)
// copy these to local variables so they can be stored in registers #else
// (volatile variables must be read from memory on every access) SIGNAL(TIMER0_OVF_vect)
unsigned long m = timer0_millis; #endif
unsigned char f = timer0_fract; {
// copy these to local variables so they can be stored in registers
m += MILLIS_INC; // (volatile variables must be read from memory on every access)
f += FRACT_INC; unsigned long m = timer0_millis;
if (f >= FRACT_MAX) { unsigned char f = timer0_fract;
f -= FRACT_MAX;
m += 1; m += MILLIS_INC;
} f += FRACT_INC;
if (f >= FRACT_MAX) {
timer0_fract = f; f -= FRACT_MAX;
timer0_millis = m; m += 1;
timer0_overflow_count++; }
}
timer0_fract = f;
unsigned long millis() timer0_millis = m;
{ timer0_overflow_count++;
unsigned long m; }
uint8_t oldSREG = SREG;
unsigned long millis()
// disable interrupts while we read timer0_millis or we might get an {
// inconsistent value (e.g. in the middle of a write to timer0_millis) unsigned long m;
cli(); uint8_t oldSREG = SREG;
m = timer0_millis;
SREG = oldSREG; // disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
return m; cli();
} m = timer0_millis;
SREG = oldSREG;
unsigned long micros() {
unsigned long m; return m;
uint8_t oldSREG = SREG, t; }
cli(); unsigned long micros() {
m = timer0_overflow_count; unsigned long m;
#if defined(TCNT0) uint8_t oldSREG = SREG, t;
t = TCNT0;
#elif defined(TCNT0L) cli();
t = TCNT0L; m = timer0_overflow_count;
#else #if defined(TCNT0)
#error TIMER 0 not defined t = TCNT0;
#endif #elif defined(TCNT0L)
t = TCNT0L;
#else
#ifdef TIFR0 #error TIMER 0 not defined
if ((TIFR0 & _BV(TOV0)) && (t < 255)) #endif
m++;
#else
if ((TIFR & _BV(TOV0)) && (t < 255)) #ifdef TIFR0
m++; if ((TIFR0 & _BV(TOV0)) && (t < 255))
#endif m++;
#else
SREG = oldSREG; if ((TIFR & _BV(TOV0)) && (t < 255))
m++;
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond()); #endif
}
SREG = oldSREG;
void delay(unsigned long ms)
{ return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
uint16_t start = (uint16_t)micros(); }
while (ms > 0) { void delay(unsigned long ms)
if (((uint16_t)micros() - start) >= 1000) { {
ms--; uint16_t start = (uint16_t)micros();
start += 1000;
} while (ms > 0) {
} if (((uint16_t)micros() - start) >= 1000) {
} ms--;
start += 1000;
/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */ }
void delayMicroseconds(unsigned int us) }
{ }
// calling avrlib's delay_us() function with low values (e.g. 1 or
// 2 microseconds) gives delays longer than desired. /* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
//delay_us(us); void delayMicroseconds(unsigned int us)
{
#if F_CPU >= 16000000L // calling avrlib's delay_us() function with low values (e.g. 1 or
// for the 16 MHz clock on most Arduino boards // 2 microseconds) gives delays longer than desired.
//delay_us(us);
// for a one-microsecond delay, simply return. the overhead
// of the function call yields a delay of approximately 1 1/8 us. #if F_CPU >= 16000000L
if (--us == 0) // for the 16 MHz clock on most Arduino boards
return;
// for a one-microsecond delay, simply return. the overhead
// the following loop takes a quarter of a microsecond (4 cycles) // of the function call yields a delay of approximately 1 1/8 us.
// per iteration, so execute it four times for each microsecond of if (--us == 0)
// delay requested. return;
us <<= 2;
// the following loop takes a quarter of a microsecond (4 cycles)
// account for the time taken in the preceeding commands. // per iteration, so execute it four times for each microsecond of
us -= 2; // delay requested.
#else us <<= 2;
// for the 8 MHz internal clock on the ATmega168
// account for the time taken in the preceeding commands.
// for a one- or two-microsecond delay, simply return. the overhead of us -= 2;
// the function calls takes more than two microseconds. can't just #else
// subtract two, since us is unsigned; we'd overflow. // for the 8 MHz internal clock on the ATmega168
if (--us == 0)
return; // for a one- or two-microsecond delay, simply return. the overhead of
if (--us == 0) // the function calls takes more than two microseconds. can't just
return; // subtract two, since us is unsigned; we'd overflow.
if (--us == 0)
// the following loop takes half of a microsecond (4 cycles) return;
// per iteration, so execute it twice for each microsecond of if (--us == 0)
// delay requested. return;
us <<= 1;
// the following loop takes half of a microsecond (4 cycles)
// partially compensate for the time taken by the preceeding commands. // per iteration, so execute it twice for each microsecond of
// we can't subtract any more than this or we'd overflow w/ small delays. // delay requested.
us--; us <<= 1;
#endif
// partially compensate for the time taken by the preceeding commands.
// busy wait // we can't subtract any more than this or we'd overflow w/ small delays.
__asm__ __volatile__ ( us--;
"1: sbiw %0,1" "\n\t" // 2 cycles #endif
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
); // busy wait
} __asm__ __volatile__ (
"1: sbiw %0,1" "\n\t" // 2 cycles
void init() "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
{ );
// this needs to be called before setup() or some functions won't }
// work there
sei(); void init()
{
// on the ATmega168, timer 0 is also used for fast hardware pwm // this needs to be called before setup() or some functions won't
// (using phase-correct PWM would mean that timer 0 overflowed half as often // work there
// resulting in different millis() behavior on the ATmega8 and ATmega168) sei();
#if defined(TCCR0A) && defined(WGM01)
sbi(TCCR0A, WGM01); // on the ATmega168, timer 0 is also used for fast hardware pwm
sbi(TCCR0A, WGM00); // (using phase-correct PWM would mean that timer 0 overflowed half as often
#endif // resulting in different millis() behavior on the ATmega8 and ATmega168)
#if defined(TCCR0A) && defined(WGM01)
// set timer 0 prescale factor to 64 sbi(TCCR0A, WGM01);
#if defined(__AVR_ATmega128__) sbi(TCCR0A, WGM00);
// CPU specific: different values for the ATmega128 #endif
sbi(TCCR0, CS02);
#elif defined(TCCR0) && defined(CS01) && defined(CS00) // set timer 0 prescale factor to 64
// this combination is for the standard atmega8 #if defined(__AVR_ATmega128__)
sbi(TCCR0, CS01); // CPU specific: different values for the ATmega128
sbi(TCCR0, CS00); sbi(TCCR0, CS02);
#elif defined(TCCR0B) && defined(CS01) && defined(CS00) #elif defined(TCCR0) && defined(CS01) && defined(CS00)
// this combination is for the standard 168/328/1280/2560 // this combination is for the standard atmega8
sbi(TCCR0B, CS01); sbi(TCCR0, CS01);
sbi(TCCR0B, CS00); sbi(TCCR0, CS00);
#elif defined(TCCR0A) && defined(CS01) && defined(CS00) #elif defined(TCCR0B) && defined(CS01) && defined(CS00)
// this combination is for the __AVR_ATmega645__ series // this combination is for the standard 168/328/1280/2560
sbi(TCCR0A, CS01); sbi(TCCR0B, CS01);
sbi(TCCR0A, CS00); sbi(TCCR0B, CS00);
#else #elif defined(TCCR0A) && defined(CS01) && defined(CS00)
#error Timer 0 prescale factor 64 not set correctly // this combination is for the __AVR_ATmega645__ series
#endif sbi(TCCR0A, CS01);
sbi(TCCR0A, CS00);
// enable timer 0 overflow interrupt #else
#if defined(TIMSK) && defined(TOIE0) #error Timer 0 prescale factor 64 not set correctly
sbi(TIMSK, TOIE0); #endif
#elif defined(TIMSK0) && defined(TOIE0)
sbi(TIMSK0, TOIE0); // enable timer 0 overflow interrupt
#else #if defined(TIMSK) && defined(TOIE0)
#error Timer 0 overflow interrupt not set correctly sbi(TIMSK, TOIE0);
#endif #elif defined(TIMSK0) && defined(TOIE0)
sbi(TIMSK0, TOIE0);
// timers 1 and 2 are used for phase-correct hardware pwm #else
// this is better for motors as it ensures an even waveform #error Timer 0 overflow interrupt not set correctly
// note, however, that fast pwm mode can achieve a frequency of up #endif
// 8 MHz (with a 16 MHz clock) at 50% duty cycle
// timers 1 and 2 are used for phase-correct hardware pwm
#if defined(TCCR1B) && defined(CS11) && defined(CS10) // this is better for motors as it ensures an even waveform
TCCR1B = 0; // note, however, that fast pwm mode can achieve a frequency of up
// 8 MHz (with a 16 MHz clock) at 50% duty cycle
// set timer 1 prescale factor to 64
sbi(TCCR1B, CS11); #if defined(TCCR1B) && defined(CS11) && defined(CS10)
sbi(TCCR1B, CS10); TCCR1B = 0;
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
sbi(TCCR1, CS11); // set timer 1 prescale factor to 64
sbi(TCCR1, CS10); sbi(TCCR1B, CS11);
#endif #if F_CPU >= 8000000L
// put timer 1 in 8-bit phase correct pwm mode sbi(TCCR1B, CS10);
#if defined(TCCR1A) && defined(WGM10) #endif
sbi(TCCR1A, WGM10); #elif defined(TCCR1) && defined(CS11) && defined(CS10)
#elif defined(TCCR1) sbi(TCCR1, CS11);
#warning this needs to be finished #if F_CPU >= 8000000L
#endif sbi(TCCR1, CS10);
#endif
// set timer 2 prescale factor to 64 #endif
#if defined(TCCR2) && defined(CS22) // put timer 1 in 8-bit phase correct pwm mode
sbi(TCCR2, CS22); #if defined(TCCR1A) && defined(WGM10)
#elif defined(TCCR2B) && defined(CS22) sbi(TCCR1A, WGM10);
sbi(TCCR2B, CS22); #elif defined(TCCR1)
#else #warning this needs to be finished
#warning Timer 2 not finished (may not be present on this CPU) #endif
#endif
// set timer 2 prescale factor to 64
// configure timer 2 for phase correct pwm (8-bit) #if defined(TCCR2) && defined(CS22)
#if defined(TCCR2) && defined(WGM20) sbi(TCCR2, CS22);
sbi(TCCR2, WGM20); #elif defined(TCCR2B) && defined(CS22)
#elif defined(TCCR2A) && defined(WGM20) sbi(TCCR2B, CS22);
sbi(TCCR2A, WGM20); #else
#else #warning Timer 2 not finished (may not be present on this CPU)
#warning Timer 2 not finished (may not be present on this CPU) #endif
#endif
// configure timer 2 for phase correct pwm (8-bit)
#if defined(TCCR3B) && defined(CS31) && defined(WGM30) #if defined(TCCR2) && defined(WGM20)
sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64 sbi(TCCR2, WGM20);
sbi(TCCR3B, CS30); #elif defined(TCCR2A) && defined(WGM20)
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode sbi(TCCR2A, WGM20);
#endif #else
#warning Timer 2 not finished (may not be present on this CPU)
#if defined(TCCR4B) && defined(CS41) && defined(WGM40) #endif
sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
sbi(TCCR4B, CS40); #if defined(TCCR3B) && defined(CS31) && defined(WGM30)
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
#endif sbi(TCCR3B, CS30);
sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
#if defined(TCCR5B) && defined(CS51) && defined(WGM50) #endif
sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
sbi(TCCR5B, CS50); #if defined(TCCR4B) && defined(CS41) && defined(WGM40)
sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
#endif sbi(TCCR4B, CS40);
sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
#if defined(ADCSRA) #endif
// set a2d prescale factor to 128
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range. #if defined(TCCR5B) && defined(CS51) && defined(WGM50)
// XXX: this will not work properly for other clock speeds, and sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
// this code should use F_CPU to determine the prescale factor. sbi(TCCR5B, CS50);
sbi(ADCSRA, ADPS2); sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
sbi(ADCSRA, ADPS1); #endif
sbi(ADCSRA, ADPS0);
#if defined(ADCSRA)
// enable a2d conversions // set a2d prescale factor to 128
sbi(ADCSRA, ADEN); // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
#endif // XXX: this will not work properly for other clock speeds, and
// this code should use F_CPU to determine the prescale factor.
// the bootloader connects pins 0 and 1 to the USART; disconnect them sbi(ADCSRA, ADPS2);
// here so they can be used as normal digital i/o; they will be sbi(ADCSRA, ADPS1);
// reconnected in Serial.begin() sbi(ADCSRA, ADPS0);
#if defined(UCSRB)
UCSRB = 0; // enable a2d conversions
#elif defined(UCSR0B) sbi(ADCSRA, ADEN);
UCSR0B = 0; #endif
#endif
} // the bootloader connects pins 0 and 1 to the USART; disconnect them
// here so they can be used as normal digital i/o; they will be
// reconnected in Serial.begin()
#if defined(UCSRB)
UCSRB = 0;
#elif defined(UCSR0B)
UCSR0B = 0;
#endif
}

View File

@@ -1,165 +1,166 @@
/* /*
wiring_digital.c - digital input and output functions wiring_digital.c - digital input and output functions
Part of Arduino - http://www.arduino.cc/ Part of Arduino - http://www.arduino.cc/
Copyright (c) 2005-2006 David A. Mellis Copyright (c) 2005-2006 David A. Mellis
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. 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, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA Boston, MA 02111-1307 USA
Modified 28 September 2010 by Mark Sproul Modified 28 September 2010 by Mark Sproul
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
*/ */
#include "wiring_private.h" #define ARDUINO_MAIN
#include "pins_arduino.h" #include "wiring_private.h"
#include "pins_arduino.h"
void pinMode(uint8_t pin, uint8_t mode)
{ void pinMode(uint8_t pin, uint8_t mode)
uint8_t bit = digitalPinToBitMask(pin); {
uint8_t port = digitalPinToPort(pin); uint8_t bit = digitalPinToBitMask(pin);
volatile uint8_t *reg; uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg;
if (port == NOT_A_PIN) return;
if (port == NOT_A_PIN) return;
// JWS: can I let the optimizer do this?
reg = portModeRegister(port); // JWS: can I let the optimizer do this?
reg = portModeRegister(port);
if (mode == INPUT) {
uint8_t oldSREG = SREG; if (mode == INPUT) {
cli(); uint8_t oldSREG = SREG;
*reg &= ~bit; cli();
SREG = oldSREG; *reg &= ~bit;
} else { SREG = oldSREG;
uint8_t oldSREG = SREG; } else {
cli(); uint8_t oldSREG = SREG;
*reg |= bit; cli();
SREG = oldSREG; *reg |= bit;
} SREG = oldSREG;
} }
}
// Forcing this inline keeps the callers from having to push their own stuff
// on the stack. It is a good performance win and only takes 1 more byte per // Forcing this inline keeps the callers from having to push their own stuff
// user than calling. (It will take more bytes on the 168.) // on the stack. It is a good performance win and only takes 1 more byte per
// // user than calling. (It will take more bytes on the 168.)
// But shouldn't this be moved into pinMode? Seems silly to check and do on //
// each digitalread or write. // But shouldn't this be moved into pinMode? Seems silly to check and do on
// // each digitalread or write.
// Mark Sproul: //
// - Removed inline. Save 170 bytes on atmega1280 // Mark Sproul:
// - changed to a switch statment; added 32 bytes but much easier to read and maintain. // - Removed inline. Save 170 bytes on atmega1280
// - Added more #ifdefs, now compiles for atmega645 // - changed to a switch statment; added 32 bytes but much easier to read and maintain.
// // - Added more #ifdefs, now compiles for atmega645
//static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline)); //
//static inline void turnOffPWM(uint8_t timer) //static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
static void turnOffPWM(uint8_t timer) //static inline void turnOffPWM(uint8_t timer)
{ static void turnOffPWM(uint8_t timer)
switch (timer) {
{ switch (timer)
#if defined(TCCR1A) && defined(COM1A1) {
case TIMER1A: cbi(TCCR1A, COM1A1); break; #if defined(TCCR1A) && defined(COM1A1)
#endif case TIMER1A: cbi(TCCR1A, COM1A1); break;
#if defined(TCCR1A) && defined(COM1B1) #endif
case TIMER1B: cbi(TCCR1A, COM1B1); break; #if defined(TCCR1A) && defined(COM1B1)
#endif case TIMER1B: cbi(TCCR1A, COM1B1); break;
#endif
#if defined(TCCR2) && defined(COM21)
case TIMER2: cbi(TCCR2, COM21); break; #if defined(TCCR2) && defined(COM21)
#endif case TIMER2: cbi(TCCR2, COM21); break;
#endif
#if defined(TCCR0A) && defined(COM0A1)
case TIMER0A: cbi(TCCR0A, COM0A1); break; #if defined(TCCR0A) && defined(COM0A1)
#endif case TIMER0A: cbi(TCCR0A, COM0A1); break;
#endif
#if defined(TIMER0B) && defined(COM0B1)
case TIMER0B: cbi(TCCR0A, COM0B1); break; #if defined(TIMER0B) && defined(COM0B1)
#endif case TIMER0B: cbi(TCCR0A, COM0B1); break;
#if defined(TCCR2A) && defined(COM2A1) #endif
case TIMER2A: cbi(TCCR2A, COM2A1); break; #if defined(TCCR2A) && defined(COM2A1)
#endif case TIMER2A: cbi(TCCR2A, COM2A1); break;
#if defined(TCCR2A) && defined(COM2B1) #endif
case TIMER2B: cbi(TCCR2A, COM2B1); break; #if defined(TCCR2A) && defined(COM2B1)
#endif case TIMER2B: cbi(TCCR2A, COM2B1); break;
#endif
#if defined(TCCR3A) && defined(COM3A1)
case TIMER3A: cbi(TCCR3A, COM3A1); break; #if defined(TCCR3A) && defined(COM3A1)
#endif case TIMER3A: cbi(TCCR3A, COM3A1); break;
#if defined(TCCR3A) && defined(COM3B1) #endif
case TIMER3B: cbi(TCCR3A, COM3B1); break; #if defined(TCCR3A) && defined(COM3B1)
#endif case TIMER3B: cbi(TCCR3A, COM3B1); break;
#if defined(TCCR3A) && defined(COM3C1) #endif
case TIMER3C: cbi(TCCR3A, COM3C1); break; #if defined(TCCR3A) && defined(COM3C1)
#endif case TIMER3C: cbi(TCCR3A, COM3C1); break;
#endif
#if defined(TCCR4A) && defined(COM4A1)
case TIMER4A: cbi(TCCR4A, COM4A1); break; #if defined(TCCR4A) && defined(COM4A1)
#endif case TIMER4A: cbi(TCCR4A, COM4A1); break;
#if defined(TCCR4A) && defined(COM4B1) #endif
case TIMER4B: cbi(TCCR4A, COM4B1); break; #if defined(TCCR4A) && defined(COM4B1)
#endif case TIMER4B: cbi(TCCR4A, COM4B1); break;
#if defined(TCCR4A) && defined(COM4C1) #endif
case TIMER4C: cbi(TCCR4A, COM4C1); break; #if defined(TCCR4A) && defined(COM4C1)
#endif case TIMER4C: cbi(TCCR4A, COM4C1); break;
#if defined(TCCR5A) #endif
case TIMER5A: cbi(TCCR5A, COM5A1); break; #if defined(TCCR5A)
case TIMER5B: cbi(TCCR5A, COM5B1); break; case TIMER5A: cbi(TCCR5A, COM5A1); break;
case TIMER5C: cbi(TCCR5A, COM5C1); break; case TIMER5B: cbi(TCCR5A, COM5B1); break;
#endif case TIMER5C: cbi(TCCR5A, COM5C1); break;
} #endif
} }
}
void digitalWrite(uint8_t pin, uint8_t val)
{ void digitalWrite(uint8_t pin, uint8_t val)
uint8_t timer = digitalPinToTimer(pin); {
uint8_t bit = digitalPinToBitMask(pin); uint8_t timer = digitalPinToTimer(pin);
uint8_t port = digitalPinToPort(pin); uint8_t bit = digitalPinToBitMask(pin);
volatile uint8_t *out; uint8_t port = digitalPinToPort(pin);
volatile uint8_t *out;
if (port == NOT_A_PIN) return;
if (port == NOT_A_PIN) return;
// If the pin that support PWM output, we need to turn it off
// before doing a digital write. // If the pin that support PWM output, we need to turn it off
if (timer != NOT_ON_TIMER) turnOffPWM(timer); // before doing a digital write.
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
out = portOutputRegister(port);
out = portOutputRegister(port);
uint8_t oldSREG = SREG;
cli(); uint8_t oldSREG = SREG;
cli();
if (val == LOW) {
*out &= ~bit; if (val == LOW) {
} else { *out &= ~bit;
*out |= bit; } else {
} *out |= bit;
}
SREG = oldSREG;
} SREG = oldSREG;
}
int digitalRead(uint8_t pin)
{ int digitalRead(uint8_t pin)
uint8_t timer = digitalPinToTimer(pin); {
uint8_t bit = digitalPinToBitMask(pin); uint8_t timer = digitalPinToTimer(pin);
uint8_t port = digitalPinToPort(pin); uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
if (port == NOT_A_PIN) return LOW;
if (port == NOT_A_PIN) return LOW;
// If the pin that support PWM output, we need to turn it off
// before getting a digital reading. // If the pin that support PWM output, we need to turn it off
if (timer != NOT_ON_TIMER) turnOffPWM(timer); // before getting a digital reading.
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
if (*portInputRegister(port) & bit) return HIGH;
return LOW; if (*portInputRegister(port) & bit) return HIGH;
} return LOW;
}

View File

@@ -1,68 +1,67 @@
/* /*
wiring_private.h - Internal header file. wiring_private.h - Internal header file.
Part of Arduino - http://www.arduino.cc/ Part of Arduino - http://www.arduino.cc/
Copyright (c) 2005-2006 David A. Mellis Copyright (c) 2005-2006 David A. Mellis
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. 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, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA Boston, MA 02111-1307 USA
$Id: wiring.h 239 2007-01-12 17:58:39Z mellis $ $Id: wiring.h 239 2007-01-12 17:58:39Z mellis $
*/ */
#ifndef WiringPrivate_h #ifndef WiringPrivate_h
#define WiringPrivate_h #define WiringPrivate_h
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/delay.h> #include <stdio.h>
#include <stdio.h> #include <stdarg.h>
#include <stdarg.h>
#include "Arduino.h"
#include "Arduino.h"
#ifdef __cplusplus
#ifdef __cplusplus extern "C"{
extern "C"{ #endif
#endif
#ifndef cbi
#ifndef cbi #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #endif
#endif #ifndef sbi
#ifndef sbi #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) #endif
#endif
#define EXTERNAL_INT_0 0
#define EXTERNAL_INT_0 0 #define EXTERNAL_INT_1 1
#define EXTERNAL_INT_1 1 #define EXTERNAL_INT_2 2
#define EXTERNAL_INT_2 2 #define EXTERNAL_INT_3 3
#define EXTERNAL_INT_3 3 #define EXTERNAL_INT_4 4
#define EXTERNAL_INT_4 4 #define EXTERNAL_INT_5 5
#define EXTERNAL_INT_5 5 #define EXTERNAL_INT_6 6
#define EXTERNAL_INT_6 6 #define EXTERNAL_INT_7 7
#define EXTERNAL_INT_7 7
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) #define EXTERNAL_NUM_INTERRUPTS 8
#define EXTERNAL_NUM_INTERRUPTS 8 #else
#else #define EXTERNAL_NUM_INTERRUPTS 2
#define EXTERNAL_NUM_INTERRUPTS 2 #endif
#endif
typedef void (*voidFuncPtr)(void);
typedef void (*voidFuncPtr)(void);
#ifdef __cplusplus
#ifdef __cplusplus } // extern "C"
} // extern "C" #endif
#endif
#endif
#endif

View File

@@ -1,69 +1,69 @@
/* /*
wiring_pulse.c - pulseIn() function wiring_pulse.c - pulseIn() function
Part of Arduino - http://www.arduino.cc/ Part of Arduino - http://www.arduino.cc/
Copyright (c) 2005-2006 David A. Mellis Copyright (c) 2005-2006 David A. Mellis
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. 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, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA Boston, MA 02111-1307 USA
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
*/ */
#include "wiring_private.h" #include "wiring_private.h"
#include "pins_arduino.h" #include "pins_arduino.h"
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
* to 3 minutes in length, but must be called at least a few dozen microseconds * to 3 minutes in length, but must be called at least a few dozen microseconds
* before the start of the pulse. */ * before the start of the pulse. */
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
{ {
// cache the port and bit of the pin in order to speed up the // cache the port and bit of the pin in order to speed up the
// pulse width measuring loop and achieve finer resolution. calling // pulse width measuring loop and achieve finer resolution. calling
// digitalRead() instead yields much coarser resolution. // digitalRead() instead yields much coarser resolution.
uint8_t bit = digitalPinToBitMask(pin); uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin); uint8_t port = digitalPinToPort(pin);
uint8_t stateMask = (state ? bit : 0); uint8_t stateMask = (state ? bit : 0);
unsigned long width = 0; // keep initialization out of time critical area unsigned long width = 0; // keep initialization out of time critical area
// convert the timeout from microseconds to a number of times through // convert the timeout from microseconds to a number of times through
// the initial loop; it takes 16 clock cycles per iteration. // the initial loop; it takes 16 clock cycles per iteration.
unsigned long numloops = 0; unsigned long numloops = 0;
unsigned long maxloops = microsecondsToClockCycles(timeout) / 16; unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
// wait for any previous pulse to end // wait for any previous pulse to end
while ((*portInputRegister(port) & bit) == stateMask) while ((*portInputRegister(port) & bit) == stateMask)
if (numloops++ == maxloops) if (numloops++ == maxloops)
return 0; return 0;
// wait for the pulse to start // wait for the pulse to start
while ((*portInputRegister(port) & bit) != stateMask) while ((*portInputRegister(port) & bit) != stateMask)
if (numloops++ == maxloops) if (numloops++ == maxloops)
return 0; return 0;
// wait for the pulse to stop // wait for the pulse to stop
while ((*portInputRegister(port) & bit) == stateMask) { while ((*portInputRegister(port) & bit) == stateMask) {
if (numloops++ == maxloops) if (numloops++ == maxloops)
return 0; return 0;
width++; width++;
} }
// convert the reading to microseconds. The loop has been determined // convert the reading to microseconds. The loop has been determined
// to be 20 clock cycles long and have about 16 clocks between the edge // to be 20 clock cycles long and have about 16 clocks between the edge
// and the start of the loop. There will be some error introduced by // and the start of the loop. There will be some error introduced by
// the interrupt handlers. // the interrupt handlers.
return clockCyclesToMicroseconds(width * 21 + 16); return clockCyclesToMicroseconds(width * 21 + 16);
} }

View File

@@ -1,55 +1,55 @@
/* /*
wiring_shift.c - shiftOut() function wiring_shift.c - shiftOut() function
Part of Arduino - http://www.arduino.cc/ Part of Arduino - http://www.arduino.cc/
Copyright (c) 2005-2006 David A. Mellis Copyright (c) 2005-2006 David A. Mellis
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. 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, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA Boston, MA 02111-1307 USA
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
*/ */
#include "wiring_private.h" #include "wiring_private.h"
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) { uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
uint8_t value = 0; uint8_t value = 0;
uint8_t i; uint8_t i;
for (i = 0; i < 8; ++i) { for (i = 0; i < 8; ++i) {
digitalWrite(clockPin, HIGH); digitalWrite(clockPin, HIGH);
if (bitOrder == LSBFIRST) if (bitOrder == LSBFIRST)
value |= digitalRead(dataPin) << i; value |= digitalRead(dataPin) << i;
else else
value |= digitalRead(dataPin) << (7 - i); value |= digitalRead(dataPin) << (7 - i);
digitalWrite(clockPin, LOW); digitalWrite(clockPin, LOW);
} }
return value; return value;
} }
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
{ {
uint8_t i; uint8_t i;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
if (bitOrder == LSBFIRST) if (bitOrder == LSBFIRST)
digitalWrite(dataPin, !!(val & (1 << i))); digitalWrite(dataPin, !!(val & (1 << i)));
else else
digitalWrite(dataPin, !!(val & (1 << (7 - i)))); digitalWrite(dataPin, !!(val & (1 << (7 - i))));
digitalWrite(clockPin, HIGH); digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW); digitalWrite(clockPin, LOW);
} }
} }