//============================================================================================
//      tc_mssgs.c            ver 2.0   (6/20/06)
//
//      copyright (c) 2001-2006 Vaux Electronics, Inc.
//============================================================================================


//============================================================================================
//
//  Public routine send_tc_sysinfo_mssg() sends -.TC,0,4,0,0,svh,svl <cr> sys-info message
//
//  Public routine send_tc_connect_mode_mssg() reads connection params from globals, and
//  sends -.TC,mod,num,px... connection messages
//
//  Public routine send_tc_error_mssg(err_id) sends -.TC,240,1,err_id <cr> error messages
//
//  Public routine send_tc_diag_mssg(err_id) sends -.TC,249,1,diag_id <cr> diagnostic-error messages
//
//  Public routine send_tc_mssg_params(params) sends header chars (-.TC), then sends
//  numeric portion of TC message as comma-delimited numbers, followed by a cr/lf.
//  A total of params bytes are sent as ascii-encoded-decimal numbers using one to three
//  characters.  If use_leading_zeroes_flg is OFF, then leading zeroes are suppressed (factory
//  setting: 0-255), if use_leading_zeroes_flg is ON, then all numbers have fixed three-digits
//  (000-255).  Flash STATUS light also.
//
//  Public routine output_digit(d) output digit as 1 to 3 chars
//
//    In main prog:
//
//          #include  "tc_mssgs.c"                                              // routines for TTY-Connect messages
//          ...
//          void main()
//          {
//            ...
//            use_leading_zeroes_flg = OFF;                                     // option: ON for fixed 3-char mssg params
//            ...
//          }
//
//============================================================================================
//
//      to do:
//          -
//
//      tc_mssgs.c            ver 2.0   (6/20/06)
//          - replaced short (was typedef) with int1
//          - replaced byte (was typedef) with int8
//
//      tc_mssgs.c            ver 1.0   (9/8/04)
//          - created from lattis mx_vp_mssgs.c
//
//============================================================================================


//  ----- prototypes:
void send_tc_sysinfo_mssg(void);                                                // sends -.TC,0,4,0,0,svh,svl <cr> sys-info message
void send_tc_connect_mode_mssg(void);                                           // sends -.TC,mod,num,px... connection messages
void send_tc_error_mssg(int8 err_id);                                           // sends -.TC,240,1,err_id <cr> error messages
void send_tc_diag_mssg(int8 diag_id);                                           // sends -.TC,249,1,diag_id <cr> diag-error messages

void send_tc_mssg_params(int8 params);                                          // sends message as "-.TC,x,x,x...<cr>"
void output_digit(int8 d);                                                      // outputs digit as 1 to 3 chars


// ----- defines:
#define       VP_MSG_SIZE                 16                                    // max data bytes (params) after mssg header


// ----- globals:
static int8   vp_msg[VP_MSG_SIZE];                                              // data byte buffer for current tc message
static int1   use_leading_zeroes_flg=OFF;                                       // option: ON for fixed 3-char mssg params


//--------------------------------------------------------------------------------------------
//  void send_tc_sysinfo_mssg(void)                     -.TC,0,4,0,0,svh,svl <cr> sys-info message
//
//  Public routine send_tc_sysinfo_mssg() sends -.TC,0,4,0,0,svh,svl <cr> sys-info message

void send_tc_sysinfo_mssg(void)
{
  vp_msg[0] = 0;                                                                // mssg-id = sysinfo mssg
  vp_msg[1] = 4;                                                                // num params
  vp_msg[2] = 0;                                                                // (reserved)
  vp_msg[3] = 0;                                                                // (reserved)
  vp_msg[4] = FIRMWARE_VERSION_HI;
  vp_msg[5] = FIRMWARE_VERSION_LO;
  send_tc_mssg_params(6);                                                       // send mssg and flash status led
}


