In this project we're going to show you how to get you your Meadow device up and running with Visual Studio 2019 and deploying your first application that will control the onboard RGB LED.
PrerequisitesWindows
- Install .NET CORE SDK
- Install Meadow.CLI by using the following command:
dotnet tool install WildernessLabs.Meadow.CLI --global
- Install dfu-util by using the following command:
meadow install dfu-util
macOS
- Install .NET CORE SDK
- Install Meadow.CLI by using the following command:
dotnet tool install WildernessLabs.Meadow.CLI --global
- Install dfu-util by using the following command:
brew install dfu-util
Step 1. Download Meadow.OS and binariesWith the meadow tool installed, you can now download all the necessary binaries and Meadow.OS by using the command:
meadow --download os
Step 2. Check Meadow drivers (Windows)To ensure that your board is in a good state to flash it with the latest and greatest version available of Meadow.OS, when connecting it in bootloader mode (connecting the board while holding the BOOT button), it should show up as STM32 BOOTLOADER under the Universal Serial Bus devices, when connecting it normally it should show up as a USB Serial Device (COMX) under the Ports (COM & LPT) section section in the Device Manager.
If your board appears as Meadow F7 Micro (COMX) in regular mode, right-click on it and select Uninstall Device, and make sure to select Attempt to remove the driver for this device.
Once its done, connect your board in bootloader mode again, and run the Zadig Tool. Click on Options->List All Devices, and you should see STM32 BOOTLOADER in the dropdown menu. Click the install driver button, and wait until you get the Driver Installation: SUCCESS message.
At this point, your Meadow device should be good to go for a proper flash of Meadow.OS.
Step 2. Flash the Meadow board with Meadow.OSTo update the OS, Meadow must be in DFU bootloader mode. To enter this mode, the BOOT
button needs to be held down while the board boots up. This can be accomplished one of two ways.
If the board is disconnected: hold the BOOT
button down and connect the board to your computer via a Micro USB Cable.
If the board is connected: hold the BOOT
button down, and then press and release the RST
(Reset) button. Then release the BOOT
button.
Enter the following command to start the flashing process:
meadow flash os
Reset the device (push the RST button or disconnect and reconnect) and identify the serial port name that the Meadow is connecting on:
Windows
On Windows, serial port name looks something like COM5. To locate, open Device Manager; the Meadow device should show up as USB Serial Device [COMXX]:
macOS
Run the following from terminal:
ls /dev/tty.usb*
The port should be something like /dev/tty.usbmodem01
.
Once you’ve identified out the port name, you'll need it to deploy Applications using the VS extension.
Your board is now ready to have a Meadow application deployed to it!
Notes:- Linux may require
sudo
to access USB devices.
Download and install Visual Studio 2022. You can use the Community Edition which is free for personal use.
Once installed, run Visual Studio and Click Extensions -> Manage Extensions. In the Manage Extensions window, search for VS Tools for Meadow.
Click install to add the extension to your IDE. You can now close this window.
macOS
Download and install the latest version of Visual Studio for Mac. There is a Community Edition which is free for personal use.
Launch Visual Studio and click Extensions... on the menu bar. This should open the Extension Manager window. Select the Gallery Tab, type Meadow in the search box and you should see one result.
Select the Meadow extension and click the Install... button. It will prompt you with a dialog stating that it will install the Meadow v0.4.0 package. Click Install to proceed with the installation.
Once installed, go the the Installed tab, and ensure the extension is listed and enabled.
Windows
In Visual Studio, you can now click File => New Project. Search for Meadow Application and click next.
In the Configure your new project window, name your project HelloWorld.
Click Create to open your new Meadow Application project.
macOS
In Visual Studio, click on File => New Solution... to open the New Project template dialog window. Go to the Meadow section, and there select App on the left panel. Select Meadow Application in the main panel and click next.
Name the project HelloWorld, and click Create.
Understanding the ProjectTemplate
When creating a new project, in the Solution Explorer you'll get the following files and Dependencies
Dependencies
Notice that every Meadow Project comes with a group of Meadow NuGet Packages:
- Meadow.Foundation is a framework that greatly simplifies the task of building connected things with Meadow, by providing a unified driver and library framework that includes drivers and abstractions for common peripherals such as sensors, displays, motors, and more.
- Meadow is the main package that contains Meadow application and OS classes, enabling you to interact with the Meadow.OS
- WildernessLabs.Meadow.Assemblies contains all the underlying dependencies for Meadow.
MeadowApp.cs
Let’s take a brief look at the MeadowApp class:
using System;
using System.Threading;
using Meadow;
using Meadow.Devices;
using Meadow.Hardware;
namespace HelloWorld
{
public class MeadowApp : App<F7Micro, MeadowApp>
{
IDigitalOutputPort redLed;
IDigitalOutputPort blueLed;
IDigitalOutputPort greenLed;
public MeadowApp()
{
ConfigurePorts();
BlinkLeds();
}
public void ConfigurePorts()
{
Console.WriteLine("Creating Outputs...");
redLed = Device.CreateDigitalOutputPort(Device.Pins.OnboardLedRed);
blueLed = Device.CreateDigitalOutputPort(Device.Pins.OnboardLedBlue);
greenLed = Device.CreateDigitalOutputPort(Device.Pins.OnboardLedGreen);
}
public void BlinkLeds()
{
var state = false;
while (true)
{
int wait = 200;
state = !state;
Console.WriteLine($"State: {state}");
redLed.State = state;
Thread.Sleep(wait);
blueLed.State = state;
Thread.Sleep(wait);
greenLed.State = state;
Thread.Sleep(wait);
}
}
}
}
Meadow Namespaces
These are the typical minimum set of namespaces in a Meadow app class and provide the following functionality:
Meadow
- The root namespace contains Meadow application and OS classes, enabling you to interact with the Meadow.OS.Meadow.Devices
- Contains device-specific definitions for different Meadow boards, such as the F7 Micro Dev board, or the F7 Micro embeddable board.Meadow.Hardware
- This namespace contains hardware classes that enable you to interact directly with hardware IO.Meadow.Foundation
- Meadow.Foundation is a set of open-source peripheral drivers and hardware control libraries that make hardware development with Meadow, plug-and-play.
MeadowApp Class Definition
Notice that MeadowApp
application class inherits from App
, and has a generic argument, in this case F7F
eatherV2
to indicate what Meadow form factor the app will run:
public class MeadowApp : App<F7FeatherV2>
All Meadow applications should inherit from the App base class. Under the hood, App
registers itself with the Meadow.OS. It also provides hooks for getting notified during system events, such as the board being put to sleep.
App
requires two parameters; first, the current device definition, and second, the type definition of your custom IApp
class. These are passed to provide a strongly-typed reference to the current device, as well as the current instance of the application from anywhere in the app.
The device class defines properties and capabilities of the current device such as the pins, via the Device
property on the App
base class, and allows you to access them using autocomplete, via the specific device type:
var redLed = Device.CreateDigitalOutputPort(Device.Pins.OnboardLedRed);
Controlling the Onboard LED via Ports
Direct access to hardware Input/Output (IO) is generally available via ports and buses. In this case, we create a IDigitalOutputPort
for each color component (red, green, and blue) of the onboard LED:
IDigitalOutputPort redLed;
IDigitalOutputPort blueLed;
IDigitalOutputPort greenLed;
...
redLed = Device.CreateDigitalOutputPort(Device.Pins.OnboardLedRed);
blueLed = Device.CreateDigitalOutputPort(Device.Pins.OnboardLedBlue);
greenLed = Device.CreateDigitalOutputPort(Device.Pins.OnboardLedGreen);
Ports are created from the device itself, and the Pins
property provides named pins that map to the pins available on the particular device specified above in the App
definition.
Digital Output
To vary the color of the light emitted via the onboard LED, we can write to the internal pins that are connected to the LED, via the State
property, causing them to have a voltage of HIGH
/ON
, or LOW
/OFF
:
public void BlinkLeds()
{
var state = false;
while (true) {
state = !state;
Console.WriteLine($"State: {state}");
redLed.State = state;
Thread.Sleep(500);
blueLed.State = state;
Thread.Sleep(500);
greenLed.State = state;
Thread.Sleep(500);
}
}
Program.cs
If you’ve created a.Net console app before, the Program
class should look familiar; it’s very simple and only includes a static void Main()
method that instantiates our Meadow app:
using Meadow;
namespace HelloWorld
{
class Program
{
static IApp app;
public static void Main(string[] args)
{
// instantiate and run new meadow app
app = new MeadowApp();
}
}
}
This pattern allows us to have an App
instance, in which all things Meadow are done.
Now you're project is all set and ready to run. Click the run button on visual studio and observe the files being transferred to Meadow in the Output window. If this is the first time running your app, it will take longer since Visual Studio will transfer all the core libraries like mscorlib.dll, System.dll and System.Core.dll, so be patient while it finishes transferring and you will eventually see the onboard LED lighting up.
At this point, when making any code changes in the project, the deployment will be much faster, since Meadow has all the necessary files and the extension is smart enough to replace only those files that have been modified which in your case, will be the actual App.exe file.
Get Meadow
References
Comments