Add seven segment display array widget

master
Timothy Pearson 9 years ago
parent 83cee4e365
commit fb77cfbd28

@ -75,6 +75,26 @@ void Display7Segment::setLitSegments(unsigned char segs) {
prevSegments = segs;
}
unsigned char Display7Segment::segmentsForNumericDigit(unsigned char val, bool dp) {
unsigned char ret = 0;
if (val == 0) { ret = 0x7e; }
else if (val == 1) { ret = 0x30; }
else if (val == 2) { ret = 0x6d; }
else if (val == 3) { ret = 0x79; }
else if (val == 4) { ret = 0x33; }
else if (val == 5) { ret = 0x5b; }
else if (val == 6) { ret = 0x5f; }
else if (val == 7) { ret = 0x70; }
else if (val == 8) { ret = 0x7f; }
else if (val == 9) { ret = 0x73; }
if (dp) {
ret |= 0x80;
}
return ret;
}
void Display7Segment::drawContents( TQPainter *p )
{
// Draw all segments
@ -453,4 +473,149 @@ void Display7Segment::drawSegment(const TQPoint &pos, char segmentNo, TQPainter
#undef DARK
}
Display7SegmentArray::Display7SegmentArray( TQWidget *parent, const char *name )
: TQFrame( parent, name ),
m_numberOfDigits(0),
m_segmentStyle(Flat)
{
init();
}
Display7SegmentArray::~Display7SegmentArray() {
unsigned int i;
// Delete displays
for (i=0; i < m_numberOfDigits; i++) {
delete m_displayArray[i];
}
delete m_displayArray;
m_displayArray = NULL;
}
void Display7SegmentArray::init() {
unsigned int i;
// Set up grid layout
m_layout = new TQGridLayout(this, 1, m_numberOfDigits);
m_layout->setAutoAdd(true);
// Allocate display array
m_displayArray = (Display7Segment**)malloc(sizeof(Display7Segment*) * m_numberOfDigits);
// Create displays
for (i=0; i < m_numberOfDigits; i++) {
m_displayArray[i] = new Display7Segment(this);
}
setSizePolicy(TQSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Minimum));
// Set up displays
for (i=0; i < m_numberOfDigits; i++) {
m_displayArray[i]->setBackgroundColor(TQt::black);
m_displayArray[i]->setPaletteForegroundColor(TQColor(0, 255, 64));
}
// The LED display can work one of two ways
#if 0
// Separated segments
for (i=0; i < m_numberOfDigits; i++) {
m_displayArray[i]->setFrameStyle(TQFrame::Box | TQFrame::Raised);
}
setFrameStyle(TQFrame::NoFrame);
#else
// Combined segments
for (i=0; i < m_numberOfDigits; i++) {
m_displayArray[i]->setFrameStyle(TQFrame::NoFrame);
}
setBackgroundColor(TQt::black);
setFrameStyle(TQFrame::Box | TQFrame::Raised);
#endif
}
void Display7SegmentArray::setNumberOfDigits(unsigned int count) {
unsigned int i;
if (m_numberOfDigits != count) {
// Delete displays
if (m_displayArray) {
for (i=0; i < m_numberOfDigits; i++) {
delete m_displayArray[i];
}
delete m_displayArray;
m_displayArray = NULL;
}
// Destroy layout
if (m_layout) {
delete m_layout;
}
// Set display count
m_numberOfDigits = count;
// Reinitialize
init();
}
}
void Display7SegmentArray::setSegmentStyle(SegmentStyle s) {
unsigned int i;
m_segmentStyle = s;
for (i=0; i < m_numberOfDigits; i++) {
m_displayArray[i]->setSegmentStyle((Display7Segment::SegmentStyle)m_segmentStyle);
}
}
Display7SegmentArray::SegmentStyle Display7SegmentArray::segmentStyle() const {
return m_segmentStyle;
}
void Display7SegmentArray::setValue(double value, int maxDecimalLength, bool forceMinDecimalLength) {
bool dp;
unsigned int i;
TQString displayString = TQString("%1").arg(value);
if (maxDecimalLength >= 0) {
int decPos = displayString.find(".");
if (decPos >= 0) {
int decLen = (displayString.length() - 1) - decPos;
if (decLen > maxDecimalLength) {
displayString.truncate(displayString.length() - (decLen - maxDecimalLength));
}
}
if (forceMinDecimalLength) {
if (decPos < 0) {
displayString.append(".");
decPos = displayString.length() - 1;
}
for (int i = displayString.length(); i < decPos + maxDecimalLength + 1; i++) {
displayString.append("0");
}
}
}
int last_char = displayString.length();
int current_char = (last_char - (int)m_numberOfDigits);
if (displayString.contains(".")) {
current_char--;
}
for (i=0; i < m_numberOfDigits; i++) {
if (current_char < 0) {
m_displayArray[i]->setLitSegments(0x0);
}
else {
dp = false;
if (current_char < (displayString.length() - 1)) {
if (displayString[current_char + 1] == '.') {
dp = true;
}
}
if (displayString[current_char] == '.') {
current_char++;
}
m_displayArray[i]->setLitSegments(Display7Segment::segmentsForNumericDigit(TQString(displayString[current_char]).toInt(NULL, 10), dp));
}
current_char++;
}
}
#include "sevensegment.moc"

@ -24,6 +24,7 @@
#define REMOTELAB_DISPLAY7SEGEMENT_H
#include <tqframe.h>
#include <tqlayout.h>
class Display7SegmentPrivate;
@ -43,6 +44,7 @@ class TQ_EXPORT Display7Segment : public TQFrame
virtual void setSegmentStyle(SegmentStyle);
void setLitSegments(unsigned char);
static unsigned char segmentsForNumericDigit(unsigned char val, bool dp);
TQSize sizeHint() const;
@ -65,4 +67,33 @@ class TQ_EXPORT Display7Segment : public TQFrame
Display7SegmentPrivate *d;
};
class TQ_EXPORT Display7SegmentArray : public TQFrame
{
Q_OBJECT
TQ_ENUMS(SegmentStyle)
TQ_PROPERTY(SegmentStyle segmentStyle READ segmentStyle WRITE setSegmentStyle)
public:
Display7SegmentArray(TQWidget* parent=0, const char* name=0);
~Display7SegmentArray();
enum SegmentStyle { Outline, Filled, Flat };
SegmentStyle segmentStyle() const;
virtual void setSegmentStyle(SegmentStyle);
void setNumberOfDigits(unsigned int count);
void setValue(double value, int maxDecimalLength=-1, bool forceMinDecimalLength=false);
protected:
unsigned int m_numberOfDigits;
Display7Segment** m_displayArray;
TQGridLayout* m_layout;
SegmentStyle m_segmentStyle;
private:
void init();
};
#endif
Loading…
Cancel
Save