2016-11-27 22:14:17 +00:00
|
|
|
#include "SegmentDisplayConfig.h"
|
2016-09-26 21:14:03 +00:00
|
|
|
#include "SegmentDisplay.h"
|
|
|
|
|
|
|
|
|
|
|
|
void SegmentDisplay::begin()
|
|
|
|
{
|
|
|
|
pinMode(latchPin, OUTPUT);
|
2016-11-27 22:14:17 +00:00
|
|
|
|
|
|
|
#if !defined(SDUseSPI)
|
2016-09-26 21:14:03 +00:00
|
|
|
pinMode(clockPin, OUTPUT);
|
|
|
|
pinMode(dataPin, OUTPUT);
|
|
|
|
|
|
|
|
digitalWrite(clockPin, LOW);
|
|
|
|
digitalWrite(dataPin, LOW);
|
|
|
|
#endif
|
|
|
|
|
2016-11-27 22:14:17 +00:00
|
|
|
writeDisplay(0, 0);
|
|
|
|
|
2016-09-26 21:14:03 +00:00
|
|
|
digitalWrite(latchPin, HIGH);
|
|
|
|
digitalWrite(latchPin, LOW);
|
|
|
|
digitalWrite(latchPin, HIGH);
|
|
|
|
|
2016-11-27 22:14:17 +00:00
|
|
|
#if defined(SDUseSPI)
|
2016-09-26 21:14:03 +00:00
|
|
|
SPI.begin();
|
|
|
|
#endif
|
2016-11-29 19:00:47 +00:00
|
|
|
|
|
|
|
#if defined(SDSerialDebug)
|
|
|
|
Serial.begin(9600);
|
|
|
|
Serial.print("SegmentDisplay::begin\n");
|
|
|
|
#endif
|
2016-09-26 21:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SegmentDisplay::end()
|
|
|
|
{
|
2016-11-27 22:14:17 +00:00
|
|
|
#if defined(SDUseSPI)
|
2016-09-26 21:14:03 +00:00
|
|
|
SPI.end();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-05 22:42:20 +00:00
|
|
|
void SegmentDisplay::clear()
|
|
|
|
{
|
|
|
|
writeDisplay(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-30 21:14:29 +00:00
|
|
|
void SegmentDisplay::writeNumber(uint32_t value)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
2016-11-29 19:00:47 +00:00
|
|
|
#if defined(SDSerialDebug)
|
|
|
|
String textValue = String(value);
|
|
|
|
Serial.print("SegmentDisplay::writeNumber: " + textValue + "\n");
|
|
|
|
#endif
|
|
|
|
|
2016-09-30 19:03:16 +00:00
|
|
|
byte digitMask = 1;
|
2016-09-26 21:14:03 +00:00
|
|
|
|
2016-09-30 19:03:16 +00:00
|
|
|
for (byte digit = digits; digit > 0; digit--)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
2016-09-30 19:03:16 +00:00
|
|
|
writeDigit((byte)(value % 10), digitMask);
|
2016-09-26 21:14:03 +00:00
|
|
|
|
|
|
|
value /= 10;
|
|
|
|
digitMask <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-05 22:42:20 +00:00
|
|
|
void SegmentDisplay::writeTextLeft(char const* value)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
2016-11-29 19:00:47 +00:00
|
|
|
#if defined(SDSerialDebug)
|
|
|
|
Serial.print("SegmentDisplay::writeTextLeft: " + value + "\n");
|
|
|
|
#endif
|
2016-09-26 21:14:03 +00:00
|
|
|
|
2016-11-29 19:00:47 +00:00
|
|
|
byte digitMask = 1 << (digits - 1);
|
|
|
|
byte charIndex = 0;
|
2016-09-26 21:14:03 +00:00
|
|
|
|
2016-11-29 19:00:47 +00:00
|
|
|
while (charIndex < digits && value[charIndex] != '\0')
|
|
|
|
{
|
|
|
|
writeChar(value[charIndex], digitMask);
|
2016-09-26 21:14:03 +00:00
|
|
|
|
2016-11-29 19:00:47 +00:00
|
|
|
digitMask >>= 1;
|
|
|
|
charIndex++;
|
2016-09-26 21:14:03 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 19:00:47 +00:00
|
|
|
while (charIndex < digits)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
|
|
|
writeDisplay(0, digitMask);
|
2016-11-29 19:00:47 +00:00
|
|
|
digitMask >>= 1;
|
|
|
|
charIndex++;
|
2016-09-26 21:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-05 22:42:20 +00:00
|
|
|
void SegmentDisplay::writeRaw(char const* value)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
2016-12-05 22:42:20 +00:00
|
|
|
#if defined(SDSerialDebug)
|
|
|
|
Serial.print("SegmentDisplay::writeRaw: " + value + "\n");
|
|
|
|
#endif
|
2016-11-27 22:14:17 +00:00
|
|
|
|
2016-12-05 22:42:20 +00:00
|
|
|
byte digitMask = 1 << (digits - 1);
|
|
|
|
byte charIndex = 0;
|
2016-11-29 19:00:47 +00:00
|
|
|
|
2016-12-05 22:42:20 +00:00
|
|
|
while (charIndex < digits && value[charIndex] != '\0')
|
|
|
|
{
|
|
|
|
writeDisplay(value[charIndex], digitMask);
|
|
|
|
|
|
|
|
digitMask >>= 1;
|
|
|
|
charIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (charIndex < digits)
|
|
|
|
{
|
|
|
|
writeDisplay(0, digitMask);
|
|
|
|
digitMask >>= 1;
|
|
|
|
charIndex++;
|
|
|
|
}
|
2016-09-26 21:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-29 19:00:47 +00:00
|
|
|
inline void SegmentDisplay::writeDigit(byte value, byte digitMask)
|
|
|
|
{
|
|
|
|
writeDisplay(pgm_read_byte_near(SDNumberSegments + value), digitMask);
|
|
|
|
}
|
2016-09-26 21:14:03 +00:00
|
|
|
|
2016-11-29 19:00:47 +00:00
|
|
|
|
|
|
|
inline void SegmentDisplay::writeChar(char value, byte digitMask)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
2016-11-29 19:00:47 +00:00
|
|
|
// Lowercase
|
|
|
|
if (value >= ASCIILowercaseA && value <= ASCIILowercaseZ)
|
|
|
|
writeDisplay(pgm_read_byte_near(SDCharacterSegments + (value - ASCIILowercaseA)), digitMask);
|
|
|
|
|
|
|
|
// Uppercase
|
|
|
|
else if (value >= ASCIIUppercaseA && value <= ASCIIUppercaseZ)
|
|
|
|
writeDisplay(pgm_read_byte_near(SDCharacterSegments + (value - ASCIIUppercaseA)), digitMask);
|
|
|
|
|
|
|
|
// Numbers
|
|
|
|
else if (value >= ASCIIZero && value <= ASCIINine)
|
|
|
|
writeDisplay(pgm_read_byte_near(SDNumberSegments + (value - ASCIIZero)), digitMask);
|
|
|
|
|
|
|
|
// Space / unknown
|
|
|
|
else
|
|
|
|
writeDisplay(0, digitMask);
|
2016-09-26 21:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-04 18:53:43 +00:00
|
|
|
void SegmentDisplay::writeDisplay(byte segmentMask, byte digitMask)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
2016-11-29 19:00:47 +00:00
|
|
|
// If no segments should be lit, clear the digit mask as well to prevent
|
|
|
|
// power leaking through (it's probably an issue in my circuit or with the
|
|
|
|
// specific displays I used, but it still makes sense to close both sides).
|
|
|
|
//
|
|
|
|
// Still push out the zeros though, if we don't the display will vary
|
|
|
|
// in brightness depending on what's on it.
|
|
|
|
if (segmentMask == 0)
|
|
|
|
digitMask = 0;
|
|
|
|
|
|
|
|
#if defined(SDSerialDebug)
|
|
|
|
String segmentValue = String(segmentMask);
|
|
|
|
String digitValue = String(digitMask);
|
|
|
|
|
|
|
|
Serial.print("SegmentDisplay::writeDisplay: " + segmentValue + " " + digitValue + "\n");
|
|
|
|
#endif
|
2016-09-26 21:14:03 +00:00
|
|
|
|
2016-11-29 19:00:47 +00:00
|
|
|
#if defined(SDUseSPI)
|
|
|
|
SPI.beginTransaction(SPISettings(clockSpeed, MSBFIRST, SPI_MODE0));
|
|
|
|
#if defined(SDPushSegmentsFirst)
|
|
|
|
SPI.transfer(segmentMask);
|
|
|
|
SPI.transfer(digitMask);
|
|
|
|
#else
|
|
|
|
SPI.transfer(digitMask);
|
|
|
|
SPI.transfer(segmentMask);
|
|
|
|
#endif
|
|
|
|
SPI.endTransaction();
|
2016-11-27 22:14:17 +00:00
|
|
|
#else
|
2016-11-29 19:00:47 +00:00
|
|
|
#if defined(SDPushSegmentsFirst)
|
|
|
|
shiftOut(dataPin, clockPin, MSBFIRST, segmentMask);
|
|
|
|
shiftOut(dataPin, clockPin, MSBFIRST, digitMask);
|
|
|
|
#else
|
|
|
|
shiftOut(dataPin, clockPin, MSBFIRST, digitMask);
|
|
|
|
shiftOut(dataPin, clockPin, MSBFIRST, segmentMask);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
digitalWrite(latchPin, HIGH);
|
|
|
|
digitalWrite(latchPin, LOW);
|
2016-11-27 22:14:17 +00:00
|
|
|
#endif
|
2016-09-26 21:14:03 +00:00
|
|
|
|
2016-11-29 19:00:47 +00:00
|
|
|
#if defined(SPHandleLargeDelays)
|
|
|
|
if (digitDelayMicroseconds > 15000)
|
|
|
|
delay(digitDelayMicroseconds / 1000);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
delayMicroseconds(digitDelayMicroseconds);
|
2016-09-26 21:14:03 +00:00
|
|
|
}
|