Moved lib files to project root to be compatible with the standard Arduino IDE
Implemented 2-digit counter as a test
This commit is contained in:
parent
15dc4ee679
commit
8bb9959f67
|
@ -1,57 +1,30 @@
|
||||||
#include "lib/SegmentDisplay.h"
|
#include "SegmentDisplay.h"
|
||||||
|
|
||||||
const int PinBottom = 2;
|
|
||||||
const int PinTop = 5;
|
|
||||||
|
|
||||||
const int PinDigit3 = 3;
|
|
||||||
const int PinDigit2 = 4;
|
|
||||||
|
|
||||||
|
SegmentDisplay* display;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(PinTop, OUTPUT);
|
display = new SegmentDisplay();
|
||||||
pinMode(PinBottom, OUTPUT);
|
//display->setClockSpeed(4000000UL);
|
||||||
pinMode(PinDigit3, OUTPUT);
|
display->setDigits(2);
|
||||||
pinMode(PinDigit2, OUTPUT);
|
display->begin();
|
||||||
|
|
||||||
digitalWrite(PinTop, LOW);
|
|
||||||
digitalWrite(PinBottom, LOW);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int lastTime = 0;
|
unsigned long lastCounterTime = 0;
|
||||||
bool top = true;
|
uint32_t counter = 0;
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
int currentTime = millis();
|
unsigned long currentTime = millis();
|
||||||
if (currentTime - lastTime > 1000)
|
|
||||||
|
if (currentTime - lastCounterTime > 1000UL)
|
||||||
{
|
{
|
||||||
top = !top;
|
counter++;
|
||||||
lastTime = currentTime;
|
if (counter > 99)
|
||||||
|
counter = 0;
|
||||||
|
|
||||||
|
lastCounterTime = currentTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Digit 3 (right) - switch between top and bottom
|
display->writeNumber(counter);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,6 @@ void SegmentDisplay::begin()
|
||||||
|
|
||||||
#if defined(SDSupportSPI)
|
#if defined(SDSupportSPI)
|
||||||
SPI.begin();
|
SPI.begin();
|
||||||
//SPI.setClockDivider(SPI_CLOCK_DIV128);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +30,7 @@ void SegmentDisplay::end()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void SegmentDisplay::writeNumber(int value)
|
void SegmentDisplay::writeNumber(uint32_t value)
|
||||||
{
|
{
|
||||||
byte digitMask = 1;
|
byte digitMask = 1;
|
||||||
|
|
||||||
|
@ -97,6 +96,7 @@ void SegmentDisplay::writeText(char* value)
|
||||||
inline void SegmentDisplay::writeDigit(byte value, byte digitMask)
|
inline void SegmentDisplay::writeDigit(byte value, byte digitMask)
|
||||||
{
|
{
|
||||||
writeDisplay(SDNumberSegments[value], digitMask);
|
writeDisplay(SDNumberSegments[value], digitMask);
|
||||||
|
delayMicroseconds(digitDelayMicroseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
Loading…
Reference in New Issue