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_CONTROLLINO_RTC_h
11#define _MITOV_CONTROLLINO_RTC_h
12
13#include <Mitov.h>
14#include <Controllino.h>
15
16namespace Mitov
17{
18//---------------------------------------------------------------------------
19 class ControllinoRTCModule : public OpenWire::Component, public Mitov::ClockingSupport
20 {
21 typedef OpenWire::Component inherited;
22
23 public:
24 OpenWire::SourcePin OutputPin;
25 OpenWire::SinkPin SetInputPin;
26
27 public:
28 bool Enabled = true;
29
30 public:
31 void SetEnabled( bool AValue )
32 {
33 if( Enabled == AValue )
34 return;
35
36 Enabled = AValue;
37 if( Enabled )
38 StartModule();
39
40 else
41 StopModule();
42
43 }
44
45 protected:
46 virtual void SystemInit()
47 {
48 if( Enabled )
49 StartModule();
50
51 inherited::SystemInit();
52 }
53
54 virtual void SystemLoopBegin( unsigned long currentMicros )
55 {
56 if( ! ClockInputPin.IsConnected() )
57 DoClockReceive( NULL );
58
59 inherited::SystemLoopBegin( currentMicros );
60 }
61
62 void StopModule()
63 {
64 }
65
66 void StartModule()
67 {
68// Serial.println( "START" );
69// SPI.begin();
70// Controllino_RTC_init( 65 );
71 Controllino_RTC_init( 0 );
72// Controllino_RTCSSInit();
73// Controllino_SetRTCSS( true );
74 }
75
76 virtual void DoClockReceive( void *_Data ) override
77 {
78 if( ! Enabled )
79 return;
80
81// Serial.println( "Test1" );
82 unsigned char aDay, aWeekDay, aMonth, aYear, aHour, aMinute, aSecond;
83// Controllino_PrintTimeAndDate();
84
85 Controllino_ReadTimeDate( &aDay, &aWeekDay, &aMonth, &aYear, &aHour, &aMinute, &aSecond );
86// Serial.println( "Test2" );
87 uint16_t ALongYear = 2000 + aYear;
88// Serial.println( ALongYear );
89// Serial.println( aMonth );
90// Serial.println( aDay );
91 TDateTime ADateTime;
92// ADateTime.TryEncodeDate( 2000 + aYear, aMonth, aDay );
93// ADateTime.TryEncodeTime( aHour, aMinute, aSecond, 0 );
94 ADateTime.TryEncodeDateTime( ALongYear, aMonth, aDay, aHour, aMinute, aSecond, 0 );
95 OutputPin.Notify( &ADateTime );
96 }
97
98 void DoSetTimeReceive( void *_Data )
99 {
100 if( ! Enabled )
101 return;
102
103 uint16_t AYear, AMonth, ADay, aWeekDay, AHour, AMinute, ASecond, AMilliSecond;
104
105// Serial.println( "Test2" );
106 ((TDateTime *)_Data)->DecodeDateTime( AYear, AMonth, ADay, aWeekDay, AHour, AMinute, ASecond, AMilliSecond );
107 Controllino_SetTimeDate( ADay, aWeekDay, AMonth, AYear, AHour, AMinute, ASecond );
108
109 }
110
111 public:
112 ControllinoRTCModule()
113 {
114 SetInputPin.SetCallback( MAKE_CALLBACK( ControllinoRTCModule::DoSetTimeReceive ));
115 }
116
117 };
118//---------------------------------------------------------------------------
119}
120
121#endif