/* pause.c - parse and execute pause statement.
 *  Copyright (C) 2022 by Jed Marti
 *   This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see https://www.gnu.org/licenses/.
 */


#include <C8051F380.h>
#include <stdint.h>

#include "lfxmit.h"
#include "config.h"
#include "serial.h"
#include "storage.h"



// See morse.c
extern __xdata uint32_t          ditMilliseconds;
extern volatile __xdata uint32_t ditCounter;
extern volatile __bit            ditbit;
extern __xdata floatOrInt        IACC;
extern __xdata floatOrInt        FACC;
extern uint16_t                  DREG;

// Error and run time interface.
extern __bit                     doit;
extern __bit                     errorThrown;
__xdata uint32_t oldDitMilliseconds;



void runPause(void)
{

  // Expression is in milliseconds.
  incPC();
  parseExpression();
  if (errorThrown || !doit) return;


  // Look for ms millisecond(s) s second(s) m minute(s)
  DREG = popST();
  if (tag() == DESC_FLOAT) getFACC();
  else if (tag() == DESC_INT)
  {
    getIACC();
    FACC.f = (float) IACC.i;
  }
  else
  {
    putstring("PAUSE no count\n");
    errorThrown = 1;
    return;
  }
  if (getPByte() == MILLISECONDS) incPC();
  else if (getPByte() == SECONDS) { FACC.f *= 1000.0f; incPC(); }
  else if (getPByte() == MINUTES) { FACC.f *= 60000.0f; incPC(); }
  IACC.i = (int32_t) FACC.f;

  //  putstring("Waiting "); putint32(IACC.i); putstring(" milliseconds ...");
  oldDitMilliseconds = ditMilliseconds;
  ditMilliseconds = IACC.i;
  ditCounter = 0;
  ditbit = 0;
  TMR3 = RELOAD;
  EIE1 |= ET3;
  while(!ditbit);
  EIE1 &= ~ET3;
  //  terpri();

  // Restore dit size.
  ditMilliseconds = oldDitMilliseconds;
}


// EOF
