def __init__(self):
        super().__init__(None)

        # set counter to count up touch
        self.counter = 0

        # connect to mui display
        self.display = Display()

        # you can choice LED brightness(1 - 100)
        self.display.setDuty(100)

        # clear and turn on display
        self.display.clearDisplay()
        self.display.turnOn(0)

        # connect to mui touchpanel(and set InputEventListener)
        self.input = InputHandler(self)

        # create gesture detector for swipe and long press event(and set GestureListener)
        self._gestureDetector = GestureDetector(self)

        # create UI
        text = Text('Hello world')
        text.setSize(0, 0, 100,
                     32)  # set position and size (x, y, width, height)
        self.addView(text)  # add view to this application
        self.setView(
            text, 'text'
        )  # set view with reference key. when you'd like to access this view, you can get this view via self.getView('key')
        text.addOnTouchViewListener(
            self)  # set callback method(onTouch()) to catch touch event.
Example #2
0
    def __init__(self):
        # connect to mui touchpanel
        self.input = InputHandler(self)

        # create gesture detector(for swipe)
        self.gesture_detector = GestureDetector(listener=self,
                                                longpress_timeout=2)

        # connect to mui display
        self.display = Display()
        # set birghtness level
        self.display.setDuty(100)
        # clear and turn on display
        self.display.clearDisplay()
        self.display.turnOn(0)

        # create display manager(for auto turn off display)
        # after 15 seconds from last user touch to mui, turn off display.
        self.display_manager = DisplayManager(self, time_to_dismiss=15)

        # create base application and set it to current application
        self.app = self._baseApp = HomeApp(appEventListener=self)
Example #3
0
class MuiMain(InputEventListener, GestureListener, AppEventListener,
              DisplayEventListener):
    """
    Main class
    this class connect to mui's hardware(display and touch panel) and control applications transition.
    """
    def __init__(self):
        # connect to mui touchpanel
        self.input = InputHandler(self)

        # create gesture detector(for swipe)
        self.gesture_detector = GestureDetector(listener=self,
                                                longpress_timeout=2)

        # connect to mui display
        self.display = Display()
        # set birghtness level
        self.display.setDuty(100)
        # clear and turn on display
        self.display.clearDisplay()
        self.display.turnOn(0)

        # create display manager(for auto turn off display)
        # after 15 seconds from last user touch to mui, turn off display.
        self.display_manager = DisplayManager(self, time_to_dismiss=15)

        # create base application and set it to current application
        self.app = self._baseApp = HomeApp(appEventListener=self)

    def main_loop(self):
        # start display manager check
        self.display_manager.startDismissTimer()
        # start capture touch event
        self.input.startEventLoop()

    def startTask(self):
        self.app.startTask()

    def updateUI(self, fade=0, turn_on=False, turn_off=False):
        with mutex:
            if (fade > 0) and (turn_on is False):
                # fade out (1 - 4 : 4 is very slow)
                self.display.turnOff(fade)

            if (turn_on is False) and (turn_off is False):
                # set layout data
                self.display.setLayout(self.app.getUI())
                # update Display internal data buffer (do not refesh display until call refreshDisplay())
                self.display.updateLayout()
                # refresh Display
                self.display.refreshDisplay()

            if (fade > 0) and (turn_off is False):
                # fade in
                self.display.turnOn(fade)

    # ------------------------
    # InputEventListener implementation

    def onInputEvent(self, e: MotionEvent):
        if self.display_manager.on is False:
            self.display_manager.on = True
            self.updateUI(fade=2, turn_on=True)

        # dispath touch event to current application
        self.app.dispatchTouchEvent(e)

        # pass to gesture detector
        self.gesture_detector.onTouchEvent(e)

        # reset display turn off timer
        self.display_manager.startDismissTimer()

    # ------------------------
    # AppEventListener implementation

    def requestUpdateDisplay(self, app, fade):
        self.updateUI(fade)

    def setNextApp(self, app):
        self.display_manager.startDismissTimer()

        # stop old application task
        self.app.stopTask()

        # change current application
        self.app = app
        # update UI
        self.updateUI(2)
        # start new application task
        self.app.startTask()

    def onCloseApp(self, app):
        self.display_manager.startDismissTimer()

        # close current application, so return to base application
        self.app = self._baseApp
        self.updateUI(2)
        self.app.startTask()

    def onChangeDuty(self, duty):
        # request brightness change to display class
        self.display.setDuty(duty)

    # ------------------------
    # DisplayEventListener implementation

    def onDismissTime(self) -> bool:
        # check app allow turn off
        return self.app.onTurnOffDisplay()

    def onDismiss(self):
        # turn off display with fade effect
        self.updateUI(fade=3, turn_off=True)

    # ------------------------
    # GestureListener implementation

    def onFling(self, e1: MotionEvent, e2: MotionEvent, x, y):
        # swipe event occured, pass to current application
        self.app.dispatchFlingEvent(e1, e2, x, y)

    def onLongPress(self, e: MotionEvent):
        # long press event occured, pass to current application
        self.app.dispathLongPressEvent(e)
class SimpleApplication(AbsApp, InputEventListener, OnTouchEventListener,
                        GestureListener):
    def __init__(self):
        super().__init__(None)

        # set counter to count up touch
        self.counter = 0

        # connect to mui display
        self.display = Display()

        # you can choice LED brightness(1 - 100)
        self.display.setDuty(100)

        # clear and turn on display
        self.display.clearDisplay()
        self.display.turnOn(0)

        # connect to mui touchpanel(and set InputEventListener)
        self.input = InputHandler(self)

        # create gesture detector for swipe and long press event(and set GestureListener)
        self._gestureDetector = GestureDetector(self)

        # create UI
        text = Text('Hello world')
        text.setSize(0, 0, 100,
                     32)  # set position and size (x, y, width, height)
        self.addView(text)  # add view to this application
        self.setView(
            text, 'text'
        )  # set view with reference key. when you'd like to access this view, you can get this view via self.getView('key')
        text.addOnTouchViewListener(
            self)  # set callback method(onTouch()) to catch touch event.

    def mainLoop(self):
        # start touch event loop
        self.input.startEventLoop()

    def startTask(self):
        pass

    def stopTask(self):
        pass

    def updateUI(self, fade=0):
        """
        update UI.
        """
        with mutex:
            if fade > 0:
                # fade out(0 - 4 : 0 is do not fade, 4 is very slow)
                self.display.turnOff(fade)

            # set layout data
            self.display.setLayout(self.getUI())
            # update Display internal data buffer (do not refesh display until call refreshDisplay())
            self.display.updateLayout()
            # refresh Display
            self.display.refreshDisplay()

            if fade > 0:
                # fade in(0 - 4)
                self.display.turnOn(fade)

    def onInputEvent(self, e: MotionEvent):
        # determin gesture
        handle = self._gestureDetector.onTouchEvent(e)

        # dispatch touch event to all views
        if handle is False:
            self.dispatchTouchEvent(e)

    def onTouch(self, view, e: MotionEvent):
        # handling touch events
        self.counter += 1
        text = self.getView("text")
        text.setText("touch %d" % self.counter)
        self.updateUI()

    def onFling(self, e1: MotionEvent, e2: MotionEvent, x, y):
        # handling swipe event
        print('*** swipe ***')
        return False