MySoftwareSerial.h 2.99 KB
Newer Older
1
/*
Eric Duminil's avatar
Eric Duminil committed
2
MySoftwareSerial.h
3

Eric Duminil's avatar
Eric Duminil committed
4
MySoftwareSerial.cpp - Implementation of the Arduino software serial for ESP8266.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Copyright (c) 2015-2016 Peter Lerup. All rights 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

*/

Eric Duminil's avatar
Eric Duminil committed
23
24
#ifndef MySoftwareSerial_h
#define MySoftwareSerial_h
25
26
27
28
29
30
31
32
33
34

#include <inttypes.h>
#include <Stream.h>


// This class is compatible with the corresponding AVR one,
// the constructor however has an optional rx buffer size.
// Speed up to 115200 can be used.


Eric Duminil's avatar
Eric Duminil committed
35
class MySoftwareSerial : public Stream {
36
public:
Eric Duminil's avatar
Eric Duminil committed
37
38
  MySoftwareSerial(int receivePin, int transmitPin, bool inverse_logic = false, unsigned int buffSize = 64, bool edge_triggered = false);
  virtual ~MySoftwareSerial();
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

  void begin(long speed);
  long baudRate();
  // Transmit control pin
  void setTransmitEnablePin(int transmitEnablePin);
  // Enable or disable interrupts during tx
  void enableIntTx(bool on);

  bool overflow();
  int peek();

  virtual size_t write(uint8_t byte);
  virtual int read();
  virtual int available();
  virtual void flush();
  operator bool() {return m_rxValid || m_txValid;}

  // Disable or enable interrupts on the rx pin
  void enableRx(bool on);
  // One wire control
  void enableTx(bool on);

  void rxRead();

  volatile boolean SerialBusy = false;

  // AVR compatibility methods
  bool listen() { enableRx(true); return true; }
  void end() { stopListening(); }
  bool isListening() { return m_rxEnabled; }
  bool stopListening() { enableRx(false); return true; }

  using Print::write;

  void setWaitingForStart();
  void setStartBit(unsigned long start);
  bool propgateBits(bool level, int pulseBitLength);

private:
  bool isValidGPIOpin(int pin);

  // Member variables
  bool m_edge;
  bool m_oneWire;
  int m_rxPin, m_txPin, m_txEnablePin;
  bool m_rxValid, m_rxEnabled;
  bool m_txValid, m_txEnableValid;
  bool m_invert;
  bool m_overflow;
  unsigned long m_bitTime;
  bool m_intTxEnabled;
  unsigned int m_inPos, m_outPos;
  int m_buffSize;
  uint8_t *m_buffer;

  // Edge detection management
  enum GetByteState { awaitingStart, gotStart, readingBits };

  volatile GetByteState m_getByteState = awaitingStart;

  volatile unsigned long m_byteStart;
  volatile unsigned long m_pulseStart;

  volatile uint8_t m_rec = 0;
  volatile uint8_t m_bitNo = 0;

};

// If only one tx or rx wanted then use this as parameter for the unused pin
#define SW_SERIAL_UNUSED_PIN -1


#endif