This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALogic.qml
184 lines (167 loc) · 4.95 KB
/
ALogic.qml
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
181
182
183
184
import QtQuick 2.0
Item {
id: serialTask
visible: false
function isBusy() {
return task.busy
}
function getProgress() {
if (task.waiting)
return timer.progress
else {
var top = task.writing ? task.data.length : (task.counter + task.data.length)
var curr = task.writing ? task.counter : task.data.length
var step = 1 / top
return (curr / top) + timer.progress * step
}
}
function describe() {
if (task.busy) {
if (task.waiting)
return "Waiting for start message"
else {
if (task.writing)
return "%1 of %2 bytes written to address %3".arg(task.counter, task.data.length, task.address)
else
return "%1 of %2 bytes read from address %3".arg(task.data.length, task.counter + task.data.length, task.address)
}
}
else
return serialIO.open ? "Connected" : "Disconnected"
}
Timer {
id: timer
interval: 500
repeat: false
running: task.busy
onTriggered: {
serialIO.open = false
serialError.error("Request timed out")
}
property real progress
NumberAnimation on progress{
id: timerAnimation
from: 0
to: 1
duration: timer.interval
}
function start() {
restart()
timerAnimation.restart()
}
}
QtObject {
id: task
property bool busy
property bool waiting
property bool writing
property int address
property var data
property int counter
property int phase
property var onReadyComplete
function process(incoming) {
if (waiting) {
if (incoming !== 0) {
serialIO.open = false
serialError.error("Opening failed", "Unknown start code message")
}
else {
busy = false
waiting = false
}
}
else {
if (writing) {
if (counter < data.length){
switch(phase) {
case 0:
serialIO.write((address + counter) & 0xFF)
break
case 1:
serialIO.write(((address + counter) >> 8) | (1 << 7))
break
case 2:
serialIO.write(data[counter++])
break
}
phase = (phase + 1) % 3
}
else {
serialIO.open = false
serialIO.open = true
}
}
else {
if (phase == 2) {
data.push(incoming)
if (--counter <= 0){
busy = false
onReadyComplete(data)
return
}
}
switch(phase % 2) {
case 0:
serialIO.write((address + data.length) & 0xFF)
phase = 1
break
case 1:
serialIO.write((address + data.length) >> 8)
phase = 2
break
}
}
}
}
}
function write(address, data) {
if (task.busy)
throw new Error("Already busy")
task.writing = true
task.address = address
task.data = data
task.counter = 0
task.phase = 0
task.busy = true
task.process()
}
function read(address, size, onReadComplete) {
if (task.busy)
throw new Error("Already busy")
task.writing = false
task.address = address
task.data = []
task.counter = size
task.phase = 0
task.busy = true
task.onReadyComplete = onReadComplete
task.process()
}
Connections {
target: serialIO
onIncoming: {
if (task.busy) {
timer.start()
task.process(data)
}
else {
serialIO.open = false
serialError.error("Unexpected data")
}
}
onOpenChanged: {
if (serialIO.open) {
task.waiting = true
task.busy = true
timer.start()
}
else
task.busy = false
}
onError: {
serialIO.open = false
serialError.error(message, messageExt)
}
}
}