File: calibration.py

package info (click to toggle)
python-whiteboard 1.0%2Bgit20170915-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,568 kB
  • sloc: python: 1,702; xml: 61; makefile: 23; sh: 12
file content (367 lines) | stat: -rw-r--r-- 9,278 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-

import sys,time

import wiimote

from PyQt5 import QtCore, QtGui, QtWidgets, uic
import PyQt5.Qt as qt

from configuration import Configuration


class CalibrationAbort(Exception):
	pass


def clock():
	return int(time.time()*1000)


class SandClock:
	READY, FIN1, FIN2 = range(3)

	def __init__(self,scene,px,py,radius=30):
		self.scene = scene
		self.radius = radius
		self.elipse = None
		self.circle = None
		self.setCenter(px,py)
		self.initialize()


	def setCenter(self,x,y):
		if self.elipse:
			self.scene.removeItem(self.elipse)
			self.scene.removeItem(self.circle)

		self.elipse = self.scene.addEllipse(x-self.radius/2, y-self.radius/2, self.radius, self.radius,
			qt.QPen(QtCore.Qt.red, 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin),
			qt.QBrush(QtCore.Qt.red))
		self.circle = self.scene.addEllipse(x-self.radius/2, y-self.radius/2, self.radius, self.radius,
			qt.QPen(QtCore.Qt.black, 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
		self.elipse.setVisible(False)
		self.circle.setVisible(True)
                self.scene.update() 


	def initialize(self):
		self.totalTicks = 0
		self.lastTick = 0
		self.state = SandClock.READY

	def update(self,p):
		t = clock()
		delta = 0
		if self.lastTick != 0: delta = t - self.lastTick
		if p == None:
			if delta > 100:
				if self.state == SandClock.FIN1:
					self.state = SandClock.FIN2
				if delta > 250:
					self.initialize()
			return
		self.point = list(p)
		self.lastTick = t
		if self.totalTicks < 700:
			self.totalTicks += delta
		else:
			self.state = SandClock.FIN1

	def draw(self):
		if self.totalTicks:
			self.elipse.setVisible(True)
			dgrs = 5760*self.totalTicks/700
			self.elipse.setSpanAngle(dgrs)
                        self.scene.update()
		else:
			self.elipse.setVisible(False)
                        self.scene.update()

	def finished(self):
		return self.state == SandClock.FIN2

	def getPoint(self):
		return self.point



class SmallScreen:
	def __init__(self,scx,scy,scene):
		self.scene = scene
		self.parentx = scx
		self.parenty = scy
		self.dx = 200
		self.dy = 200
		self.square = scene.addRect(qt.QRectF(scx/2-100,scy/2-100,200,200))
		self.point = scene.addRect(qt.QRectF(self.parentx/2-2,self.parenty/2-2,4,4))
	def drawPoint(self,pos):
		px = -100 + pos[0]*self.dx/wiimote.Wiimote.MAX_X-2
		py = 100 - pos[1]*self.dy/wiimote.Wiimote.MAX_Y-2
		self.point.setPos(px,py)



def crossPoly(x,y):
	pol = QtGui.QPolygonF()
	pol.append(qt.QPointF(x,y))
	pol.append(qt.QPointF(x-5,y-5))
	pol.append(qt.QPointF(x,y))
	pol.append(qt.QPointF(x+5,y+5))
	pol.append(qt.QPointF(x,y))
	pol.append(qt.QPointF(x-5,y+5))
	pol.append(qt.QPointF(x,y))
	pol.append(qt.QPointF(x+5,y-5))
	return pol



class CalibrateDialog2(QtWidgets.QDialog):
	def __init__(self,parent,wii):
		QtGui.QWidget.__init__(self,parent)
		self.wii = wii
		self.ui = uic.loadUi("calibration2.ui",self)

		screenGeom = QtGui.QDesktopWidget().screenGeometry()
		wdt = screenGeom.width()-2
		hgt = screenGeom.height()-2

		viewport = [ self.ui.graphicsView.maximumViewportSize().width(), 
			self.ui.graphicsView.maximumViewportSize().height() 
		]

		self.scene = qt.QGraphicsScene()
		self.scene.setSceneRect(0,0, viewport[0], viewport[1])
		self.ui.graphicsView.setScene(self.scene)

		self.smallScreen = SmallScreen(viewport[0], viewport[1], self.scene)
		self.sandclock = SandClock(self.scene,viewport[0]/2,viewport[1]/2)

		self.CalibrationPoints = [
			[0,0], [wdt,0], [wdt,hgt], [0,hgt]
		]
		self.wiiPoints = []
		self.textMessages = [ 
			self.tr("TOP-LEFT"), 
			self.tr("TOP-RIGHT"), 
			self.tr("BOTTOM-RIGHT"), 
			self.tr("BOTTOM-LEFT") ]

		self.ui.label.setText(self.textMessages.pop(0))

		self.connect(self.ui.but_cancel,
			QtCore.SIGNAL("clicked()"), self.close)

		self.shcut1 = QtGui.QShortcut(self)
		self.shcut1.setKey("Esc")
		self.connect(self.shcut1, QtCore.SIGNAL("activated()"), self.close)

		self.mutex = qt.QMutex()

		self.wii.disable()
		self.wii.putCallbackIR(self.makeWiiCallback())
		self.wii.enable()

		self.timer = qt.QTimer(self)
		self.timer.setInterval(70)
		self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.doWork)
		self.timer.start()


	def makeWiiCallback(self):
		def callback(pos):
			self.mutex.lock()
			self.smallScreen.drawPoint(pos)
			self.sandclock.update(pos)
			self.sandclock.draw()
			# Restart the timer
			self.timer.start()
			self.mutex.unlock()
		return callback


	def doWork(self):
		self.mutex.lock()
		self.sandclock.update(None)
		self.sandclock.draw()

		if self.sandclock.finished():
			self.wiiPoints.append(self.sandclock.getPoint())
			self.sandclock.initialize()
			if len(self.wiiPoints) == 4:
				self.mutex.unlock()
				self.close()
				return
			self.ui.label.setText(self.textMessages.pop(0))

		self.mutex.unlock()

	def closeEvent(self,e):
		self.timer.stop()
		self.wii.disable()
		e.accept()




class CalibrateDialog(QtWidgets.QDialog):
	def __init__(self,parent,wii):
		screenGeom = QtWidgets.QDesktopWidget().screenGeometry()
		self.wdt = screenGeom.width()
		self.hgt = screenGeom.height()

		# Thanks, Pietro Pilolli!!
		QtWidgets.QWidget.__init__(self, parent,
			QtCore.Qt.FramelessWindowHint | 
			QtCore.Qt.WindowStaysOnTopHint  | 
			QtCore.Qt.X11BypassWindowManagerHint )
		self.setGeometry(0, 0, self.wdt, self.hgt)

		self.wii = wii
		self.setContentsMargins(0,0,0,0)

		sh = QtWidgets.QShortcut(self)
		sh.setKey("Esc")
		self.connect(sh, 
			QtCore.SIGNAL("activated()"), self.close)

		sh = QtWidgets.QShortcut(self)
		sh.setKey("Down")
		self.connect(sh, 
			QtCore.SIGNAL("activated()"), self.decCrosses)

		sh = QtWidgets.QShortcut(self)
		sh.setKey("Up")
		self.connect(sh, 
			QtCore.SIGNAL("activated()"), self.incCrosses)

		self.scene = qt.QGraphicsScene()
		self.scene.setSceneRect(0,0, self.wdt, self.hgt)
		self.gv = QtWidgets.QGraphicsView()
		self.gv.setScene(self.scene)
		self.gv.setStyleSheet( "QGraphicsView { border-style: none; }" )
		self.layout = QtWidgets.QVBoxLayout()
		self.layout.setMargin(0)
		self.layout.setSpacing(0)
		self.layout.addWidget(self.gv)
		self.setLayout(self.layout)

		self.CalibrationPoints = [
			[40,40], [self.wdt-40,40], [self.wdt-40,self.hgt-40], [40,self.hgt-40]
		]
		self.clock = clock()
		self.mutex = qt.QMutex()
		self.updateCalibrationPoints(0)

		self.wii.putCallbackIR(self.makeWiiCallback())
		self.wii.enable()

		self.timer = qt.QTimer(self)
		self.timer.setInterval(70)
		self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.doWork)
		self.timer.start()


	def decCrosses(self):
		if self.CalibrationPoints[0][0] < 350:
			self.updateCalibrationPoints(10)

	def incCrosses(self):
		if self.CalibrationPoints[0][0] > 40: 
			self.updateCalibrationPoints(-10)

	def updateCalibrationPoints(self,delta=0):
		self.mutex.lock()
		self.scene.clear()
		self.marks = []
		self.wiiPoints = []
		self.CalibrationPoints[0][0] += delta
		self.CalibrationPoints[1][0] -= delta
		self.CalibrationPoints[2][0] -= delta
		self.CalibrationPoints[3][0] += delta
		self.CalibrationPoints[0][1] += delta
		self.CalibrationPoints[1][1] += delta
		self.CalibrationPoints[2][1] -= delta
		self.CalibrationPoints[3][1] -= delta
		for p in self.CalibrationPoints:
			self.scene.addPolygon(crossPoly(*p))
			m = self.scene.addRect(p[0]-5,p[1]-5,10,10,
				qt.QPen(QtCore.Qt.red, 2, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
			m.setVisible(False)
			self.marks.append([m, p])
                        self.scene.update()

		self.smallScreen = SmallScreen(self.wdt,self.hgt,self.scene)
		self.sandclock = SandClock(self.scene,*self.marks[0][1])
		txt = self.scene.addSimpleText(self.tr("Push UP/DOWN to alter the crosses' position"))
		txt.setPos(self.wdt/2 - txt.boundingRect().width()/2, 40)
		self.mutex.unlock()

	def makeWiiCallback(self):
		k = [0]
		def callback(pos):
			t = clock()
			if (t-k[0]) < 30: return
			self.mutex.lock()
			self.smallScreen.drawPoint(pos)
			self.sandclock.update(pos)
			self.sandclock.draw()
			# Restart the timer
			self.timer.start()
			self.mutex.unlock()
			k[0] = t
		return callback

	def doWork(self):
		self.mutex.lock()
		self.sandclock.update(None)
		self.sandclock.draw()

		if len(self.marks):
			m = self.marks[0][0]
			c = clock() - self.clock
			if c >= 300:
				if m.isVisible(): m.setVisible(False)
				else: m.setVisible(True)
				self.clock = clock()

		if self.sandclock.finished():
			self.wiiPoints.append(self.sandclock.getPoint())
			self.marks.pop(0)[0].setVisible(True)
			if len(self.wiiPoints) == 4:
				self.mutex.unlock()
				self.close()
				return
			self.sandclock.initialize()
			self.sandclock.setCenter(*self.marks[0][1])

		self.mutex.unlock()


	def closeEvent(self,e):
		self.timer.stop()
		self.wii.disable()
		e.accept()


def doCalibration(parent,wii):
	conf = Configuration()
	wii.disable()
	wii.putCallbackBTN(None)

	if conf.getValueStr("fullscreen") == "Yes":
		dialog = CalibrateDialog(parent,wii)
		dialog.showFullScreen()
		dialog.grabKeyboard()
		dialog.exec_()
		dialog.releaseKeyboard()
	else:
		dialog = CalibrateDialog2(parent,wii)
		dialog.show()
		dialog.exec_()

	if len(dialog.wiiPoints) == 4:
		wii.calibrate(dialog.CalibrationPoints,dialog.wiiPoints)
	else:
		raise CalibrationAbort()