Skip to content

Commit 4c5dfe1

Browse files
authored
Add M5Stack, M5Stick, M5StickCPlus board package (#32)
1 parent b2c273b commit 4c5dfe1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3443
-834
lines changed

M5StackCommon/Console.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using nanoFramework.Presentation.Media;
5+
using nanoFramework.UI;
6+
using System;
7+
8+
namespace nanoFramework
9+
{
10+
/// <summary>
11+
/// Console class to display text on screens
12+
/// </summary>
13+
public static class Console
14+
{
15+
/// <summary>
16+
/// The font to use in the screen
17+
/// </summary>
18+
public static Font Font { get; set; }
19+
20+
/// <summary>
21+
/// The foreground color. Default is White.
22+
/// </summary>
23+
public static Color ForegroundColor { get; set; } = Color.White;
24+
25+
/// <summary>
26+
/// The background color. Default is Black.
27+
/// </summary>
28+
public static Color BackgroundColor { get; set; } = Color.Black;
29+
30+
/// <summary>
31+
/// Gets the height of the screen in rows
32+
/// </summary>
33+
public static int WindowHeight { get => DisplayControl.ScreenHeight / Font.Height; }
34+
35+
/// <summary>
36+
/// Gets the width of the screen in columns. Note this is based on the max character size.
37+
/// If you are using a non fix font, you will most likely be able to writ"e more characters.
38+
/// </summary>
39+
public static int WindowWidth { get => DisplayControl.ScreenWidth / Font.MaxWidth; }
40+
41+
/// <summary>
42+
/// Gets or sets the column position.
43+
/// </summary>
44+
public static int CursorLeft { get; set; } = 0;
45+
46+
/// <summary>
47+
/// Gets or sets the row position.
48+
/// </summary>
49+
public static int CursorTop { get; set; } = 0;
50+
51+
/// <summary>
52+
/// Clears the screen.
53+
/// </summary>
54+
public static void Clear()
55+
{
56+
DisplayControl.Clear();
57+
CursorLeft = 0;
58+
CursorTop = 0;
59+
}
60+
61+
/// <summary>
62+
/// Resets the colors to their default, White for the foreground and Black for the background.
63+
/// </summary>
64+
public static void ResetColor()
65+
{
66+
ForegroundColor = Color.White;
67+
BackgroundColor = Color.Black;
68+
}
69+
70+
/// <summary>
71+
/// Writes a text on the screen at the cursor position.
72+
/// Cursor position will automatically increase.
73+
/// </summary>
74+
/// <remarks>No new line character will be recognized, use WriteLine instead.</remarks>
75+
/// <param name="text">The text to display.</param>
76+
public static void Write(string text)
77+
{
78+
// NOTE: Some work needs to be adjusted on the native side to properly handle the character display. Once done, adjustments will be needed.
79+
// Make the math for the lines, it's based out the Max width of the character and won't be perfect
80+
// Console is recommended with fixed size fonts
81+
ushort width = (ushort)(DisplayControl.ScreenWidth - CursorLeft * Font.MaxWidth);
82+
if (text.Length <= width / Font.MaxWidth)
83+
{
84+
DisplayControl.Write(text, (ushort)(CursorLeft * Font.MaxWidth), (ushort)(CursorTop * Font.Height), (ushort)(DisplayControl.ScreenWidth - 1), (ushort)(DisplayControl.ScreenHeight - 1), Font, ForegroundColor, BackgroundColor);
85+
CursorLeft += text.Length;
86+
}
87+
else
88+
{
89+
DisplayControl.Write(text.Substring(0, width / Font.MaxWidth + 1), (ushort)(CursorLeft * Font.MaxWidth), (ushort)(CursorTop * Font.Height), (ushort)(DisplayControl.ScreenWidth - 1), (ushort)(DisplayControl.ScreenHeight - 1), Font, ForegroundColor, BackgroundColor);
90+
CursorTop++;
91+
string newTxt = text.Substring(width / Font.MaxWidth + 1);
92+
DisplayControl.Write(newTxt, 0, (ushort)(CursorTop * Font.Height), (ushort)(DisplayControl.ScreenWidth - 1), (ushort)(DisplayControl.ScreenHeight - 1), Font, ForegroundColor, BackgroundColor);
93+
CursorLeft = newTxt.Length % WindowWidth;
94+
CursorTop += newTxt.Length / WindowWidth;
95+
CursorTop = CursorTop > WindowHeight ? WindowHeight : CursorTop;
96+
}
97+
}
98+
99+
/// <summary>
100+
/// Writes a text on the screen at the cursor position and goes to the next line.
101+
/// </summary>
102+
/// <param name="text">The text to display.</param>
103+
public static void WriteLine(string text)
104+
{
105+
Write(text);
106+
CursorLeft = 0;
107+
CursorTop++;
108+
CursorTop = CursorTop > WindowHeight ? WindowHeight : CursorTop;
109+
}
110+
}
111+
}

M5StackCommon/M5StackCommon.projitems

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5+
<HasSharedItems>true</HasSharedItems>
6+
<SharedGUID>00e23322-2401-4087-abae-24f90c8a0422</SharedGUID>
7+
</PropertyGroup>
8+
<PropertyGroup Label="Configuration">
9+
<Import_RootNamespace>M5StackCommon</Import_RootNamespace>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)Console.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)ScreenBase.cs" />
14+
</ItemGroup>
15+
</Project>

