1////////////////////////////////////////////////////////////////////////////////
2// //
3// This software is supplied under the terms of a license agreement or //
4// nondisclosure agreement with Mitov Software and may not be copied //
5// or disclosed except in accordance with the terms of that agreement. //
6// Copyright(c) 2002-2016 Mitov Software. All Rights Reserved. //
7// //
8////////////////////////////////////////////////////////////////////////////////
9
10#ifndef _MITOV_FORMATTED_SERIAL_h
11#define _MITOV_FORMATTED_SERIAL_h
12
13#include <Mitov.h>
14
15namespace Mitov
16{
17#ifdef VISUINO_ESP8266
18 const SerialConfig CSerialInits[] =
19#else
20 const long CSerialInits[] =
21#endif
22 {
23 SERIAL_5N1,
24 SERIAL_6N1,
25 SERIAL_7N1,
26 SERIAL_8N1,
27 SERIAL_5N2,
28 SERIAL_6N2,
29 SERIAL_7N2,
30 SERIAL_8N2,
31 SERIAL_5E1,
32 SERIAL_6E1,
33 SERIAL_7E1,
34 SERIAL_8E1,
35 SERIAL_5E2,
36 SERIAL_6E2,
37 SERIAL_7E2,
38 SERIAL_8E2,
39 SERIAL_5O1,
40 SERIAL_6O1,
41 SERIAL_7O1,
42 SERIAL_8O1,
43 SERIAL_5O2,
44 SERIAL_6O2,
45 SERIAL_7O2,
46 SERIAL_8O2
47 };
48//---------------------------------------------------------------------------
49 template<typename T_SERIAL_TYPE, T_SERIAL_TYPE *T_SERIAL> class SerialPort : public Mitov::SpeedSerialPort<T_SERIAL_TYPE, T_SERIAL>
50 {
51 typedef Mitov::SpeedSerialPort<T_SERIAL_TYPE, T_SERIAL> inherited;
52
53 public:
54 TArduinoSerialParity Parity : 4;
55 unsigned int StopBits : 4;
56 unsigned int DataBits : 4;
57
58#ifdef VISUINO_ESP8266
59 bool ReceiveEnabled : 1;
60 bool TransmitEnabled : 1;
61
62 void SetReceiveEnabled( bool AValue )
63 {
64 if( ReceiveEnabled == AValue )
65 return;
66
67 ReceiveEnabled = AValue;
68 ResetSerial();
69 }
70
71 void SetTransmitEnabled( bool AValue )
72 {
73 if( TransmitEnabled == AValue )
74 return;
75
76 TransmitEnabled = AValue;
77 ResetSerial();
78 }
79
80 protected:
81 void ResetSerial()
82 {
83 T_SERIAL->end();
84 StartPort();
85 }
86
87
88#endif // VISUINO_ESP8266
89 public:
90 void SetParity( TArduinoSerialParity AValue )
91 {
92 if( Parity == AValue )
93 return;
94
95 Parity = AValue;
96 inherited::RestartPort();
97 }
98
99 void SetStopBits( unsigned int AValue )
100 {
101 if( StopBits == AValue )
102 return;
103
104 StopBits = AValue;
105 inherited::RestartPort();
106 }
107
108 void SetDataBits( unsigned int AValue )
109 {
110 if( DataBits == AValue )
111 return;
112
113 DataBits = AValue;
114 inherited::RestartPort();
115 }
116
117 protected:
118 virtual void StartPort()
119 {
120 int AIndex = ((int)Parity) * 8 + ( StopBits - 1 ) * 4 + ( DataBits - 5);
121#ifdef VISUINO_ESP8266
122 if( ReceiveEnabled && TransmitEnabled )
123 T_SERIAL->begin( inherited::Speed, CSerialInits[ AIndex ], SERIAL_FULL );
124
125 else if( ReceiveEnabled )
126 T_SERIAL->begin( inherited::Speed, CSerialInits[ AIndex ], SERIAL_RX_ONLY );
127
128 else if( TransmitEnabled )
129 T_SERIAL->begin( inherited::Speed, CSerialInits[ AIndex ], SERIAL_TX_ONLY );
130
131#else // VISUINO_ESP8266
132 T_SERIAL->begin( inherited::Speed, CSerialInits[ AIndex ] );
133#endif // VISUINO_ESP8266
134 }
135
136 public:
137 SerialPort() :
138#ifdef VISUINO_ESP8266
139 ReceiveEnabled( true ),
140 TransmitEnabled( true ),
141#endif // VISUINO_ESP8266
142 Parity( spNone ),
143 StopBits( 1 ),
144 DataBits( 8 )
145 {
146 }
147 };
148//---------------------------------------------------------------------------
149} // Mitov
150
151#endif
152