From 8bb9959f67f1d9a8325d8b9f72671f636b157a85 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Fri, 30 Sep 2016 23:14:29 +0200 Subject: [PATCH] Moved lib files to project root to be compatible with the standard Arduino IDE Implemented 2-digit counter as a test --- NerfStatTrek.ino | 61 +++++---------- lib/SegmentDisplay.cpp => SegmentDisplay.cpp | 4 +- SegmentDisplay.h | 76 +++++++++++++++++++ ...entDisplayChars.h => SegmentDisplayChars.h | 0 lib/SegmentDisplay.h | 71 ----------------- 5 files changed, 95 insertions(+), 117 deletions(-) rename lib/SegmentDisplay.cpp => SegmentDisplay.cpp (96%) create mode 100644 SegmentDisplay.h rename lib/SegmentDisplayChars.h => SegmentDisplayChars.h (100%) delete mode 100644 lib/SegmentDisplay.h 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