summaryrefslogtreecommitdiffstats
path: root/host/pymoistcontrol/util.py
blob: 576975ed941e66e38cd7d0c45f657b1606cac6d5 (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
#
#  Moistcontrol - utility functions
#
#  Copyright (c) 2013 Michael Buesch <m@bues.ch>
#  Licensed under the terms of the GNU General Public License version 2.
#

import sys

# Import the QT toolkit library (PySide).
try:
	from PySide.QtCore import *
	from PySide.QtGui import *
except ImportError as e:
	print("PLEASE INSTALL PySide (http://www.pyside.org/)")
	input("Press enter to exit.")
	sys.exit(1)


# Program version number
VERSION			= "1.0.1"
# Maximum number of flowerpots available.
MAX_NR_FLOWERPOTS	= 6


def clamp(value, minValue, maxValue):
	"""Limit 'value' to the range 'minValue':'maxValue'"""
	return max(min(value, maxValue), minValue)

def boolListToBitMask(boolList):
	"""Convert an iterable of Bools to an integer bit-mask."""
	mask = 0
	for i, b in enumerate(boolList):
		mask |= (1 << i) if b else 0
	return mask

def bitMaskToBoolList(bitMask):
	"""Convert an integer bit-mask to a list of Bools."""
	return list(bool(bitMask & (1 << i))
		    for i in range(bitMask.bit_length()))

class Error(Exception):
	"""Generic exception."""
	pass
bues.ch cgit interface