/* audechob.c
   L. Stewart September 27, 1982  4:20 PM
   L. Stewart November 30, 1982  10:21 AM removed net stuff
   L. Stewart December 3, 1982  1:33 PM, added relays
   L. Stewart March 11, 1983  9:14 AM, new slave
 */

#include "Lark.h"
#include "Env.h"
#include "LarkSlave.h"
#include "LarkNet.h"

extern GetHex();

extern wf1();
extern wf();
extern wfcr();
extern StartSlave();
extern SetCodec();
extern GetDTMF();
extern ScanIn();
extern Led();
extern SideTone();
extern RingEnable();
extern Revert();
extern GoOffHook();
extern ARelay();
extern RevertHS();
extern SetXbar();
extern ClearXbar();
extern XbarClear();
extern SetIngain();
extern SetOutgain();

extern int igint;
extern int ogint;
extern int audioMode;
extern int first;
extern int *silval1;
extern int *silval2;
extern int *ingain1;
extern int *ingain2;
extern int *outgain;
extern int *bufptr;

extern int ledb;
extern int fbb;
extern int ohb;
extern int arb;
extern int hsrb;
extern int reb;
extern int stb;
extern int xrow;
extern int xcol;
extern int encb;

static int spmodeb;

static int oldAS;

ElecPhone()
  {
  int was;
  was = 0;
  wf("Electronic phone\r");
  Revert(false);
  RevertHS(false);
  XbarClear();
  SetXbar(1, 0);
  SetXbar(2, 1);
  for (;;) {
    Block();
    if (Chav()) break;
    if ((oldAS&AIHookSwitch) != was) {
      was = oldAS & AIHookSwitch;
      GoOffHook(was);
      ARelay(was);
      };
    };
  GetChar();
  GoOffHook(false);
  ARelay(false);
  };

PrintBP()
  {
  wf1("BPtr = %d\r", BPtr());
  };

NewAudio()
  {
  int i, prog, len;
  wf("audio program: ");
  prog = GetHex();
  switch (prog) {
    case 0: {
      StartSlave(0,0);
      break;
      };
    case 1: {
      StartSlave(1,0);
      break;
      };
    case 2: {
      StartSlave(2,0);
      break;
      };
    case 3: {
      GenProg();
      break;
      };
    case 4: {
      BltProg();
      break;
      };
    case 5: {
      JmpProg();
      break;
      };
    default: wf("1: O3I1, 2: O2I2, 3: arb, 4: BLT, 5: JMP\n");
    };
  };

SPMode()
  {
  int *mfp;
  mfp = (int *) ModeFlag;
  if (spmodeb) {
    *mfp &= 0xfffc;
    spmodeb = false;
    wf("speakerphone mode off\r");
    };
  else {
    *mfp |= 3;
    spmodeb = true;
    wf("speakerphone mode on\r");
    };
  };

static GenProg() {
  int prog, len;
  wf("\raudio program address (in main CPU) (hex): ");
  prog = GetHex();
  wf("\raudio code length (words)(hex): ");
  len = GetHex();
  wfcr();
  StartSlave(prog, len);
  };

static BltProg() {
  int from, to, count;
  wf("\r Slave blt from (hex): ");
  from = GetHex();
  wf("\r Slave blt to (hex): ");
  to = GetHex();
  wf("\r count (words) (hex): ");
  count = GetHex();
  wfcr();
  StoreW(from, BltFrom);
  StoreW(to, BltTo);
  StoreW(count, BltCount);
  StartSlave(4, 0);
  };

static JmpProg() {
  int prog;
  wf("\raudio program address in slave (hex): ");
  prog = GetHex();
  wfcr();
  StoreW(prog, BltFrom);
  StartSlave(5, 0);
  };

SetTS()
  {
  wf("Time slot (hex): ");
  SetCodec(GetHex());
  };

