Skip to content

Commit 20d05ab

Browse files
author
Nathan Seidle
committed
Changing setBlinkRate so it doesn't turn on screen automatically. Add displayOn to initialize/begin.
1 parent a39cdf6 commit 20d05ab

File tree

3 files changed

+105
-76
lines changed

3 files changed

+105
-76
lines changed

examples/Example3_PrintString/Example3_PrintString.ino

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,29 @@
1616
*
1717
* Distributed as-is; no warranty is given.
1818
****************************************************************************************/
19-
#include <SparkFun_Alphanumeric_Display.h>
19+
#include <Wire.h>
20+
21+
#include <SparkFun_Alphanumeric_Display.h> //Click here to get the library:
2022
HT16K33 display;
2123

22-
void setup() {
24+
void setup()
25+
{
2326
Serial.begin(115200);
2427
Serial.println("Qwiic Alphanumeric examples");
28+
2529
Wire.begin(); //Join I2C bus
2630

27-
//check if display will acknowledge
2831
if (display.begin() == false)
2932
{
3033
Serial.println("Device did not acknowledge! Freezing.");
31-
while(1);
34+
while (1)
35+
;
3236
}
33-
Serial.println("Display acknnowledged.");
37+
Serial.println("Display acknowledged.");
3438

35-
// display.print("Hello World!");
36-
display.print("Hell");
37-
// display.write('l');
38-
// display.write('a');
39-
// display.write('D');
39+
display.print("Milk");
4040
}
4141

42-
void loop(){
42+
void loop()
43+
{
4344
}

src/SparkFun_Alphanumeric_Display.cpp

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,15 @@ bool HT16K33::begin(uint8_t addressLeft, uint8_t addressLeftCenter, uint8_t addr
3333
_deviceAddressRight = addressRight; //grab the address of the alphanumeric
3434

3535
if (_deviceAddressRight != DEFAULT_NOTHING_ATTACHED)
36-
{
3736
sizeOfDisplay = 16;
38-
}
3937
else if (_deviceAddressRightCenter != DEFAULT_NOTHING_ATTACHED)
40-
{
4138
sizeOfDisplay = 12;
42-
}
4339
else if (_deviceAddressLeftCenter != DEFAULT_NOTHING_ATTACHED)
44-
{
4540
sizeOfDisplay = 8;
46-
Serial.println("Hello, I've increased sizeOfDisplay");
47-
}
41+
else
42+
sizeOfDisplay = 4;
4843

49-
//TODO: try to figure this out
50-
//malloc more displayRAM
44+
//TODO: malloc more displayRAM
5145

5246
//DEBUGGING
5347
Serial.print("sizeOfDisplay: ");
@@ -92,45 +86,48 @@ bool HT16K33::isConnected(uint8_t displayNumber)
9286

9387
_i2cPort->beginTransmission(lookUpDisplayAddress(displayNumber));
9488
if (_i2cPort->endTransmission() == 0)
95-
{
9689
return true;
97-
}
9890
return false;
9991
}
10092

10193
bool HT16K33::initialize(uint8_t displayNumber)
10294
{
103-
//DEBUGGIN
104-
Serial.println("In initialize()");
105-
10695
uint8_t address = lookUpDisplayAddress(displayNumber);
10796

108-
Serial.print("The current address is: 0x");
97+
Serial.print("Initialize: 0x");
10998
Serial.println(address, HEX);
11099

111-
//We need this temp zero value when we only write one byte to HT16K33 RAM
112-
uint8_t temp = 0;
113-
114100
//internal system clock enable
115101
if (writeOne(address, 0x21) == false)
116102
return false;
117-
// //ROW/INT output pin set, INT pin output level set
118-
// if (writeRAM(address, 0xA0, (uint8_t *)&temp, 0) == false)
119-
// return false;
120-
// //Set brightness of display by duty cycle
121-
// if (setBrightnessDisplay(displayNumber, 16) == false)
122-
// {
123-
// Serial.println("I've failed setBrightness()");
124-
// return false;
125-
// }
126-
// //Blinking set - blinking off, display on
127-
// if (setBlinkRateDisplay(displayNumber, 0) == false)
128-
// {
129-
// Serial.println("I've failed setBlinkRate()");
130-
// blinkRate = 0b000;
131-
// onState = 1;
132-
// return false;
133-
// }
103+
104+
//ROW/INT output pin set, INT pin output level set
105+
uint8_t temp = 0;
106+
if (writeRAM(address, 0xA0, (uint8_t *)&temp, 0) == false)
107+
return false;
108+
109+
//Set brightness of display by duty cycle
110+
if (setBrightnessDisplay(displayNumber, 16) == false)
111+
{
112+
Serial.println("I've failed setBrightness()");
113+
return false;
114+
}
115+
116+
//Blinking set - blinking off, display on
117+
if (setBlinkRateDisplay(displayNumber, 0) == false)
118+
{
119+
Serial.println("I've failed setBlinkRate()");
120+
blinkRate = ALPHA_BLINK_RATE_NOBLINK;
121+
displayOnOff = 1;
122+
return false;
123+
}
124+
125+
if (singleDisplayOn(displayNumber) == false)
126+
{
127+
Serial.println("Failed display on");
128+
return false;
129+
}
130+
134131
return true;
135132
}
136133

@@ -239,67 +236,79 @@ bool HT16K33::setBlinkRateDisplay(uint8_t displayNumber, float rate)
239236

240237
if (rate == 2)
241238
{
242-
blinkRate = 0b010;
239+
blinkRate = ALPHA_BLINK_RATE_2HZ;
243240
}
244241
else if (rate == 1)
245242
{
246-
blinkRate = 0b100;
243+
blinkRate = ALPHA_BLINK_RATE_1HZ;
247244
}
248245
else if (rate == 0.5)
249246
{
250-
blinkRate = 0b110;
247+
blinkRate = ALPHA_BLINK_RATE_0_5HZ;
251248
}
252249
//default to no blink
253250
else
254251
{
255-
blinkRate = 0b000;
252+
blinkRate = ALPHA_BLINK_RATE_NOBLINK;
256253
}
257254

258-
//If the display is blinking, then it must also be on
259-
onState = 1;
255+
uint8_t dataToWrite = ALPHA_CMD_DISPLAY_SETUP | (blinkRate << 1) | displayOnOff;
256+
257+
return (writeRAM(lookUpDisplayAddress(displayNumber), dataToWrite, (uint8_t *)&temp, 0));
258+
}
259+
260+
bool HT16K33::singleDisplayOn(uint8_t displayNumber)
261+
{
262+
return setSingleDisplayOn(displayNumber, true);
263+
}
264+
bool HT16K33::singleDisplayOff(uint8_t displayNumber)
265+
{
266+
return setSingleDisplayOn(displayNumber, false);
267+
}
268+
269+
//Set or clear the display on/off bit of a given display number
270+
bool HT16K33::setSingleDisplayOn(uint8_t displayNumber, bool turnOnDisplay)
271+
{
272+
if (turnOnDisplay == true)
273+
displayOnOff = ALPHA_DISPLAY_ON;
274+
else
275+
displayOnOff = ALPHA_DISPLAY_OFF;
276+
277+
int temp = 0;
278+
uint8_t dataToWrite = ALPHA_CMD_DISPLAY_SETUP | (blinkRate << 1) | displayOnOff;
260279

261-
uint8_t writeBlinkRate = 0b10000001 ^ blinkRate;
262-
return (writeRAM(lookUpDisplayAddress(displayNumber), writeBlinkRate, (uint8_t *)&temp, 0));
280+
return (writeRAM(lookUpDisplayAddress(displayNumber), dataToWrite, (uint8_t *)&temp, 0));
263281
}
264282

283+
//Turn on/off the entire display
265284
bool HT16K33::displayOn()
266285
{
267286
bool status = true;
287+
288+
displayOnOff = ALPHA_DISPLAY_ON;
289+
268290
for (uint8_t i = 0; i < sizeOfDisplay / 4; i++)
269291
{
270292
if (singleDisplayOn(i) == false)
271293
status = false;
272294
}
273-
onState = 1;
274-
return status;
275-
}
276295

277-
bool HT16K33::singleDisplayOn(uint8_t displayNumber)
278-
{
279-
int temp = 0;
280-
uint8_t state = 0b10000001 ^ blinkRate;
281-
return (writeRAM(lookUpDisplayAddress(displayNumber), state, (uint8_t *)&temp, 0));
296+
return status;
282297
}
283298

284299
bool HT16K33::displayOff()
285300
{
286301
bool status = true;
302+
303+
displayOnOff = ALPHA_DISPLAY_OFF;
304+
287305
for (uint8_t i = 0; i < sizeOfDisplay / 4; i++)
288306
{
289307
if (singleDisplayOff(i) == false)
290308
status = false;
291309
}
292-
onState = 0;
293-
return status;
294-
}
295310

296-
bool HT16K33::singleDisplayOff(uint8_t displayNumber)
297-
{
298-
//DEBUGGING:
299-
Serial.println("Turning single display off");
300-
int temp = 0;
301-
uint8_t state = 0b10000000 ^ blinkRate;
302-
return (writeRAM(lookUpDisplayAddress(displayNumber), state, (uint8_t *)&temp, 0));
311+
return status;
303312
}
304313

305314
/*---------------------------- Light up functions ---------------------------------*/

src/SparkFun_Alphanumeric_Display.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ Distributed as-is; no warranty is given.
2828
#define DEV_ID 0x12 //Device ID that I just made up
2929
#define DEFAULT_NOTHING_ATTACHED 0xFF
3030

31+
typedef enum
32+
{
33+
ALPHA_BLINK_RATE_NOBLINK = 0b00,
34+
ALPHA_BLINK_RATE_2HZ = 0b01,
35+
ALPHA_BLINK_RATE_1HZ = 0b10,
36+
ALPHA_BLINK_RATE_0_5HZ = 0b11,
37+
};
38+
39+
typedef enum
40+
{
41+
ALPHA_DISPLAY_ON = 0b1,
42+
ALPHA_DISPLAY_OFF = 0b0,
43+
};
44+
45+
typedef enum
46+
{
47+
ALPHA_CMD_SYSTEM_SETUP = 0b00100000,
48+
ALPHA_CMD_DISPLAY_SETUP = 0b10000000,
49+
};
50+
3151
// class HT16K33
3252
class HT16K33 : public Print
3353
{
@@ -39,10 +59,8 @@ class HT16K33 : public Print
3959
uint8_t _deviceAddressRight;
4060
uint8_t digitPosition = 0;
4161
uint8_t sizeOfDisplay = 4;
42-
//Start with display off
43-
bool onState = 0;
44-
//Start with no blinking
45-
uint8_t blinkRate = 0b000;
62+
bool displayOnOff = 0; //Tracks display on/off bit of display setup register
63+
uint8_t blinkRate = ALPHA_BLINK_RATE_NOBLINK; //Tracks blink bits in display setup register
4664

4765
//Enough RAM for up to 4 displays on same I2C bus
4866
uint8_t displayRAM[16 * 4];
@@ -71,6 +89,7 @@ class HT16K33 : public Print
7189
bool singleDisplayOn(uint8_t displayNumber);
7290
bool displayOff();
7391
bool singleDisplayOff(uint8_t displayNumber);
92+
bool setSingleDisplayOn(uint8_t displayNumber, bool turnOnDisplay);
7493

7594
//Light up functions
7695
void illuminateSegment(uint8_t segment, uint8_t digit);

0 commit comments

Comments
 (0)