summaryrefslogtreecommitdiff
path: root/scalesgui.py
blob: 6e2a8fb058253bc2dafef0efd3c48ce969a7edb5 (plain)
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
#!/usr/bin/python
"""scalesgui.py

<http://abstrakraft.org/cwiid/attachment/ticket/63/scalesgui.py>

"""

import sys

try:
	import pygame
except:
	print "Sorry, I can't seem to import pygame for some reason."
	print "Please check that the python-pygame package is installed, or get the latest version of pygame from http://www.pygame.org/"
	sys.exit(1)
	
try:
	import cwiid
except:
	print "Sorry, I can't seem to import cwiid for some reason."
	print "Please check that it and it's python bindings are installed, and also the balance board patch from:"
	print "http://abstrakraft.org/cwiid/ticket/63"
	sys.exit(1)

import os, math, random
import time as ptime
from pygame.locals import *
from ConfigParser import ConfigParser
from threading import Thread

class WeightSprite(pygame.sprite.Sprite):
	"""This class describes a sprite containing the weight."""
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.weight = 0.0
		self.update()
		
	def update(self):
		global screen_res, sys_font_weight_fgcolour, sys_font_weight, screen_res
		
		if self.weight > 2:
			self.text = "%.2f" % self.weight
		else:
			self.text = "_.__"
			#print "LESS THAN 2"
		#while len(self.text) < 4:
		#	self.text = "0" + self.text
			
		self.image = sys_font_weight.render(self.text, True, sys_font_weight_fgcolour)

		self.rect = self.image.get_rect()
		self.rect.bottomright = screen_res

def quit_app():
	pygame.quit()
	sys.exit(0)
	
def calcweight( readings, calibrations ):
	"""
	Determine the weight of the user on the board in hundredths of a kilogram
	"""
	weight = 0
	for sensor in ('right_top', 'right_bottom', 'left_top', 'left_bottom'):
		reading = readings[sensor]
		calibration = calibrations[sensor]
		#if reading < calibration[0]:
		#	print "Warning, %s reading below lower calibration value" % sensor
		if reading > calibration[2]:
			print "Warning, %s reading above upper calibration value" % sensor
		# 1700 appears to be the step the calibrations are against.
		# 17kg per sensor is 68kg, 1/2 of the advertised Japanese weight limit.
		if reading < calibration[1]:
			weight += 1700 * (reading - calibration[0]) / (calibration[1] - calibration[0])
		else:
			weight += 1700 * (reading - calibration[1]) / (calibration[2] - calibration[1]) + 1700

	return weight
	
def gsc(readings, pos):
	global named_calibration
	reading = readings[pos]
	calibration = named_calibration[pos]
	
	if reading < calibration[1]:
		return 1700 * (reading - calibration[0]) / (calibration[1] - calibration[0])
	else:
		return 1700 * (reading - calibration[1]) / (calibration[2] - calibration[1]) + 1700
		
	
print "Please press the red 'connect' button on the balance board, inside the battery compartment."
print "Do not step on the balance board."

global wiimote
if len(sys.argv) > 1:
	wiimote = cwiid.Wiimote(sys.argv[1])
else:
	wiimote = cwiid.Wiimote()

wiimote.rpt_mode = cwiid.RPT_BALANCE | cwiid.RPT_BTN
wiimote.request_status()

balance_calibration = wiimote.get_balance_cal()
named_calibration = { 'right_top': balance_calibration[0],
					  'right_bottom': balance_calibration[1],
					  'left_top': balance_calibration[2],
					  'left_bottom': balance_calibration[3],
					}

system_file = "system.ini"

if not os.path.lexists(system_file):
	print "Problem: System configuration file (system.ini) doesn't exist."
	sys.exit(1)

sconf = ConfigParser()
sconf.read(system_file)


xdisplay = sconf.get("display", "xdisplay")
if len(xdisplay) > 1:
	# using alternate display.
	print "Attempting to use device", xdisplay, "instead of the default."
	os.putenv("DISPLAY", xdisplay)

pygame.init()

sys_font_weight = pygame.font.SysFont(sconf.get("font_weight", "face"), int(sconf.get("font_weight", "size")))

sys_font_weight.set_italic(False)
sys_font_weight.set_underline(False)

bgcolour = (0, 0, 0)
sys_font_weight_fgcolour = (255, 255, 255)
screen_res = (int(sconf.get("display", "width")), int(sconf.get("display", "height")))
refresh_delay = int(sconf.get("display", "refresh_delay"))

screen_options = 0
if int(sconf.get("display", "fullscreen")) >= 1 and len(xdisplay) <= 1:
	screen_options = screen_options | pygame.FULLSCREEN

if int(sconf.get("display", "double_buffers")) >= 1:
	screen_options = screen_options | pygame.DOUBLEBUF

if int(sconf.get("display", "hardware_surface")) >= 1:
	screen_options = screen_options | pygame.HWSURFACE

if int(sconf.get("display", "opengl")) >= 1:
	screen_options = screen_options | pygame.OPENGL

screen = pygame.display.set_mode(screen_res, screen_options)
pygame.display.set_caption("scales application")

weight_sprite = WeightSprite()
weight_sprite.weight = 40.33
frame = 0
while True:
	for event in pygame.event.get():
		if event.type == KEYDOWN:
			if event.key == K_F12:
				quit_app()
				
	wiimote.request_status()
	frame = frame + 1
	if frame == 50:
		frame = 0
		weight = (calcweight(wiimote.state['balance'], named_calibration) / 100.0)
		#print "%.2fkg" % weight
		weight_sprite.weight = weight
	
	
	readings = wiimote.state['balance']
	
	try:
		x_balance = (float(gsc(readings,'right_top')+gsc(readings,'right_bottom'))) / (float(gsc(readings,'left_top')+gsc(readings,'left_bottom')))
		if x_balance > 1:
			x_balance = (((float(gsc(readings,'left_top')+gsc(readings,'left_bottom'))) / (float(gsc(readings,'right_top')+gsc(readings,'right_bottom'))))*-1.)+1.
		else:
			x_balance = x_balance -1.
		y_balance = (float(gsc(readings,'left_bottom')+gsc(readings,'right_bottom'))) / (float(gsc(readings,'left_top')+gsc(readings,'right_top')))
		if y_balance > 1:
			y_balance = (((float(gsc(readings,'left_top')+gsc(readings,'right_top'))) / (float(gsc(readings,'left_bottom')+gsc(readings,'right_bottom'))))*-1.)+1.
		else:
			y_balance = y_balance -1.
	except:
		x_balance = 1.
		y_balance = 1.
	
	#print "readings:",readings

	screen.fill(bgcolour) # blank the screen.
	
	# line up the lines
	pygame.draw.line(screen, (0,0,255), (screen_res[0]/2,0), (screen_res[0]/2,screen_res[1]), 2)
	pygame.draw.line(screen, (0,0,255), (0,screen_res[1]/2), (screen_res[0],screen_res[1]/2), 2)
	
	weight_sprite.update()
	
	screen.blit(weight_sprite.image, weight_sprite.rect)
	
	xpos = (x_balance * (screen_res[0]/2)) + (screen_res[0]/2)
	ypos = (y_balance * (screen_res[1]/2)) + (screen_res[1]/2)
		
	#print "balance:", x_balance, y_balance
	#print "position:", xpos,ypos
	pygame.draw.circle(screen, (255,0,0), (int(xpos), int(ypos)), 5)
	pygame.display.flip()
	pygame.time.wait(refresh_delay)