CheckAS()
  {
  int v, diff, newAS;
  newAS = ScanIn();
  if (oldAS != newAS) {
    newAS = ScanIn();
    diff = oldAS ↑ newAS;
    if (diff & AIDTMF) {
      v = GetDTMF();
      if (v == 0) v = '.';
      wf1("dtmf = %c\r", v);
      };
    if (diff & AISwitch) wf1("switch %s\r", OnOff(newAS & AISwitch));
    if (diff & AIRing) wf1("ring %s\r", OnOff(newAS & AIRing));
    if (diff & AIHookSwitch) wf1("hooksw %s\r", OnOff(newAS & AIHookSwitch));
    oldAS = newAS;
    };
  };

OnOff(i)
  int i;
  {
  if (i) return("on");
  else return("off");
  };

PrintAS()
  {
  int v;
  v = GetDTMF();
  if (v == 0) v = '.';
  wf1("dtmf = %c  ", v);
  v = ScanIn();
  if (v&AIRing) wf("ring ");
  else wf("     ");
  if (v&AIHookSwitch) wf("hooksw ");
  else wf("       ");
  if (v&AISwitch) wf("switch ");
  wfcr();
  };

TogEncrypt()
  {
  wf("Encryption ");
  togb(&encb);
  };

TogLED()
  {
  wf("Led ");
  togb(&ledb);
  Led(ledb);
  };

TogSideTone()
  {
  wf("SideTone ");
  togb(&stb);
  SideTone(stb);
  };

TogRingEnable()
  {
  wf("RingEnable ");
  togb(&reb);
  RingEnable(reb);
  };

togb(b)
  int *b;
  {
  if (*b) *b = false;
  else *b = true;
  wf1("%s\r", OnOff(*b));
  };

TogFallback()
  {
  if (fbb) fbb = false;
  else fbb = true;
  if (fbb) wf("Revert to back door\r");
  else wf("Teleset active\r");
  Revert(fbb);
  };

TogARelay()
  {
  if (arb) arb = false;
  else arb = true;
  if (arb) wf("ARelay Closed\r");
  else wf("ARelay Open\r");
  ARelay(arb);
  };

TogHSRelay()
  {
  if (hsrb) hsrb = false;
  else hsrb = true;
  if (hsrb) wf("HS Revert to back door\r");
  else wf("HS active\r");
  RevertHS(hsrb);
  };

TogOnHook()
  {
  if (ohb) ohb = false;
  else ohb = true;
  if (ohb) wf("Telewall Off Hook\r");
  else wf("Telewall On Hook\r");
  GoOffHook(ohb);
  };

XbarOn()
  {
  wf("xbar set ");
  GetRowCol();
  SetXbar(xrow, xcol);
  };

XbarOff()
  {
  wf("xbar clear ");
  GetRowCol();
  ClearXbar(xrow, xcol);
  };

static GetRowCol()
  {
  wf(" row ");
  xrow = GetHex();
  wf(" col ");
  xcol = GetHex();
  wfcr();
  };

xbarreset()
  {
  wf("xbar reset\r");
  XbarClear();
  };

int BPtr()
  {
  return(*bufptr);
  };

SetIG()
  {
  int v;
  wf1("ingain = %04x\r", *ingain1);
  wf("new value: ");
  v = GetHex();
  *ingain1 = v;
  *ingain2 = v;
  wf1("ingain = %04x\r", *ingain1);
  };

SetOG()
  {
  int v;
  wf1("outgain = %04x\r", *outgain);
  wf("new value: ");
  v = GetHex();
  *outgain = v;
  wf1("outgain = %04x\r", *outgain);
  };

PSV()
  {
  wf2("silval1 = %04x, silval2 = %04x\r", *silval1, *silval2);
  };

CycleIG()
  {
  igint = (igint + 1) % 5;
  wf1("input gain now %d dB\r", igint * -5);
  SetIngain(3, igint)
  };

CycleOG()
  {
  ogint = (ogint + 1) % 3;
  wf1("output gain now %d dB\r", ogint * -3);
  SetOutgain(ogint)
  };

TogEcho()
  {
  AudioIdle();
  if (audioMode == AMEcho) {
    audioMode = AMOff;
    wf("echo off\r");
    };
  else {
    audioMode = AMEcho;
    wf("echo on\r");
    };
  };