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_SOFTWARE_SERIAL_h
11#define _MITOV_SOFTWARE_SERIAL_h
12
13#include <Mitov.h>
14#include <SoftwareSerial.h>
15
16namespace Mitov
17{
18 template<int RX_PIN_NUMBER, int TX_PIN_NUMBER> class VisuinoSoftwareSerial : public Mitov::BasicSerialPort
19 {
20 typedef Mitov::BasicSerialPort inherited;
21
22 public:
23 OpenWire::SourcePin OutputPin;
24
25 public:
26 unsigned Speed = 9600;
27
28 protected:
29 SoftwareSerial *FSerial = nullptr;
30
31 public:
32 void SetEnabled( bool AValue )
33 {
34 if( Enabled == AValue )
35 return;
36
37 Enabled = AValue;
38 UpdateSerial();
39 }
40
41 public:
42 virtual Stream &GetStream() { return *FSerial; }
43
44 protected:
45 void UpdateSerial()
46 {
47 if( Enabled )
48 {
49 if( ! FSerial )
50 {
51 FSerial = new SoftwareSerial( RX_PIN_NUMBER, TX_PIN_NUMBER );
52 FSerial->begin( Speed );
53 }
54 }
55 else
56 {
57 if( FSerial )
58 {
59 delete FSerial;
60 FSerial = nullptr;
61 }
62 }
63 }
64
65 protected:
66 virtual void SystemInit()
67 {
68 UpdateSerial();
69
70 inherited::SystemInit();
71 }
72
73 virtual void SystemLoopBegin( unsigned long currentMicros )
74 {
75 if( Enabled )
76 {
77 int AData = FSerial->read();
78 if( AData >= 0 )
79 {
80 unsigned char AByte = AData;
81 OutputPin.SendValue( Mitov::TDataBlock( 1, &AByte ));
82// OutputPin.Notify( &AByte );
83 }
84 }
85
86 inherited::SystemLoopBegin( currentMicros );
87 }
88
89/*
90 virtual ~VisuinoSoftwareSerial()
91 {
92 if( FSerial )
93 delete FSerial;
94 }
95*/
96 };
97}
98
99#endif