Create Your Own OSC subsystem
This How-to is intended for:
Microcontroller Developers
Problem
You're writing some code for the Make Controller, and you want to be able to interact with it via OSC.
Solution
Writing your own subsystem is fairly straight forward thanks to the existence of several Osc helpers. Check there API documentation here. There are a couple different kinds of helpers:
- Osc_IntReceiverHelper( ) - for simple subsystems that have only a single device (like Led), and for which access is limited to properties with integer data types.
- Osc_IndexIntReceiverHelper( ) - supports integer-based properties but when there are many devices (like DigitalOut).
- Osc_GeneralReceiverHelper( ) - supports a combination of integer and string data types.
- Osc_BlobReceiverHelper( ) - supports the OSC blob data type.
As an example, I'll include the steps for creating a fictional GPS system. It's a good idea to adhere to the existing naming convention for your functions, although obviously not necessary.
The steps to setting up your subsystem are:
- Name your new subsystem.
static char* MyGPSOsc_Name = "gps";
- Name the properties that your subsystem will support.
static char* MyGPSOsc_PropertyNames[] = { "latitude", "longitude", 0 }; // must end with a zero - Define getters and setters for each of those properties and functions to call them by property index.
int MyGPSOsc_PropertySet( int index, int property, int value )
{
// it doesn't make sense to write values to the latitude and longitude properties
// so we don't have anything here
return CONTROLLER_OK;
}
int MyGPSOsc_PropertyGet( int index, int property )
{
int value;
switch ( property )
{
case 0: // latitude in our list of properties above
value = MyGPS_GetLatitude( index );
break;
case 1: // longitude in our list of properties above
value = MyGPS_GetLongitude( index );
break;
}
return value;
} - Implement a function to call the correct helper when Osc calls you.
// this is called when the OSC system determines an incoming message is for you.
int MyGPSOsc_ReceiveMessage( int channel, char* message, int length )
{
return Osc_IntReceiverHelper( channel, message, length,
MyGPSOsc_Name,
MyGPSOsc_PropertySet,
MyGPSOsc_PropertyGet,
MyGPSOsc_PropertyNames );
} - Finally, add your subsystem to the list that Osc maintains. This is typically done in the Run( ) routine in make.c that gets called on startup.
void Run( ) // this task gets called as soon as we boot up.
{
// other startup stuff
Osc_RegisterSubsystem( MyGPSOsc_GetName(), MyGPSOsc_ReceiveMessage, NULL );
}
Shopping Cart