|
| 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 | +} |
0 commit comments