Statistics
| Branch: | Revision:

root / quad1 / AeroQuad / NewSoftSerial.h @ 9240aaa3

History | View | Annotate | Download (3.32 KB)

1
/*
2
NewSoftSerial.h - Multi-instance software serial library
3
Copyright (c) 2006 David A. Mellis.  All rights reserved.
4
-- Interrupt-driven receive and other improvements by ladyada
5
-- Tuning, circular buffer, derivation from class Print,
6
   multi-instance support, porting to 8MHz processors,
7
   various optimizations, PROGMEM delay tables, inverse logic and 
8
   direct port writing by Mikal Hart
9

10
This library is free software; you can redistribute it and/or
11
modify it under the terms of the GNU Lesser General Public
12
License as published by the Free Software Foundation; either
13
version 2.1 of the License, or (at your option) any later version.
14

15
This library is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
Lesser General Public License for more details.
19

20
You should have received a copy of the GNU Lesser General Public
21
License along with this library; if not, write to the Free Software
22
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23

24
The latest version of this library can always be found at
25
http://arduiniana.org.
26
*/
27

    
28
#ifndef NewSoftSerial_h
29
#define NewSoftSerial_h
30

    
31
#include <inttypes.h>
32
#include "Print.h"
33

    
34
/******************************************************************************
35
* Definitions
36
******************************************************************************/
37

    
38
#define _NewSS_MAX_RX_BUFF 64 // RX buffer size
39
#define _NewSS_VERSION 10 // software version of this library
40
#ifndef GCC_VERSION
41
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
42
#endif
43

    
44
class NewSoftSerial : public Print
45
{
46
private:
47
  // per object data
48
  uint8_t _receivePin;
49
  uint8_t _receiveBitMask;
50
  volatile uint8_t *_receivePortRegister;
51
  uint8_t _transmitBitMask;
52
  volatile uint8_t *_transmitPortRegister;
53

    
54
  uint16_t _rx_delay_centering;
55
  uint16_t _rx_delay_intrabit;
56
  uint16_t _rx_delay_stopbit;
57
  uint16_t _tx_delay;
58

    
59
  uint16_t _buffer_overflow:1;
60
  uint16_t _inverse_logic:1;
61

    
62
  // static data
63
  static char _receive_buffer[_NewSS_MAX_RX_BUFF]; 
64
  static volatile uint8_t _receive_buffer_tail;
65
  static volatile uint8_t _receive_buffer_head;
66
  static NewSoftSerial *active_object;
67

    
68
  // private methods
69
  void recv();
70
  bool activate();
71
  virtual void write(uint8_t byte);
72
  uint8_t rx_pin_read();
73
  void tx_pin_write(uint8_t pin_state);
74
  void setTX(uint8_t transmitPin);
75
  void setRX(uint8_t receivePin);
76

    
77
  // private static method for timing
78
  static inline void tunedDelay(uint16_t delay);
79

    
80
public:
81
  // public methods
82
  NewSoftSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
83
  ~NewSoftSerial();
84
  void begin(long speed);
85
  void end();
86
  int read();
87
  uint8_t available(void);
88
  bool active() { return this == active_object; }
89
  bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
90
  static int library_version() { return _NewSS_VERSION; }
91
  static void enable_timer0(bool enable);
92
  void flush();
93

    
94
  // public only for easy access by interrupt handlers
95
  static inline void handle_interrupt();
96
};
97

    
98
// Arduino 0012 workaround
99
#undef int
100
#undef char
101
#undef long
102
#undef byte
103
#undef float
104
#undef abs
105
#undef round
106

    
107
#endif