Skip to content

Commit 2634399

Browse files
authored
adding generic driver graphics (#289)
1 parent b33e0b9 commit 2634399

14 files changed

+537
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>2d5f8128-a01a-4edb-8d08-298d9e56c564</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>GenericDriver</RootNamespace>
16+
<AssemblyName>GenericDriver</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Program.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Reference Include="mscorlib">
26+
<HintPath>packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll</HintPath>
27+
</Reference>
28+
<Reference Include="nanoFramework.Graphics">
29+
<HintPath>packages\nanoFramework.Graphics.1.1.23\lib\nanoFramework.Graphics.dll</HintPath>
30+
</Reference>
31+
<Reference Include="nanoFramework.Hardware.Esp32">
32+
<HintPath>packages\nanoFramework.Hardware.Esp32.1.6.1\lib\nanoFramework.Hardware.Esp32.dll</HintPath>
33+
</Reference>
34+
<Reference Include="nanoFramework.ResourceManager">
35+
<HintPath>packages\nanoFramework.ResourceManager.1.2.13\lib\nanoFramework.ResourceManager.dll</HintPath>
36+
</Reference>
37+
<Reference Include="nanoFramework.Runtime.Events">
38+
<HintPath>packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
39+
</Reference>
40+
<Reference Include="nanoFramework.Runtime.Native">
41+
<HintPath>packages\nanoFramework.Runtime.Native.1.6.6\lib\nanoFramework.Runtime.Native.dll</HintPath>
42+
</Reference>
43+
<Reference Include="nanoFramework.System.Collections">
44+
<HintPath>packages\nanoFramework.System.Collections.1.5.18\lib\nanoFramework.System.Collections.dll</HintPath>
45+
</Reference>
46+
</ItemGroup>
47+
<ItemGroup>
48+
<None Include="packages.config" />
49+
</ItemGroup>
50+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
51+
<ProjectExtensions>
52+
<ProjectCapabilities>
53+
<ProjectConfigurationsDeclaredAsItems />
54+
</ProjectCapabilities>
55+
</ProjectExtensions>
56+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33502.453
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "GenericDriver", "GenericDriver.nfproj", "{2D5F8128-A01A-4EDB-8D08-298D9E56C564}"
7+
EndProject
8+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "UsingGenericDriver", "..\UsingGenericDriver\UsingGenericDriver.nfproj", "{E296B89E-2C30-49A8-8C9C-88D2C353AC26}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{2D5F8128-A01A-4EDB-8D08-298D9E56C564}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{2D5F8128-A01A-4EDB-8D08-298D9E56C564}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{2D5F8128-A01A-4EDB-8D08-298D9E56C564}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
19+
{2D5F8128-A01A-4EDB-8D08-298D9E56C564}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{2D5F8128-A01A-4EDB-8D08-298D9E56C564}.Release|Any CPU.Build.0 = Release|Any CPU
21+
{2D5F8128-A01A-4EDB-8D08-298D9E56C564}.Release|Any CPU.Deploy.0 = Release|Any CPU
22+
{E296B89E-2C30-49A8-8C9C-88D2C353AC26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{E296B89E-2C30-49A8-8C9C-88D2C353AC26}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{E296B89E-2C30-49A8-8C9C-88D2C353AC26}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
25+
{E296B89E-2C30-49A8-8C9C-88D2C353AC26}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{E296B89E-2C30-49A8-8C9C-88D2C353AC26}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{E296B89E-2C30-49A8-8C9C-88D2C353AC26}.Release|Any CPU.Deploy.0 = Release|Any CPU
28+
EndGlobalSection
29+
GlobalSection(SolutionProperties) = preSolution
30+
HideSolutionNode = FALSE
31+
EndGlobalSection
32+
GlobalSection(ExtensibilityGlobals) = postSolution
33+
SolutionGuid = {8A531F62-BF6F-4DA2-8E90-F9C9614A6F9B}
34+
EndGlobalSection
35+
EndGlobal
+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using System;
8+
using System.Diagnostics;
9+
using System.Threading;
10+
using nanoFramework.Hardware.Esp32;
11+
using nanoFramework.Presentation.Media;
12+
using nanoFramework.Runtime.Native;
13+
using nanoFramework.UI;
14+
15+
namespace GenericDriver
16+
{
17+
/// <summary>
18+
/// You **MUST** have a build of nanoFramework with a generic graphic driver
19+
/// </summary>
20+
public class Program
21+
{
22+
private const int ChipSelect = 5;
23+
private const int DataCommand = 23;
24+
private const int Reset = 18;
25+
26+
// Raw registers from the ST7735 datasheet
27+
private enum St7735Reg
28+
{
29+
NOP = 0x00,
30+
SOFTWARE_RESET = 0x01,
31+
POWER_STATE = 0x10,
32+
Sleep_Out = 0x11,
33+
Invertion_Off = 0x20,
34+
Invertion_On = 0x21,
35+
Gamma_Set = 0x26,
36+
Display_OFF = 0x28,
37+
Display_ON = 0x29,
38+
Column_Address_Set = 0x2A,
39+
Page_Address_Set = 0x2B,
40+
Memory_Write = 0x2C,
41+
Colour_Set = 0x2D,
42+
Memory_Read = 0x2E,
43+
Partial_Area = 0x30,
44+
Memory_Access_Control = 0x36,
45+
Pixel_Format_Set = 0x3A,
46+
Memory_Write_Continue = 0x3C,
47+
Write_Display_Brightness = 0x51,
48+
Frame_Rate_Control_Normal = 0xB1,
49+
Frame_Rate_Control_2 = 0xB2,
50+
Frame_Rate_Control_3 = 0xB3,
51+
Invert_On = 0xB4,
52+
Display_Function_Control = 0xB6,
53+
Entry_Mode_Set = 0xB7,
54+
Power_Control_1 = 0xC0,
55+
Power_Control_2 = 0xC1,
56+
Power_Control_3 = 0xC2,
57+
Power_Control_4 = 0xC3,
58+
Power_Control_5 = 0xC4,
59+
VCOM_Control_1 = 0xC5,
60+
VCOM_Control_2 = 0xC7,
61+
Power_Control_A = 0xCB,
62+
Power_Control_B = 0xCF,
63+
Positive_Gamma_Correction = 0xE0,
64+
Negative_Gamma_Correction = 0XE1,
65+
Driver_Timing_Control_A = 0xE8,
66+
Driver_Timing_Control_B = 0xEA,
67+
Power_On_Sequence = 0xED,
68+
Enable_3G = 0xF2,
69+
Pump_Ratio_Control = 0xF7,
70+
Power_Control_6 = 0xFC,
71+
};
72+
73+
[Flags]
74+
private enum St7735Orientation
75+
{
76+
MADCTL_MH = 0x04, // sets the Horizontal Refresh, 0=Left-Right and 1=Right-Left
77+
MADCTL_ML = 0x10, // sets the Vertical Refresh, 0=Top-Bottom and 1=Bottom-Top
78+
MADCTL_MV = 0x20, // sets the Row/Column Swap, 0=Normal and 1=Swapped
79+
MADCTL_MX = 0x40, // sets the Column Order, 0=Left-Right and 1=Right-Left
80+
MADCTL_MY = 0x80, // sets the Row Order, 0=Top-Bottom and 1=Bottom-Top
81+
82+
MADCTL_BGR = 0x08 // Blue-Green-Red pixel order
83+
};
84+
85+
public static void Main()
86+
{
87+
Debug.WriteLine("Hello from and generic grahic drivers!");
88+
89+
// You **MUST** have a build of nanoFramework with a generic graphic driver
90+
91+
// If you're using an ESP32, don't forget to set the pins for the screen
92+
// Set the pins for the screen
93+
Configuration.SetPinFunction(15, DeviceFunction.SPI1_MOSI);
94+
Configuration.SetPinFunction(13, DeviceFunction.SPI1_CLOCK);
95+
// This is not used but must be defined
96+
Configuration.SetPinFunction(4, DeviceFunction.SPI1_MISO);
97+
98+
var displaySpiConfig = new SpiConfiguration(
99+
1,
100+
ChipSelect,
101+
DataCommand,
102+
Reset,
103+
-1);
104+
105+
GraphicDriver graphicDriver = new GraphicDriver()
106+
{
107+
MemoryWrite = (byte)St7735Reg.Memory_Write,
108+
SetColumnAddress = (byte)St7735Reg.Column_Address_Set,
109+
SetRowAddress = (byte)St7735Reg.Page_Address_Set,
110+
InitializationSequence = new byte[]
111+
{
112+
(byte)GraphicDriverCommandType.Command, 1, (byte)St7735Reg.SOFTWARE_RESET,
113+
// Sleep for 50 ms
114+
(byte)GraphicDriverCommandType.Sleep, 5,
115+
(byte)GraphicDriverCommandType.Command, 1, (byte)St7735Reg.Sleep_Out,
116+
// Sleep for 500 ms
117+
(byte)GraphicDriverCommandType.Sleep, 50,
118+
(byte)GraphicDriverCommandType.Command, 4, (byte)St7735Reg.Frame_Rate_Control_Normal, 0x01, 0x2C, 0x2D,
119+
(byte)GraphicDriverCommandType.Command, 4, (byte)St7735Reg.Frame_Rate_Control_2, 0x01, 0x2C, 0x2D,
120+
(byte)GraphicDriverCommandType.Command, 7, (byte)St7735Reg.Frame_Rate_Control_3, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D,
121+
(byte)GraphicDriverCommandType.Command, 2, (byte)St7735Reg.Invert_On, 0x07,
122+
(byte)GraphicDriverCommandType.Command, 1, (byte)St7735Reg.Invertion_On,
123+
// 0x55 -> 16 bit
124+
(byte)GraphicDriverCommandType.Command, 2, (byte)St7735Reg.Pixel_Format_Set, 0x55,
125+
(byte)GraphicDriverCommandType.Command, 4, (byte)St7735Reg.Power_Control_1, 0xA2, 0x02, 0x84,
126+
(byte)GraphicDriverCommandType.Command, 2, (byte)St7735Reg.Power_Control_2, 0xC5,
127+
(byte)GraphicDriverCommandType.Command, 3, (byte)St7735Reg.Power_Control_3, 0x0A, 0x00,
128+
(byte)GraphicDriverCommandType.Command, 3, (byte)St7735Reg.Power_Control_4, 0x8A, 0x2A,
129+
(byte)GraphicDriverCommandType.Command, 3, (byte)St7735Reg.Power_Control_5, 0x8A, 0xEE,
130+
(byte)GraphicDriverCommandType.Command, 4, (byte)St7735Reg.VCOM_Control_1, 0x0E, 0xFF, 0xFF,
131+
(byte)GraphicDriverCommandType.Command, 17, (byte)St7735Reg.Positive_Gamma_Correction, 0x02, 0x1c, 0x7, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2B, 0x39, 0x00, 0x01, 0x03, 0x10,
132+
(byte)GraphicDriverCommandType.Command, 17, (byte)St7735Reg.Negative_Gamma_Correction, 0x03, 0x1d, 0x07, 0x06, 0x2E, 0x2C, 0x29, 0x2D, 0x2E, 0x2E, 0x37, 0x3F, 0x00, 0x00, 0x02, 0x1,
133+
(byte)GraphicDriverCommandType.Command, 1, (byte)St7735Reg.Sleep_Out,
134+
(byte)GraphicDriverCommandType.Command, 1, (byte)St7735Reg.Display_ON,
135+
// Sleep 100 ms
136+
(byte)GraphicDriverCommandType.Sleep, 10,
137+
(byte)GraphicDriverCommandType.Command, 1, (byte)St7735Reg.NOP,
138+
// Sleep 20 ms
139+
(byte)GraphicDriverCommandType.Sleep, 2,
140+
},
141+
OrientationLandscape = new byte[]
142+
{
143+
(byte)GraphicDriverCommandType.Command, 2, (byte)St7735Reg.Memory_Access_Control, (byte)(St7735Orientation.MADCTL_MY | St7735Orientation.MADCTL_MX | St7735Orientation.MADCTL_BGR),
144+
},
145+
PowerModeNormal = new byte[]
146+
{
147+
(byte)GraphicDriverCommandType.Command, 3, (byte)St7735Reg.POWER_STATE, 0x00, 0x00,
148+
},
149+
PowerModeSleep = new byte[]
150+
{
151+
(byte)GraphicDriverCommandType.Command, 3, (byte)St7735Reg.POWER_STATE, 0x00, 0x01,
152+
},
153+
DefaultOrientation = DisplayOrientation.Landscape,
154+
Brightness = (byte)St7735Reg.Write_Display_Brightness,
155+
SetWindowType = SetWindowType.X16bitsY16Bit,
156+
};
157+
158+
var screenConfig = new ScreenConfiguration(
159+
26,
160+
1,
161+
80,
162+
160,
163+
graphicDriver);
164+
165+
var init = DisplayControl.Initialize(
166+
displaySpiConfig,
167+
screenConfig,
168+
1024);
169+
170+
Debug.WriteLine($"init screen initialized");
171+
172+
ushort[] toSend = new ushort[100];
173+
var blue = ColorUtility.To16Bpp(Color.Blue);
174+
var red = ColorUtility.To16Bpp(Color.Red);
175+
var green = ColorUtility.To16Bpp(Color.Green);
176+
var white = ColorUtility.To16Bpp(Color.White);
177+
178+
for (int i = 0; i < toSend.Length; i++)
179+
{
180+
toSend[i] = blue;
181+
}
182+
183+
DisplayControl.Write(0, 0, 10, 10, toSend);
184+
185+
for (int i = 0; i < toSend.Length; i++)
186+
{
187+
toSend[i] = red;
188+
}
189+
190+
DisplayControl.Write(69, 0, 10, 10, toSend);
191+
192+
for (int i = 0; i < toSend.Length; i++)
193+
{
194+
toSend[i] = green;
195+
}
196+
197+
DisplayControl.Write(0, 149, 10, 10, toSend);
198+
199+
for (int i = 0; i < toSend.Length; i++)
200+
{
201+
toSend[i] = white;
202+
}
203+
204+
DisplayControl.Write(69, 149, 10, 10, toSend);
205+
206+
Thread.Sleep(Timeout.Infinite);
207+
}
208+
}
209+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("GenericGraphic.Example")]
9+
[assembly: AssemblyProduct("GenericGraphic.Example")]
10+
[assembly: AssemblyCopyright("Copyright .NET nanoFramework and contributors © 2023")]
11+
12+
// Setting ComVisible to false makes the types in this assembly not visible
13+
// to COM components. If you need to access a type in this assembly from
14+
// COM, set the ComVisible attribute to true on that type.
15+
[assembly: ComVisible(false)]
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 🌶️🌶️🌶️ - Creating your own generic graphic driver
2+
3+
This sample shows how you can create your own generic driver. This usually requires quite some experience. You have great examples in the [nanoFramework.Graphic libraries](https://github.com/nanoframework/nanoFramework.Graphics/tree/main/ManagedDrivers) with the existing drivers.
4+
5+
> **Important**: you **MUST** use a .NET nanoFramework image that is built with the Generic Graphic Driver. Using an image that is not will either throw because you don't have the graphic library at all either have absolutely no effect if you have one of the built in driver in the image.
6+
7+
The M5Stack devices have all an existing built in driver. So except if you are not using the standard image on them but one containing a generic image, you can't use a generic graphic driver on them. This won't have any effect at all.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
graphics
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.14.2" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.Graphics" version="1.1.23" targetFramework="netnano1.0" />
5+
<package id="nanoFramework.Hardware.Esp32" version="1.6.1" targetFramework="netnano1.0" />
6+
<package id="nanoFramework.ResourceManager" version="1.2.13" targetFramework="netnano1.0" />
7+
<package id="nanoFramework.Runtime.Events" version="1.11.6" targetFramework="netnano1.0" />
8+
<package id="nanoFramework.Runtime.Native" version="1.6.6" targetFramework="netnano1.0" />
9+
<package id="nanoFramework.System.Collections" version="1.5.18" targetFramework="netnano1.0" />
10+
</packages>

samples/Graphics/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ This is a nanoFramework version of the Tetris game with high scores.
6868

6969
Requires the GPIO pins numbers to be defined for the Left, Right, Up, Down & Select keys.
7070

71+
## Using the Generic Graphic Drivers
72+
73+
Those samples will show you how to use the generic driver capabilities. One of the example shows how to crate fully a driver and the other one how to use it once created from our nuget library.
74+
7175
## Build the sample
7276

7377
1. Start Microsoft Visual Studio 2019 (VS 2017 should be OK too) and select `File > Open > Project/Solution`.

0 commit comments

Comments
 (0)