nanoFramework.M5Stack
1.0.0-preview.98
Prefix Reserved
See the version list below for details.
dotnet add package nanoFramework.M5Stack --version 1.0.0-preview.98
NuGet\Install-Package nanoFramework.M5Stack -Version 1.0.0-preview.98
<PackageReference Include="nanoFramework.M5Stack" Version="1.0.0-preview.98" />
paket add nanoFramework.M5Stack --version 1.0.0-preview.98
#r "nuget: nanoFramework.M5Stack, 1.0.0-preview.98"
// Install nanoFramework.M5Stack as a Cake Addin #addin nuget:?package=nanoFramework.M5Stack&version=1.0.0-preview.98&prerelease // Install nanoFramework.M5Stack as a Cake Tool #tool nuget:?package=nanoFramework.M5Stack&version=1.0.0-preview.98&prerelease
Welcome to the .NET nanoFramework M5Stack Library repository
Build status
Usage
Those 3 nugets provide a support for the M5Stack, M5StickC, M5StickCPlus and M5Core2.
They bring support for the screens as well and require to be flashed with the proper image:
# Replace the com port number by your COM port
# For the M5Stack:
nanoff --target M5Stack --update --preview --serialport COM3
# For the M5StickC:
nanoff --target M5StickC --update --preview --serialport COM3 --baud 115200
# For the M5StickCPlus:
nanoff --target M5StickCPlus --update --preview --serialport COM3 --baud 115200
# For the M5Core2:
nanoff --target M5Core2 --update --preview --serialport COM3
Once you have the nugets, you can then enjoy accessing the screen, the accelerometer, get a Grove I2C connecter, add events on the buttons. And you don't even need to think about anything, all is done for you in the most transparent way!
Note: All the classes that you'll have access are all using the Lazy pattern to be instantiated including the screen. This have the advantage to use as little memory and setup time as possible.
In the samples below, we'll use either M5Stack, either M5Stick as examples, they are all working in a very similar way.
Screen
The only thing you need to do to access the screen is to initialize it:
M5Stack.InitializeScreen();
Once you've initialized it, you can access both a Screen
static class and a Console
static class.
THe Screen
one brings primitives to write directly on the screen points or scare of colors as well as writing a text.
For example, you can write a blue square of 10x10 at the position 0, 0 like this:
ushort[] toSend = new ushort[100];
ushort blue = ColorUtility.To16Bpp(Color.Blue);
for (int i = 0; i < toSend.Length; i++)
{
toSend[i] = blue;
}
Screen.Write(0, 0, 10, 10, toSend);
The Console class works in a similar way as the classic System.Console
:
Console.Clear();
// Test the console display
Console.Write("This is a short text. ");
Console.ForegroundColor = Color.Red;
Console.WriteLine("This one displays in red after the previous one and is already at the next line.");
Console.BackgroundColor = Color.Yellow;
Console.ForegroundColor = Color.RoyalBlue;
Console.WriteLine("And this is blue on yellow background");
Console.ResetColor();
Console.CursorLeft = 0;
Console.CursorTop = 8;
Console.Write("This is white on black again and on 9th line");
Note: You can change the default font as well, you need to provide it as a property. The Cursor positions are calculated with the largest possible character.
M5Core2 has SPRAM, so you can get a full screen buffer as well. Refer to the Graphics samples to understand all you can do with it.
Buttons
The main buttons except the power buttons are exposed.
On the M5Stack they are called ButtonLeft
, ButtonCenter
and ButtonRight
. You can get access to the events as well. For example:
M5Stack.ButtonLeft.Press += (sender, e) =>
{
Console.ForegroundColor = Color.Yellow;
Console.CursorLeft = 0;
Console.CursorTop = 0;
Console.Write($"Left Pressed ");
};
On the M5StickC/CPlus they are called ButtonM5
and ButtonRight
. You can get access to the status of the button, the events and everything you need. For example:
while (!M5StickC.ButtonRight.IsPressed)
{
Thread.Sleep(10);
}
M5StickC.M5Button.IsHoldingEnabled = true;
M5StickC.M5Button.Holding += (sender, e) =>
{
Console.Write("M5 button hold long.");
};
Note: The M5Core2 has touch screen and the buttons are part of it. So far .NET nanoFramework does not have the support for them and thus, they are not supported yet. PR to improve this situation welcome!
Power management
Both M5Stack and M5StickC/CPlus are exposing their power management elements. It is not recommended to change any default value except if you know what you are doing.
Please refer to the detailed examples for the AXP192 used in the M5StickC/CPlus; M5Core2 and IP5306 for the M5Stack.
Accelerometer and Gyroscope
You can get access to the Accelerometer and Gyroscope like this:
var ag = M5Stack.AccelerometerGyroscope;
// Do not move the M5Stack/M5Stick during the calibration
ag.Calibrate(100);
var acc = ag.GetAccelerometer();
var gyr = ag.GetGyroscope();
Debug.WriteLine($"Accelerometer data x:{acc.X} y:{acc.Y} z:{acc.Z}");
Debug.WriteLine($"Gyroscope data x:{gyr.X} y:{gyr.Y} z:{gyr.Z}\n");
Refer to the MPU6886 documentation for more detailed usage on this sensor.
Magnetometer
The M5Stack has a magnetometer, you can access it as well:
var mag = M5Stack.Magnetometer;
// It is more than strongly recommended to calibrate the magnetometer.
// Move the M5Stack in all directions to have a proper calibration.
mag.CalibrateMagnetometer(100);
var magVal = mag.ReadMagnetometer();
Console.WriteLine($"x={magVal.X:N2} ");
Console.WriteLine($"y={magVal.Y:N2} ");
Console.WriteLine($"Z={magVal.Z:N2} ");
var headDir = Math.Atan2(magVal.X, magVal.Y) * 180.0 / Math.PI;
Console.WriteLine($"h={headDir:N2} ");
SerialPort
The M5Stack and M5Core2 can provide a Serial Port, just get it like this:
// You can access any of the Serial Port feature
M5Stack.SerialPort.Open(115200);
// Do anything else you need
M5Stack.SerialPort.Close();
Refer to the SerialPort documentation for more information.
ADC Channels
ADC Channels are pre setup on the M5Stack, access them like this:
// This will give you the ADC1 channel 7 which is on pin 35
AdcChannel myChannel = M5Stack.GetAdcGpio(35);
Refer to the M5Stack documentation to have the mapping of the ADC channels and the pins.
I2C Device/Grove
You can get an I2cDevice/Grove like this:
// In this example, the I2C address of the device is 0x42:
I2cDevice myDevice = M5Stack.GetGrove(0x42);
// replacing GetGrove by GetI2cDevice will have the same impact
SPI Device
The M5Stack provides as well an SPiDevice:
// In this case GPIO5 will be used as chip select:
SpiDevice mySpi = M5Stack.GetSpiDevice(5);
GPIO Controller
Similar as previously, you can get the GpioController
on any of the M5Stack, M5Core2 and M5StickC/CPlus:
// This will open the GPIO 36 as output
var pin5 = M5StickC.GpioController.OpenPin(36, PinMode.Output);
DAC
The M5Stack exposes 2 DAC and you can access them thru the Dac1
and Dac2
properties. Refer to the DAC documentation for more information.
Led
The M5StickC/CPlus exposes a led. You can access it thru the Led
property:
// This will toggle the led:
M5StickC.Led.Toggle();
Infrared Led
The M5StickC/CPlus exposes an infrared led. You can access it thru the InfraredLed
property. This will give you a TransmitterChannel
. Check out the sample pack to understand how to use it.
Feedback and documentation
For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.
Join our Discord community here.
Credits
The list of contributors to this project can be found at CONTRIBUTORS.
License
The nanoFramework Class Libraries are licensed under the MIT license.
Code of Conduct
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.
.NET Foundation
This project is supported by the .NET Foundation.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net is compatible. |
-
- nanoFramework.CoreLibrary (>= 1.11.7)
- nanoFramework.Graphics (>= 1.0.1)
- nanoFramework.Hardware.Esp32 (>= 1.3.4)
- nanoFramework.Iot.Device.Bmm150 (>= 1.0.260)
- nanoFramework.Iot.Device.Button (>= 1.0.259)
- nanoFramework.Iot.Device.Buzzer (>= 1.0.260)
- nanoFramework.Iot.Device.Ip5306 (>= 1.0.259)
- nanoFramework.Iot.Device.Mpu6886 (>= 1.0.260)
- nanoFramework.ResourceManager (>= 1.1.3)
- nanoFramework.Runtime.Events (>= 1.9.2)
- nanoFramework.Runtime.Native (>= 1.5.2)
- nanoFramework.System.Buffers.Binary.BinaryPrimitives (>= 1.0.259)
- nanoFramework.System.Collections (>= 1.3.0)
- nanoFramework.System.Device.Adc (>= 1.0.0-preview.13)
- nanoFramework.System.Device.Dac (>= 1.4.2)
- nanoFramework.System.Device.Gpio (>= 1.0.2)
- nanoFramework.System.Device.I2c (>= 1.0.2)
- nanoFramework.System.Device.Model (>= 1.0.259)
- nanoFramework.System.Device.Pwm (>= 1.0.0-preview.20)
- nanoFramework.System.Device.Spi (>= 1.0.2)
- nanoFramework.System.Diagnostics.Stopwatch (>= 1.0.259)
- nanoFramework.System.IO.Ports (>= 1.0.2)
- nanoFramework.System.Math (>= 1.4.3)
- nanoFramework.System.Numerics (>= 1.0.259)
- UnitsNet.nanoFramework.ElectricCurrent (>= 4.110.0)
- UnitsNet.nanoFramework.Frequency (>= 4.110.0)
- UnitsNet.nanoFramework.Temperature (>= 4.110.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.