Arduino-GPIO
General Purpose Input/Output (GPIO) library for Arduino
Serial.h
Go to the documentation of this file.
1 
19 #ifndef SOFTWARE_SERIAL_H
20 #define SOFTWARE_SERIAL_H
21 
22 #include "GPIO.h"
23 
29 namespace Software {
30 template<BOARD::pin_t TX_PIN>
31 class Serial : public ::Stream {
32 public:
38  Serial() :
39  m_count(((F_CPU / 57600) / 4) - 4)
40  {
41  m_tx.output();
42  m_tx.high();
43  }
44 
50  void begin(uint32_t baudrate)
51  {
52 #if defined(AVR)
53  m_count = ((F_CPU / baudrate) / 4) - 4;
54 #elif defined(SAM)
55  m_count = 1000000 / baudrate;
56 #endif
57  }
58 
63  operator bool()
64  {
65  return (true);
66  }
67 
76  virtual size_t write(uint8_t byte)
77  {
78  uint16_t data = ((0xff00 | byte) << 1);
79  uint8_t bits = 10;
80  noInterrupts();
81  do {
82  m_tx = data & 0x01;
83 #if defined(AVR)
84  _delay_loop_2(m_count);
85 #elif defined(SAM)
86  delayMicroseconds(m_count);
87 #endif
88  data >>= 1;
89  } while (--bits);
90  interrupts();
91  return (1);
92  }
93 
99  virtual int available()
100  {
101  return (0);
102  }
103 
109  virtual int peek()
110  {
111  return (-1);
112  }
113 
119  virtual int read()
120  {
121  return (-1);
122  }
123 
128  virtual void flush()
129  {
130  }
131 
132 protected:
135 
137  uint16_t m_count;
138 };
139 };
140 #endif
void begin(uint32_t baudrate)
Definition: Serial.h:50
void high()
Definition: GPIO.h:104
uint16_t m_count
Definition: Serial.h:137
GPIO< TX_PIN > m_tx
Definition: Serial.h:134
virtual int peek()
Definition: Serial.h:109
void output()
Definition: GPIO.h:56
virtual void flush()
Definition: Serial.h:128
virtual int available()
Definition: Serial.h:99
virtual int read()
Definition: Serial.h:119
virtual size_t write(uint8_t byte)
Definition: Serial.h:76