|
| 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 System; |
| 5 | +using System.Device.I2c; |
| 6 | +using System.IO; |
| 7 | + |
| 8 | +namespace Iot.Device.Ft6xx6x |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Ft6xx6x touch screen |
| 12 | + /// </summary> |
| 13 | + public class Ft6xx6x : IDisposable |
| 14 | + { |
| 15 | + private I2cDevice _i2cDevice; |
| 16 | + private byte[] _data = new byte[2]; |
| 17 | + private byte[] _toReadPoint = new byte[6]; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Ft6xx6x I2C Address |
| 21 | + /// </summary> |
| 22 | + public const byte DefaultI2cAddress = 0x38; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets or sets the consumption mode. |
| 26 | + /// </summary> |
| 27 | + public ConsumptionMode ConsumptionMode |
| 28 | + { |
| 29 | + get => (ConsumptionMode)ReadByte(Register.ID_G_PMODE); |
| 30 | + set => WriteByte(Register.ID_G_PMODE, (byte)value); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Gets or sets the proximity sensing. |
| 35 | + /// </summary> |
| 36 | + public bool ProximitySensingEnabled |
| 37 | + { |
| 38 | + get => ReadByte(Register.ID_G_FACE_DEC_MODE) == 0x01; |
| 39 | + set => WriteByte(Register.ID_G_FACE_DEC_MODE, (byte)(value ? 0x01 : 0x00)); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Gets or sets the charger mode type. |
| 44 | + /// </summary> |
| 45 | + public bool ChargerOn |
| 46 | + { |
| 47 | + get => ReadByte(Register.ID_G_FREQ_HOPPING_EN) == 0x01; |
| 48 | + set => WriteByte(Register.ID_G_FREQ_HOPPING_EN, (byte)(value ? 0x01 : 0x00)); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets or sets the touch screen to go to monitor mode. |
| 53 | + /// </summary> |
| 54 | + public bool MonitorModeEnabled |
| 55 | + { |
| 56 | + get => ReadByte(Register.ID_G_CTRL) == 0x01; |
| 57 | + set => WriteByte(Register.ID_G_CTRL, (byte)(value ? 0x01 : 0x00)); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Gets or sets the time to go to monitor mode in seconds. |
| 62 | + /// Maximum is 0x64. |
| 63 | + /// </summary> |
| 64 | + public byte MonitorModeDelaySeconds |
| 65 | + { |
| 66 | + get => ReadByte(Register.ID_G_TIMEENTERMONITOR); |
| 67 | + set => WriteByte(Register.ID_G_TIMEENTERMONITOR, (byte)(value > 0x64 ? 0x64 : value)); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Period for scaning and making results available. |
| 72 | + /// Values between 0x04 and 0x14. |
| 73 | + /// </summary> |
| 74 | + /// <remarks>Do not pull results faster than this perdiod.</remarks> |
| 75 | + public byte PeriodActive |
| 76 | + { |
| 77 | + get => ReadByte(Register.ID_G_PERIODACTIVE); |
| 78 | + set => WriteByte(Register.ID_G_PERIODACTIVE, (byte)(value > 0x14 ? 0x14 : (value < 0x04 ? 0x04 : value))); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Gets or sets the period for scaning and making results available. |
| 83 | + /// Values between 0x04 and 0x14. |
| 84 | + /// </summary> |
| 85 | + /// <remarks>Do not pull results faster than this perdiod.</remarks> |
| 86 | + public byte MonitorModePeriodActive |
| 87 | + { |
| 88 | + get => ReadByte(Register.ID_G_PERIODMONITOR); |
| 89 | + set => WriteByte(Register.ID_G_PERIODMONITOR, (byte)(value > 0x14 ? 0x14 : (value < 0x04 ? 0x04 : value))); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Gets or setes the touch threshold |
| 94 | + /// </summary> |
| 95 | + public byte TouchThreshold |
| 96 | + { |
| 97 | + get => ReadByte(Register.ID_G_THGROUP); |
| 98 | + set => WriteByte(Register.ID_G_THGROUP, value); |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Gets or setes the point filter threshold |
| 103 | + /// </summary> |
| 104 | + public byte PointFilterThreshold |
| 105 | + { |
| 106 | + get => ReadByte(Register.ID_G_THDIFF); |
| 107 | + set => WriteByte(Register.ID_G_THDIFF, value); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Gets or sets the gesture. |
| 112 | + /// </summary> |
| 113 | + public bool GestureEnabled |
| 114 | + { |
| 115 | + get => ReadByte(Register.ID_G_SPEC_GESTURE_ENABLE) == 0x01; |
| 116 | + set => WriteByte(Register.ID_G_SPEC_GESTURE_ENABLE, (byte)(value ? 0x01 : 0x00)); |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Creates a new instance of the Ft6xx6x |
| 121 | + /// </summary> |
| 122 | + /// <param name="i2cDevice">The I2C device used for communication.</param> |
| 123 | + public Ft6xx6x(I2cDevice i2cDevice) |
| 124 | + { |
| 125 | + _i2cDevice = i2cDevice ?? throw new ArgumentException(nameof(i2cDevice)); |
| 126 | + // Check if we do have a valid chip code high. Must be 0x64 |
| 127 | + if (ReadByte(Register.ID_G_CIPHER_HIGH) != 0x64) |
| 128 | + { |
| 129 | + throw new IOException("Not a valid Ft6xx6x"); |
| 130 | + } |
| 131 | + |
| 132 | + // Switch to normal mode |
| 133 | + WriteByte(Register.Mode_Switch, (byte)workingMode.Normal); |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Gets the library version. |
| 138 | + /// </summary> |
| 139 | + /// <returns>The library version.</returns> |
| 140 | + public Version GetVersion() |
| 141 | + { |
| 142 | + var low = ReadByte(Register.ID_G_LIB_VERSION_L); |
| 143 | + var high = ReadByte(Register.ID_G_LIB_VERSION_H); ; |
| 144 | + return new Version(high, low); |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Sets the interrupt mode. |
| 149 | + /// </summary> |
| 150 | + /// <param name="modeLow">True to have int low when extending the report point otherwise when reporting poirt is not etended.</param> |
| 151 | + public void SetInterruptMode(bool modeLow) |
| 152 | + { |
| 153 | + WriteByte(Register.ID_G_MODE, (byte)(modeLow ? 0x01 : 0x00)); |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Gets the chip type. |
| 158 | + /// </summary> |
| 159 | + /// <returns>The chip type.</returns> |
| 160 | + public ChipType GetChipType() |
| 161 | + { |
| 162 | + return (ChipType)ReadByte(Register.ID_G_CIPHER_LOW); |
| 163 | + } |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Sets the factory mode. |
| 167 | + /// </summary> |
| 168 | + /// <param name="mode">The factory mode.</param> |
| 169 | + public void SetFactoryMode(FactoryMode mode) |
| 170 | + { |
| 171 | + WriteByte(Register.Mode_Switch, (byte)workingMode.Factory); |
| 172 | + WriteByte(Register.ID_G_FACTORY_MODE, (byte)mode); |
| 173 | + } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Gets the number of points detected. |
| 177 | + /// </summary> |
| 178 | + /// <returns>Number of points detected.</returns> |
| 179 | + public byte GetNumberPoints() |
| 180 | + { |
| 181 | + return ReadByte(Register.TD_STATUS); |
| 182 | + } |
| 183 | + |
| 184 | + /// <summary> |
| 185 | + /// Gets a point. |
| 186 | + /// </summary> |
| 187 | + /// <param name="first">True to get the first point.</param> |
| 188 | + /// <returns>The point.</returns> |
| 189 | + public Point GetPoint(bool first) |
| 190 | + { |
| 191 | + Point pt = new Point(); |
| 192 | + Read(first ? Register.P1_XH : Register.P2_XH, _toReadPoint); |
| 193 | + pt.X = (_toReadPoint[0] << 8 | _toReadPoint[1]) & 0x0FFF; |
| 194 | + pt.Y = (_toReadPoint[2] << 8 | _toReadPoint[3]) & 0x0FFF; |
| 195 | + pt.TouchId = (byte)(_toReadPoint[2] >> 4); |
| 196 | + pt.Weigth = _toReadPoint[4]; |
| 197 | + pt.Miscelaneous = _toReadPoint[5]; |
| 198 | + pt.Event = (Event)(_toReadPoint[0] >> 6); |
| 199 | + return pt; |
| 200 | + } |
| 201 | + |
| 202 | + /// <summary> |
| 203 | + /// Get the double touch points. |
| 204 | + /// </summary> |
| 205 | + /// <returns>Double touch points.</returns> |
| 206 | + public DoublePoints GetDoublePoints() |
| 207 | + { |
| 208 | + DoublePoints pt = new DoublePoints(); |
| 209 | + pt.Point1 = GetPoint(true); |
| 210 | + pt.Point2 = GetPoint(false); |
| 211 | + return pt; |
| 212 | + } |
| 213 | + |
| 214 | + /// <summary> |
| 215 | + /// Cleanup |
| 216 | + /// </summary> |
| 217 | + public void Dispose() |
| 218 | + { |
| 219 | + _i2cDevice?.Dispose(); |
| 220 | + _i2cDevice = null!; |
| 221 | + } |
| 222 | + |
| 223 | + private byte ReadByte(Register reg) |
| 224 | + { |
| 225 | + _i2cDevice.WriteByte((byte)reg); |
| 226 | + return _i2cDevice.ReadByte(); |
| 227 | + } |
| 228 | + |
| 229 | + private void WriteByte(Register reg, byte data) |
| 230 | + { |
| 231 | + _data[0] = (byte)reg; |
| 232 | + _data[1] = data; |
| 233 | + _i2cDevice.Write(_data); |
| 234 | + } |
| 235 | + |
| 236 | + private void Read(Register reg, SpanByte data) |
| 237 | + { |
| 238 | + _i2cDevice.WriteByte((byte)reg); |
| 239 | + _i2cDevice.Read(data); |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments