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:
Mark van Renswoude 2016-09-30 23:14:29 +02:00
parent 15dc4ee679
commit 8bb9959f67
5 changed files with 95 additions and 117 deletions

View File

@ -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);
}

View File

@ -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);
}

76
SegmentDisplay.h Normal file
View File

@ -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

View File

@ -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