/****************************************************************************************
 * Notice:  When compiling, be sure to use the flags -lX11, -lpthread and of course,    *
 *          be sure the DLP-2232PB-G is plugged into a USB port. I use:                 *
 *                                                                                      *
 *          cc -Wall -lX11 -lm -lpthread -o thermometer_monitor thermometer_monitor.c   *
 *                                                                                      *
 *          Pin 5 (AN1) of the DLP-2232PB-G is being used for the input.  Remember, its *
 *          input impedance is quite low and a preamp may be necessary.                 *
 *                                                                                      *
 *          Edit main() to try the various capabilities.                                *
 *          The thermometer routines need a pull-up resistor.  See ds18s20 spec sheet.  *
 *                                                                                      *
 *          Study the 16F877A port specs carefully.  Different pins/ports have varied   *
 *          capabilities - Schmidtt trigger, open-collector, TTL                        *
 *                                                                                      *
 *          Written by Craig Van Degrift <craig@yosemitefoothills.com>                  *
 *                     http://yosemitefoothills.com                                     *
 *                                                                                      *
 *          This demo code is only a best effort.  Use at your own risk.                *
 *          Uncomment the function calls in main() to try the different capabilities.   *
 *                                                                                      *
 *          Consider this code a framework which will need considerable modification    *
 *          to meet the needs of a particular measurement task.                         *
 *                                                                                      *
 ****************************************************************************************/

#include "core.h"
const char* device="/dev/ttyUSB1";
const char* dlpId="DLP-T0";
#include "dlp2232pbg.h"
//#include "scope.h"
#include "thermometers.h"
//#include "mavica.h"



