MakeBlock Drive Updated
Updated library for MakeBlock Ranger
Loading...
Searching...
No Matches
SoftwareSerial.h
1/*
2SoftwareSerial.h (formerly NewSoftSerial.h) -
3Multi-instance software serial library for Arduino/Wiring
4-- Interrupt-driven receive and other improvements by ladyada
5 (http://ladyada.net)
6-- Tuning, circular buffer, derivation from class Print/Stream,
7 multi-instance support, porting to 8MHz processors,
8 various optimizations, PROGMEM delay tables, inverse logic and
9 direct port writing by Mikal Hart (http://www.arduiniana.org)
10-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
11-- 20MHz processor support by Garrett Mace (http://www.macetech.com)
12-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
13
14This library is free software; you can redistribute it and/or
15modify it under the terms of the GNU Lesser General Public
16License as published by the Free Software Foundation; either
17version 2.1 of the License, or (at your option) any later version.
18
19This library is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22Lesser General Public License for more details.
23
24You should have received a copy of the GNU Lesser General Public
25License along with this library; if not, write to the Free Software
26Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
28The latest version of this library can always be found at
29http://arduiniana.org.
30*/
31
32#ifndef SoftwareSerial_h
33#define SoftwareSerial_h
34
35#include <inttypes.h>
36#include <Stream.h>
37
38/******************************************************************************
39* Definitions
40******************************************************************************/
41
42#define _SS_MAX_RX_BUFF 64 // RX buffer size
43#ifndef GCC_VERSION
44#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
45#endif
46
47class SoftwareSerial : public Stream
48{
49private:
50 // per object data
51 uint8_t _receivePin;
52 uint8_t _receiveBitMask;
53 volatile uint8_t *_receivePortRegister;
54 uint8_t _transmitBitMask;
55 volatile uint8_t *_transmitPortRegister;
56 volatile uint8_t *_pcint_maskreg;
57 uint8_t _pcint_maskvalue;
58
59 // Expressed as 4-cycle delays (must never be 0!)
60 uint16_t _rx_delay_centering;
61 uint16_t _rx_delay_intrabit;
62 uint16_t _rx_delay_stopbit;
63 uint16_t _tx_delay;
64
65 uint16_t _buffer_overflow:1;
66 uint16_t _inverse_logic:1;
67
68 // static data
69 static char _receive_buffer[_SS_MAX_RX_BUFF];
70 static volatile uint8_t _receive_buffer_tail;
71 static volatile uint8_t _receive_buffer_head;
72 static SoftwareSerial *active_object;
73
74 // private methods
75 inline void recv() __attribute__((__always_inline__));
76 uint8_t rx_pin_read();
77 void setTX(uint8_t transmitPin);
78 void setRX(uint8_t receivePin);
79 inline void setRxIntMsk(bool enable) __attribute__((__always_inline__));
80
81 // Return num - sub, or 1 if the result would be < 1
82 static uint16_t subtract_cap(uint16_t num, uint16_t sub);
83
84 // private static method for timing
85 static inline void tunedDelay(uint16_t delay);
86
87public:
88 // public methods
89 SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
91 void begin(long speed);
92 bool listen();
93 void end();
94 bool isListening() { return this == active_object; }
95 bool stopListening();
96 bool overflow() { bool ret = _buffer_overflow; if (ret) _buffer_overflow = false; return ret; }
97 int peek();
98
99 virtual size_t write(uint8_t byte);
100 virtual int read();
101 virtual int available();
102 virtual void flush();
103 operator bool() { return true; }
104
105 using Print::write;
106
107 // public only for easy access by interrupt handlers
108 static inline void handle_interrupt() __attribute__((__always_inline__));
109};
110
111// Arduino 0012 workaround
112#undef int
113#undef char
114#undef long
115#undef byte
116#undef float
117#undef abs
118#undef round
119
120#endif
Definition SoftwareSerial.h:48