-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathht16k33-matrix-test.ino
311 lines (270 loc) · 6.43 KB
/
ht16k33-matrix-test.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* Testing HT16K33 I2C LED driver with 8x8 or 8x16 LED matrix
*/
#include <Arduino.h>
#include <Wire.h>
#include <ht16k33.h>
#include <Adafruit_LEDBackpack.h>
#include <Adafruit_GFX.h>
#include <Fonts/Picopixel.h>
// X mirror class start - this is required becuase of different wiring of LED matrix
#ifndef _swap_int16_t
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
#endif
class Adafruit_X_Mirrored_8x8matrix: public Adafruit_LEDBackpack, public Adafruit_GFX {
public:
Adafruit_X_Mirrored_8x8matrix(void);
void drawPixel(int16_t x, int16_t y, uint16_t color);
private:
};
Adafruit_X_Mirrored_8x8matrix::Adafruit_X_Mirrored_8x8matrix(void) : Adafruit_GFX(8, 8) {
}
void Adafruit_X_Mirrored_8x8matrix::drawPixel(int16_t x, int16_t y, uint16_t color) {
if ((y < 0) || (y >= 8)) return;
if ((x < 0) || (x >= 8)) return;
// check rotation, move pixel around if necessary
switch (getRotation()) {
case 1:
_swap_int16_t(x, y);
x = 8 - x - 1;
break;
case 2:
x = 8 - x - 1;
y = 8 - y - 1;
break;
case 3:
_swap_int16_t(x, y);
y = 8 - y - 1;
break;
}
x = 8 - x - 1; // X pixel mirrored
if (color) {
displaybuffer[y] |= 1 << x;
} else {
displaybuffer[y] &= ~(1 << x);
}
}
// X mirror class end
// define Adafruit library class
Adafruit_X_Mirrored_8x8matrix matrix = Adafruit_X_Mirrored_8x8matrix();
static const uint8_t PROGMEM
smile_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10100101,
B10011001,
B01000010,
B00111100 },
neutral_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10111101,
B10000001,
B01000010,
B00111100 },
frown_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10011001,
B10100101,
B01000010,
B00111100 };
// Define lpaseen library class
HT16K33 HT;
void I2c_Scanner(void) {
byte error, address;
int nDevices;
Serial.println("I2C Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print(F("I2C device found at address 0x"));
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(2000);
}
void Ht16k33_Init(void) {
HT.begin(0x70);
HT.setBrightness(5);
HT.clearAll();
}
void Adafruit_Ht_Matrix_init() {
matrix.begin(0x70);
}
/****************************************************************/
void setup() {
Serial.begin(115200);
// First let's do a quick scan on the I2C bus
I2c_Scanner();
Ht16k33_Init();
Adafruit_Ht_Matrix_init();
Serial.println(F("HT16K33 light test v1.0"));
Serial.println();
}
void Gradually_Turning_On_All_Leds(void) {
uint8_t led;
Serial.println(F("Turning on all 16x8 LEDs"));
for (led=0; led<128; led++) {
HT.setLedNow(led);
delay(16);
}
Serial.println(F("All LEDs are on"));
}
void Brightness_Test(void) {
uint8_t led;
Serial.println(F("Brightness test"));
for (led=0; led<16; led++) {
HT.setBrightness(led);
delay(500);
}
HT.setBrightness(5);
}
void Gradually_Turning_Off_All_Leds(void) {
uint8_t led;
Serial.println(F("Clear all LEDs"));
for (led=0; led<128; led++) {
HT.clearLedNow(led);
delay(16);
}
Serial.println(F("All LEDs are off"));
}
void Led_Test_One_By_One(void) {
uint8_t led;
for (led=0; led<128; led++) {
HT.setLedNow(led);
Serial.print(F("Led no "));
Serial.print(led,DEC);
Serial.print(F(": On"));
delay(200);
HT.clearLedNow(led);
Serial.println(F(" Off"));
}
}
void Draw_Faces(void) {
Serial.println(F("Smile"));
matrix.clear();
matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_ON);
matrix.drawBitmap(0, 8, smile_bmp, 8, 8, LED_ON);
matrix.writeDisplay();
delay(1500);
Serial.println(F("Neutral"));
matrix.clear();
matrix.drawBitmap(0, 0, neutral_bmp, 8, 8, LED_ON);
matrix.drawBitmap(0, 8, neutral_bmp, 8, 8, LED_ON);
matrix.writeDisplay();
delay(1500);
Serial.println(F("Frown"));
matrix.clear();
matrix.drawBitmap(0, 0, frown_bmp, 8, 8, LED_ON);
matrix.drawBitmap(0, 8, frown_bmp, 8, 8, LED_ON);
matrix.writeDisplay();
delay(1500);
}
void Adafruit_Text_Display_Test() {
int8_t x,y;
matrix.setRotation(0);
matrix.setTextSize(1);
matrix.setTextWrap(false);
Serial.println(F("Scrolling, rotation=0"));
matrix.setTextColor(LED_ON);
for (y=0; y<=1; y++) {
for (x=7; x>=-77; x--) {
matrix.clear();
matrix.setCursor(x,1);
matrix.print("- Hello World");
matrix.writeDisplay();
delay(60);
}
}
delay(2000);
Serial.println(F("Scrolling, rotation=1"));
matrix.setTextColor(LED_ON);
matrix.setRotation(1);
for (y=0; y<=1; y++) {
for (x=7; x>=-77; x--) {
matrix.clear();
matrix.setCursor(x,1);
matrix.print("- Hello World");
matrix.writeDisplay();
delay(60);
}
}
matrix.setRotation(0);
}
void Line_test(void) {
int8_t x,y,rotation;
matrix.clear();
matrix.writeDisplay();
delay(500);
for (rotation=0; rotation<=3; rotation++) {
matrix.setRotation(rotation);
matrix.clear();
matrix.writeDisplay();
for (y=0; y<=8; y++) {
Serial.print(F("Rotation: "));
Serial.print(rotation);
Serial.print(F(" "));
Serial.print(F("Y: "));
Serial.print(y);
Serial.println(F(""));
Serial.print(F("X: "));
for (x=0; x<=7; x++) {
Serial.print(x);
Serial.print(F(" "));
if (y<8) {
matrix.drawPixel(x, y, LED_ON);
}
if (y>0) {
matrix.drawPixel(x, y-1, LED_OFF);
}
matrix.writeDisplay();
delay(100);
}
Serial.println(F(" "));
}
}
}
/****************************************************************/
void loop() {
matrix.setRotation(0);
Draw_Faces();
matrix.setRotation(1);
Draw_Faces();
matrix.setRotation(0);
Line_test();
Adafruit_Text_Display_Test();
delay(2000);
Gradually_Turning_On_All_Leds();
delay(2000);
Brightness_Test();
delay(2000);
Gradually_Turning_Off_All_Leds();
delay(2000);
Led_Test_One_By_One();
}