//--------------------------------------------------------------------------------------------
//  void send_tc_connect_mode_mssg(void)                sends -.TC,mod,num,px... connection messages
//
//  Public routine send_tc_connect_mode_mssg() gets connection params from globals, and
//  sends -.TC,mod,num,px... connection messages

void send_tc_connect_mode_mssg(void)
{
  // ----- which connection mode?
  switch (mode)
  {

  // ----- mode 1:  Ascii PC to Baudot TTY/TU        // mssg:   -.TC,1,2,ptu,spd <cr>
  case 1:
    vp_msg[0] = 1;                                                              // mssg-id = connection-mode 1
    vp_msg[1] = 2;                                                              // num params to follow
    vp_msg[2] = port_B_port;                                                    // ptu
    vp_msg[3] = port_B_rate;                                                    // spd
    send_tc_mssg_params(4);                                                     // send mssg and flash status led
    break;

  // ----- mode 2:  Ascii PC to 110-Ascii TTY/TU    // mssg:   -.TC,2,1,ptu <cr>
  case 2:
    vp_msg[0] = 2;                                                              // mssg-id = connection-mode 2
    vp_msg[1] = 1;                                                              // num params to follow
    vp_msg[2] = port_B_port;                                                    // ptu
    send_tc_mssg_params(3);                                                     // send mssg and flash status led
    break;

  // ----- mode 3:  Raw PC to Raw TTY/TU            // mssg:   -.TC,3,3,bit,ptu,spx <cr>
  case 3:
    vp_msg[0] = 3;                                                              // mssg-id = connection-mode 3
    vp_msg[1] = 3;                                                              // num params to follow
    vp_msg[2] = port_B_port;                                                    // ptu
    vp_msg[3] = port_B_rate;                                                    // spx
    vp_msg[4] = port_B_bits;                                                    // bit
    send_tc_mssg_params(5);                                                     // send mssg and flash status led
    break;

  // ----- mode 4:  Baudot TU to Baudot TTY         // mssg:   -.TC,4,2,pty,spd <cr>
  case 4:
    vp_msg[0] = 4;                                                              // mssg-id = connection-mode 4
    vp_msg[1] = 2;                                                              // num params to follow
    vp_msg[2] = port_B_port;                                                    // pty
    vp_msg[3] = port_B_rate;                                                    // spd
    send_tc_mssg_params(4);                                                     // send mssg and flash status led
    break;

  // ----- mode 5:  Baudot TU to 110-Ascii TTY/TU   // mssg:   -.TC,5,2,spd,pty <cr>
  case 5:
    vp_msg[0] = 5;                                                              // mssg-id = connection-mode 5
    vp_msg[1] = 2;                                                              // num params to follow
    vp_msg[2] = port_A_rate;                                                    // spd (baudot port)
    vp_msg[3] = port_B_port;                                                    // pty (ascii port)
    send_tc_mssg_params(4);                                                     // send mssg and flash status led
    break;

  // ----- mode 6:  Baudot ports w/ speed change    // mssg:   -.TC,6,4,pta,spa,ptb,spb <cr>
  case 6:
    vp_msg[0] = 6;                                                              // mssg-id = connection-mode 6
    vp_msg[1] = 4;                                                              // num params to follow
    vp_msg[2] = port_A_port;                                                    // pta
    vp_msg[3] = port_A_rate;                                                    // spa
    vp_msg[4] = port_B_port;                                                    // ptb
    vp_msg[5] = port_B_rate;                                                    // spb
    send_tc_mssg_params(6);                                                     // send mssg and flash status led
    break;

  // ----- mode-10:  No-connection; loops to space  // mssg:   -.TC,10,0
  // ----- mode-11:  No-connection; loops to mark   // mssg:   -.TC,11,0
  case 10:
  case 11:
    vp_msg[0] = mode;                                                           // mssg-id = connection-mode 10 or 11
    vp_msg[1] = 0;                                                              // num params to follow
    send_tc_mssg_params(2);                                                     // send mssg and flash status led
    break;

  default:
    // ----- invalid mode param value:
    diag_code = DIAG_MSSG_MODE_PARAM;
    return;
  }
}


