nanoFramework.MagicBit 1.2.130

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package nanoFramework.MagicBit --version 1.2.130
NuGet\Install-Package nanoFramework.MagicBit -Version 1.2.130
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoFramework.MagicBit" Version="1.2.130" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.MagicBit --version 1.2.130
#r "nuget: nanoFramework.MagicBit, 1.2.130"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install nanoFramework.MagicBit as a Cake Addin
#addin nuget:?package=nanoFramework.MagicBit&version=1.2.130

// Install nanoFramework.MagicBit as a Cake Tool
#tool nuget:?package=nanoFramework.MagicBit&version=1.2.130

Quality Gate Status Reliability Rating License NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework MagicBit Library repository

Build status

Component Build Status NuGet Package
MagicBit Build Status NuGet

Usage

This nuget can be used with the great MagicBit board.

It does bring support for almost all the sensors and the robot elements. Still, some are not natively in this nuget as they are part of the existing IoT.Device bindings. See the known limitations as well.

You just need to make sure your MagicBit is flashed like this:

# Replace the com port number by your COM port
nanoff --platform esp32 --update --preview --serialport COM3

A detailed example is available in the Test application as well.

Screen

The only thing you need to do to access the screen is to initialize it:

MagicBit.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, select colors as well as writing a text.

For example, you can write a small buffer square of 8x8 at the position 0, 26 with a width of 8 like this:

byte[] _heart = new byte[] {
            0b0100_0010,
            0b0110_0110,
            0b1111_1111,
            0b1111_1111,
            0b1111_1111,
            0b0011_1100,
            0b0011_1100,
            0b0001_1000,
        };
Screen.DrawBitmap(0, 26, 8, _heart);

Note that only multiple of 8 are possible for the width, the buffer should be a multiple of the width / 8. Each bit is representing a pixel. This is the way you can display images.

The screen provides as well other primitives, here is a quick example:

// You can use the Screen primitives like this:
Screen.Clear();
Screen.Write(2, 0, "MagicBit", 2);
Screen.Write(2, 26, "loves .NET", 1, true);
Screen.Write(2, 40, "nanoFramework", 1, true);
Screen.Write(2, 50, "Clk right button", 1, false);
Screen.DrawBitmap(0, 26, 8, _heart);
Screen.DrawBitmap(Screen.Width - 9, 26, 8, _heart);
// You should make sure that you call Display
Screen.Display();

And you will get even more thru Screen.Device.

The Console class works in a similar way as the classic System.Console:

Console.Clear();
Console.WriteLine("Motors");
Console.CursorTop = 2;
Console.WriteLine("Motors will run reverse then rotate both direction");

Note: You can change the default font as well, you need to provide it as a property. The Cursor positions are calculated with the width of the font.

Buttons

The 2 buttons are exposed.

They are called ButtonLeft and ButtonRight. You can get access to the events as well. For example:

MagicBit.ButtonLeft.Press += (sender, e) =>
{
    Console.CursorLeft = 0;
    Console.CursorTop = 0;
    Console.Write($"Left Pressed  ");
};

// Simple way of getting the button status
while (!MagicBit.ButtonRight.IsPressed)
{
    Thread.Sleep(20);
}

Another sample with events:

MagicBit.ButtonRight.IsHoldingEnabled = true;
MagicBit.ButtonRight.Holding += (sender, e) =>
{
    Console.Write("ButtonRight hold long.");
};

Motors

The MagicBit has a driver allowing to control 2 DC motors. If you have the robot kit, you'll be able to control them. If you don't have the robot kit, you still can use your own if you plug them on the correct pins.

Console.Clear();
Console.WriteLine("Motors");
Console.CursorTop = 2;
Console.WriteLine("Motors will run reverse then rotate both direction");
var motor1 = MagicBit.Motor1;
var motor2 = MagicBit.Motor2;
motor1.Speed = -0.5;
motor2.Speed = -0.5;
Thread.Sleep(2000);
motor1.Speed = +0.5;
motor2.Speed = -0.5;
Thread.Sleep(2000);
motor1.Speed = -0.5;
motor2.Speed = +0.5;
Thread.Sleep(2000);
motor1.Speed = 0;
motor2.Speed = 0;

Buzzer

It's of course possible to use the buzzer. Here is an example playing tones:

var buzz = MagicBit.Buzzer;
for (int i = 0; i < 10; i++)
{
    buzz.PlayTone(500 + i * 25, 1000);
}

Potentiometer and Luminosity

Those 2 embedded sensors can be directly accessed and used. Here is a complete example reading them, displaying the value on the screen and interrupting them when the left button is pressed:

CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
MagicBit.ButtonLeft.Press += (sender, e) =>
{
    cancellationTokenSource.Cancel();
};

Console.Clear();
Console.WriteLine("Sensors");
Console.CursorTop = 2;
Console.WriteLine("Clk left button to stop");

