|
| 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 Iot.Device.Button; |
| 5 | +using Iot.Device.Buzzer; |
| 6 | +using Iot.Device.EPaper.Drivers.Jd796xx; |
| 7 | +using Iot.Device.Rtc; |
| 8 | +using nanoFramework.Hardware.Esp32; |
| 9 | +using System; |
| 10 | +using System.Device.Adc; |
| 11 | +using System.Device.Gpio; |
| 12 | +using System.Device.I2c; |
| 13 | +using System.Device.Spi; |
| 14 | + |
| 15 | +namespace nanoFramework.M5Stack |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// M5 CoreInk board. |
| 19 | + /// </summary> |
| 20 | + public static class M5CoreInk |
| 21 | + { |
| 22 | + private readonly static I2cDevice _device; |
| 23 | + private static SpiDevice _spi; |
| 24 | + private static AdcController _adc; |
| 25 | + private static Buzzer _buzzer; |
| 26 | + private static GpioPin _led; |
| 27 | + private static GpioButton _button; |
| 28 | + private static GpioButton _left; |
| 29 | + private static GpioButton _center; |
| 30 | + private static GpioButton _right; |
| 31 | + private static GpioButton _power; |
| 32 | + private static GpioController _gpio; |
| 33 | + private static Pcf8563 _rtc; |
| 34 | + private static Gdew0154m09 _screen; |
| 35 | + |
| 36 | + private const int ScreenBusyPin = 4; |
| 37 | + private const int ScreenResetPin = 0; |
| 38 | + private const int ScreenDCPin = 15; |
| 39 | + |
| 40 | + #region properties |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Gets the upper button. |
| 44 | + /// </summary> |
| 45 | + public static GpioButton RollerLeft |
| 46 | + { |
| 47 | + get |
| 48 | + { |
| 49 | + _left ??= new(37, GpioController, false, PinMode.Input); |
| 50 | + |
| 51 | + return _left; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets the upper button. |
| 57 | + /// </summary> |
| 58 | + public static GpioButton RollerRight |
| 59 | + { |
| 60 | + get |
| 61 | + { |
| 62 | + _right ??= new(39, GpioController, false, PinMode.Input); |
| 63 | + |
| 64 | + return _right; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Gets the upper button. |
| 70 | + /// </summary> |
| 71 | + public static GpioButton RollerButton |
| 72 | + { |
| 73 | + get |
| 74 | + { |
| 75 | + _center ??= new(38, GpioController, false, PinMode.Input); |
| 76 | + |
| 77 | + return _center; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Gets the upper button. |
| 83 | + /// </summary> |
| 84 | + public static GpioButton Button |
| 85 | + { |
| 86 | + get |
| 87 | + { |
| 88 | + _button ??= new(5, GpioController, false); |
| 89 | + |
| 90 | + return _button; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Gets the power button. |
| 96 | + /// </summary> |
| 97 | + public static GpioButton Power |
| 98 | + { |
| 99 | + get |
| 100 | + { |
| 101 | + _power ??= new(27, GpioController, false); |
| 102 | + |
| 103 | + return _power; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Gets the green led. |
| 109 | + /// </summary> |
| 110 | + public static GpioPin Led |
| 111 | + { |
| 112 | + get |
| 113 | + { |
| 114 | + _led ??= GpioController.OpenPin(10, PinMode.Output); |
| 115 | + |
| 116 | + return _led; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Gets the Buzzer. |
| 122 | + /// </summary> |
| 123 | + public static Buzzer Buzzer |
| 124 | + { |
| 125 | + get |
| 126 | + { |
| 127 | + // SetPinFunction already made in the static constructor |
| 128 | + _buzzer ??= new(2); |
| 129 | + |
| 130 | + return _buzzer; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Gets the main <see cref="GpioController"/>. |
| 136 | + /// </summary> |
| 137 | + public static GpioController GpioController |
| 138 | + { |
| 139 | + get |
| 140 | + { |
| 141 | + _gpio ??= new(); |
| 142 | + |
| 143 | + return _gpio; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Gets the internal clock. |
| 149 | + /// </summary> |
| 150 | + public static Pcf8563 RTC |
| 151 | + { |
| 152 | + get |
| 153 | + { |
| 154 | + _rtc ??= new(_device); |
| 155 | + |
| 156 | + return _rtc; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Get the ePaper screen. |
| 162 | + /// </summary> |
| 163 | + public static Gdew0154m09 Screen |
| 164 | + { |
| 165 | + get |
| 166 | + { |
| 167 | + InitializeScreen(); |
| 168 | + |
| 169 | + return _screen; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + #endregion |
| 174 | + |
| 175 | + static M5CoreInk() |
| 176 | + { |
| 177 | + Configuration.SetPinFunction(2, DeviceFunction.PWM1); |
| 178 | + Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK); |
| 179 | + Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI); |
| 180 | + |
| 181 | + // RTC settings |
| 182 | + Configuration.SetPinFunction(21, DeviceFunction.I2C1_DATA); |
| 183 | + Configuration.SetPinFunction(22, DeviceFunction.I2C1_CLOCK); |
| 184 | + I2cConnectionSettings settings = new(1, Pcf8563.DefaultI2cAddress); |
| 185 | + _device = I2cDevice.Create(settings); |
| 186 | + } |
| 187 | + |
| 188 | + /// <summary> |
| 189 | + /// Gets an ADC channel. |
| 190 | + /// </summary> |
| 191 | + /// <param name="gpioNumber">The GPIO pin number.</param> |
| 192 | + /// <returns>An AdcChannel</returns> |
| 193 | + public static AdcChannel GetAdcGpio(int gpioNumber) |
| 194 | + { |
| 195 | + _adc ??= new(); |
| 196 | + |
| 197 | + switch (gpioNumber) |
| 198 | + { |
| 199 | + case 35: |
| 200 | + Configuration.SetPinFunction(35, DeviceFunction.ADC1_CH7); |
| 201 | + return _adc.OpenChannel(7); |
| 202 | + case 36: |
| 203 | + Configuration.SetPinFunction(36, DeviceFunction.ADC1_CH0); |
| 204 | + return _adc.OpenChannel(0); |
| 205 | + case 2: |
| 206 | + Configuration.SetPinFunction(2, DeviceFunction.ADC1_CH12); |
| 207 | + return _adc.OpenChannel(12); |
| 208 | + case 12: |
| 209 | + Configuration.SetPinFunction(12, DeviceFunction.ADC1_CH15); |
| 210 | + return _adc.OpenChannel(15); |
| 211 | + case 15: |
| 212 | + Configuration.SetPinFunction(15, DeviceFunction.ADC1_CH13); |
| 213 | + return _adc.OpenChannel(13); |
| 214 | + case 25: |
| 215 | + Configuration.SetPinFunction(25, DeviceFunction.ADC1_CH18); |
| 216 | + return _adc.OpenChannel(18); |
| 217 | + case 26: |
| 218 | + Configuration.SetPinFunction(26, DeviceFunction.ADC1_CH19); |
| 219 | + return _adc.OpenChannel(19); |
| 220 | + case 13: |
| 221 | + Configuration.SetPinFunction(13, DeviceFunction.ADC1_CH14); |
| 222 | + return _adc.OpenChannel(14); |
| 223 | + case 0: |
| 224 | + Configuration.SetPinFunction(0, DeviceFunction.ADC1_CH11); |
| 225 | + return _adc.OpenChannel(11); |
| 226 | + case 34: |
| 227 | + Configuration.SetPinFunction(34, DeviceFunction.ADC1_CH6); |
| 228 | + return _adc.OpenChannel(6); |
| 229 | + default: |
| 230 | + throw new ArgumentException(nameof(gpioNumber)); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + /// <summary> |
| 235 | + /// Initialize the eInk screen. |
| 236 | + /// </summary> |
| 237 | + /// <returns>An instance of the <see cref="Gdew0154m09"/> driver.</returns> |
| 238 | + private static void InitializeScreen() |
| 239 | + { |
| 240 | + if (_screen == null) |
| 241 | + { |
| 242 | + var spiConnectionSettings = new SpiConnectionSettings(busId: 1, chipSelectLine: 9) |
| 243 | + { |
| 244 | + ClockFrequency = Gdew0154m09.SpiClockFrequency, |
| 245 | + Mode = Gdew0154m09.SpiMode, |
| 246 | + ChipSelectLineActiveState = PinValue.Low, |
| 247 | + Configuration = SpiBusConfiguration.HalfDuplex, |
| 248 | + DataFlow = DataFlow.MsbFirst, |
| 249 | + }; |
| 250 | + |
| 251 | + _spi = new SpiDevice(spiConnectionSettings); |
| 252 | + _screen = new Gdew0154m09(_spi, ScreenResetPin, ScreenBusyPin, ScreenDCPin, _gpio); |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments