Module: qbuttons.cpp

class QButtons
.h

constructorQButtons(int iCount)
destructor~QButtons()
Initvoid Init()
Flushvoid Flush()
HandleMsgvoid HandleMsg()

cmd[] contains message

Handlebool Handle()
Resetvoid Reset()
GetStateint GetState(int n)
GetLatchint GetLatch(int n)
SetLatchvoid SetLatch(int n,int state)
DbgShowStatevoid DbgShowState()


/*
 * QButtons - definition/implementation
 * NOTES:
 * - V&R interface
 * - Generated by mkclass
 * (C) ??-??-98 MarketGraph/RVG
 */

#include <qlib/buttons.h>

#define STX 2
#define ETX 3

QButtons::QButtons(int iCount)
{
  count=iCount;
  ser=new QSerial(QSerial::UNIT2,9600 /*,QSerial::WRITEONLY*/);
  if(!ser->IsOpen())
  { qerr("QButtons: serial not open");
  }
  cmdIndex=0;
  Reset();
}

QButtons::~QButtons()
{
  delete ser;
}

void QButtons::Init()
{
  char buf[2];
  qdbg("QButtons:Init\n");
  buf[0]=STX;
  buf[1]=ETX;
  ser->Write(buf,2);
}

void QButtons::Flush()
{ char c;
  while(ser->Read(&c,1)>0);
}

void QButtons::HandleMsg()
// cmd[] contains message
{
  char *s;
  int i,b;

  s=cmd;
  if(cmd[0]!=STX)return;
  s++;
  for(i=0;i<cmdIndex-2;i++,s++)
  { // Buttons
    for(b=0;b<4;b++)
    { if(*s&(1<<b))
      { // Press
        butLatch[i*4+b]=1;
        butState[i*4+b]=1;
      } else
      { // Release
        butState[i*4+b]=0;
      }
    }
  }
}
bool QButtons::Handle()
{ char buf[2],c;
  //qdbg("QButtons:Handle\n");
  if(!ser->CharsWaiting())return FALSE;
  //qdbg("  read\n");
  if(ser->Read(&c,1)<1)
  { return FALSE;
  }
  if(cmdIndex>=QB_CMDLEN)
    return FALSE;
  cmd[cmdIndex]=c;
  cmdIndex++;
  if(c==ETX)
  {
    HandleMsg();
    cmdIndex=0;
    return TRUE;
  }
  return FALSE;
  //qdbg("QButtons:Handle RET\n");
}

void QButtons::Reset()
{ int i;
  for(i=0;i<count;i++)
  { butState[i]=0;
    butLatch[i]=0;
  }
}

int QButtons::GetState(int n)
{
  return butState[n];
}

int QButtons::GetLatch(int n)
{
  return butLatch[n];
}
void QButtons::SetLatch(int n,int state)
{
  butLatch[n]=state;
}

/********
* DEBUG *
********/
void QButtons::DbgShowState()
{
  int i;
  qdbg("QButtons state: ");
  for(i=0;i<16;i++)
  { qdbg("%d",GetState(i));
  }
  qdbg(", latch: ");
  for(i=0;i<16;i++)
  { qdbg("%d",GetLatch(i));
  }
  qdbg("\n");
}