while (!cancellationTokenSource.Token.IsCancellationRequested)
{
    // Read the potentiometer ratio, from 0.0 to 1.0
    var ratio = MagicBit.Potentiometer.ReadRatio();
    // Read the luminosity sensor ratio from 0.0 (full dark) to 1.0 (full light)
    var lumi = MagicBit.Luminosity.ReadRatio();
    Console.CursorTop = 4;
    Console.CursorLeft = 0;
    Console.WriteLine($"Pot: {ratio * 100:N2}%  ");
    Console.CursorTop = 5;
    Console.CursorLeft = 0;
    Console.WriteLine($"lum: {lumi * 100:N2}%  ");
    cancellationTokenSource.Token.WaitHandle.WaitOne(200, true);
}

Servo motor

Servo motor can be attached to. So far, you have a direct and easy way on the blue pin. This full sample shows how to change the angle from 0 to 180 degrees, displays the next angle and wait for the left button to be pressed to stop:

CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
MagicBit.ButtonLeft.Press += (sender, e) =>
{
    cancellationTokenSource.Cancel();
};

Console.Clear();
Console.WriteLine("Servo");
Console.CursorTop = 2;
Console.WriteLine("Clk left button to stop");

// The servo can be different and you can adjust the values as needed
var servo = MagicBit.GetServoPinBlue(180, 500, 2400);
// This is needed to start the servo
servo.Start();
int angle = 0;
while (!cancellationTokenSource.Token.IsCancellationRequested)
{
    servo.WriteAngle(angle);
    angle = angle == 0 ? 180 : 0;
    Console.CursorTop = 4;
    Console.CursorLeft = 0;
    Console.WriteLine($"Angle: {angle}deg  ");
    cancellationTokenSource.Token.WaitHandle.WaitOne(3000, true);
}

// Don't forget to stop it at the end.
servo.Stop();

Note: it is technically possible to use any available pin to plug a servo motor. The board package will make your life easy if you are using the blue pin. Otherwise, you'll have to set up the Servo motor yourself.

Leds

The 4 embedded leds are available.

MagicBit.LedBlue.Write(PinValue.High);

Important: You cannot use them with the motors as they are using the same pins.

I2C Device

By default you can get an I2C Device thorough the red pins. You can either use GetRed or GetI2cDevice. For example if you wan to create an I2C Device with the address 0x42, just do:

var myI2cDevice = MagicBit.GetI2cDevice(0x42);

Black left and right pins

You can get a GPIO Pin, so a pin you can use a single output or input from the Black pins directly. Both GetPinBlackLeft and GetPinBlackRight will give it to you:

// This will create an output mode pint, you can for example attach a led
var myPin = MagicBit.GetPinBlackLeft(PinMode.Output);
// This will change the pin to high
myPin.Write(PinValue.High);

Blue pin

The blue pin is setup by default to be used as PWM. When getting it, you'll get a PWM Channel:

var myPwm = MagicBit.GetPinBlue();

Changing default pin behavior

This is a more advance scenario but you can change the function of any pin if you did not use the default function before. For example, you can from the black left pin create a PWM chanel as well:

Configuration.SetPinFunction(32, DeviceFunction.PWM11);
var myBlackPwm = PwmChannel.CreateFromPin(32);

Even if the blue pin default behavior is PWM, if you do not ask for it, you can use it in a different way. For example as a simple input:

var myBluePin = MagicBit.GpioController.Open(26, PinMode.Input);

Known limitations

