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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-09-30 21:14:29 +00:00
|
|
|
void SegmentDisplay::writeNumber(uint32_t value)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define ASCIIZero 48
|
|
|
|
#define ASCIINine 57
|
|
|
|
#define ASCIIUppercaseA 65
|
|
|
|
#define ASCIIUppercaseZ 90
|
|
|
|
#define ASCIILowercaseA 97
|
|
|
|
#define ASCIILowercaseZ 122
|
|
|
|
|
|
|
|
|
|
|
|
void SegmentDisplay::writeText(char* value)
|
|
|
|
{
|
2016-09-30 19:03:16 +00:00
|
|
|
byte digitMask = 1;
|
|
|
|
byte digit = digits;
|
|
|
|
byte charValue;
|
2016-09-26 21:14:03 +00:00
|
|
|
|
|
|
|
while (digit > 0 && value != '\0')
|
|
|
|
{
|
|
|
|
charValue = *value;
|
|
|
|
|
|
|
|
// Lowercase
|
|
|
|
if (charValue >= ASCIILowercaseA && charValue <= ASCIILowercaseZ)
|
2016-11-27 22:14:17 +00:00
|
|
|
writeDisplay(pgm_read_byte_near(SDCharacterSegments + (charValue - ASCIILowercaseA)), digitMask);
|
2016-09-26 21:14:03 +00:00
|
|
|
|
|
|
|
// Uppercase
|
|
|
|
else if (charValue >= ASCIIUppercaseA && charValue <= ASCIIUppercaseZ)
|
2016-11-27 22:14:17 +00:00
|
|
|
writeDisplay(pgm_read_byte_near(SDCharacterSegments + (charValue - ASCIIUppercaseA)), digitMask);
|
2016-09-26 21:14:03 +00:00
|
|
|
|
|
|
|
// Numbers
|
|
|
|
else if (charValue >= ASCIIZero && charValue <= ASCIINine)
|
2016-11-27 22:14:17 +00:00
|
|
|
writeDisplay(pgm_read_byte_near(SDNumberSegments + (charValue - ASCIIZero)), digitMask);
|
2016-09-26 21:14:03 +00:00
|
|
|
|
|
|
|
// Space / unknown
|
|
|
|
else
|
|
|
|
writeDisplay(0, digitMask);
|
|
|
|
|
|
|
|
digit--;
|
|
|
|
digitMask <<= 1;
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (digit > 0)
|
|
|
|
{
|
|
|
|
writeDisplay(0, digitMask);
|
|
|
|
|
|
|
|
digit--;
|
|
|
|
digitMask <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-30 19:03:16 +00:00
|
|
|
inline void SegmentDisplay::writeDigit(byte value, byte digitMask)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
2016-11-27 22:14:17 +00:00
|
|
|
writeDisplay(pgm_read_byte_near(SDNumberSegments + value), digitMask);
|
|
|
|
|
|
|
|
if (digitDelayMicroseconds > 1000)
|
|
|
|
delay(digitDelayMicroseconds / 1000);
|
|
|
|
else
|
|
|
|
delayMicroseconds(digitDelayMicroseconds);
|
2016-09-26 21:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-27 22:14:17 +00:00
|
|
|
#if defined(SDUseSPI)
|
2016-09-26 21:14:03 +00:00
|
|
|
|
2016-11-27 22:14:17 +00:00
|
|
|
inline void SegmentDisplay::writeDisplay(byte segmentMask, byte digitMask)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
|
|
|
SPI.beginTransaction(SPISettings(clockSpeed, MSBFIRST, SPI_MODE0));
|
2016-11-27 22:14:17 +00:00
|
|
|
#if defined(SDPushSegmentsFirst)
|
|
|
|
SPI.transfer(segmentMask);
|
|
|
|
SPI.transfer(digitMask);
|
|
|
|
#else
|
2016-09-26 21:14:03 +00:00
|
|
|
SPI.transfer(digitMask);
|
2016-11-27 22:14:17 +00:00
|
|
|
SPI.transfer(segmentMask);
|
|
|
|
#endif
|
2016-09-26 21:14:03 +00:00
|
|
|
SPI.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2016-11-27 22:14:17 +00:00
|
|
|
inline void SegmentDisplay::writeDisplay(byte segmentMask, byte digitMask)
|
2016-09-26 21:14:03 +00:00
|
|
|
{
|
|
|
|
digitalWrite(latchPin, LOW);
|
|
|
|
|
2016-11-27 22:14:17 +00:00
|
|
|
#if defined(SDPushSegmentsFirst)
|
|
|
|
shiftOut(dataPin, clockPin, MSBFIRST, segmentMask);
|
|
|
|
shiftOut(dataPin, clockPin, MSBFIRST, digitMask);
|
|
|
|
#else
|
2016-09-26 21:14:03 +00:00
|
|
|
shiftOut(dataPin, clockPin, MSBFIRST, digitMask);
|
2016-11-27 22:14:17 +00:00
|
|
|
shiftOut(dataPin, clockPin, MSBFIRST, segmentMask);
|
|
|
|
#endif
|
2016-09-26 21:14:03 +00:00
|
|
|
|
|
|
|
digitalWrite(latchPin, HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|