Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 96 additions & 7 deletions src/DigitLedDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define DISPLAYTEST_ADDR 15


DigitLedDisplay::DigitLedDisplay(int dinPin, int csPin, int clkPin) {
DigitLedDisplay::DigitLedDisplay(int dinPin, int csPin, int clkPin, int digitLimit) {
DIN_PIN = dinPin;
CS_PIN = csPin;
CLK_PIN = clkPin;
Expand All @@ -16,6 +16,14 @@ DigitLedDisplay::DigitLedDisplay(int dinPin, int csPin, int clkPin) {
pinMode(CS_PIN, OUTPUT);
pinMode(CLK_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);

setDigitLimit(digitLimit);
}

DigitLedDisplay::~DigitLedDisplay(){
if(currentDisplayState){
delete[] currentDisplayState;
}
}

void DigitLedDisplay::setBright(int brightness) {
Expand All @@ -25,6 +33,7 @@ void DigitLedDisplay::setBright(int brightness) {

void DigitLedDisplay::setDigitLimit(int limit) {
_digitLimit = limit;
currentDisplayState = new byte[_digitLimit];

write(DISPLAYTEST_ADDR, 0);
write(SCANLIMIT_ADDR, limit-1);
Expand All @@ -46,15 +55,15 @@ void DigitLedDisplay::off() {
}

void DigitLedDisplay::clear() {
for (int i = 1; i <=_digitLimit; i++) {
write(i, B00000000);
for (int i = 1; i <= _digitLimit; i++) {
writeDigit(i, DIGIT_BLANK);
currentDisplayState[i - 1] = DIGIT_BLANK;
}
}

void DigitLedDisplay::table(byte address, int val) {
byte tableValue;
tableValue = pgm_read_byte_near(charTable + val);
write(address, tableValue);
byte tableValue = pgm_read_byte_near(charTable + val);
writeDigit(address, tableValue);
}

void DigitLedDisplay::write(volatile byte address, volatile byte data) {
Expand All @@ -64,6 +73,12 @@ void DigitLedDisplay::write(volatile byte address, volatile byte data) {
digitalWrite(CS_PIN, HIGH);
}

void DigitLedDisplay::writeDigit(volatile byte address, volatile byte data) {
address = (address % _digitLimit) + 1;
write(address, data);
currentDisplayState[address - 1] = data;
}

void DigitLedDisplay::printDigit(long number, byte startDigit) {
String figure = String(number);
int figureLength = figure.length();
Expand All @@ -76,4 +91,78 @@ void DigitLedDisplay::printDigit(long number, byte startDigit) {
parseInt = (int) strtol(str, NULL, 10);
table(figureLength - i + startDigit, parseInt);
}
}
}

void DigitLedDisplay::printText(String number, int rightShift, bool clearDisplay) {
int figureLength = number.length();

int parseInt;
char str[2];

byte buffer[_digitLimit];
for(int i = 0; i < _digitLimit; i++){
if(clearDisplay){
buffer[_digitLimit - i - 1] = DIGIT_BLANK;
continue;
}
buffer[_digitLimit - i - 1] = currentDisplayState[i];
}

int printedDigits = 0;
int spacesInRow = 0;
for(int i = 0; i < figureLength; i++) {
int digitIndex = (printedDigits + rightShift) % _digitLimit;
if(digitIndex < 0){
digitIndex = _digitLimit + digitIndex;
}

if(number[i] != '.'){

if(number[i] == ' '){
spacesInRow++;
buffer[digitIndex] = DIGIT_BLANK;
if(spacesInRow % 2 == 0){
printedDigits++;
}
continue;
}
else if(number[i] == '-'){
buffer[digitIndex] = DIGIT_HYPHEN;
printedDigits++;
continue;
}
else if(number[i] == '_'){
buffer[digitIndex] = DIGIT_UNDERSCORE;
printedDigits++;
continue;
}

spacesInRow = 0;
str[0] = number[i];
str[1] = '\0';
parseInt = (int) strtol(str, NULL, 10);
byte tableValue = pgm_read_byte_near(charTable + parseInt);
buffer[digitIndex] = tableValue;
printedDigits++;
}
// Add period to previous digit
else if(printedDigits > 0 && number[i - 1] != ' ' && number[i - 1] != '.'){
if(digitIndex < 1){
digitIndex = (printedDigits - 1) % _digitLimit;
}
else {
digitIndex--;
}
buffer[digitIndex] = buffer[digitIndex] | DIGIT_PERIOD;
}
// Add period alone
else {
buffer[digitIndex] = DIGIT_PERIOD;
printedDigits++;
}
}

for(int i = 0; i < _digitLimit; i++){
writeDigit(i, buffer[_digitLimit - i - 1]);
}
}
24 changes: 19 additions & 5 deletions src/DigitLedDisplay.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#ifndef DigitLedDisplay_h
#define DigitLedDisplay_h

#if defined(__AVR__)
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif

#if (ARDUINO >= 100)
#include <Arduino.h>
Expand All @@ -13,23 +17,33 @@ const static byte charTable [] PROGMEM = {
B01111110,B00110000,B01101101,B01111001,B00110011,B01011011,B01011111,B01110000,B01111111,B01111011
};

const static byte DIGIT_BLANK = B00000000;
const static byte DIGIT_PERIOD = B10000000;
const static byte DIGIT_HYPHEN = B00000001;
const static byte DIGIT_UNDERSCORE = B00001000;


class DigitLedDisplay
{
private:
int DIN_PIN;
int CS_PIN;
int CLK_PIN;
int _digitLimit;
void table(byte address, int val);
byte* currentDisplayState;
void table(byte address, int val);
void setDigitLimit(int limit);
public:
DigitLedDisplay(int dinPin, int csPin, int clkPin);
DigitLedDisplay(int dinPin, int csPin, int clkPin, int digitLimit);
void setBright(int brightness);
void setDigitLimit(int limit);
void printDigit(long number, byte startDigit = 0);
void printText(String number, int rightShift = 0, bool clearDisplay = false);
void write(byte address, byte data);
void writeDigit(byte address, byte data);
void clear();
void on();
void off();
void off();
~DigitLedDisplay();
};

#endif //DigitLedDisplay.h
#endif //DigitLedDisplay.h