|
| 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 nanoFramework.Hardware.Esp32; |
| 6 | +using nanoFramework.Hardware.Esp32.Rmt; |
| 7 | +using System; |
| 8 | +using System.Device.Adc; |
| 9 | +using System.Device.Dac; |
| 10 | +using System.Device.Gpio; |
| 11 | +using System.Device.I2c; |
| 12 | +using System.Device.Spi; |
| 13 | + |
| 14 | +#if ATOM_MATRIX |
| 15 | +namespace nanoFramework.AtomMatrix |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// The AtomMatrix Board. |
| 19 | + /// </summary> |
| 20 | + public static partial class AtomMatrix |
| 21 | +#else |
| 22 | +namespace nanoFramework.AtomLite |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// M5Stack board |
| 26 | + /// </summary> |
| 27 | + public static partial class AtomLite |
| 28 | +#endif |
| 29 | + { |
| 30 | + private static GpioButton _button; |
| 31 | + private static GpioController _gpio; |
| 32 | + private static DacChannel _dac1; |
| 33 | + private static DacChannel _dac2; |
| 34 | + private static AdcController _adc; |
| 35 | + private static TransmitterChannel _irLed; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Main button. |
| 39 | + /// </summary> |
| 40 | + public static GpioButton Button |
| 41 | + { |
| 42 | + get |
| 43 | + { |
| 44 | + if (_button == null) |
| 45 | + { |
| 46 | + _button = new(39, _gpio, false); |
| 47 | + } |
| 48 | + |
| 49 | + return _button; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets the main <see cref="GpioController"/>. |
| 55 | + /// </summary> |
| 56 | + public static GpioController GpioController => _gpio; |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets <see cref="DacChannel"/> connected to GPIO 25. |
| 60 | + /// </summary> |
| 61 | + public static DacChannel Dac1 |
| 62 | + { |
| 63 | + get |
| 64 | + { |
| 65 | + // We are creating it on demand |
| 66 | + if (_dac1 == null) |
| 67 | + { |
| 68 | + _dac1 = DacController.GetDefault().OpenChannel(0); |
| 69 | + } |
| 70 | + |
| 71 | + return _dac1; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Gets <see cref="DacChannel"/> connected to GPIO 26. |
| 77 | + /// </summary> |
| 78 | + public static DacChannel Dac2 |
| 79 | + { |
| 80 | + get |
| 81 | + { |
| 82 | + // We are creating it on demand |
| 83 | + if (_dac2 == null) |
| 84 | + { |
| 85 | + |
| 86 | + _dac2 = DacController.GetDefault().OpenChannel(1); |
| 87 | + } |
| 88 | + |
| 89 | + return _dac2; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Gets an <see cref="I2cDevice"/>. |
| 95 | + /// </summary> |
| 96 | + /// <param name="i2cDeviceAddress">The address of the <see cref="I2cDevice"/> on the bus.</param> |
| 97 | + /// <returns>The I2cDevice.</returns> |
| 98 | + public static I2cDevice GetI2cDevice(int i2cDeviceAddress) => new(new I2cConnectionSettings(1, i2cDeviceAddress)); |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Gets an <see cref="I2cDevice"/>. |
| 102 | + /// </summary> |
| 103 | + /// <param name="i2cDeviceAddress">The address of the <see cref="I2cDevice"/> on the bus.</param> |
| 104 | + /// <returns>The I2cDevice.</returns> |
| 105 | + public static I2cDevice GetGrove(int i2cDeviceAddress) => new(new I2cConnectionSettings(1, i2cDeviceAddress)); |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Gets the infrared led as a RMT transmitter channel. |
| 109 | + /// </summary> |
| 110 | + public static TransmitterChannel InfraredLed |
| 111 | + { |
| 112 | + get |
| 113 | + { |
| 114 | + if (_irLed == null) |
| 115 | + { |
| 116 | + _irLed = new(12); |
| 117 | + } |
| 118 | + |
| 119 | + return _irLed; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Gets an <see cref="SpiDevice"/>. |
| 125 | + /// </summary> |
| 126 | + /// <param name="chipSelect">The chip select of the device, needs to be any valid GPIO.</param> |
| 127 | + /// <returns>An SpiDevice.</returns> |
| 128 | + public static SpiDevice GetSpiDevice(int chipSelect) => new(new SpiConnectionSettings(1, chipSelect)); |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Gets an <see cref="AdcChannel"/> |
| 132 | + /// </summary> |
| 133 | + /// <param name="gpioNumber">The GPIO pin number where the <see cref="AdcChannel"/> is connected to.</param> |
| 134 | + /// <returns>An AdcChannel</returns> |
| 135 | + public static AdcChannel GetAdcGpio(int gpioNumber) |
| 136 | + { |
| 137 | + if (_adc == null) |
| 138 | + { |
| 139 | + _adc = new(); |
| 140 | + } |
| 141 | + |
| 142 | + switch (gpioNumber) |
| 143 | + { |
| 144 | + case 33: |
| 145 | + Configuration.SetPinFunction(12, DeviceFunction.ADC1_CH5); |
| 146 | + return _adc.OpenChannel(5); |
| 147 | + case 32: |
| 148 | + Configuration.SetPinFunction(25, DeviceFunction.ADC1_CH4); |
| 149 | + return _adc.OpenChannel(4); |
| 150 | + default: |
| 151 | + throw new ArgumentException(nameof(gpioNumber)); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments