File indexing completed on 2024-04-21 05:43:35

0001 /***************************************************************************
0002  *   Copyright (C) 2003 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 "delay.h"
0012 
0013 #include "flowcode.h"
0014 #include "libraryitem.h"
0015 #include "icndocument.h"
0016 
0017 #include <KLocalizedString>
0018 
0019 Item *Delay::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0020 {
0021     return new Delay(static_cast<ICNDocument *>(itemDocument), newItem, id);
0022 }
0023 
0024 LibraryItem *Delay::libraryItem()
0025 {
0026     return new LibraryItem(QStringList(QString("flow/delay")), i18n("Delay"), i18n("Functions"), "delay.png", LibraryItem::lit_flowpart, Delay::construct);
0027 }
0028 
0029 Delay::Delay(ICNDocument *icnDocument, bool newItem, const char *id)
0030     : FlowPart(icnDocument, newItem, id ? id : "delay")
0031 {
0032     m_name = i18n("Delay");
0033     initProcessSymbol();
0034     createStdInput();
0035     createStdOutput();
0036 
0037     createProperty("delay_length", Variant::Type::Double);
0038     property("delay_length")->setCaption(i18n("Pause Length"));
0039     property("delay_length")->setUnit("sec");
0040     property("delay_length")->setValue(1.0);
0041 }
0042 
0043 Delay::~Delay()
0044 {
0045 }
0046 
0047 void Delay::dataChanged()
0048 {
0049     double delay = dataDouble("delay_length");
0050     setCaption(i18n("Delay for %1 sec", QString::number(delay / getMultiplier(delay), 'g', 3) + getNumberMag(delay)));
0051 }
0052 
0053 void Delay::generateMicrobe(FlowCode *code)
0054 {
0055     const double delayLength_ms = dataDouble("delay_length") * 1e3;
0056     code->addCode("delay " + QString::number(delayLength_ms));
0057     code->addCodeBranch(outputPart("stdoutput"));
0058 
0059     //  code->addVariable("COUNT_REPEAT");
0060 
0061 #if 0
0062     // Code for pauses less than 769uS
0063     if ( pauseLength < 769 )
0064     {
0065         code->addCodeBlock( id(),   "movlw " + QString::number(pauseLength/3) + "\n"
0066                                     "movwf COUNT_REPEAT\n"
0067                                     "call count_3uS\n"
0068                                     + gotoCode("stdoutput") );
0069                                     
0070         code->addCodeBlock( "count_3uS",    "decfsz COUNT_REPEAT,1\n"
0071                                             "goto count_3uS\n"
0072                                             "return" );
0073     }
0074     else if ( pauseLength < 196609 )
0075     {
0076         code->addVariable("COUNT_LOOP_1");
0077         
0078         code->addCodeBlock( id(),   "movlw " + QString::number(pauseLength/(3*256)) + "\n"
0079                                     "movwf COUNT_REPEAT\n"
0080                                     "call count_768uS\n"
0081                                     + gotoCode("stdoutput") );
0082                                     
0083         code->addCodeBlock( "count_768uS",  "decfsz COUNT_LOOP_1,1\n"
0084                                             "goto count_768uS\n"
0085                                             "decfsz COUNT_REPEAT,1\n"
0086                                             "goto count_768uS\n"
0087                                             "return" );
0088     }
0089     else if ( pauseLength < 50331649 )
0090     {
0091         code->addVariable("COUNT_LOOP_1");
0092         code->addVariable("COUNT_LOOP_2");
0093         
0094         code->addCodeBlock( id(),   "movlw " + QString::number(pauseLength/(3*256*256)) + "\n"
0095                                     "movwf COUNT_REPEAT\n"
0096                                     "call count_200mS\n"
0097                                     + gotoCode("stdoutput") );
0098                                     
0099         code->addCodeBlock( "count_200mS",  "decfsz COUNT_LOOP_1,1\n"
0100                                             "goto count_200mS\n"
0101                                             "decfsz COUNT_LOOP_2,1\n"
0102                                             "goto count_200mS\n"
0103                                             "decfsz COUNT_REPEAT,1\n"
0104                                             "goto count_200mS\n"
0105                                             "return" );
0106     }
0107     else/* if ( pauseLength < 12884901889 )*/
0108     {
0109         code->addVariable("COUNT_LOOP_1");
0110         code->addVariable("COUNT_LOOP_2");
0111         code->addVariable("COUNT_LOOP_3");
0112         
0113         code->addCodeBlock( id(),   "movlw " + QString::number(pauseLength/(3*256*256*256)) + "\n"
0114                                     "movwf COUNT_REPEAT\n"
0115                                     "call count_50S\n"
0116                                     + gotoCode("stdoutput") );
0117                                     
0118         code->addCodeBlock( "count_50S",    "decfsz COUNT_LOOP_1,1\n"
0119                                             "goto count_50S\n"
0120                                             "decfsz COUNT_LOOP_2,1\n"
0121                                             "goto count_50S\n"
0122                                             "decfsz COUNT_LOOP_3,1\n"
0123                                             "goto count_50S\n"
0124                                             "decfsz COUNT_REPEAT,1\n"
0125                                             "goto count_50S\n"
0126                                             "return" );
0127     }
0128 #endif
0129 }