Giter Site home page Giter Site logo

Comments (7)

larsblumberg avatar larsblumberg commented on July 29, 2024

Hi Jochen,

the Nuimo SDK runs a D-Bus event loop on the same thread where you call ControllerManager().run. This said, if you do time.sleep(1) on the same thread, you will also block the SDK's D-Bus event loop so it won't send and receive Bluetooth messages. Please note that duration parameter is only a hint how long Nuimo's firmware will display the matrix until it disappears.

You have three options here:

  1. Call ControllerManager().run on a different thread
  2. Call both time.sleep(1) and display_matrix() on a different thread
  3. Use an asynchronous timer function that doesn't block the thread

Please let me know if you need further assistance.

from nuimo-linux-python.

jo-me avatar jo-me commented on July 29, 2024

Hmm.. I already put the ControllerManager.run() on a different thread because I noticed that due to that event loop my script wouldn't react to Ctrl-C anymore (for an orderly shutdown & cleanup).

I don't recall why I put sleeps in the old version, but it must've been a similar problem of the durations not working. So, can I expect that calling display_matrix() several times with different patterns and durations will result in those patterns being displayed exactly for those durations?

from nuimo-linux-python.

larsblumberg avatar larsblumberg commented on July 29, 2024

I would use an asynchronous timer function that makes the next display_matrix() call only after some seconds. I guess that's the easiest way. But using a seconds thread to call this method should work as well.

If you call display_matrix() multiple times, it will actually only send the first matrix and the last matrix to Nuimo. Why is that? As soon as you call display_matrix(), the matrix is immediately sent to Nuimo (this is if you don't block the thread). If you do another display_matrix() right after the first call, the matrix will immediately overwrite the previous matrix. display_matrix()'s interval is not the duration how long Nuimo should wait before showing the next matrix. It is the interval how long the matrix should be shown if no other matrix is sent in the meantime.

This said, if you make multiple display_matrix() calls in a row without waiting between those calls you will only see the first matrix because it is immediately being sent to Nuimo. Subsequent calls will be ignored if Nuimo has not yet acknowledged to have received the first matrix. Hence having no delay between two display_matrix() calls will only show the first matrix, ignore further calls until a response is (internally) received and then finally send the last matrix after an acknowledgement has been received.

This sounds all very complicated, but it's actually not :) Here are two rules that you can follow:

  1. interval parameter is the time the matrix is shown on Nuimo before it fades out in case no other matrix is sent in the meanwhile.
  2. If you want to show a sequence of matrices (like in "animation") you as the SDK user are responsible for adding a delay between calls to display_matrix(). Make sure that delay doesn't block the same thread where ControllerManager.run() has been called. I.e. use asynchronous timer functions.

In the future we might add built-in support for showing a sequence of matrices with pre-defined delays between each matrix. For now it's your responsibility to ensure the delay.

Please let us know if it's still unclear and we're happy to support.

from nuimo-linux-python.

grunskis avatar grunskis commented on July 29, 2024

@jo-me I had better experience with putting ControllerManager on the main thread and display manipulation on a different one. I put together a small example here, I hope this helps:

from threading import Thread
from time import sleep

from nuimo import Controller, ControllerManager, ControllerListener, LedMatrix


DEFAULT_ADAPTER_NAME = "hci0"
MAC_ADDRESS = "c4:d7:54:71:e2:ce"

ICON_NUMBER_1 = LedMatrix("         "
                          "    #    "
                          "   ##    "
                          "  # #    "
                          "    #    "
                          "    #    "
                          "    #    "
                          "   ###   "
                          "         ")

ICON_NUMBER_2 = LedMatrix("         "
                          "   ##    "
                          "  #  #   "
                          "     #   "
                          "     #   "
                          "    #    "
                          "   #     "
                          "  ####   "
                          "         ")

ICON_NUMBER_3 = LedMatrix("         "
                          "   ##    "
                          "  #  #   "
                          "     #   "
                          "    #    "
                          "     #   "
                          "  #  #   "
                          "   ##    "
                          "         ")

DOT_MATRIX = [
    ICON_NUMBER_1,
    ICON_NUMBER_2,
    ICON_NUMBER_3,
]


class NuimoListener(ControllerListener):
    def __init__(self, controller):
        self.controller = controller

        self.stopping = False
        self.t = Thread(target=self.show_dots)

    def connect_succeeded(self):
        self.t.start()

    def show_dots(self):
        i = 0
        while not self.stopping:
            m = DOT_MATRIX[i]
            self.controller.display_matrix(m, interval=3.0, brightness=1.0, fading=True)
            i += 1
            if i >= len(DOT_MATRIX):
                i = 0
            sleep(1.5)

    def stop(self):
        self.controller.disconnect()
        self.stopping = True


controller = Controller(adapter_name=DEFAULT_ADAPTER_NAME, mac_address=MAC_ADDRESS)
listener = NuimoListener(controller)
controller.listener = listener
controller.connect()


manager = ControllerManager()

try:
    manager.run()
except KeyboardInterrupt:
    print("Stopping...")
    listener.stop()
    manager.stop()

from nuimo-linux-python.

larsblumberg avatar larsblumberg commented on July 29, 2024

Hi @jo-me, are you now having success implementing a matrix animation? We have added the above example also to the examples folder.

from nuimo-linux-python.

jo-me avatar jo-me commented on July 29, 2024

Hi @larsblumberg, I read your and @grunskis explanations & examples but I did not have the time to try them out yet. They do sound very reasonable, though. So, thank you for that! I think the examples will also help others to understand how things are meant to work and quickstart new projects.

from nuimo-linux-python.

larsblumberg avatar larsblumberg commented on July 29, 2024

Cool – let us know when there's any more question

from nuimo-linux-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.