|
| 1 | +//Copyright (c) 2016 Logic Ethos Ltd |
| 2 | +// |
| 3 | +//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 4 | +//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 5 | +//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 6 | + |
| 7 | + |
| 8 | +using System; |
| 9 | +using Common.Logging; |
| 10 | +using Raspberry.IO.InterIntegratedCircuit; |
| 11 | +using Raspberry.Timers; |
| 12 | +using UnitsNet; |
| 13 | +using System.IO; |
| 14 | + |
| 15 | +namespace Raspberry.IO.Components.Controllers.HT16K33 |
| 16 | +{ |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Driver for Holtek HT16K33 LED Matrix driver |
| 20 | + /// As used by Adafruit devices |
| 21 | + /// </summary> |
| 22 | + public class HT16K33Connection //: IPwmDevice |
| 23 | + { |
| 24 | + |
| 25 | + |
| 26 | + public enum Flash : byte |
| 27 | + { |
| 28 | + Off = 0x00, |
| 29 | + On = 0x01, |
| 30 | + TwoHZ = 0x02, |
| 31 | + OneHZ = 0x04, |
| 32 | + HalfHZ = 0x06, |
| 33 | + } |
| 34 | + |
| 35 | + public enum Command : byte |
| 36 | + { |
| 37 | + DisplayDataAddress = 0x00, |
| 38 | + System_Setup = 0x20, |
| 39 | + KeyDataAddressPointer = 0x40, |
| 40 | + INTFlagAddressPointer = 0x60, |
| 41 | + Flash = 0x80, |
| 42 | + RowIntSet = 0xA0, |
| 43 | + DimmingSet = 0xE0, |
| 44 | + TestMode = 0xD9, |
| 45 | + } |
| 46 | + |
| 47 | + const byte DEFAULT_ADDRESS = 0x70; |
| 48 | + const byte HT16K33_Oscillator = 0x01; |
| 49 | + const byte HT16K33_DisplayOn = 0x01; |
| 50 | + |
| 51 | + private readonly I2cDeviceConnection connection; |
| 52 | + private static readonly ILog log = LogManager.GetLogger<HT16K33Connection>(); |
| 53 | + |
| 54 | + public byte[] LEDBuffer {get; private set;} //Max 16 rows, 8 bits (leds) |
| 55 | + |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Initializes a new instance of the |
| 59 | + /// <see cref="Raspberry.IO.Components.Controllers.HT16K33.HT16K33Connection"/> class. |
| 60 | + /// </summary> |
| 61 | + /// <param name="connection">I2c connection.</param> |
| 62 | + /// <param name="RowCount">Rows in use (1 to 16) </param> |
| 63 | + public HT16K33Connection(I2cDeviceConnection connection, int RowCount) |
| 64 | + { |
| 65 | + LEDBuffer = new byte[RowCount]; |
| 66 | + this.connection = connection; |
| 67 | + |
| 68 | + log.Info(m => m("Resetting HT16K33")); |
| 69 | + |
| 70 | + connection.Write((byte)Command.System_Setup | (byte)HT16K33_Oscillator); //Turn on the oscillator. |
| 71 | + connection.Write((byte)Command.Flash | (byte)HT16K33_DisplayOn | (byte)Flash.Off); |
| 72 | + connection.Write((byte)Command.DimmingSet | (byte)15); |
| 73 | + |
| 74 | + // connection.Write(SetupSequence); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Flash display at specified frequency. |
| 80 | + /// </summary> |
| 81 | + /// <param name="">.</param> |
| 82 | + public void SetFlash(Flash frequency) |
| 83 | + { |
| 84 | + connection.WriteByte((byte)((byte)Command.Flash | HT16K33_DisplayOn | (byte)frequency)); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Set brightness of entire display to specified value (0 to 15). |
| 89 | + /// </summary> |
| 90 | + /// <param name="">.</param> |
| 91 | + public void SetBrightness(uint brightness) |
| 92 | + { |
| 93 | + if (brightness > 15) brightness = 15; |
| 94 | + connection.WriteByte((byte)((byte)Command.DimmingSet | (byte)brightness)); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Sets specified LED (0-[row-count] rows, 0 to 7 leds) |
| 99 | + /// </summary> |
| 100 | + /// <param name="">.</param> |
| 101 | + public void SetLed(uint row, uint led, bool OutputOn) |
| 102 | + { |
| 103 | + if (row >= LEDBuffer.Length) throw new Exception("Row out of range"); |
| 104 | + if (led > 7) throw new Exception("LED out of range 0 to 7"); |
| 105 | + |
| 106 | + if (OutputOn) |
| 107 | + { |
| 108 | + LEDBuffer[row] |= (byte)(1 << (int)led); //Turn on the speciried LED (set bit to one). |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + LEDBuffer[row] &= (byte)~(1 << (int)led); //Turn off the specified LED (set bit to zero). |
| 113 | + } |
| 114 | + connection.Write(new byte[] {(byte)row, LEDBuffer[row]}); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Write display buffer to display hardware. |
| 120 | + /// </summary> |
| 121 | + /// <param name="">.</param> |
| 122 | + public void WriteDisplayBuffer() |
| 123 | + { |
| 124 | + for (int i = 0; i < LEDBuffer.Length;i++) |
| 125 | + { |
| 126 | + connection.Write((byte)i,LEDBuffer[i]); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Clear contents of display buffer. |
| 132 | + /// </summary> |
| 133 | + /// <param name="">.</param> |
| 134 | + public void Clear() |
| 135 | + { |
| 136 | + for (int i = 0; i < LEDBuffer.Length;i++) |
| 137 | + { |
| 138 | + LEDBuffer[i] = 0; |
| 139 | + } |
| 140 | + WriteDisplayBuffer(); |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Set all LEDs On. |
| 145 | + /// </summary> |
| 146 | + /// <param name="">.</param> |
| 147 | + public void SetAllOn() |
| 148 | + { |
| 149 | + for (int i = 0; i < LEDBuffer.Length;i++) |
| 150 | + { |
| 151 | + LEDBuffer[i] = 1; |
| 152 | + } |
| 153 | + WriteDisplayBuffer(); |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | +} |
0 commit comments