bidirled.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005 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 "bidirled.h"
00012 #include "colorcombo.h"
00013 #include "diode.h"
00014 #include "ecled.h"
00015 #include "ecnode.h"
00016 #include "libraryitem.h"
00017 #include "simulator.h"
00018 
00019 #include <klocale.h>
00020 #include <qpainter.h>
00021 
00022 Item* BiDirLED::construct(ItemDocument *itemDocument, bool newItem, const char *id)
00023 {
00024         return new BiDirLED((ICNDocument*)itemDocument, newItem, id);
00025 }
00026 
00027 LibraryItem* BiDirLED::libraryItem()
00028 {
00029         return new LibraryItem (
00030                 "ec/bidir_led",
00031                 i18n("Bidirectional LED"),
00032                 i18n("Outputs"),
00033                 "bidirled.png",
00034                 LibraryItem::lit_component,
00035                 BiDirLED::construct);
00036 }
00037 
00038 BiDirLED::BiDirLED(ICNDocument *icnDocument, bool newItem, const char *id)
00039         : Component(icnDocument, newItem, id ? id : "bidir_led")
00040 {
00041         m_name = i18n("Bidirectional LED");
00042         m_desc = i18n("Bidrectional Light Emitting Diode");
00043         m_bDynamicContent = true;
00044         
00045         setSize( -8, -16, 16, 32 );
00046         init1PinLeft();
00047         init1PinRight();
00048         setSize( -8, -24, 24, 40 );
00049         
00050         m_pDiode[0] = createDiode( m_pNNode[0], m_pPNode[0]);
00051         m_pDiode[1] = createDiode( m_pPNode[0], m_pNNode[0]);
00052         
00053         avg_brightness[0] = avg_brightness[1] = 255;
00054         lastUpdatePeriod = 0.;
00055         r[0]=r[1]=g[0]=g[1]=b[0]=b[1]=0;
00056         last_brightness[0] = last_brightness[1] = 255;
00057         
00058         createProperty( "0-color1", Variant::Type::Color );
00059         property("0-color1")->setCaption( i18n("Color 1") );
00060         property("0-color1")->setColorScheme( ColorCombo::LED );
00061         
00062         createProperty( "0-color2", Variant::Type::Color );
00063         property("0-color2")->setCaption( i18n("Colour 2") );
00064         property("0-color2")->setColorScheme( ColorCombo::LED );
00065 }
00066 
00067 BiDirLED::~BiDirLED()
00068 {
00069 }
00070 
00071 void BiDirLED::dataChanged()
00072 {
00073         QString colors[] = { "0-color1", "0-color2" };
00074         for ( unsigned i = 0; i < 2; i++ )
00075         {
00076                 QColor color = dataColor(colors[i]);
00077                 r[i] = color.red()   / 255.0f;
00078                 g[i] = color.green() / 255.0f;
00079                 b[i] = color.blue()  / 255.0f;
00080         }
00081 }
00082 
00083 void BiDirLED::stepNonLogic()
00084 {
00085         double interval = 1./LINEAR_UPDATE_RATE;
00086         lastUpdatePeriod += interval;
00087         
00088         for ( unsigned i = 0; i < 2; i++ )
00089                 avg_brightness[i] += ECLed::brightness(m_pDiode[i]->current())*interval;
00090 }
00091 
00092 void BiDirLED::drawShape( QPainter &p )
00093 {
00094         initPainter(p);
00095         
00096         for ( unsigned i = 0; i < 2; i++ ) {
00097                 uint _b;
00098                 if ( lastUpdatePeriod == 0. ) _b = last_brightness[i];
00099                 else {
00100                         _b = uint(avg_brightness[i]/lastUpdatePeriod);
00101                         last_brightness[i] = _b;
00102                 }
00103 
00104                 avg_brightness[i] = 0.;
00105         
00106                 p.setBrush( QColor( uint(255-(255-_b)*(1-r[i])), uint(255-(255-_b)*(1-g[i])), uint(255-(255-_b)*(1-b[i])) ) );
00107                 
00108                 
00109                 QPointArray pa(3);
00110                 if ( i == 0 ) {
00111                         pa[0] = QPoint( 8, -8 );
00112                         pa[1] = QPoint( -8, -16 );
00113                         pa[2] = QPoint( -8, 0 );
00114                 } else {
00115                         pa[0] = QPoint( -8, 8 );
00116                         pa[1] = QPoint( 8, 0 );
00117                         pa[2] = QPoint( 8, 16 );
00118                 }
00119                 
00120                 pa.translate( int(x()), int(y()));
00121                 p.drawPolygon(pa);
00122                 p.drawPolyline(pa);
00123         }
00124         lastUpdatePeriod = 0.;
00125                 
00126         // Draw the arrows indicating it's a LED
00127         int _x = (int)x()-2;
00128         int _y = (int)y()-21;
00129         
00130         p.drawLine( _x+9, _y+3, _x+12, _y ); // Tail of left arrow
00131         p.drawLine( _x+12, _y, _x+10, _y ); // Left edge of left arrow tip
00132         p.drawLine( _x+12, _y, _x+12, _y+2 ); // Right edge of left arrow tip
00133         
00134         p.drawLine( _x+12, _y+6, _x+15, _y+3 ); // Tail of right arrow
00135         p.drawLine( _x+15, _y+3, _x+13, _y+3 ); // Left edge of right arrow tip
00136         p.drawLine( _x+15, _y+3, _x+15, _y+5 ); // Right edge of right arrow tip
00137         
00138         p.drawLine( _x+10, _y, _x+15, _y+5 ); // Diagonal line that forms base of both arrow tips
00139         
00140         _x = int(x());
00141         _y = int(y());
00142         p.drawLine( _x+8, _y-16, _x+8, _y );
00143         p.drawLine( _x-8, _y, _x-8, _y+16 );
00144         
00145         deinitPainter(p);
00146 }
00147 

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