-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucommon.pas
181 lines (148 loc) · 4.88 KB
/
ucommon.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//-----------------------------------------------------------------------------
// USB / Serial, Multi Channel Timer Programmer Console.
// Common routines.
//
// Copyright (C) 2020 SriKIT contributors.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Last updated: Dilshan Jayakody [24th Nov 2020]
//
// Update log:
// [24/11/2020] - Initial version - Dilshan Jayakody.
//-----------------------------------------------------------------------------
unit ucommon;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, DateUtils;
const
DEFAULT_ALARM_LENGTH : Int64 = 60 * 60;
DEFAULT_CHANNEL : Byte = 1;
type
TTimeInfo = record
Year: Word;
Month: Byte;
Date: Byte;
Hour: Byte;
Minute: Byte;
Second: Byte;
end;
TAlarmInfo = record
StartTime: TTimeInfo;
EndTime: TTimeInfo;
Channel: Byte;
end;
function DateTimeToTimeInfo(date: TDateTime) : TTimeInfo;
function TimeInfoToDateTime(date: TTimeInfo) : TDateTime;
function NewAlarmInfo() : TAlarmInfo;
procedure SaveTimeInfoToStream(data: TTimeInfo; var stream: TMemoryStream);
procedure SaveAlarmInfoToStream(data: TAlarmInfo; var stream: TMemoryStream);
function LoadTimeInfoFromStream(var stream: TMemoryStream) : TTimeInfo;
function LoadAlarmInfoFromStream(var stream: TMemoryStream) : TAlarmInfo;
implementation
function DateTimeToTimeInfo(date : TDateTime) : TTimeInfo;
var
tempInfo: TTimeInfo;
tmpYear, tmpMonth, tmpDate: Word;
tmpHour, tmpMinute, tmpSecond, tmpMs: Word;
begin
// Extract date and time components from sepcified TDateTime value.
DecodeDateTime(date, tmpYear, tmpMonth, tmpDate, tmpHour, tmpMinute, tmpSecond, tmpMs);
// Assign extracted values into data structure.
tempInfo.Year := tmpYear;
tempInfo.Month := tmpMonth;
tempInfo.Date := tmpDate;
tempInfo.Hour := tmpHour;
tempInfo.Minute := tmpMinute;
tempInfo.Second := tmpSecond;
Result := tempInfo;
end;
function TimeInfoToDateTime(date : TTimeInfo) : TDateTime;
begin
result := EncodeDateTime(date.Year, date.Month, date.Date, date.Hour, date.Minute, date.Second, 0);
end;
function NewAlarmInfo() : TAlarmInfo;
var
unixTime: Int64;
tmpAlarmInfo: TAlarmInfo;
begin
// Create alarm start time.
unixTime := DateTimeToUnix(Now);
tmpAlarmInfo.StartTime := DateTimeToTimeInfo(UnixToDateTime(unixTime));
// Create alarm stop time.
unixTime := unixTime + DEFAULT_ALARM_LENGTH;
tmpAlarmInfo.EndTime := DateTimeToTimeInfo(UnixToDateTime(unixTime));
// Specify default channel;
tmpAlarmInfo.Channel := DEFAULT_CHANNEL;
result := tmpAlarmInfo;
end;
procedure SaveTimeInfoToStream(data: TTimeInfo; var stream: TMemoryStream);
begin
// 1 Year (2 bytes).
stream.WriteWord(data.Year);
// 2 Month (1 byte).
stream.WriteByte(data.Month);
// 3 Day (1 byte).
stream.WriteByte(data.Date);
// 4 Hour (1 byte).
stream.WriteByte(data.Hour);
// 5 Minute (1 byte).
stream.WriteByte(data.Minute);
// 6 Seconds (1 byte).
stream.WriteByte(data.Second);
end;
procedure SaveAlarmInfoToStream(data: TAlarmInfo; var stream: TMemoryStream);
begin
// Save alarm start configuration.
SaveTimeInfoToStream(data.StartTime, stream);
// Save alarm stop configuration.
SaveTimeInfoToStream(data.EndTime, stream);
// 13 Channel Number (1 byte).
stream.WriteByte(data.Channel);
end;
function LoadTimeInfoFromStream(var stream: TMemoryStream) : TTimeInfo;
var
tmpTimeInfo: TTimeInfo;
begin
// 1 Year (2 bytes).
tmpTimeInfo.Year := stream.ReadWord();
// 2 Month (1 byte).
tmpTimeInfo.Month := stream.ReadByte();
// 3 Day (1 byte).
tmpTimeInfo.Date := stream.ReadByte();
// 4 Hour (1 byte).
tmpTimeInfo.Hour := stream.ReadByte();
// 5 Minute (1 byte).
tmpTimeInfo.Minute := stream.ReadByte();
// 6 Seconds (1 byte).
tmpTimeInfo.Second := stream.ReadByte();
result := tmpTimeInfo;
end;
function LoadAlarmInfoFromStream(var stream: TMemoryStream) : TAlarmInfo;
var
tempAlarmInfo: TAlarmInfo;
tempStartTime, tempEndTime: TTimeInfo;
begin
// Load alarm start configuration.
tempStartTime := LoadTimeInfoFromStream(stream);
// Load alarm stop configuration.
tempEndTime := LoadTimeInfoFromStream(stream);
tempAlarmInfo.StartTime := tempStartTime;
tempAlarmInfo.EndTime := tempEndTime;
// 13 Channel Number (1 byte).
tempAlarmInfo.Channel := stream.ReadByte();
result := tempAlarmInfo;
end;
end.