/*
 * task8 - task for os
 *
 * runs every 53 msec, takes 35 msec.
 *
 * $Id: display.c,v 1.6 2002/08/02 20:05:07 tomdean Exp $
 */

#include <os.h>
#include <lcd.h>        /* lcd display items */
#include <libeeprom.h>  /* lcd functions */
#include <sys/ports_def.h>
#include <gps.h>

unsigned char display_stack[STACK_SIZE];

/*
 * show task states in the designated row
 */
void show_states(unsigned char *ptr) {
  unsigned short idx;
  /* 1:s 2:s 3:s 4:s 5:s 6:s 7:s 8:s */
  *ptr++ = 'S';
  *ptr++ = 't';
  *ptr++ = 'a';
  *ptr++ = 't';
  *ptr++ = 'e';
  *ptr++ = 's';
  *ptr++ = ' ';
  for (idx=1; idx<NUM_TASK+1; idx++) {
	*ptr++ = (unsigned char)idx+'0';
	*ptr++ = ':';
	*ptr++ = context[idx]->state+'0';
	*ptr++ = ' ';
  }
  *ptr++ = ' ';
  *ptr++ = ' ';

  return;
}

/*
 * display task
 */
void display() {
#ifndef SHOW_GPS_DATA
  register unsigned short loop_count = 0;
#endif
#ifdef DEBUG
  register short idx;
#endif
  
  while (1) {

	DEBUG_ON(BIT03);

#ifdef SHOW_GPS_DATA
	gps_row_1[40] = 0;  /* null termination */
	lcd_write_row_1(gps_row_1);
	gps_row_2[40] = 0;  /* null termination */
	lcd_write_row_2(gps_row_2);
	gps_row_3[40] = 0;  /* null termination */
	lcd_write_row_3(gps_row_3);
	gps_row_4[40] = 0;  /* null termination */
	lcd_write_row_4(gps_row_4);
#else
	
	hex_to_ascii_4(++loop_count, TASK8_COUNT);
#ifdef DEBUG
	for (idx=0; idx<NUM_SCI; idx++)error1[19+idx] = sci_buf[idx].empty+'0';
	error1[40] = 0;  /* null termination */
	lcd_write_row_1(error1);
#else
	lcd_row_1[40] = 0;  /* null termination */
	lcd_write_row_1(lcd_row_1);
#endif
	
	show_states(lcd_row_2);
	lcd_row_2[40] = 0;  /* null termination */
	lcd_write_row_2(lcd_row_2);

#ifdef DEBUG
#ifdef SHOW_GPS_COUNT
	if (error3_in_use == 0) {
	  show_gps_count(error3);
	    
	  hex_to_ascii_4(gpbod_count + gpgga_count + gpgll_count
					 + gpgsa_count + gpgsv_count + gprmb_count
					 + gprmc_count + gprte_count + gpwpl_count
					 + pgrme_count + pgrmm_count + pgrmz_count
					 + ignore_count, error1+12);
	}
#endif  /* show gps counts */
	error3[40] = 0;  /* null termination */
	lcd_write_row_3(error3);
#else
	lcd_row_3[40] = 0;  /* null termination */
	lcd_write_row_3(lcd_row_3);
#endif  /* debug */
	
	lcd_row_4[40] = 0;  /* null termination */
	lcd_write_row_4(lcd_row_4);

	DEBUG_OFF(BIT03);

#endif /* if show gps data */
	
	SYS_DONE();
  }
}

/*
 * initialize the lcd and put some junk on it
 */
void display_init() {

  lcd_init();

  display_clear();

  lcd_write_row_1(lcd_row_1);
  lcd_write_row_2(lcd_row_2);
  lcd_write_row_3(lcd_row_3);
  lcd_write_row_4(lcd_row_4);

  return;
}

/*
 * clear the lcd display
 */
void display_clear() {
  short idx;
  
  for (idx=0; idx<40; idx++) {
	lcd_row_1[idx] = ' ';
	lcd_row_2[idx] = ' ';
	lcd_row_3[idx] = ' ';
	lcd_row_4[idx] = ' ';
	error1[idx] = ' ';
	error3[idx] = ' ';
  }
	
  lcd_row_1[40] = 0;
  lcd_row_2[40] = 0;
  lcd_row_3[40] = 0;
  lcd_row_4[40] = 0;
  error1[40]    = 0;
  error3[40]    = 0;

  return;
}
