nanoFramework.MagicBit 1.2.2.16170

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoFramework.MagicBit --version 1.2.2.16170                
NuGet\Install-Package nanoFramework.MagicBit -Version 1.2.2.16170                
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.2.16170" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.MagicBit --version 1.2.2.16170                
#r "nuget: nanoFramework.MagicBit, 1.2.2.16170"                
#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.2.16170

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

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.158 77 10/24/2024
1.2.157 72 10/21/2024
1.2.156 69 10/7/2024
1.2.155 82 9/30/2024
1.2.154 66 9/9/2024
1.2.153 101 9/5/2024
1.2.152 88 8/29/2024
1.2.151 101 8/26/2024
1.2.150 113 8/12/2024
1.2.149 80 8/8/2024
1.2.148 47 7/29/2024
1.2.147 87 7/22/2024
1.2.146 95 7/18/2024
1.2.145 94 7/15/2024
1.2.144 99 7/11/2024
1.2.142 108 6/20/2024
1.2.140 85 6/17/2024
1.2.137 107 5/30/2024
1.2.135 111 5/20/2024
1.2.133 109 5/16/2024
1.2.130 122 4/18/2024
1.2.128 108 4/8/2024
1.2.126 115 3/25/2024
1.2.124 126 3/18/2024
1.2.122 113 3/14/2024
1.2.120 120 2/29/2024
1.2.118 97 2/26/2024
1.2.116 127 1/26/2024
1.2.103 218 11/13/2023
1.2.101 112 11/9/2023
1.2.98 113 11/9/2023
1.2.96 160 10/9/2023
1.2.94 156 10/5/2023
1.2.92 138 9/28/2023
1.2.90 133 9/18/2023
1.2.88 139 9/7/2023
1.2.86 162 9/4/2023
1.2.84 149 8/17/2023
1.2.82 150 8/14/2023
1.2.80 177 8/3/2023
1.2.78 164 7/31/2023
1.2.76 159 7/27/2023
1.2.74 134 7/24/2023
1.2.72 179 7/20/2023
1.2.70 167 7/17/2023
1.2.68 159 7/13/2023
1.2.66 182 6/22/2023
1.2.64 156 6/19/2023
1.2.62 144 6/15/2023
1.2.60 177 6/12/2023
1.2.58 214 6/8/2023
1.2.56 150 6/5/2023
1.2.54 170 5/29/2023
1.2.52 169 5/25/2023
1.2.50 182 5/22/2023
1.2.46 194 5/15/2023
1.2.44 194 5/11/2023
1.2.42 156 5/8/2023
1.2.40 222 4/3/2023
1.2.38 231 3/27/2023
1.2.36 235 3/23/2023
1.2.34 236 3/20/2023
1.2.32 263 3/16/2023
1.2.30 248 3/13/2023
1.2.28 286 3/9/2023
1.2.26 248 3/2/2023
1.2.24 286 2/27/2023
1.2.22 265 2/23/2023
1.2.20 254 2/20/2023
1.2.18 295 2/16/2023
1.2.16 315 2/6/2023
1.2.13 352 1/16/2023
1.2.2.16170 333 12/25/2022
1.1.0.96 405 11/15/2022
1.1.0.94 401 11/14/2022
1.1.0.92 403 11/6/2022
1.1.0.90 396 11/4/2022
1.1.0.88 388 11/3/2022
1.1.0.86 388 11/1/2022
1.1.0.84 439 10/27/2022
1.1.0.82 459 10/26/2022
1.1.0.80 430 10/25/2022
1.1.0.78 465 10/24/2022
1.1.0.76 458 10/23/2022
1.1.0.74 448 10/22/2022
1.1.0.72 464 10/13/2022
1.1.0.70 451 10/12/2022
1.1.0.68 446 10/9/2022
1.1.0.65 493 9/23/2022
1.1.0.63 489 9/22/2022
1.1.0.61 507 9/16/2022
1.1.0.59 524 9/15/2022
1.1.0.57 487 9/9/2022
1.1.0.55 445 9/8/2022
1.1.0.53 443 9/5/2022
1.1.0.51 490 8/16/2022
1.1.0.49 469 8/15/2022
1.1.0.47 486 8/7/2022
1.1.0.45 462 8/6/2022
1.1.0.43 469 8/5/2022
1.1.0.41 462 8/4/2022
1.1.0.39 468 8/3/2022
1.1.0.37 463 8/2/2022
1.1.0.34 485 7/25/2022
1.1.0.32 466 7/24/2022
1.1.0.30 492 7/23/2022
1.1.0.28 502 7/10/2022
1.1.0.26 505 7/9/2022
1.1.0.24 498 7/8/2022
1.1.0.22 487 7/1/2022
1.1.0.20 489 6/30/2022
1.1.0.18 503 6/29/2022
1.1.0.16 524 6/27/2022
1.1.0.14 485 6/25/2022
1.1.0.12 484 6/24/2022
1.1.0.10 473 6/16/2022
1.1.0.8 471 6/15/2022
1.1.0.6 463 6/14/2022
1.1.0.4 473 6/1/2022
1.0.1-preview.101 121 3/26/2022
1.0.1-preview.99 121 3/25/2022
1.0.1-preview.97 112 3/22/2022
1.0.1-preview.95 120 3/21/2022
1.0.1-preview.93 125 3/20/2022
1.0.1-preview.91 124 3/18/2022
1.0.1-preview.89 129 3/18/2022
1.0.1-preview.87 129 3/17/2022
1.0.1-preview.85 123 3/16/2022
1.0.1-preview.83 119 3/14/2022
1.0.1-preview.81 122 3/14/2022
1.0.1-preview.79 126 3/12/2022
1.0.1-preview.77 118 3/9/2022
1.0.1-preview.75 128 3/7/2022
1.0.1-preview.73 127 3/4/2022
1.0.1-preview.71 122 3/3/2022
1.0.1-preview.69 129 2/28/2022
1.0.1-preview.67 121 2/27/2022
1.0.1-preview.65 129 2/26/2022
1.0.1-preview.63 124 2/19/2022
1.0.1-preview.61 118 2/18/2022
1.0.1-preview.57 130 2/17/2022
1.0.1-preview.55 132 2/16/2022
1.0.1-preview.53 121 2/13/2022
1.0.1-preview.51 125 2/12/2022
1.0.1-preview.49 124 2/11/2022
1.0.1-preview.47 126 2/10/2022
1.0.1-preview.45 118 2/9/2022
1.0.1-preview.43 141 2/5/2022
1.0.1-preview.41 142 2/1/2022
1.0.1-preview.39 145 1/28/2022
1.0.1-preview.37 141 1/28/2022
1.0.1-preview.35 135 1/28/2022
1.0.1-preview.33 140 1/28/2022
1.0.1-preview.31 143 1/27/2022
1.0.1-preview.29 139 1/25/2022
1.0.1-preview.27 130 1/22/2022
1.0.1-preview.25 131 1/21/2022
1.0.1-preview.24 140 4/16/2022
1.0.1-preview.23 135 1/21/2022
1.0.1-preview.22 132 4/15/2022
1.0.1-preview.21 138 1/21/2022
1.0.1-preview.20 127 4/6/2022
1.0.1-preview.19 137 1/21/2022
1.0.1-preview.18 124 4/3/2022
1.0.1-preview.17 141 1/20/2022
1.0.1-preview.16 120 3/31/2022
1.0.1-preview.15 140 1/17/2022
1.0.1-preview.13 139 1/11/2022
1.0.1-preview.10 178 11/10/2021