#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) { byte digitMask = 1; for (byte digit = digits; digit > 0; digit--) { writeDigit((byte)(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) { byte digitMask = 1; byte digit = digits; byte 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(byte value, byte digitMask) { writeDisplay(SDNumberSegments[value], digitMask); } #if defined(SDSupportSPI) inline void SegmentDisplay::writeDisplay(byte characterMask, byte digitMask) { SPI.beginTransaction(SPISettings(clockSpeed, MSBFIRST, SPI_MODE0)); SPI.transfer(characterMask); SPI.transfer(digitMask); SPI.endTransaction(); } #else inline void SegmentDisplay::writeDisplay(byte characterMask, byte digitMask) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, characterMask); shiftOut(dataPin, clockPin, MSBFIRST, digitMask); digitalWrite(latchPin, HIGH); } #endif