Description
If there is already a mechanism in place for this, please let me know. I did not see one, so am proposing this.
If within my own library I want to support multiple target devices (ESP32 and NetDuino) because each might have just a bit different way of doing something, I have no way of knowing what target I am running on.
Following the classes written in the .NET Framework, but modified for the smaller scale, I have implemented the "System.Runtime.InteropServices.RuntimeInformation" class. I removed the OSProcessor and OSArchitecture properties, and added a simpler OSTarget enum.
Each target platform would need to implement 2 native calls:
string NativeOSDescription()
int NativeOSTarget()
The OSTarget would be the following
public enum OSTarget
{
Unknown = 0,
ST_STM32F4_DISCOVERY = 1,
ST_STM32F429I_DISCOVERY = 2,
ST_NUCLEO64_F091RC = 3,
ST_NUCLEO144_F746ZG = 4,
ST_STM32F769I_DISCOVERY = 5,
MBN_QUAIL = 6,
NETDUINO3_WIFI = 7,
ESP32_DEVKITC = 8
}
I just took these values form the target list on the home page for the project. They could actually be anything. But using something that the target is commonly known by would be best.
Then within my shared library I could write (this is just a made up example)
public TimeSpan GetUptime()
{
switch (RuntimeInformation.OSTarget)
{
case OSTarget.NETDUINO3_WIFI:
case OSTarget.ESP32_DEVKITC:
var ticks = NativeMethods.GetUptime();
break;
default:
var ticks = System.Environment.TickCount;
}
return TimeSpan.FromTicks(ticks);
}
Anything in the "NativeMethods" would need to be available in the core firmware. However, something like this needs nothing extra from the firmware.
public double FixValue(double value)
{
switch (RuntimeInformation.OSTarget)
{
case OSTarget.NETDUINO3_WIFI:
// Netduino has things swapped when reading ADC values
value = SwapHiLow(value);
break;
case OSTarget.ESP32_DEVKITC:
// Need to check precision and recalculate
if (preceision == 2)
{
value /= 2;
}
break;
}
return value;
}
Now I can deploy a single NuGet package that can support multiple targets.
I have the initial code here if you want to take a look at what it would take.
Could even add the Processor and Architecture back if you want. So you could have:
Processor: RISC ARM
Archecurre: Cortex-M7F
Or whatever.