int main(int argc, char** argv) {
//  double temperature_average = 0;
//  double variance = 0.0;
//  double standard_deviation;
//  int i;
  verbose = false;
  thermometer_init();
  echo_byte('C');
  check_id("DLP-T0");
  setup_a_to_d(Vdd_Vss, 0, 32);
 
  search_temp_sensors(PortB, Pin0);
//  search_temp_sensors(PortB, Pin5);
  verbose = true;
//  read_ds18s20(PortB, Pin0, "\x10\x48\xb6\x35\x01\x08\x00\xe8");
//  read_ds18s20(PortB, Pin5, "\x28\xc5\xe2\x14\x01\x00\x00\xae");
//  read_ds18s20(PortB, Pin5, "\x28\x18\xee\x14\x01\x00\x00\x01");
/*
  read_ds18s20(PortB, Pin0, "\x10\x48\xb6\x35\x01\x08\x00\xe8");
  read_ds18s20(PortB, Pin0, "\x10\x18\xbf\x35\x01\x08\x00\x35");
  read_ds18s20(PortB, Pin0, "\x10\xec\xbe\x35\x01\x08\x00\x56");
  read_ds18s20(PortB, Pin0, "\x10\xe2\xae\x35\x01\x08\x00\x39");
  read_ds18s20(PortB, Pin0, "\x10\x96\xb4\x35\x01\x08\x00\xbc");
  read_ds18s20(PortB, Pin0, "\x10\xf5\xad\x35\x01\x08\x00\xa9");
  read_ds18s20(PortB, Pin0, "\x10\xc3\x9c\x35\x01\x08\x00\xbf");
  read_ds18s20(PortB, Pin0, "\x10\x4b\xb5\x35\x01\x08\x00\xff");
  read_ds18s20(PortB, Pin0, "\x10\xc7\xab\x35\x01\x08\x00\xb6");
  read_ds18s20(PortB, Pin0, "\x10\x7f\xb1\x35\x01\x08\x00\xd1");
  read_ds18s20(PortB, Pin0, "\x28\xe0\xe4\x14\x01\x00\x00\x6f");
  read_ds18s20(PortB, Pin0, "\x28\x18\xe0\x14\x01\x00\x00\xa3");
  read_ds18s20(PortB, Pin0, "\x28\xb4\xf9\x14\x01\x00\x00\x0d");
  read_ds18s20(PortB, Pin0, "\x28\x9c\xfd\x14\x01\x00\x00\x05");
  read_ds18s20(PortB, Pin0, "\x28\xc2\xf8\x14\x01\x00\x00\xea");
  read_ds18s20(PortB, Pin0, "\x28\xd2\xde\x14\x01\x00\x00\xd5");
  read_ds18s20(PortB, Pin0, "\x28\xde\xec\x14\x01\x00\x00\xaf");
  read_ds18s20(PortB, Pin0, "\x28\xa1\xe2\x14\x01\x00\x00\xb1");
  read_ds18s20(PortB, Pin0, "\x28\x85\xf7\x14\x01\x00\x00\x75");
  read_ds18s20(PortB, Pin0, "\x28\xd7\xe4\x14\x01\x00\x00\x07");
*/
//  sio_deinit();
//exit(-1);
/*
  read_ds18s20(PortB, Pin0, "\x10\x48\xb6\x35\x01\x08\x00\xe8");
  read_ds18s20(PortB, Pin0, "\x10\x18\xbf\x35\x01\x08\x00\x35");
  read_ds18s20(PortB, Pin0, "\x10\xec\xbe\x35\x01\x08\x00\x56");
  read_ds18s20(PortB, Pin0, "\x10\xe2\xae\x35\x01\x08\x00\x39");
  read_ds18s20(PortB, Pin0, "\x10\x96\xb4\x35\x01\x08\x00\xbc");
  read_ds18s20(PortB, Pin0, "\x10\xf5\xad\x35\x01\x08\x00\xa9");
  read_ds18s20(PortB, Pin0, "\x10\xc3\x9c\x35\x01\x08\x00\xbf");
  read_ds18s20(PortB, Pin0, "\x10\x4b\xb5\x35\x01\x08\x00\xff");
  read_ds18s20(PortB, Pin0, "\x10\xc7\xab\x35\x01\x08\x00\xb6");
  read_ds18s20(PortB, Pin0, "\x10\x7f\xb1\x35\x01\x08\x00\xd1");
  read_ds18s20(PortB, Pin0, "\x28\xe0\xe4\x14\x01\x00\x00\x6f");
  read_ds18s20(PortB, Pin0, "\x28\x18\xe0\x14\x01\x00\x00\xa3");
  read_ds18s20(PortB, Pin0, "\x28\xb4\xf9\x14\x01\x00\x00\x0d");
  read_ds18s20(PortB, Pin0, "\x28\x9c\xfd\x14\x01\x00\x00\x05");
  read_ds18s20(PortB, Pin0, "\x28\xc2\xf8\x14\x01\x00\x00\xea");
  read_ds18s20(PortB, Pin0, "\x28\xd2\xde\x14\x01\x00\x00\xd5");
  read_ds18s20(PortB, Pin0, "\x28\xde\xec\x14\x01\x00\x00\xaf");
  read_ds18s20(PortB, Pin0, "\x28\xa1\xe2\x14\x01\x00\x00\xb1");
  read_ds18s20(PortB, Pin0, "\x28\x85\xf7\x14\x01\x00\x00\x75");
  read_ds18s20(PortB, Pin0, "\x28\xd7\xe4\x14\x01\x00\x00\x07");
  read_ds18s20(PortB, Pin0, "\x28\xef\x03\x15\x01\x00\x00\x4f");
  read_ds18s20(PortB, Pin5, "\x28\x18\xee\x14\x01\x00\x00\x01");
  read_ds18s20(PortB, Pin5, "\x28\xd8\xf4\x14\x01\x00\x00\x5f");
  read_ds18s20(PortB, Pin5, "\x28\xd8\xfd\x14\x01\x00\x00\xac");
  read_ds18s20(PortB, Pin5, "\x28\x94\xfc\x14\x01\x00\x00\x69");
  read_ds18s20(PortB, Pin5, "\x28\xf4\x00\x15\x01\x00\x00\xa2");
  read_ds18s20(PortB, Pin5, "\x28\xcc\x00\x15\x01\x00\x00\xee");
  read_ds18s20(PortB, Pin5, "\x28\x96\xfb\x14\x01\x00\x00\x56");
  read_ds18s20(PortB, Pin5, "\x28\x11\xde\x14\x01\x00\x00\x13");
  read_ds18s20(PortB, Pin5, "\x28\x89\xf4\x14\x01\x00\x00\x46");
  read_ds18s20(PortB, Pin5, "\x28\xb9\xe7\x14\x01\x00\x00\x99");
  read_ds18s20(PortB, Pin5, "\x28\xc5\xe2\x14\x01\x00\x00\xae");
  read_ds18s20(PortB, Pin5, "\x28\xfd\xef\x14\x01\x00\x00\x0e");
  read_ds18s20(PortB, Pin5, "\x28\xcb\xea\x14\x01\x00\x00\x83");
  read_ds18s20(PortB, Pin5, "\x28\x97\xef\x14\x01\x00\x00\x02");
*/
  read_ds18s20(PortB, Pin0, NULL);
  read_ds18s20(PortB, Pin5, NULL);
/*
  temperature_average = 0;
  while (T_i_finished != thermometer_count) sleep(1);
  for (i=1; i <= thermometer_count; i++)
    temperature_average += T[i].T_Celsius;
  temperature_average = temperature_average/thermometer_count;
  for (i=1; i <= thermometer_count; i++)
    variance += pow((T[i].T_Celsius - temperature_average), 2.0);
  standard_deviation = sqrt(variance/(thermometer_count-1));
  printf("Average temperature = %6.2f +- %3.2f degrees C ",temperature_average, standard_deviation);
  printf(" = %6.2f +- %3.2f degrees F\n",9*temperature_average/5+32, 9*standard_deviation/5);
*/

  thermometer_deinit();
  return 0;
}

