ecbcdto7segment.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2003-2004 by David Saxton                               *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "ecbcdto7segment.h"
00012 
00013 #include "logic.h"
00014 #include "libraryitem.h"
00015 
00016 #include <kiconloader.h>
00017 #include <klocale.h>
00018 
00019 // Values for a,b,c,d,e,f,g of common-anode 7 segment display
00020 static bool numbers[16][7] =
00021         { { 1, 1, 1, 1, 1, 1, 0 }, // 0
00022           { 0, 1, 1, 0, 0, 0, 0 }, // 1
00023           { 1, 1, 0, 1, 1, 0, 1 }, // 2
00024           { 1, 1, 1, 1, 0, 0, 1 }, // 3
00025           { 0, 1, 1, 0 ,0, 1, 1 }, // 4
00026           { 1, 0, 1, 1, 0, 1, 1 }, // 5
00027           { 1, 0, 1, 1, 1, 1, 1 }, // 6
00028           { 1, 1, 1, 0, 0, 0, 0 }, // 7
00029           { 1, 1, 1, 1, 1, 1, 1 }, // 8
00030           { 1, 1, 1, 0, 0, 1, 1 }, // 9
00031           { 1, 1, 1, 0, 1, 1, 1 }, // A
00032           { 0, 0, 1, 1, 1, 1, 1 }, // b
00033           { 1, 0, 0, 1, 1, 1, 0 }, // C
00034           { 0, 1, 1, 1, 1, 0, 1 }, // d
00035           { 1, 0, 0, 1, 1, 1, 1 }, // E
00036           { 1, 0, 0, 0, 1, 1, 1 } }; // F
00037 
00038 Item* ECBCDTo7Segment::construct( ItemDocument *itemDocument, bool newItem, const char *id )
00039 {
00040         return new ECBCDTo7Segment( (ICNDocument*)itemDocument, newItem, id );
00041 }
00042 
00043 LibraryItem* ECBCDTo7Segment::libraryItem()
00044 {
00045         return new LibraryItem(
00046                 QString::QString("ec/bcd_to_seven_segment"),
00047                 i18n("BCD to 7 Segment"),
00048                 i18n("Integrated Circuits"),
00049                 "ic2.png",
00050                 LibraryItem::lit_component,
00051                 ECBCDTo7Segment::construct
00052                         );
00053 }
00054 
00055 ECBCDTo7Segment::ECBCDTo7Segment( ICNDocument *icnDocument, bool newItem, const char *id )
00056         : Component( icnDocument, newItem, (id) ? id : "bcd_to_seven_segment" )
00057 {
00058         m_name = i18n("BCD to Seven Segment");
00059         m_desc = i18n("Converts a binary-coded-input to a form displayable by a seven segment display.<br><br>"
00060                                 "Normal operation: <i>lt</i> (Lamp Test) and the <i>rb</i> (Ripple Blanking) are held high, <i>en</i> (Enable) is held low.");
00061                                 
00062         ALogic = BLogic = CLogic = DLogic = 0;
00063         ltLogic = rbLogic = enLogic = 0;
00064         
00065         for ( int i=0; i<7; i++ ) {
00066                 outLogic[i] = 0;
00067                 oldOut[i] = false;
00068         }
00069 
00070         QStringList pins = QStringList::split( ',', "A,B,C,D,,lt,rb,en,d,e,f,g,,a,b,c", true );
00071         initDIPSymbol( pins, 48 );
00072         initDIP(pins);
00073         
00074         ALogic = createLogicIn( ecNodeWithID("A") );
00075         BLogic = createLogicIn( ecNodeWithID("B") );
00076         CLogic = createLogicIn( ecNodeWithID("C") );
00077         DLogic = createLogicIn( ecNodeWithID("D") );
00078         ltLogic = createLogicIn( ecNodeWithID("lt") );
00079         rbLogic = createLogicIn( ecNodeWithID("rb") );
00080         enLogic = createLogicIn( ecNodeWithID("en") );
00081         
00082         ALogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
00083         BLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
00084         CLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
00085         DLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
00086         ltLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
00087         rbLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
00088         enLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
00089         
00090         for ( uint i=0; i<7; ++i )
00091         {
00092                 outLogic[i] = createLogicOut( ecNodeWithID( QChar('a'+i) ), false );
00093                 outLogic[i]->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
00094         }
00095         inStateChanged(false);
00096 }
00097 
00098 ECBCDTo7Segment::~ECBCDTo7Segment()
00099 {
00100 }
00101 
00102 void ECBCDTo7Segment::inStateChanged(bool)
00103 {
00104         bool A = ALogic->isHigh();
00105         bool B = BLogic->isHigh();
00106         bool C = CLogic->isHigh();
00107         bool D = DLogic->isHigh();
00108         bool lt = ltLogic->isHigh(); // Lamp test
00109         bool rb = rbLogic->isHigh(); // Ripple Blank
00110         bool en = enLogic->isHigh(); // Enable (store)
00111 
00112         int n = A + 2*B + 4*C + 8*D;
00113 //      if ( n > 9 ) n = 0;
00114 
00115         bool out[7];
00116 
00117         if (lt) // Lamp test
00118         {
00119                 if (rb) // Ripple blanking
00120                 {
00121                         if (en) // Enable (store)
00122                         {
00123                                 for ( int i=0; i<7; i++ ) {
00124                                         out[i] = oldOut[i];
00125                                 }
00126                         } else {
00127                                 for ( int i=0; i<7; i++ ) {
00128                                         out[i] = numbers[n][i];
00129                                         oldOut[i] = out[i];
00130                                 }
00131                         }
00132                 } else {
00133                         for ( int i=0; i<7; i++ ) {
00134                                 out[i] = false;
00135                         }
00136                 }
00137         } else {
00138                 for ( int i=0; i<7; i++ ) {
00139                         out[i] = true;
00140                 }
00141         }
00142         
00143         for ( int i=0; i<7; i++ ) {
00144                 outLogic[i]->setHigh( out[i] );
00145         }
00146 }

Generated on Tue May 8 17:05:29 2007 for KTechLab by  doxygen 1.5.1