nanoFramework.MagicBit 1.2.162

Prefix Reserved
dotnet add package nanoFramework.MagicBit --version 1.2.162                
NuGet\Install-Package nanoFramework.MagicBit -Version 1.2.162                
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.162" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.MagicBit --version 1.2.162                
#r "nuget: nanoFramework.MagicBit, 1.2.162"                
#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.162

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

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.162 0 12/26/2024
1.2.161 72 12/19/2024
1.2.159 78 12/5/2024
1.2.158 89 10/24/2024
1.2.157 81 10/21/2024
1.2.156 80 10/7/2024
1.2.155 89 9/30/2024
1.2.154 75 9/9/2024
1.2.153 108 9/5/2024
1.2.152 95 8/29/2024
1.2.151 110 8/26/2024
1.2.150 120 8/12/2024
1.2.149 89 8/8/2024
1.2.148 54 7/29/2024
1.2.147 96 7/22/2024
1.2.146 107 7/18/2024
1.2.145 103 7/15/2024
1.2.144 108 7/11/2024
1.2.142 115 6/20/2024
1.2.140 92 6/17/2024
1.2.137 114 5/30/2024
1.2.135 118 5/20/2024
1.2.133 118 5/16/2024
1.2.130 127 4/18/2024
1.2.128 115 4/8/2024
1.2.126 120 3/25/2024
1.2.124 133 3/18/2024
1.2.122 120 3/14/2024
1.2.120 125 2/29/2024
1.2.118 102 2/26/2024
1.2.116 132 1/26/2024
1.2.103 223 11/13/2023
1.2.101 117 11/9/2023
1.2.98 118 11/9/2023
1.2.96 165 10/9/2023
1.2.94 161 10/5/2023
1.2.92 143 9/28/2023
1.2.90 140 9/18/2023
1.2.88 146 9/7/2023
1.2.86 167 9/4/2023
1.2.84 154 8/17/2023
1.2.82 155 8/14/2023
1.2.80 182 8/3/2023
1.2.78 169 7/31/2023
1.2.76 164 7/27/2023
1.2.74 139 7/24/2023
1.2.72 184 7/20/2023
1.2.70 172 7/17/2023
1.2.68 164 7/13/2023
1.2.66 187 6/22/2023
1.2.64 161 6/19/2023
1.2.62 149 6/15/2023
1.2.60 182 6/12/2023
1.2.58 219 6/8/2023
1.2.56 155 6/5/2023
1.2.54 177 5/29/2023
1.2.52 174 5/25/2023
1.2.50 187 5/22/2023
1.2.46 199 5/15/2023
1.2.44 199 5/11/2023
1.2.42 161 5/8/2023
1.2.40 227 4/3/2023
1.2.38 236 3/27/2023
1.2.36 240 3/23/2023
1.2.34 241 3/20/2023
1.2.32 268 3/16/2023
1.2.30 255 3/13/2023
1.2.28 291 3/9/2023
1.2.26 253 3/2/2023
1.2.24 291 2/27/2023
1.2.22 272 2/23/2023
1.2.20 259 2/20/2023
1.2.18 300 2/16/2023
1.2.16 320 2/6/2023
1.2.13 357 1/16/2023
1.2.2.16170 338 12/25/2022
1.1.0.96 412 11/15/2022
1.1.0.94 406 11/14/2022
1.1.0.92 408 11/6/2022
1.1.0.90 401 11/4/2022
1.1.0.88 395 11/3/2022
1.1.0.86 395 11/1/2022
1.1.0.84 446 10/27/2022
1.1.0.82 464 10/26/2022
1.1.0.80 435 10/25/2022
1.1.0.78 470 10/24/2022
1.1.0.76 463 10/23/2022
1.1.0.74 453 10/22/2022
1.1.0.72 469 10/13/2022
1.1.0.70 458 10/12/2022
1.1.0.68 451 10/9/2022
1.1.0.65 498 9/23/2022
1.1.0.63 496 9/22/2022
1.1.0.61 512 9/16/2022
1.1.0.59 531 9/15/2022
1.1.0.57 494 9/9/2022
1.1.0.55 450 9/8/2022
1.1.0.53 448 9/5/2022
1.1.0.51 495 8/16/2022
1.1.0.49 480 8/15/2022
1.1.0.47 493 8/7/2022
1.1.0.45 467 8/6/2022
1.1.0.43 474 8/5/2022
1.1.0.41 469 8/4/2022
1.1.0.39 473 8/3/2022
1.1.0.37 468 8/2/2022
1.1.0.34 490 7/25/2022
1.1.0.32 471 7/24/2022
1.1.0.30 501 7/23/2022
1.1.0.28 507 7/10/2022
1.1.0.26 510 7/9/2022
1.1.0.24 503 7/8/2022
1.1.0.22 496 7/1/2022
1.1.0.20 496 6/30/2022
1.1.0.18 508 6/29/2022
1.1.0.16 531 6/27/2022
1.1.0.14 490 6/25/2022
1.1.0.12 489 6/24/2022
1.1.0.10 478 6/16/2022
1.1.0.8 476 6/15/2022
1.1.0.6 468 6/14/2022
1.1.0.4 480 6/1/2022
1.0.1-preview.101 124 3/26/2022
1.0.1-preview.99 124 3/25/2022
1.0.1-preview.97 115 3/22/2022
1.0.1-preview.95 125 3/21/2022
1.0.1-preview.93 128 3/20/2022
1.0.1-preview.91 127 3/18/2022
1.0.1-preview.89 132 3/18/2022
1.0.1-preview.87 132 3/17/2022
1.0.1-preview.85 126 3/16/2022
1.0.1-preview.83 122 3/14/2022
1.0.1-preview.81 125 3/14/2022
1.0.1-preview.79 131 3/12/2022
1.0.1-preview.77 121 3/9/2022
1.0.1-preview.75 131 3/7/2022
1.0.1-preview.73 130 3/4/2022
1.0.1-preview.71 127 3/3/2022
1.0.1-preview.69 134 2/28/2022
1.0.1-preview.67 124 2/27/2022
1.0.1-preview.65 132 2/26/2022
1.0.1-preview.63 127 2/19/2022
1.0.1-preview.61 121 2/18/2022
1.0.1-preview.57 133 2/17/2022
1.0.1-preview.55 135 2/16/2022
1.0.1-preview.53 124 2/13/2022
1.0.1-preview.51 132 2/12/2022
1.0.1-preview.49 127 2/11/2022
1.0.1-preview.47 129 2/10/2022
1.0.1-preview.45 121 2/9/2022
1.0.1-preview.43 144 2/5/2022
1.0.1-preview.41 145 2/1/2022
1.0.1-preview.39 148 1/28/2022
1.0.1-preview.37 144 1/28/2022
1.0.1-preview.35 138 1/28/2022
1.0.1-preview.33 143 1/28/2022
1.0.1-preview.31 146 1/27/2022
1.0.1-preview.29 142 1/25/2022
1.0.1-preview.27 133 1/22/2022
1.0.1-preview.25 134 1/21/2022
1.0.1-preview.24 143 4/16/2022
1.0.1-preview.23 138 1/21/2022
1.0.1-preview.22 135 4/15/2022
1.0.1-preview.21 141 1/21/2022
1.0.1-preview.20 130 4/6/2022
1.0.1-preview.19 140 1/21/2022
1.0.1-preview.18 127 4/3/2022
1.0.1-preview.17 144 1/20/2022
1.0.1-preview.16 123 3/31/2022
1.0.1-preview.15 143 1/17/2022
1.0.1-preview.13 142 1/11/2022
1.0.1-preview.10 181 11/10/2021