126 lines
2.5 KiB
C++
126 lines
2.5 KiB
C++
|
#include "SegmentDisplay.h"
|
||
|
|
||
|
|
||
|
void SegmentDisplay::begin()
|
||
|
{
|
||
|
#if !defined(SDSupportSPI)
|
||
|
pinMode(latchPin, OUTPUT);
|
||
|
pinMode(clockPin, OUTPUT);
|
||
|
pinMode(dataPin, OUTPUT);
|
||
|
|
||
|
digitalWrite(clockPin, LOW);
|
||
|
digitalWrite(dataPin, LOW);
|
||
|
#endif
|
||
|
|
||
|
digitalWrite(latchPin, HIGH);
|
||
|
digitalWrite(latchPin, LOW);
|
||
|
digitalWrite(latchPin, HIGH);
|
||
|
|
||
|
#if defined(SDSupportSPI)
|
||
|
SPI.begin();
|
||
|
//SPI.setClockDivider(SPI_CLOCK_DIV128);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
|
||
|
void SegmentDisplay::end()
|
||
|
{
|
||
|
#if defined(SDSupportSPI)
|
||
|
SPI.end();
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
|
||
|
void SegmentDisplay::writeNumber(int value)
|
||
|
{
|
||
|
unsigned char digitMask = 1;
|
||
|
|
||
|
for (unsigned char digit = digits; digit > 0; digit--)
|
||
|
{
|
||
|
writeDigit((unsigned char)(value % 10), digitMask);
|
||
|
|
||
|
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)
|
||
|
{
|
||
|
unsigned char digitMask = 1;
|
||
|
unsigned char digit = digits;
|
||
|
unsigned char charValue;
|
||
|
|
||
|
while (digit > 0 && value != '\0')
|
||
|
{
|
||
|
charValue = *value;
|
||
|
|
||
|
// Lowercase
|
||
|
if (charValue >= ASCIILowercaseA && charValue <= ASCIILowercaseZ)
|
||
|
writeDisplay(SDCharacterSegments[charValue - ASCIILowercaseA], digitMask);
|
||
|
|
||
|
// Uppercase
|
||
|
else if (charValue >= ASCIIUppercaseA && charValue <= ASCIIUppercaseZ)
|
||
|
writeDisplay(SDCharacterSegments[charValue - ASCIIUppercaseA], digitMask);
|
||
|
|
||
|
// Numbers
|
||
|
else if (charValue >= ASCIIZero && charValue <= ASCIINine)
|
||
|
writeDisplay(SDNumberSegments[charValue - ASCIIZero], digitMask);
|
||
|
|
||
|
// Space / unknown
|
||
|
else
|
||
|
writeDisplay(0, digitMask);
|
||
|
|
||
|
digit--;
|
||
|
digitMask <<= 1;
|
||
|
value++;
|
||
|
}
|
||
|
|
||
|
while (digit > 0)
|
||
|
{
|
||
|
writeDisplay(0, digitMask);
|
||
|
|
||
|
digit--;
|
||
|
digitMask <<= 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
inline void SegmentDisplay::writeDigit(unsigned char value, unsigned char digitMask)
|
||
|
{
|
||
|
writeDisplay(SDNumberSegments[value], digitMask);
|
||
|
}
|
||
|
|
||
|
|
||
|
#if defined(SDSupportSPI)
|
||
|
|
||
|
inline void SegmentDisplay::writeDisplay(unsigned char characterMask, unsigned char digitMask)
|
||
|
{
|
||
|
SPI.beginTransaction(SPISettings(clockSpeed, MSBFIRST, SPI_MODE0));
|
||
|
SPI.transfer(characterMask);
|
||
|
SPI.transfer(digitMask);
|
||
|
SPI.endTransaction();
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
|
||
|
inline void SegmentDisplay::writeDisplay(unsigned char characterMask, unsigned char digitMask)
|
||
|
{
|
||
|
digitalWrite(latchPin, LOW);
|
||
|
|
||
|
shiftOut(dataPin, clockPin, MSBFIRST, characterMask);
|
||
|
shiftOut(dataPin, clockPin, MSBFIRST, digitMask);
|
||
|
|
||
|
digitalWrite(latchPin, HIGH);
|
||
|
}
|
||
|
|
||
|
#endif
|