//--------------------------------------------------------------------------------------------
//  void send_tc_error_mssg(int8 err_id)                sends -.TC,240,1,err_id <cr> error messages
//
//  Public routine send_tc_error_mssg(err_id) sends -.TC,240,1,err_id error messages

void send_tc_error_mssg(int8 err_id)
{
  vp_msg[0] = 240;                                                              // mssg-id = error mssg
  vp_msg[1] = 1;                                                                // num params
  vp_msg[2] = err_id;
  send_tc_mssg_params(3);                                                       // send mssg and flash status led
}


//--------------------------------------------------------------------------------------------
//  void send_tc_diag_mssg(int8 diag_id)                sends -.TC,249,1,diag_id <cr> diag-error messages
//
//  Public routine send_tc_diag_mssg(diag_id) sends -.TC,249,1,diag_id diagnostic-error messages

void send_tc_diag_mssg(int8 diag_id)
{
  vp_msg[0] = 249;                                                              // mssg-id = diagnostic-error mssg
  vp_msg[1] = 1;                                                                // num params
  vp_msg[2] = diag_id;
  send_tc_mssg_params(3);                                                       // send mssg and flash status led
}


//--------------------------------------------------------------------------------------------
//  void send_tc_mssg_params(int8 params)           sends tc message as "-.TC,x,x,x...<cr>"
//
//  Public routine send_tc_mssg_params(params) sends header chars (-.TC), then sends
//  numeric portion of TC message as comma-delimited numbers, followed by a cr/lf.
//  A total of params bytes are sent as ascii-encoded-decimal numbers using one to three
//  characters.  If use_leading_zeroes_flg is OFF, then leading zeroes are suppressed (factory
//  setting: 0-255), if use_leading_zeroes_flg is ON, then all numbers have fixed three-digits
//  (000-255).  Flash STATUS light also.
//
//  The parameter bytes are stored in array vp_msg[i], where 0 <= i <= params-1.
//  The vp_msg[i] array currently has a max size of 16 bytes.

void send_tc_mssg_params(int8 params)
{
  int8 p;

  // ----- send header (less code space to let compiler loop for 4-char string, than four calls)
  //            ?? do we want to prefix messages with cr/lf ("\r\n-.TC") also ??
  send_sci_tx_byte("\r\n-.TC");

  for (p=0; p<params; p++)                                                      // all params in buffer
  {
    send_sci_tx_byte (',');                                                     // prepend delimiter

    output_digit(vp_msg[p]);                                                    // output digit as 1 to 3 chars
  }

  // ----- send cr/lf to end mssg (less code space with two calls, than compiler loop for 2-char string):
//  send_sci_tx_byte("\r\n");
  send_sci_tx_byte('\r');
  send_sci_tx_byte('\n');


  // ----- flash Status led:
  output_high (STATUS_LED);
  delay_ms (100);
  output_low (STATUS_LED);

}


//--------------------------------------------------------------------------------------------
//  void output_digit(int8 d)                           outputs digit as 1 to 3 chars
//
//  Public routine output_digit(d) output digit as 1 to 3 chars

void output_digit(int8 d)
{
  int8 digit_ms, digit_mid;

  digit_ms = d / 100;                                                           // calc MS digit
  d -= digit_ms * 100;
  if (use_leading_zeroes_flg || digit_ms)
    send_sci_tx_byte (digit_ms + '0');                                          // send MS digit (unless zero trapped)

  digit_mid = d / 10;                                                           // middle digit
  d -= digit_mid * 10;
  if (use_leading_zeroes_flg || digit_ms || digit_mid)
    send_sci_tx_byte (digit_mid + '0');                                         // send mid digit (unless zero trapped)

  send_sci_tx_byte (d + '0');                                                   // send LS digit
}
