|
| 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.Rtc; |
| 7 | +using nanoFramework.Hardware.Esp32; |
| 8 | +using System; |
| 9 | +using System.Device.Adc; |
| 10 | +using System.Device.Gpio; |
| 11 | +using System.Device.I2c; |
| 12 | + |
| 13 | +namespace nanoFramework.M5Stack |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// M5 CoreInk board |
| 17 | + /// </summary> |
| 18 | + public static class M5CoreInk |
| 19 | + { |
| 20 | + private readonly static I2cDevice _device; |
| 21 | + private static AdcController _adc; |
| 22 | + private static Buzzer _buzzer; |
| 23 | + private static GpioPin _led; |
| 24 | + private static GpioButton _button; |
| 25 | + private static GpioButton _left; |
| 26 | + private static GpioButton _center; |
| 27 | + private static GpioButton _right; |
| 28 | + private static GpioButton _power; |
| 29 | + private static GpioController _gpio; |
| 30 | + private static Pcf8563 _rtc; |
| 31 | + |
| 32 | + #region properties |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets the upper button. |
| 36 | + /// </summary> |
| 37 | + public static GpioButton RollerLeft |
| 38 | + { |
| 39 | + get |
| 40 | + { |
| 41 | + _left ??= new(37, GpioController, false, PinMode.Input); |
| 42 | + |
| 43 | + return _left; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets the upper button. |
| 49 | + /// </summary> |
| 50 | + public static GpioButton RollerRight |
| 51 | + { |
| 52 | + get |
| 53 | + { |
| 54 | + _right ??= new(39, GpioController, false, PinMode.Input); |
| 55 | + |
| 56 | + return _right; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Gets the upper button. |
| 62 | + /// </summary> |
| 63 | + public static GpioButton RollerButton |
| 64 | + { |
| 65 | + get |
| 66 | + { |
| 67 | + _center ??= new(38, GpioController, false, PinMode.Input); |
| 68 | + |
| 69 | + return _center; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Gets the upper button. |
| 75 | + /// </summary> |
| 76 | + public static GpioButton Button |
| 77 | + { |
| 78 | + get |
| 79 | + { |
| 80 | + _button ??= new(5, GpioController, false); |
| 81 | + |
| 82 | + return _button; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Gets the power button. |
| 88 | + /// </summary> |
| 89 | + public static GpioButton Power |
| 90 | + { |
| 91 | + get |
| 92 | + { |
| 93 | + _power ??= new(27, GpioController, false); |
| 94 | + |
| 95 | + return _power; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Gets the green led. |
| 101 | + /// </summary> |
| 102 | + public static GpioPin Led |
| 103 | + { |
| 104 | + get |
| 105 | + { |
| 106 | + _led ??= GpioController.OpenPin(10, PinMode.Output); |
| 107 | + |
| 108 | + return _led; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Gets the Buzzer. |
| 114 | + /// </summary> |
| 115 | + public static Buzzer Buzzer |
| 116 | + { |
| 117 | + get |
| 118 | + { |
| 119 | + // SetPinFunction already made in the static constructor |
| 120 | + _buzzer ??= new(2); |
| 121 | + |
| 122 | + return _buzzer; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Gets the main <see cref="GpioController"/>. |
| 128 | + /// </summary> |
| 129 | + public static GpioController GpioController |
| 130 | + { |
| 131 | + get |
| 132 | + { |
| 133 | + _gpio ??= new(); |
| 134 | + |
| 135 | + return _gpio; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public static Pcf8563 RTC |
| 140 | + { |
| 141 | + get |
| 142 | + { |
| 143 | + _rtc ??= new(_device); |
| 144 | + |
| 145 | + return _rtc; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + #endregion |
| 150 | + |
| 151 | + static M5CoreInk() |
| 152 | + { |
| 153 | + Configuration.SetPinFunction(2, DeviceFunction.PWM1); |
| 154 | + Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK); |
| 155 | + Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI); |
| 156 | + |
| 157 | + // RTC settings |
| 158 | + Configuration.SetPinFunction(21, DeviceFunction.I2C1_DATA); |
| 159 | + Configuration.SetPinFunction(22, DeviceFunction.I2C1_CLOCK); |
| 160 | + I2cConnectionSettings settings = new(1, Pcf8563.DefaultI2cAddress); |
| 161 | + _device = I2cDevice.Create(settings); |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Gets an ADC channel |
| 166 | + /// </summary> |
| 167 | + /// <param name="gpioNumber">The GPIO pin number</param> |
| 168 | + /// <returns>An AdcChannel</returns> |
| 169 | + public static AdcChannel GetAdcGpio(int gpioNumber) |
| 170 | + { |
| 171 | + _adc ??= new(); |
| 172 | + |
| 173 | + switch (gpioNumber) |
| 174 | + { |
| 175 | + case 35: |
| 176 | + Configuration.SetPinFunction(35, DeviceFunction.ADC1_CH7); |
| 177 | + return _adc.OpenChannel(7); |
| 178 | + case 36: |
| 179 | + Configuration.SetPinFunction(36, DeviceFunction.ADC1_CH0); |
| 180 | + return _adc.OpenChannel(0); |
| 181 | + case 2: |
| 182 | + Configuration.SetPinFunction(2, DeviceFunction.ADC1_CH12); |
| 183 | + return _adc.OpenChannel(12); |
| 184 | + case 12: |
| 185 | + Configuration.SetPinFunction(12, DeviceFunction.ADC1_CH15); |
| 186 | + return _adc.OpenChannel(15); |
| 187 | + case 15: |
| 188 | + Configuration.SetPinFunction(15, DeviceFunction.ADC1_CH13); |
| 189 | + return _adc.OpenChannel(13); |
| 190 | + case 25: |
| 191 | + Configuration.SetPinFunction(25, DeviceFunction.ADC1_CH18); |
| 192 | + return _adc.OpenChannel(18); |
| 193 | + case 26: |
| 194 | + Configuration.SetPinFunction(26, DeviceFunction.ADC1_CH19); |
| 195 | + return _adc.OpenChannel(19); |
| 196 | + case 13: |
| 197 | + Configuration.SetPinFunction(13, DeviceFunction.ADC1_CH14); |
| 198 | + return _adc.OpenChannel(14); |
| 199 | + case 0: |
| 200 | + Configuration.SetPinFunction(0, DeviceFunction.ADC1_CH11); |
| 201 | + return _adc.OpenChannel(11); |
| 202 | + case 34: |
| 203 | + Configuration.SetPinFunction(34, DeviceFunction.ADC1_CH6); |
| 204 | + return _adc.OpenChannel(6); |
| 205 | + default: |
| 206 | + throw new ArgumentException(nameof(gpioNumber)); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments