Min-DevKit sharing data between objects
Jan M
2月 11 2024 | 10:20 午後
To brush up a bit my C++ knowledge and out of curiosity I started to dip my toes into the Min-Devkit.
As a starting project I chose to work on a new implementation of an object to connect to the ENTTEC DMX USB Pro, because it has a simple serial API and offers some interesting possibilities like multithreading, concurrency etc. One object I am working on is called [jam.dmxusbpro].
As part of the project I want to allow only one instance of [jam.dmxusbpro] to be able to open a serial port to an ENTTEC DMX USB Pro.
The way implemented this is by creating a separate device manager class - a singleton - that tracks which devices are opened by which instance of [jam.dmxusbpro] . That works fine between objects off the same class.
As a second step I am creating another object, called [jam.dmxusbpro~] that allows to control DMX channels with MSP signals.
However, my approach of using a singleton device manager class only works within instances of the same Max object not between [jam.dmxusbpro] and [jam.dmxusbpro~].
Is there a way to share data between different Max objects using the Min-DevKit?
PS: I am working on MacOS and am using the POSIX termios API to open the serial ports.
PPS: Min-DevKit is a great addition!! It got me started very fast and is easily accessible for me who's working more in other languages and has a quite basic experience with C and C++.
Jan M
2月 13 2024 | 10:10 午前
I got it figured out: Dictionaries are global data structures and can do the job of sharing data between different Max classes.
Iain Duncan
2月 13 2024 | 2:20 午後
There are various other global data structures in addition to Dicts too: buffers, tables, various
You can also make your own in C, but it requires you making an "extension" instead of an "external". This allows you to define your datastructures on Max boot. I do this for Scheme for Max with the s4m-arrays (for passing data between s4m objects) and the Bach project does too for their llllls.
Scheme for Max is open source in case you want to know how I did the s4m-arrays. They are very fast unprotected global data structures meant for single-producer-single-consumer communication.
hth!
Jan M
2月 13 2024 | 4:25 午後
Thanks for the insights, Iain!
Iain Duncan
2月 13 2024 | 7:57 午後
Your device manager class approach will work as an extension btw. I'm not sure if this is the case here, but it's worth mentioning that there are still a lot of SDK facilitiies that are either not in the min-devkit or aren't well documented in it, but which have examples in the plain C API.
Jan M
2月 13 2024 | 11:03 午後
My device manager class works fine for sharing data between objects of the same Max class (Max object) with the Min-DevKit but I couldn't get it to work across Max objects. It is defined in its own file (translation unit) and the header included in both objects.
Maybe it gets compiled directly into the .mxo and therefore acts like two different classes?
I can work around that issues with regular dict, but i would be curious how to make and include an extension within the Min-DevKit if it is possible. I saw your code, but I couldn't figure out how to implement it.
... much to learn still ....
Iain Duncan
2月 15 2024 | 12:37 午前
Hi Jan, this is why you need to make an extension, not an external. An external loads only when the object is created, while an extension load on Max startup (assuming the package is installed). So you can run code that is necessary to have initialized before any objects are made that way. I had to switch to an extension for this same reason when I implemented s4m-arrays for sharing between the s4m objects and the s4m.grid objects.
Jan M
2月 16 2024 | 1:52 午後
Hello Iain,
would you mind to explain to a noob like me a bit the concept of an extension? After looking into same code and other packages I saw that that extensions are places inside the extensions folder in a package and that they use the same .mxo extension. I also figured out that externals, that are placed inside the extensions folder seem to load just like regular externals.
Are extensions just externals that are placed in a different folder? Do they have a special structure and/or do they need to be compiled differently that externals? And am I asking the right questions?
Thanks a lot!
Iain Duncan
2月 17 2024 | 2:36 午後
Hi Jan, I'm travelling right now so not at a proper desk, but in brief it's a different folder you put your compiled object in so that Max will load it on start. I can't recall right now whether I had to do anything other than that to get it to work, I seem to remember I had some mucking about with class names that I had to do. If you want to check the Scheme for Max source code, you can see how i have it loading.
Iain Duncan
2月 17 2024 | 2:41 午後
If you look in my main file, s4m.c, you will see ext_main is the function that will run on startup (without the user needing to make an s4m object). If you have it right, a post message in your ext_main will show up as soon as you open Max.
HTH!