Examples

This section contains a growing list of sample lampix applications which you can use as starting point to develop your own ones.

The Tea Pot Example

This is the lampix version of Hello World.

It is a simple tea timer which shows 4 timer buttons once you place a cup of tea under lampix. The user can select a time span and the timer will count down. When the time elapses, the lamp will change its color to signal that yout tea is ready.

import cv2
from threading import Timer
import const
from lampixapp import LampixApp
from cameraeventsource import SurfaceWatcher
from applicationmanager import ApplicationManager, RequiredFeature, HasMethods
from buttons import DelegateButton, Label


class TeaTimerApp(LampixApp):
    """
    The main application class
    """
    def __init__(self):
        # this is our custom detector component
        self.rp = RoundPotDetector(self)

    def activateApp(self):
        self.rp.activate()

    def deactivateApp(self):
        self.rp.deactivate()


class RoundPotDetector(SurfaceWatcher):
    """
    Detector looking for round pots over the whole surface under lampix
    """
    def __init__(self):
        SurfaceWatcher.__init__(self, "potdetector")
        # we need to watch for movement on the desk framework provides this component
        self.desk = RequiredFeature('deskwatcher')

    def onMovement(self, movement, baseDifference, frame, rawFrame, colorFrame, segFrame):
        for p in self._recognizeRoundPot(frame):
            TeaTimer(p).activate()

    def _recognizeRoundPot(self, img):
        img = cv2.medianBlur(img, 5)
        img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        return cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)


class TeaTimer(SurfaceWatcher):
    """
    Single tea timer which takes care of one tea pot
    """

    timernumber = 0

    def __init__(self, location):
        """
        Initializes a tea timer at a given location
        :param location: the rectangle surrounding the pot
        :return:
        """
        SurfaceWatcher.__init__(self, "teatimer%s" % TeaTimer.timernumber)
        TeaTimer.timernumber += 1
        self.timebuttons = [DelegateButton(self, "%d minutes" % i, startTimer, i) for i in range(2, 5)]
        self.stopbutton = DelegateButton(self, "stop timer", stopTimer)
        self.timelabel = Label(self, "")
        for b in self.timebuttons:
            b.activate()

        self.lightcontroller = RequiredFeature('lightcontroller')

        self.s = sched.scheduler(time.time, time.sleep)
        self.secs = 0  # this is the current number of seconds remaining while doing the countdown
        self.minutes = 0  # timer was set for this amount of minutes
        # we need to display the timer, framework provides access to display
        self.display = RequiredFeature('display')
        self.location = location
        self.trafo = RequiredFeature('coordinatetrafo')
        self.setMaskFromRect(trafo.PointToProjector(location))

    def update(self, qp):
        qp.setFont(QtGui.QFont('Decorative', 15))
        penWhite = QtGui.QPen(QtCore.Qt.white, 6, QtCore.Qt.SolidLine)
        qp.setPen(penWhite)
        startAngle = 0
        maxAngle = 360 * 16
        currentAngle = maxAngle * self.secs / self.minutes * 60
        qp.drawArc(self.location, startAngle, currentAngle)

    def startTimer(self, minutes):
        self.minutes = minutes
        for b in self.timebuttons:
            b.deactivate()
        self.secs = minutes * 60
        self.lightcontroller.SetLampColor(80, 10, 10)
        Timer(1.0/60.0, _timerElapsed, self).start()
        self.stopbutton.setText("Cancel")
        self.stopbutton.activate()
        self.timelabel.activate()

    def _timerElapsed(self):
        if self.secs > 0:
            self.secs -= 1
            Timer(1.0/60.0, _timerElapsed, self).start()
            self.timelabel.setText("Remaining: %d" % self.secs)
        else:
            self.stopbutton.setText("Restart")
            self.timelabel.setText("Tea ready!")
            self.lightcontroller.SetLampColor(10, 80, 10)
        self.display.updateDisplay()

    def stopTimer(self):
        for b in self.timebuttons:
            b.activate()
        self.lightcontroller.SetLampColor(20, 20, 20)
        self.stopbutton.deactivate()
        self.timelabel.deactivate()

    def onMovement(self, movement, baseDifference, frame, rawFrame, colorFrame, segFrame):
        # cancel this timer, someone moved the pot. alternative: move with pot
        self.Deactivate()


ApplicationManager().ProvideApp(TeaTimer())