There are few sensors that will not work, see the list below, the reasons and possible mitigation:

  • DHT sensor is not supported yet on ESP32 .NET managed code. You can use one of the other supported temperature and humidity sensor. See here.
  • The HCSR04 which is on the robot is not yet supported as it's using the same pin for emission and reception of the signal. This is work in progress to find a solution. The one sold separately, when used with the red port will perfectly work.
  • The QRT Sensors are not yet supported as a group, this is work in progress. In the mean time, you can read them as analog sensors individually.

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 Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.130 71 4/18/2024
1.2.128 77 4/8/2024
1.2.126 86 3/25/2024
1.2.124 94 3/18/2024
1.2.122 84 3/14/2024
1.2.120 92 2/29/2024
1.2.118 81 2/26/2024
1.2.116 101 1/26/2024
1.2.103 189 11/13/2023
1.2.101 100 11/9/2023
1.2.98 99 11/9/2023
1.2.96 134 10/9/2023
1.2.94 126 10/5/2023
1.2.92 111 9/28/2023
1.2.90 106 9/18/2023
1.2.88 115 9/7/2023
1.2.86 134 9/4/2023
1.2.84 124 8/17/2023
1.2.82 129 8/14/2023
1.2.80 150 8/3/2023
1.2.78 140 7/31/2023
1.2.76 135 7/27/2023
1.2.74 125 7/24/2023
1.2.72 153 7/20/2023
1.2.70 145 7/17/2023
1.2.68 133 7/13/2023
1.2.66 158 6/22/2023
1.2.64 145 6/19/2023
1.2.62 134 6/15/2023
1.2.60 153 6/12/2023
1.2.58 153 6/8/2023
1.2.56 126 6/5/2023
1.2.54 148 5/29/2023
1.2.52 144 5/25/2023
1.2.50 161 5/22/2023
1.2.46 169 5/15/2023
1.2.44 171 5/11/2023
1.2.42 133 5/8/2023
1.2.40 195 4/3/2023
1.2.38 220 3/27/2023
1.2.36 215 3/23/2023
1.2.34 228 3/20/2023
1.2.32 240 3/16/2023
1.2.30 228 3/13/2023
1.2.28 252 3/9/2023
1.2.26 226 3/2/2023
1.2.24 262 2/27/2023
1.2.22 239 2/23/2023
1.2.20 233 2/20/2023
1.2.18 273 2/16/2023
1.2.16 289 2/6/2023
1.2.13 326 1/16/2023
1.2.2.16170 310 12/25/2022
1.1.0.96 383 11/15/2022
1.1.0.94 367 11/14/2022
1.1.0.92 376 11/6/2022
1.1.0.90 375 11/4/2022
1.1.0.88 368 11/3/2022
1.1.0.86 366 11/1/2022
1.1.0.84 412 10/27/2022
1.1.0.82 432 10/26/2022
1.1.0.80 420 10/25/2022
1.1.0.78 441 10/24/2022
1.1.0.76 431 10/23/2022
1.1.0.74 425 10/22/2022
1.1.0.72 443 10/13/2022
1.1.0.70 427 10/12/2022
1.1.0.68 422 10/9/2022
1.1.0.65 466 9/23/2022
1.1.0.63 466 9/22/2022
1.1.0.61 481 9/16/2022
1.1.0.59 512 9/15/2022
1.1.0.57 468 9/9/2022
1.1.0.55 426 9/8/2022
1.1.0.53 436 9/5/2022
1.1.0.51 464 8/16/2022
1.1.0.49 449 8/15/2022
1.1.0.47 464 8/7/2022
1.1.0.45 439 8/6/2022
1.1.0.43 448 8/5/2022
1.1.0.41 440 8/4/2022
1.1.0.39 446 8/3/2022
1.1.0.37 442 8/2/2022
1.1.0.34 463 7/25/2022
1.1.0.32 446 7/24/2022
1.1.0.30 471 7/23/2022
1.1.0.28 480 7/10/2022
1.1.0.26 485 7/9/2022
1.1.0.24 478 7/8/2022
1.1.0.22 479 7/1/2022
1.1.0.20 463 6/30/2022
1.1.0.18 479 6/29/2022
1.1.0.16 498 6/27/2022
1.1.0.14 463 6/25/2022
1.1.0.12 462 6/24/2022
1.1.0.10 465 6/16/2022
1.1.0.8 452 6/15/2022
1.1.0.6 438 6/14/2022
1.1.0.4 452 6/1/2022
1.0.1-preview.101 111 3/26/2022
1.0.1-preview.99 113 3/25/2022
1.0.1-preview.97 103 3/22/2022
1.0.1-preview.95 112 3/21/2022
1.0.1-preview.93 115 3/20/2022
1.0.1-preview.91 115 3/18/2022
1.0.1-preview.89 120 3/18/2022
1.0.1-preview.87 120 3/17/2022
1.0.1-preview.85 113 3/16/2022
1.0.1-preview.83 109 3/14/2022
1.0.1-preview.81 112 3/14/2022
1.0.1-preview.79 118 3/12/2022
1.0.1-preview.77 108 3/9/2022
1.0.1-preview.75 116 3/7/2022
1.0.1-preview.73 115 3/4/2022
1.0.1-preview.71 111 3/3/2022
1.0.1-preview.69 119 2/28/2022
1.0.1-preview.67 111 2/27/2022
1.0.1-preview.65 119 2/26/2022
1.0.1-preview.63 115 2/19/2022
1.0.1-preview.61 109 2/18/2022
1.0.1-preview.57 121 2/17/2022
1.0.1-preview.55 124 2/16/2022
1.0.1-preview.53 112 2/13/2022
1.0.1-preview.51 117 2/12/2022
1.0.1-preview.49 116 2/11/2022
1.0.1-preview.47 118 2/10/2022
1.0.1-preview.45 109 2/9/2022
1.0.1-preview.43 131 2/5/2022
1.0.1-preview.41 133 2/1/2022
1.0.1-preview.39 136 1/28/2022
1.0.1-preview.37 132 1/28/2022
1.0.1-preview.35 127 1/28/2022
1.0.1-preview.33 130 1/28/2022
1.0.1-preview.31 135 1/27/2022
1.0.1-preview.29 131 1/25/2022
1.0.1-preview.27 122 1/22/2022
1.0.1-preview.25 125 1/21/2022
1.0.1-preview.24 128 4/16/2022
1.0.1-preview.23 128 1/21/2022
1.0.1-preview.22 121 4/15/2022
1.0.1-preview.21 130 1/21/2022
1.0.1-preview.20 119 4/6/2022
1.0.1-preview.19 130 1/21/2022
1.0.1-preview.18 113 4/3/2022
1.0.1-preview.17 133 1/20/2022
1.0.1-preview.16 110 3/31/2022
1.0.1-preview.15 132 1/17/2022
1.0.1-preview.13 131 1/11/2022
1.0.1-preview.10 170 11/10/2021