File indexing completed on 2024-05-05 05:46:04

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2004 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "ecbcdto7segment.h"
0012 
0013 #include "libraryitem.h"
0014 #include "logic.h"
0015 
0016 #include <KLocalizedString>
0017 
0018 // Values for a,b,c,d,e,f,g of common-anode 7 segment display
0019 static bool numbers[16][7] = {{1, 1, 1, 1, 1, 1, 0},  // 0
0020                               {0, 1, 1, 0, 0, 0, 0},  // 1
0021                               {1, 1, 0, 1, 1, 0, 1},  // 2
0022                               {1, 1, 1, 1, 0, 0, 1},  // 3
0023                               {0, 1, 1, 0, 0, 1, 1},  // 4
0024                               {1, 0, 1, 1, 0, 1, 1},  // 5
0025                               {1, 0, 1, 1, 1, 1, 1},  // 6
0026                               {1, 1, 1, 0, 0, 0, 0},  // 7
0027                               {1, 1, 1, 1, 1, 1, 1},  // 8
0028                               {1, 1, 1, 0, 0, 1, 1},  // 9
0029                               {1, 1, 1, 0, 1, 1, 1},  // A
0030                               {0, 0, 1, 1, 1, 1, 1},  // b
0031                               {1, 0, 0, 1, 1, 1, 0},  // C
0032                               {0, 1, 1, 1, 1, 0, 1},  // d
0033                               {1, 0, 0, 1, 1, 1, 1},  // E
0034                               {1, 0, 0, 0, 1, 1, 1}}; // F
0035 
0036 Item *ECBCDTo7Segment::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0037 {
0038     return new ECBCDTo7Segment(static_cast<ICNDocument *>(itemDocument), newItem, id);
0039 }
0040 
0041 LibraryItem *ECBCDTo7Segment::libraryItem()
0042 {
0043     return new LibraryItem(QStringList(QString("ec/bcd_to_seven_segment")), i18n("BCD to 7 Segment"), i18n("Integrated Circuits"), "ic2.png", LibraryItem::lit_component, ECBCDTo7Segment::construct);
0044 }
0045 
0046 void ECBCDTo7Segment_inStateChanged(void *objV, bool isHigh) {
0047     ECBCDTo7Segment *objT = static_cast<ECBCDTo7Segment*>(objV);
0048     objT->inStateChanged(isHigh);
0049 }
0050 
0051 ECBCDTo7Segment::ECBCDTo7Segment(ICNDocument *icnDocument, bool newItem, const char *id)
0052     : Component(icnDocument, newItem, id ? id : "bcd_to_seven_segment")
0053 {
0054     m_name = i18n("BCD to Seven Segment");
0055 
0056     ALogic = BLogic = CLogic = DLogic = nullptr;
0057     ltLogic = rbLogic = enLogic = nullptr;
0058 
0059     for (int i = 0; i < 7; i++) {
0060         outLogic[i] = nullptr;
0061         oldOut[i] = false;
0062     }
0063 
0064     // QStringList pins = QStringList::split( ',', "A,B,C,D,,lt,rb,en,d,e,f,g,,a,b,c", true ); // 2018.12.01
0065     QStringList pins = QString("A,B,C,D,,lt,rb,en,d,e,f,g,,a,b,c").split(',', Qt::KeepEmptyParts);
0066     initDIPSymbol(pins, 48);
0067     initDIP(pins);
0068 
0069     ALogic = createLogicIn(ecNodeWithID("A"));
0070     BLogic = createLogicIn(ecNodeWithID("B"));
0071     CLogic = createLogicIn(ecNodeWithID("C"));
0072     DLogic = createLogicIn(ecNodeWithID("D"));
0073     ltLogic = createLogicIn(ecNodeWithID("lt"));
0074     rbLogic = createLogicIn(ecNodeWithID("rb"));
0075     enLogic = createLogicIn(ecNodeWithID("en"));
0076 
0077     //ALogic->setCallback(this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged));
0078     ALogic->setCallback2(ECBCDTo7Segment_inStateChanged, this);
0079     //BLogic->setCallback(this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged));
0080     BLogic->setCallback2(ECBCDTo7Segment_inStateChanged, this);
0081     //CLogic->setCallback(this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged));
0082     CLogic->setCallback2(ECBCDTo7Segment_inStateChanged, this);
0083     //DLogic->setCallback(this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged));
0084     DLogic->setCallback2(ECBCDTo7Segment_inStateChanged, this);
0085     //ltLogic->setCallback(this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged));
0086     ltLogic->setCallback2(ECBCDTo7Segment_inStateChanged, this);
0087     //rbLogic->setCallback(this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged));
0088     rbLogic->setCallback2(ECBCDTo7Segment_inStateChanged, this);
0089     //enLogic->setCallback(this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged));
0090     enLogic->setCallback2(ECBCDTo7Segment_inStateChanged, this);
0091 
0092     for (uint i = 0; i < 7; ++i) {
0093         outLogic[i] = createLogicOut(ecNodeWithID(QChar('a' + i)), false);
0094         //outLogic[i]->setCallback(this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged));
0095         outLogic[i]->setCallback2(ECBCDTo7Segment_inStateChanged, this);
0096     }
0097     inStateChanged(false);
0098 }
0099 
0100 ECBCDTo7Segment::~ECBCDTo7Segment()
0101 {
0102 }
0103 
0104 void ECBCDTo7Segment::inStateChanged(bool)
0105 {
0106     bool A = ALogic->isHigh();
0107     bool B = BLogic->isHigh();
0108     bool C = CLogic->isHigh();
0109     bool D = DLogic->isHigh();
0110     bool lt = ltLogic->isHigh(); // Lamp test
0111     bool rb = rbLogic->isHigh(); // Ripple Blank
0112     bool en = enLogic->isHigh(); // Enable (store)
0113 
0114     int n = A + 2 * B + 4 * C + 8 * D;
0115     //  if ( n > 9 ) n = 0;
0116 
0117     bool out[7];
0118 
0119     if (lt) // Lamp test
0120     {
0121         if (rb) // Ripple blanking
0122         {
0123             if (en) // Enable (store)
0124             {
0125                 for (int i = 0; i < 7; i++) {
0126                     out[i] = oldOut[i];
0127                 }
0128             } else {
0129                 for (int i = 0; i < 7; i++) {
0130                     out[i] = numbers[n][i];
0131                     oldOut[i] = out[i];
0132                 }
0133             }
0134         } else {
0135             for (int i = 0; i < 7; i++) {
0136                 out[i] = false;
0137             }
0138         }
0139     } else {
0140         for (int i = 0; i < 7; i++) {
0141             out[i] = true;
0142         }
0143     }
0144 
0145     for (int i = 0; i < 7; i++) {
0146         outLogic[i]->setHigh(out[i]);
0147     }
0148 }