{"id":19244369,"url":"https://github.com/nanoframework/nanoframework.magicbit","last_synced_at":"2025-04-21T09:33:35.167Z","repository":{"id":37834127,"uuid":"424979165","full_name":"nanoframework/nanoFramework.MagicBit","owner":"nanoframework","description":":package: Board package for MagicBit with .NET nanoFramework","archived":false,"fork":false,"pushed_at":"2025-04-10T01:17:25.000Z","size":502,"stargazers_count":6,"open_issues_count":0,"forks_count":3,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-19T17:55:31.261Z","etag":null,"topics":["chsarp","esp32","hacktoberfest","library","nanoframework"],"latest_commit_sha":null,"homepage":"https://www.nanoframework.net","language":"C#","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nanoframework.png","metadata":{"funding":{"open_collective":"nanoframework","github":"nanoframework"},"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-11-05T14:36:11.000Z","updated_at":"2025-04-10T01:12:05.000Z","dependencies_parsed_at":"2022-06-23T02:19:30.108Z","dependency_job_id":"758cf13f-e57c-427b-8d3e-b0f6e2ad6701","html_url":"https://github.com/nanoframework/nanoFramework.MagicBit","commit_stats":{"total_commits":291,"total_committers":5,"mean_commits":58.2,"dds":"0.24742268041237114","last_synced_commit":"4bb941f2a5f1e83d6504810c6963e5ab8e429981"},"previous_names":[],"tags_count":195,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FnanoFramework.MagicBit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FnanoFramework.MagicBit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FnanoFramework.MagicBit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FnanoFramework.MagicBit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nanoframework","download_url":"https://codeload.github.com/nanoframework/nanoFramework.MagicBit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250032480,"owners_count":21363845,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chsarp","esp32","hacktoberfest","library","nanoframework"],"created_at":"2024-11-09T17:23:07.343Z","updated_at":"2025-04-21T09:33:35.147Z","avatar_url":"https://github.com/nanoframework.png","language":"C#","funding_links":["https://opencollective.com/nanoframework","https://github.com/sponsors/nanoframework"],"categories":[],"sub_categories":[],"readme":"[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_nanoFramework.MagicBit\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=nanoframework_nanoFramework.MagicBit) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_nanoFramework.MagicBit\u0026metric=reliability_rating)](https://sonarcloud.io/dashboard?id=nanoframework_nanoFramework.MagicBit) [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![NuGet](https://img.shields.io/nuget/dt/nanoFramework.MagicBit.svg?label=NuGet\u0026style=flat\u0026logo=nuget)](https://www.nuget.org/packages/nanoFramework.MagicBit/) [![#yourfirstpr](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](https://github.com/nanoframework/Home/blob/main/CONTRIBUTING.md) [![Discord](https://img.shields.io/discord/478725473862549535.svg?logo=discord\u0026logoColor=white\u0026label=Discord\u0026color=7289DA)](https://discord.gg/gCyBu8T)\n\n![nanoFramework logo](https://raw.githubusercontent.com/nanoframework/Home/main/resources/logo/nanoFramework-repo-logo.png)\n\n-----\n\n# Welcome to the .NET **nanoFramework** MagicBit Library repository\n\n## Build status\n\n| Component | Build Status | NuGet Package |\n|:---|---|---|\n| MagicBit | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.MagicBit/_apis/build/status/nanoFramework.MagicBit?repoName=nanoframework%2FnanoFramework.MagicBit\u0026branchName=main)](https://dev.azure.com/nanoframework/nanoFramework.MagicBit/_build/latest?definitionId=84\u0026repoName=nanoframework%2FnanoFramework.MagicBit\u0026branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.MagicBit.svg?label=NuGet\u0026style=flat\u0026logo=nuget)](https://www.nuget.org/packages/nanoFramework.MagicBit/) |\n\n## Usage\n\nThis nuget can be used with the great [MagicBit](https://magicbit.cc/) board.\n\nIt 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](https://github.com/nanoframework/nanoFramework.IoT.Device). See the [known limitations](#known-limitations) as well.\n\nYou just need to make sure your MagicBit is flashed like this:\n\n```shell\n# Replace the com port number by your COM port\nnanoff --platform esp32 --update --preview --serialport COM3\n```\n\nA detailed example is available in the [Test application](Tests/MagicBitTestApp/Program.cs) as well.\n\n### Screen\n\nThe only thing you need to do to access the screen is to initialize it:\n\n```csharp\nMagicBit.InitializeScreen();\n```\n\nOnce you've initialized it, you can access both a `Screen` static class and a `Console` static class.\n\nThe `Screen` one brings primitives to write directly on the screen points, select colors as well as writing a text.\n\nFor example, you can write a small buffer square of 8x8 at the position 0, 26 with a width of 8 like this:\n\n```csharp\nbyte[] _heart = new byte[] {\n            0b0100_0010,\n            0b0110_0110,\n            0b1111_1111,\n            0b1111_1111,\n            0b1111_1111,\n            0b0011_1100,\n            0b0011_1100,\n            0b0001_1000,\n        };\nScreen.DrawBitmap(0, 26, 8, _heart);\n```\n\nNote 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. \n\nThe screen provides as well other primitives, here is a quick example:\n\n```csharp\n// You can use the Screen primitives like this:\nScreen.Clear();\nScreen.Write(2, 0, \"MagicBit\", 2);\nScreen.Write(2, 26, \"loves .NET\", 1, true);\nScreen.Write(2, 40, \"nanoFramework\", 1, true);\nScreen.Write(2, 50, \"Clk right button\", 1, false);\nScreen.DrawBitmap(0, 26, 8, _heart);\nScreen.DrawBitmap(Screen.Width - 9, 26, 8, _heart);\n// You should make sure that you call Display\nScreen.Display();\n```\n\nAnd you will get even more thru `Screen.Device`.\n\nThe Console class works in a similar way as the classic `System.Console`:\n\n```csharp\nConsole.Clear();\nConsole.WriteLine(\"Motors\");\nConsole.CursorTop = 2;\nConsole.WriteLine(\"Motors will run reverse then rotate both direction\");\n```\n\n\u003e 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.\n\n### Buttons\n\nThe 2 buttons are exposed.\n\nThey are called `ButtonLeft` and `ButtonRight`. You can get access to the events as well. For example:\n\n```csharp\nMagicBit.ButtonLeft.Press += (sender, e) =\u003e\n{\n    Console.CursorLeft = 0;\n    Console.CursorTop = 0;\n    Console.Write($\"Left Pressed  \");\n};\n\n// Simple way of getting the button status\nwhile (!MagicBit.ButtonRight.IsPressed)\n{\n    Thread.Sleep(20);\n}\n```\n\nAnother sample with events:\n\n\n```csharp\nMagicBit.ButtonRight.IsHoldingEnabled = true;\nMagicBit.ButtonRight.Holding += (sender, e) =\u003e\n{\n    Console.Write(\"ButtonRight hold long.\");\n};\n```\n\n### Motors\n\nThe 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.\n\n```csharp\nConsole.Clear();\nConsole.WriteLine(\"Motors\");\nConsole.CursorTop = 2;\nConsole.WriteLine(\"Motors will run reverse then rotate both direction\");\nvar motor1 = MagicBit.Motor1;\nvar motor2 = MagicBit.Motor2;\nmotor1.Speed = -0.5;\nmotor2.Speed = -0.5;\nThread.Sleep(2000);\nmotor1.Speed = +0.5;\nmotor2.Speed = -0.5;\nThread.Sleep(2000);\nmotor1.Speed = -0.5;\nmotor2.Speed = +0.5;\nThread.Sleep(2000);\nmotor1.Speed = 0;\nmotor2.Speed = 0;\n```\n\n### Buzzer\n\nIt's of course possible to use the buzzer. Here is an example playing tones:\n\n```csharp\nvar buzz = MagicBit.Buzzer;\nfor (int i = 0; i \u003c 10; i++)\n{\n    buzz.PlayTone(500 + i * 25, 1000);\n}\n```\n\n### Potentiometer and Luminosity\n\nThose 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:\n\n```csharp\nCancellationTokenSource cancellationTokenSource = new CancellationTokenSource();\nMagicBit.ButtonLeft.Press += (sender, e) =\u003e\n{\n    cancellationTokenSource.Cancel();\n};\n\nConsole.Clear();\nConsole.WriteLine(\"Sensors\");\nConsole.CursorTop = 2;\nConsole.WriteLine(\"Clk left button to stop\");\n\nwhile (!cancellationTokenSource.Token.IsCancellationRequested)\n{\n    // Read the potentiometer ratio, from 0.0 to 1.0\n    var ratio = MagicBit.Potentiometer.ReadRatio();\n    // Read the luminosity sensor ratio from 0.0 (full dark) to 1.0 (full light)\n    var lumi = MagicBit.Luminosity.ReadRatio();\n    Console.CursorTop = 4;\n    Console.CursorLeft = 0;\n    Console.WriteLine($\"Pot: {ratio * 100:N2}%  \");\n    Console.CursorTop = 5;\n    Console.CursorLeft = 0;\n    Console.WriteLine($\"lum: {lumi * 100:N2}%  \");\n    cancellationTokenSource.Token.WaitHandle.WaitOne(200, true);\n}\n```\n\n### Servo motor\n\nServo 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:\n\n```csharp\nCancellationTokenSource cancellationTokenSource = new CancellationTokenSource();\nMagicBit.ButtonLeft.Press += (sender, e) =\u003e\n{\n    cancellationTokenSource.Cancel();\n};\n\nConsole.Clear();\nConsole.WriteLine(\"Servo\");\nConsole.CursorTop = 2;\nConsole.WriteLine(\"Clk left button to stop\");\n\n// The servo can be different and you can adjust the values as needed\nvar servo = MagicBit.GetServoPinBlue(180, 500, 2400);\n// This is needed to start the servo\nservo.Start();\nint angle = 0;\nwhile (!cancellationTokenSource.Token.IsCancellationRequested)\n{\n    servo.WriteAngle(angle);\n    angle = angle == 0 ? 180 : 0;\n    Console.CursorTop = 4;\n    Console.CursorLeft = 0;\n    Console.WriteLine($\"Angle: {angle}deg  \");\n    cancellationTokenSource.Token.WaitHandle.WaitOne(3000, true);\n}\n\n// Don't forget to stop it at the end.\nservo.Stop();\n```\n\n\u003e 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.\n\n### Leds\n\nThe 4 embedded leds are available.\n\n```csharp\nMagicBit.LedBlue.Write(PinValue.High);\n```\n\n\u003e Important: You **cannot** use them with the motors as they are using the same pins.\n\n### I2C Device\n\nBy 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:\n\n```csharp\nvar myI2cDevice = MagicBit.GetI2cDevice(0x42);\n```\n\n### Black left and right pins\n\nYou 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:\n\n```csharp\n// This will create an output mode pint, you can for example attach a led\nvar myPin = MagicBit.GetPinBlackLeft(PinMode.Output);\n// This will change the pin to high\nmyPin.Write(PinValue.High);\n```\n\n### Blue pin\n\nThe blue pin is setup by default to be used as PWM. When getting it, you'll get a PWM Channel:\n\n```csharp\nvar myPwm = MagicBit.GetPinBlue();\n```\n\n### Changing default pin behavior\n\nThis 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:\n\n```csharp\nConfiguration.SetPinFunction(32, DeviceFunction.PWM11);\nvar myBlackPwm = PwmChannel.CreateFromPin(32);\n```\n\nEven 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:\n\n```csharp\nvar myBluePin = MagicBit.GpioController.Open(26, PinMode.Input);\n```\n\n## Known limitations\n\nThere are few sensors that will not work, see the list below, the reasons and possible mitigation:\n\n- 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](https://github.com/nanoframework/nanoFramework.IoT.Device/tree/develop/devices#thermometers).\n- 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.\n- 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.\n\n## Feedback and documentation\n\nFor documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).\n\nJoin our Discord community [here](https://discord.gg/gCyBu8T).\n\n## Credits\n\nThe list of contributors to this project can be found at [CONTRIBUTORS](https://github.com/nanoframework/Home/blob/main/CONTRIBUTORS.md).\n\n## License\n\nThe **nanoFramework** Class Libraries are licensed under the [MIT license](LICENSE.md).\n\n## Code of Conduct\n\nThis project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community.\nFor more information see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).\n\n## .NET Foundation\n\nThis project is supported by the [.NET Foundation](https://dotnetfoundation.org).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanoframework%2Fnanoframework.magicbit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnanoframework%2Fnanoframework.magicbit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanoframework%2Fnanoframework.magicbit/lists"}