-
Notifications
You must be signed in to change notification settings - Fork 2
/
ton.go
75 lines (59 loc) · 1.25 KB
/
ton.go
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
package goplcblocks
import (
"time"
)
// TON The block's struct
type TON struct {
mIN bool
mQ bool
mPT int64
mRESET bool
mET int64
mM bool
mStartTime int64
mActualTime int64
}
// NewTON The block's constructor
func NewTON(pt int64) *TON {
o := &TON{false, false, pt, false, 0, false, 0, 0}
return o
}
// Exec The block's logic
func (o *TON) Exec() {
o.mActualTime = time.Now().UTC().UnixNano() / 1000 / 1000
if o.mRESET && o.mIN {
o.mStartTime = o.mActualTime
o.mRESET = false
}
/* Rising Edge of IN should reset the TIME */
if o.mIN && !(o.mM) {
o.mStartTime = o.mActualTime
}
/* Memory to detect the Rising Edge of IN */
o.mM = o.mIN
/* Count When IN is true (TON timer) */
if o.mIN {
// Should manage overflow here but we need to know G_MAX_MS_COUNTER
o.mET = o.mActualTime - o.mStartTime
} else {
o.mET = 0
}
/* Q is on after Timer is done when IN is true */
if o.mIN && (((o.mET) >= (o.mPT)) || o.mQ) {
o.mQ = true
} else {
o.mQ = false
}
}
// IN The block's input
func (o *TON) IN(in bool) {
o.mIN = in
}
// Q The block's output
func (o TON) Q() bool {
return o.mQ
}
// RESET reset command
func (o *TON) RESET() {
o.mRESET = true
}