nanoFramework.MagicBit 1.2.148

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

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

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 82 10/24/2024
1.2.157 75 10/21/2024
1.2.156 72 10/7/2024
1.2.155 83 9/30/2024
1.2.154 67 9/9/2024
1.2.153 102 9/5/2024
1.2.152 89 8/29/2024
1.2.151 102 8/26/2024
1.2.150 114 8/12/2024
1.2.149 81 8/8/2024
1.2.148 48 7/29/2024
1.2.147 88 7/22/2024
1.2.146 96 7/18/2024
1.2.145 95 7/15/2024
1.2.144 100 7/11/2024
1.2.142 109 6/20/2024
1.2.140 86 6/17/2024
1.2.137 108 5/30/2024
1.2.135 112 5/20/2024
1.2.133 110 5/16/2024
1.2.130 123 4/18/2024
1.2.128 111 4/8/2024
1.2.126 116 3/25/2024
1.2.124 129 3/18/2024
1.2.122 114 3/14/2024
1.2.120 121 2/29/2024
1.2.118 98 2/26/2024
1.2.116 128 1/26/2024
1.2.103 219 11/13/2023
1.2.101 113 11/9/2023
1.2.98 114 11/9/2023
1.2.96 161 10/9/2023
1.2.94 157 10/5/2023
1.2.92 139 9/28/2023
1.2.90 134 9/18/2023
1.2.88 142 9/7/2023
1.2.86 163 9/4/2023
1.2.84 150 8/17/2023
1.2.82 151 8/14/2023
1.2.80 178 8/3/2023
1.2.78 165 7/31/2023
1.2.76 160 7/27/2023
1.2.74 135 7/24/2023
1.2.72 180 7/20/2023
1.2.70 168 7/17/2023
1.2.68 160 7/13/2023
1.2.66 183 6/22/2023
1.2.64 157 6/19/2023
1.2.62 145 6/15/2023
1.2.60 178 6/12/2023
1.2.58 215 6/8/2023
1.2.56 151 6/5/2023
1.2.54 173 5/29/2023
1.2.52 170 5/25/2023
1.2.50 183 5/22/2023
1.2.46 195 5/15/2023
1.2.44 195 5/11/2023
1.2.42 157 5/8/2023
1.2.40 223 4/3/2023
1.2.38 232 3/27/2023
1.2.36 236 3/23/2023
1.2.34 237 3/20/2023
1.2.32 264 3/16/2023
1.2.30 251 3/13/2023
1.2.28 287 3/9/2023
1.2.26 249 3/2/2023
1.2.24 287 2/27/2023
1.2.22 266 2/23/2023
1.2.20 255 2/20/2023
1.2.18 296 2/16/2023
1.2.16 316 2/6/2023
1.2.13 353 1/16/2023
1.2.2.16170 334 12/25/2022
1.1.0.96 406 11/15/2022
1.1.0.94 402 11/14/2022
1.1.0.92 404 11/6/2022
1.1.0.90 397 11/4/2022
1.1.0.88 389 11/3/2022
1.1.0.86 389 11/1/2022
1.1.0.84 440 10/27/2022
1.1.0.82 460 10/26/2022
1.1.0.80 431 10/25/2022
1.1.0.78 466 10/24/2022
1.1.0.76 459 10/23/2022
1.1.0.74 449 10/22/2022
1.1.0.72 465 10/13/2022
1.1.0.70 452 10/12/2022
1.1.0.68 447 10/9/2022
1.1.0.65 494 9/23/2022
1.1.0.63 490 9/22/2022
1.1.0.61 508 9/16/2022
1.1.0.59 525 9/15/2022
1.1.0.57 488 9/9/2022
1.1.0.55 446 9/8/2022
1.1.0.53 444 9/5/2022
1.1.0.51 491 8/16/2022
1.1.0.49 470 8/15/2022
1.1.0.47 487 8/7/2022
1.1.0.45 463 8/6/2022
1.1.0.43 470 8/5/2022
1.1.0.41 463 8/4/2022
1.1.0.39 469 8/3/2022
1.1.0.37 464 8/2/2022
1.1.0.34 486 7/25/2022
1.1.0.32 467 7/24/2022
1.1.0.30 493 7/23/2022
1.1.0.28 503 7/10/2022
1.1.0.26 506 7/9/2022
1.1.0.24 499 7/8/2022
1.1.0.22 488 7/1/2022
1.1.0.20 490 6/30/2022
1.1.0.18 504 6/29/2022
1.1.0.16 525 6/27/2022
1.1.0.14 486 6/25/2022
1.1.0.12 485 6/24/2022
1.1.0.10 474 6/16/2022
1.1.0.8 472 6/15/2022
1.1.0.6 464 6/14/2022
1.1.0.4 474 6/1/2022
1.0.1-preview.101 122 3/26/2022
1.0.1-preview.99 122 3/25/2022
1.0.1-preview.97 113 3/22/2022
1.0.1-preview.95 123 3/21/2022
1.0.1-preview.93 126 3/20/2022
1.0.1-preview.91 125 3/18/2022
1.0.1-preview.89 130 3/18/2022
1.0.1-preview.87 130 3/17/2022
1.0.1-preview.85 124 3/16/2022
1.0.1-preview.83 120 3/14/2022
1.0.1-preview.81 123 3/14/2022
1.0.1-preview.79 127 3/12/2022
1.0.1-preview.77 119 3/9/2022
1.0.1-preview.75 129 3/7/2022
1.0.1-preview.73 128 3/4/2022
1.0.1-preview.71 125 3/3/2022
1.0.1-preview.69 130 2/28/2022
1.0.1-preview.67 122 2/27/2022
1.0.1-preview.65 130 2/26/2022
1.0.1-preview.63 125 2/19/2022
1.0.1-preview.61 119 2/18/2022
1.0.1-preview.57 131 2/17/2022
1.0.1-preview.55 133 2/16/2022
1.0.1-preview.53 122 2/13/2022
1.0.1-preview.51 126 2/12/2022
1.0.1-preview.49 125 2/11/2022
1.0.1-preview.47 127 2/10/2022
1.0.1-preview.45 119 2/9/2022
1.0.1-preview.43 142 2/5/2022
1.0.1-preview.41 143 2/1/2022
1.0.1-preview.39 146 1/28/2022
1.0.1-preview.37 142 1/28/2022
1.0.1-preview.35 136 1/28/2022
1.0.1-preview.33 141 1/28/2022
1.0.1-preview.31 144 1/27/2022
1.0.1-preview.29 140 1/25/2022
1.0.1-preview.27 131 1/22/2022
1.0.1-preview.25 132 1/21/2022
1.0.1-preview.24 141 4/16/2022
1.0.1-preview.23 136 1/21/2022
1.0.1-preview.22 133 4/15/2022
1.0.1-preview.21 139 1/21/2022
1.0.1-preview.20 128 4/6/2022
1.0.1-preview.19 138 1/21/2022
1.0.1-preview.18 125 4/3/2022
1.0.1-preview.17 142 1/20/2022
1.0.1-preview.16 121 3/31/2022
1.0.1-preview.15 141 1/17/2022
1.0.1-preview.13 140 1/11/2022
1.0.1-preview.10 179 11/10/2021