M5StackCommon/M5StackCommon.shproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<ProjectGuid>00e23322-2401-4087-abae-24f90c8a0422</ProjectGuid>
5+
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
6+
</PropertyGroup>
7+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
8+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
9+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
10+
<PropertyGroup />
11+
<Import Project="M5StackCommon.projitems" Label="Shared" />
12+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
13+
</Project>

M5StackCommon/ScreenBase.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using nanoFramework.Presentation.Media;
5+
using nanoFramework.UI;
6+
using System;
7+
using System.Device.Gpio;
8+
9+
namespace nanoFramework.M5Stack
10+
{
11+
/// <summary>
12+
/// M5 Stack screen class
13+
/// </summary>
14+
public abstract class ScreenBase
15+
{
16+
internal static int MemoryAllocationBitmap;
17+
18+
internal static int BackLightPin;
19+
internal static GpioController Controller;
20+
internal static bool IsEnabled;
21+
22+
/// <summary>
23+
/// Maximum buffer size for a Bitmap on the native side.
24+
/// </summary>
25+
// A bitmap stores colors on 3 bytes and there is an initial structure taking maximum 100 bytes
26+
public static int MaxBitmapSize { get => (MemoryAllocationBitmap - 100) / 3; }
27+
28+
/// <summary>
29+
/// Enabled or disable the screen.
30+
/// </summary>
31+
public static bool Enabled
32+
{
33+
get => IsEnabled;
34+
set
35+
{
36+
IsEnabled = value;
37+
Controller.Write(BackLightPin, IsEnabled);
38+
}
39+
}
40+
41+
/// <summary>
42+
/// Sets the screen intensity in percentage from 0 to 100.
43+
/// </summary>
44+
/// <remarks>If this is not natively supported, the threshold of 50 will make the screen either on or off.</remarks>
45+
public static byte LuminosityPercentage
46+
{
47+
get
48+
{
49+
return (byte)(IsEnabled ? 100 : 0);
50+
}
51+
52+
set
53+
{
54+
Enabled = value > 50;
55+
}
56+
}
57+
58+
/// <summary>
59+
/// Clears the screen.
60+
/// </summary>
61+
public static void Clear() => DisplayControl.Clear();
62+
63+
/// <summary>
64+
/// Write a text on the screen
65+
/// </summary>
66+
/// <param name="x">The x coordinate.</param>
67+
/// <param name="y">The y coordinate.</param>
68+
/// <param name="width">The width of the area to display.</param>
69+
/// <param name="height">The height of the area to display</param>
70+
/// <param name="colors">A 16 bits color.</param>
71+
public static void Write(ushort x, ushort y, ushort width, ushort height, ushort[] colors)
72+
=> DisplayControl.Write(x, y, width, height, colors);
73+
74+
/// <summary>
75+
/// Writes on the screen some text.
76+
/// </summary>
77+
/// <param name="text">The text to write.</param>
78+
/// <param name="x">The x coordinate.</param>
79+
/// <param name="y">The y coordinate.</param>
80+
/// <param name="width">The width of the area to display.</param>
81+
/// <param name="height">The height of the area to display.</param>
82+
/// <param name="font">The font to use.</param>
83+
/// <param name="foreground">Foreground color.</param>
84+
/// <param name="background">Background color.</param>
85+
public static void Write(string text, ushort x, ushort y, ushort width, ushort height, Font font, Color foreground, Color background)
86+
=> DisplayControl.Write(text, x, y, width, height, font, foreground, background);
87+
88+
/// <summary>
89+
/// Write a point directly on the screen.
90+
/// </summary>
91+
/// <param name="x">The x coordinate.</param>
92+
/// <param name="y">The y coordinate.</param>
93+
/// <param name="color">The 16 bits color.</param>
94+
public static void WritePoint(ushort x, ushort y, ushort color) => DisplayControl.WritePoint(x, y, color);
95+
}
96+
}

0 commit comments

Comments
 (0)