|
Treehopper C# API
|
You can write C# .NET apps that use Treehopper on macOS using Mono (formerly maintained by Xamarin, but now by Microsoft itself).
We'll be using Microsoft Visual Studio for Mac, a free IDE based on MonoDevelop, in this Getting Started guide.
We can build a console application that targets .NET Framework 4.6.1+. Console .NET Framework apps are the easiest way to produce a cross-platform binary that can be run on macOS, Linux, and Windows without recompiling.
Start Visual Studio for Mac, and create a new **.NET Console Project** named "blinky" in the directory of your choosing.
The Treehopper NuGet package contains all the core board functionality. Treehopper.Desktop contains code needed to use Treehopper with Windows, macOS, or Linux hosts.
To add these packages, right-click on the blinky project (not the solution), and choose Add > Add Nuget Packages.... Search for Treehopper and add both Treehopper and Treehopper.Desktop.
Explicitly adding both packages is actually redundant, since if we add the Treehopper.Desktop NuGet package to the project, it will automatically pull in Treehopper as a dependency.
Open Program.cs and edit it to look like the following:
Hit the play button in the top left to build and launch your app.
You should see the LED start blinking continuously once your program launches. If the LED doesn't work or there are errors, check that you copied the code properly and that you added the udev rules as mentioned above.
Before moving on, make sure you're familiar with what's going on in the code. What happens if you start up the app before plugging in a board?
As a next step, try hooking up an LED to Pin 0 and edit the program to toggle that pin instead of the onboard LED (Hint, check out Pin.ToggleOutput()).
From here, you can dive right into using other peripherals on Treehopper, as well as components from Treehopper.Libraries (make sure to add the Treehopper.Libraries NuGet package first!).