Skip to content

Commit 72d19e5

Browse files
author
Stuart
committed
HT16K33 Display Driver used by adafruit
1 parent 1b491be commit 72d19e5

File tree

3 files changed

+266
-0
lines changed

3 files changed

+266
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
using System;
8+
using Raspberry.Timers;
9+
using Raspberry.IO.Components.Controllers.HT16K33;
10+
using Raspberry.IO.InterIntegratedCircuit;
11+
12+
13+
namespace Raspberry.IO.Components.Leds.BiColor24Bargraph
14+
{
15+
public class BiColor24Bargraph : HT16K33Connection
16+
{
17+
public enum LEDState
18+
{
19+
Off,
20+
Red,
21+
Green,
22+
Yellow
23+
}
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="Raspberry.IO.Components.Leds.BiColor24Bargraph.BiColor24Bargraph"/> class.
26+
/// </summary>
27+
/// <param name="connection">I2c Connection.</param>
28+
public BiColor24Bargraph (I2cDeviceConnection connection) : base (connection,6)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Sets the led (0 to 23)
34+
/// </summary>
35+
/// <param name="ledNo">Led no.</param>
36+
/// <param name="state">State.</param>
37+
public void SetLed(uint ledNo, LEDState state)
38+
{
39+
40+
if (ledNo > 23) throw new Exception("led must be between 0 and 23");
41+
42+
long r,c;
43+
r = Math.DivRem(ledNo,4,out c) * 2;
44+
if (ledNo >= 12) c += 4;
45+
46+
if (r > 4) r -= 6;
47+
48+
switch (state)
49+
{
50+
case LEDState.Off:
51+
base.SetLed((uint)r,(uint)c, false);
52+
base.SetLed((uint)r + 1,(uint)c, false);
53+
break;
54+
case LEDState.Red:
55+
base.SetLed((uint)r,(uint)c, true);
56+
base.SetLed((uint)r + 1,(uint)c, false);
57+
break;
58+
case LEDState.Yellow:
59+
base.SetLed((uint)r,(uint)c, true);
60+
base.SetLed((uint)r + 1,(uint)c, true);
61+
break;
62+
case LEDState.Green:
63+
base.SetLed((uint)r,(uint)c, false);
64+
base.SetLed((uint)r + 1,(uint)c, true);
65+
break;
66+
}
67+
68+
}
69+
}
70+
}
71+
72+
73+
74+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using Raspberry.IO.Components.Leds.BiColor24Bargraph;
3+
using Raspberry.IO.InterIntegratedCircuit;
4+
using System.Threading;
5+
using Raspberry.IO.GeneralPurpose;
6+
7+
namespace Test.Components.BiColor24Bargraph
8+
{
9+
class MainClass
10+
{
11+
public static void Main (string[] args)
12+
{
13+
using (var i2cDriver = new I2cDriver(ProcessorPin.Pin2, ProcessorPin.Pin3))
14+
{
15+
I2cDeviceConnection i2c = i2cDriver.Connect(0x70);
16+
17+
var bargraph = new BiColor24Bargraph(i2c);
18+
19+
bargraph.Clear();
20+
21+
while (true)
22+
{
23+
foreach (BiColor24Bargraph.LEDState state in Enum.GetValues(typeof(BiColor24Bargraph.LEDState)))
24+
{
25+
for (int i=0; i<24;i++)
26+
{
27+
bargraph.SetLed((uint)i, state);
28+
Thread.Sleep(50);
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)