Skip to content

Commit 3f7f822

Browse files
authored
Adding documentation (#31)
1 parent ad489d5 commit 3f7f822

File tree

5 files changed

+150
-4
lines changed

5 files changed

+150
-4
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
This repository contains bindings which can be sensors, small screen and anything else that you can connect to your nanoFramework chip!
1212

13+
Most of the bindings have been migrated from [.NET IoT repository](https://github.com/dotnet/iot/tree/main/src/devices). Not all the bindings make sense to migrate to .NET nanoFramework, so the effort of migration has been placed into devices that can work with .NET nanoFramework. Please note as well that some devices have been migrated without been tested, so they main contain problems.
1314

1415
## Folder Structure
1516

@@ -25,6 +26,32 @@ Other folders in [/src](/src) contain nanoFramework projects that you can refere
2526

2627
**Important:** If you plan to clean up the code in [/src/devices_generated/](/src/devices_generated/), please copy your work to the [/src/devices/](/src/devices/) folder as the content of [/src/devices_generated/](/src/devices_generated/) will be overwritten by the generator tool.
2728

29+
Please check the [detail list of tips and tricks](./tips-tricks.md) to facilitate the migration. The generator takes care of some heavy lifting but there is always some manual adjustments needed.
30+
31+
We are using the following structure for the bindings:
32+
33+
```text
34+
/devices
35+
/Binding1
36+
/samples
37+
Binding1.Samples.nfproj
38+
AssicateFile.cs
39+
Program.cs
40+
/test
41+
BindingA.Test.nfproj
42+
AssociatedTestFile.cs
43+
Binding1.nfproj
44+
Binding1.nuspec
45+
version.json
46+
OtherFiles.cs
47+
OtherFiles.anythingelse
48+
Readme.md
49+
```
50+
51+
## Using the Code Converter
52+
53+
The Code Converter allows to facilitate migration of .NET Core/.NET 5.0 code into .NET nanoFramework. More information and how to [customize and run it here](./src/nanoFramework.IoT.Device.CodeConverter/README.md).
54+
2855
## Feedback and documentation
2956

3057
For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).

src/nanoFramework.IoT.Device.CodeConverter/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ static void Main(string[] args)
116116
{ "Console.WriteLine(", "Debug.WriteLine(" },
117117
{ "using System.Diagnostics.CodeAnalysis;", "" },
118118
{ "\\[MemberNotNull.*\\]", "" },
119+
{ "Environment.TickCount", "DateTime.UtcNow.Ticks" },
119120
};
120121

121122
// All generics go to the main project of the binding to avoid conflicts between the projects.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Using nanoFramework Code Converter
2+
3+
This converter is mainly design to help the migration from .NET IoT repository. That said, it can be used for any code transformation from .NET Core/.NET 5.0 to .NET nanoFramework.
4+
5+
The tools create a solution, transform the `csproj` file to a `nfproj` file, find and add automatically some of the nugets, including mscorlib, add reference to shared projects. It does as well generated static class file from a generic `List<Something>` to `ListSomething` so it can be used in nanoFramework as generics are not (yet) supported.
6+
7+
## Running the Code Converter
8+
9+
You can adjust the different paths into the `appsettings.json` file:
10+
11+
```json
12+
{
13+
"SourceDirectory": "e:\\GitHub\\DotNet-iot\\src\\devices",
14+
"FilePathFilters": "src\\devices\\",
15+
"GenericsTemplatesFolderName": "GenericsTemplates",
16+
"TargetProjectTemplateName": "BindingTemplateProject",
17+
"TargetUnitTestProjectTemplateName": "UnitTestTemplateProject",
18+
"OutputDirectoryPath": "..\\..\\..\\..\\devices_generated"
19+
}
20+
```
21+
22+
- The `SourceDirectory` is where you have the project you want to transform and the `FilePathFilters` where you want the generation.
23+
- `GenericsTemplatesFolderName` contains the generic templates you want to be analyzed and transformed.
24+
- `TargetProjectTemplateName` contains the target project templates, for libraries and executable.
25+
- `TargetUnitTestProjectTemplateName` contains the target project for tests.
26+
- `OutputDirectoryPath` contains the output directory where you want the projects to be generated.
27+
28+
Then run the tool either from the solution either from the command line. Be aware that paths are relative to where the execution is happening.
29+
30+
## Customizing the generator
31+
32+
You can include different shared projects, those are declared into [`SharedProjectImports.cs`](./SharedProjectImports.cs).
33+
34+
## Customizing the nuget packages
35+
36+
You can include different nuget packages, those are declared into [`NfNugetPackages.cs`](./NfNugetPackages.cs).

