diff --git a/NerfStatTrek.ino b/NerfStatTrek.ino index 4b5c165..24d908a 100644 --- a/NerfStatTrek.ino +++ b/NerfStatTrek.ino @@ -1,57 +1,30 @@ -#include "lib/SegmentDisplay.h" - -const int PinBottom = 2; -const int PinTop = 5; - -const int PinDigit3 = 3; -const int PinDigit2 = 4; +#include "SegmentDisplay.h" +SegmentDisplay* display; void setup() { - pinMode(PinTop, OUTPUT); - pinMode(PinBottom, OUTPUT); - pinMode(PinDigit3, OUTPUT); - pinMode(PinDigit2, OUTPUT); - - digitalWrite(PinTop, LOW); - digitalWrite(PinBottom, LOW); + display = new SegmentDisplay(); + //display->setClockSpeed(4000000UL); + display->setDigits(2); + display->begin(); } -int lastTime = 0; -bool top = true; +unsigned long lastCounterTime = 0; +uint32_t counter = 0; void loop() { - int currentTime = millis(); - if (currentTime - lastTime > 1000) + unsigned long currentTime = millis(); + + if (currentTime - lastCounterTime > 1000UL) { - top = !top; - lastTime = currentTime; + counter++; + if (counter > 99) + counter = 0; + + lastCounterTime = currentTime; } - // Digit 3 (right) - switch between top and bottom - digitalWrite(PinDigit3, HIGH); - - if (top) - { - digitalWrite(PinTop, HIGH); - delay(1); - digitalWrite(PinTop, LOW); - } - else - { - digitalWrite(PinBottom, HIGH); - delay(1); - digitalWrite(PinBottom, LOW); - } - digitalWrite(PinDigit3, LOW); - - - // Digit 2 (middle) - always bottom - digitalWrite(PinDigit2, HIGH); - digitalWrite(PinBottom, HIGH); - delay(1); - digitalWrite(PinBottom, LOW); - digitalWrite(PinDigit2, LOW); + display->writeNumber(counter); } diff --git a/lib/SegmentDisplay.cpp b/SegmentDisplay.cpp similarity index 96% rename from lib/SegmentDisplay.cpp rename to SegmentDisplay.cpp index a10d352..15bec1a 100644 --- a/lib/SegmentDisplay.cpp +++ b/SegmentDisplay.cpp @@ -18,7 +18,6 @@ void SegmentDisplay::begin() #if defined(SDSupportSPI) SPI.begin(); - //SPI.setClockDivider(SPI_CLOCK_DIV128); #endif } @@ -31,7 +30,7 @@ void SegmentDisplay::end() } -void SegmentDisplay::writeNumber(int value) +void SegmentDisplay::writeNumber(uint32_t value) { byte digitMask = 1; @@ -97,6 +96,7 @@ void SegmentDisplay::writeText(char* value) inline void SegmentDisplay::writeDigit(byte value, byte digitMask) { writeDisplay(SDNumberSegments[value], digitMask); + delayMicroseconds(digitDelayMicroseconds); } diff --git a/SegmentDisplay.h b/SegmentDisplay.h new file mode 100644 index 0000000..1237798 --- /dev/null +++ b/SegmentDisplay.h @@ -0,0 +1,76 @@ +#ifndef SegmentDisplay_h +#define SegmentDisplay_h + +#include "Arduino.h" +#include "SPI.h" + +#include "SegmentDisplayChars.h" + +/** + * Drives up to 8 digits using two 8-bit shift registers. + * + * Pushes out the segment bits first and the display selection + * second: connect the display select register first and chain the + * segment register to it's output. + * + * Least significant bit ends up at pin 0, which correlates to + * (as defined in SegmentDisplayChars.h): + * - Last digit + * - Segment A +**/ + +// Comment to use shiftOut. Set clockPin and dataPin instead. +//#define SDSupportSPI + + +class SegmentDisplay +{ + public: + void begin(); + void end(); + + void writeNumber(uint32_t value); + void writeText(char* value); + + byte getDigits() { return digits; } + void setDigits(byte value) { digits = value; } + + uint32_t getLatchPin() { return latchPin; } + void setLatchPin(uint32_t value) { latchPin = value; } + + uint32_t getDigitDelayMicroseconds() { return digitDelayMicroseconds; } + void setDigitDelayMicroseconds(uint32_t value) { digitDelayMicroseconds = value; } + + #if defined(SDSupportSPI) + uint32_t getClockSpeed() { return clockSpeed; } + void setClockSpeed(uint32_t value) { clockSpeed = value; } + #endif + + #if !defined(SDSupportSPI) + uint32_t getClockPin() { return clockPin; } + void setClockPin(uint32_t value) { clockPin = value; } + + uint32_t getDataPin() { return dataPin; } + void setDataPin(uint32_t value) { dataPin = value; } + #endif + + protected: + void writeDigit(byte value, byte digitMask); + void writeDisplay(byte characterMask, byte digitMask); + + private: + byte digits = 6; + uint32_t digitDelayMicroseconds = 1000; + uint32_t latchPin = SS; + + #if defined(SDSupportSPI) + uint32_t clockSpeed = 20000000; + #endif + + #if !defined(SDSupportSPI) + uint32_t clockPin = SCK; + uint32_t dataPin = MOSI; + #endif +}; + +#endif \ No newline at end of file diff --git a/lib/SegmentDisplayChars.h b/SegmentDisplayChars.h similarity index 100% rename from lib/SegmentDisplayChars.h rename to SegmentDisplayChars.h diff --git a/lib/SegmentDisplay.h b/lib/SegmentDisplay.h deleted file mode 100644 index a4b96a0..0000000 --- a/lib/SegmentDisplay.h +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef SegmentDisplay_h -#define SegmentDisplay_h - -#include "Arduino.h" -#include "SPI.h" - -#include "SegmentDisplayChars.h" - -/** - * Drives up to 8 digits using two 8-bit shift registers. - * - * Pushes out the segment bits first and the display selection - * second: connect the display select register first and chain the - * segment register to it's output. - * - * Least significant bit as defined in SegmentDisplayChars.h: - * - Last digit - * - Segment A -**/ - -// Comment to use shiftOut. Set clockPin and dataPin instead. -#define SDSupportSPI - - -class SegmentDisplay -{ - public: - void begin(); - void end(); - - void writeNumber(int value); - void writeText(char* value); - - int getDigits() { return digits; } - void setDigits(int value) { digits = value; } - - int getLatchPin() { return latchPin; } - void setLatchPin(int value) { latchPin = value; } - - #if defined(SDSupportSPI) - long getClockSpeed() { return clockSpeed; } - void setClockSpeed(long value) { clockSpeed = value; } - #endif - - #if !defined(SDSupportSPI) - int getClockPin() { return clockPin; } - void setClockPin(int value) { clockPin = value; } - - int getDataPin() { return dataPin; } - void setDataPin(int value) { dataPin = value; } - #endif - - protected: - void writeDigit(byte value, byte digitMask); - void writeDisplay(byte characterMask, byte digitMask); - - private: - int digits = 6; - int latchPin = SS; - - #if defined(SDSupportSPI) - long clockSpeed = 20000000; - #endif - - #if !defined(SDSupportSPI) - int clockPin = SCK; - int dataPin = MOSI; - #endif -}; - -#endif \ No newline at end of file