src/nanoFramework.IoT.Device.CodeConverter/SharedProjectImports.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,33 @@ public static SharedProjectImport[] GetnfSharedProjectImports()
1010
new SharedProjectImport {
1111
Namespace="System.Diagnostics",
1212
CodeMatchString="Stopwatch",
13-
NewProjectImport = @"<Import Project=""..\..\Stopwatch\Stopwatch.projitems"" Label=""Shared"" />"
13+
NewProjectImport = @"<Import Project=""..\..\src\Stopwatch\Stopwatch.projitems"" Label=""Shared"" />"
1414
},
1515
new SharedProjectImport {
1616
Namespace="System.Buffers.Binary",
1717
CodeMatchString="BinaryPrimitives",
18-
NewProjectImport = @"<Import Project=""..\..\BinaryPrimitives\BinaryPrimitives.projitems"" Label=""Shared"" />"
18+
NewProjectImport = @"<Import Project=""..\..src\BinaryPrimitives\BinaryPrimitives.projitems"" Label=""Shared"" />"
1919
},
2020
new SharedProjectImport {
2121
Namespace="System.Device.Model",
22-
NewProjectImport = @"<Import Project=""..\..\System.Device.Model\System.Device.Model.projitems"" Label=""Shared"" />"
22+
NewProjectImport = @"<Import Project=""..\..\src\System.Device.Model\System.Device.Model.projitems"" Label=""Shared"" />"
2323
},
2424
new SharedProjectImport {
2525
Namespace="System.Numerics",
26-
NewProjectImport = @"<Import Project=""..\..\System.Numerics\System.Numerics.projitems"" Label=""Shared"" />"
26+
NewProjectImport = @"<Import Project=""..\..\src\System.Numerics\System.Numerics.projitems"" Label=""Shared"" />"
2727
},
28+
new SharedProjectImport {
29+
Namespace="System.Runtime.CompilerService",
30+
NewProjectImport = @"<Import Project=""..\..\src\System.Runtime.CompilerService\System.Runtime.CompilerService.projitems"" Label=""Shared"" />"
31+
},
32+
new SharedProjectImport {
33+
Namespace="System.Drawing",
34+
NewProjectImport = @"<Import Project=""..\..\src\System.Drawing\System.Drawing.projitems"" Label=""Shared"" />"
35+
},
36+
new SharedProjectImport {
37+
Namespace="Iot.Device.Common",
38+
NewProjectImport = @"<Import Project=""..\..\src\Iot.Device.Common\Iot.Device.Common.projitems"" Label=""Shared"" />"
39+
}
2840
};
2941
}
3042
}

tips-trick.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Tips and tricks for .NET IoT to .NET nanoFramework migration
2+
3+
You'll find a list of tips and tricks to help in the migration. There is no specific order in this list.
4+
5+
## What's already done for you
6+
7+
The project conversion, transformation os `Span<byte>` to `SpanByte` and other associated elements are done automatically.
8+
9+
## Enums
10+
11+
- Issue: .NET nanoFramework supports a limited version of enum, so `GetValues`, `IsDefined` are not available.
12+
- Resolution: just remove the `IsDefined`, this is used for check only. If you need the nice names with the `GetValues` use either a switch or string or anything like this depending on the scenario.
13+
14+
## Multidimensional Arrays [,]
15+
16+
- Issue: multidimensional `Arrays [,]` are not supported in .NET nanoFramework
17+
- Resolution:
18+
- Replace them with `Array [][]`
19+
- Initialization is different, example:
20+
```csharp
21+
// byte[,] array = new byte[42,15];
22+
byte[][] array = new byte[42][];
23+
for(int i = 0, i < 15; i++)
24+
{
25+
array[i] = new byte[15];
26+
}
27+
```
28+
- If you have something lile `public int this[int x, int y]`, you'll need to create a class, example:
29+
```csharp
30+
class Point
31+
{
32+
public Point(int X, int y)
33+
{
34+
X = x;
35+
Y = y;
36+
}
37+
public int X { get; set; }
38+
public int Y { get; set; }
39+
}
40+
public int this[Point pt]
41+
// and get access to pt.X and pt.Y
42+
// ...
43+
// Access is then like:
44+
Somthing[new Point(12,34)];
45+
```
46+
## Queue<Something>
47+
48+
- Issue: There is no generic (yet) and no Queue in nanoFramework. Most of the time, the usage of those elements are simple.
49+
- Resolution: Replace the Queue by a `ArrayList` and use `Add` and `Remove` with a cast to read the data.
50+
51+
## Console
52+
53+
- Issue: `Console` is not available in .NET nanoFramework. It does only appear on the sample side.
54+
- Resolution: can be replaced by `Debug`. And the `Console.Read` and other elements like this from the samples can be replaced by hard coded data or constants.
55+
56+
## Unsafe
57+
58+
You may have to compile some of the projects in unsafe mode. This is needed if you are using unsafe blocks or unsafe instructions into your project.
59+
60+
Add `<AllowUnsafeBlocks>true</AllowUnsafeBlocks>` in the nfproj in the property group `PropertyGroup` right after then language version.
61+
62+
## Adjust the documentation
63+
64+
When converting, you may move some code, change some properties or functions a bit, that may need adjustment in README and other documentation, don't forget to adjust those! This does include as well replacing schema from Raspberry Pi to our lovely MCU. Any ESP32 or any STM32 or any supported MCU will be enough.
65+
66+
## Nuget restore and updates
67+
68+
It is recommended to update the nugets with a `dotnet restore` before opening the solution.
69+
70+
You may be stuck sometimes because some references may be corrupted and you won't be able to update or add a new nuget. In this case, you can try to downgrade by one version all the nugets and then update them again, this will pull potential missing references.

0 commit comments

Comments
 (0)