static void updatePressure (int live)
{
	int v ;
	v = live - 940 ;
	if (v <   0) v =   0 ;
	if (v > 120) v = 120 ;
 	genieWriteObj (GENIE_OBJ_COOL_GAUGE, 0, v) ;

}
static void updateTemp (int history [7], int value, int base, int thermometer)
{
  int i ;
  int v ;

  for (i = 0 ; i < 7 ; ++i)
  {
    v = history [i] + 10 ;
    if (v > 50) v = 50 ;
    if (v <  0) v =  0 ;

    genieWriteObj (GENIE_OBJ_GAUGE, base + i, v * 2) ;
  }

  v = value  + 10 ;
  if (v > 50) v = 50 ;
  if (v <  0) v =  0 ;
  genieWriteObj (GENIE_OBJ_THERMOMETER, thermometer, v) ;
}
static void updatePressure (int history [8], int live)
{
  int i ;
  int v ;

  for (i = 0 ; i < 8 ; ++i)
  {
    v = history [i] - 940 ;
    if (v < 0)
      v = 0 ;

    genieWriteObj (GENIE_OBJ_GAUGE, PRESSURE_BASE + i, v * 100 / 120) ;
  }

  v = live - 940 ;
  if (v <   0) v =   0 ;
  if (v > 120) v = 120 ;
  genieWriteObj (GENIE_OBJ_COOL_GAUGE, 0, v) ;
}
int main (int argc, char *argv[])
{
	if (argc > 1){
		if(strcmp(argv[1], "-L") ==0 ){
		display_LCO() ;
		exit (1);
		} else if(strcmp(argv[1], "-c") ==0) {
			int c = atoi(argv[2]);
			set_cap(c);
			exit (1);
		} else if(strcmp(argv[1], "-r") ==0){
			int i = atoi(argv[2]);
			int reg;
			reg = read_reg(i);
			printf("Value for Register %d is %#x \n", i, reg);
			fflush(stdout);
			exit (1);
		} else if(strcmp(argv[1], "-n") == 0 ){
//Create the thread
			pthread_t bmp085 ;
//Start the Calibration 
			AS3935_Calibration();
//Open file descriptor for dev/i2c only once 
			fd = bmp085_i2c_Begin();
			if (wiringPiSetupGpio () < 0) {
				fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
    				return 1 ;
  			}
// Start the interrupt reading (this creates a thread)
			wiringPiISR (BUTTON_PIN, INT_EDGE_RISING, &getdistance);
//Using the Raspberry Pi's on-board serial port.
			if (genieSetup ("/dev/ttyAMA0", 115200) < 0) {
    				fprintf (stderr, "rgb: Can't initialise Genie Display: %s\n", strerror (errno)) ;
				return 1 ;
			}
			genieWriteObj (GENIE_OBJ_FORM, 0, 0) ;
// Start the temperature and pressure sensor reading threads
			(void)pthread_create (&bmp085, NULL, handleTemperaturePressure, NULL) ;
			(void)pthread_join (bmp085, NULL);
			return 0 ;
  		} else {
		 usage();
		} 
	} else {
	 usage();
	}	
	return 0;
}
static void trend(int livep)
{
	int i ;
	int p;
	historyp [7] = livep;

	for (i = 0 ; i < 8 ; ++i){
		p = historyp [i] - 940 ;
		if (p < 0)
		p = 0 ;
		 genieWriteObj (GENIE_OBJ_GAUGE, PRESSURE_BASE + i, p * 100 / 120) ;
	}

	for (i = 1 ; i < 8 ; ++i)
 	historyp [i - 1] = historyp [i] ;
}
int main ()
{
  pthread_t myThread ;
  struct genieReplyStruct reply ;

  printf ("\n\n\n\n") ;
  printf ("Visi-Genie Weather Station Demo\n") ;
  printf ("===============================\n") ;

// Genie display setup
//	Using the Raspberry Pi's on-board serial port.

  if (genieSetup ("/dev/ttyAMA0", 115200) < 0)
  {
    fprintf (stderr, "rgb: Can't initialise Genie Display: %s\n", strerror (errno)) ;
    return 1 ;
  }

// Select form 0 (the temperature)

  genieWriteObj (GENIE_OBJ_FORM, 0, 0) ;

// Start the temperature and pressure sensor reading threads

  (void)pthread_create (&myThread, NULL, handleTemperature, NULL) ;
  (void)pthread_create (&myThread, NULL, handlePressure, NULL) ;

// Big loop - just wait for events from the display now

  for (;;)
  {
    while (genieReplyAvail ())
    {
      genieGetReply    (&reply) ;
      handleGenieEvent (&reply) ;
    }
    usleep (10000) ; // 10mS - Don't hog the CPU in-case anything else is happening...
  }

  return 0 ;
}
/*
** 	pushToDisplay
**
**	Pushes the given data to the appropriate object described by the options provided.
** 	opts - OptionsStruct with details about the object to be written
**	val - char* to be sent, for object types this will be converted into a uint
*/
int pushToDisplay (OptionsStruct opts, char *val)
{
    int result = 0;
    printf("pushToDisplay: opts->objType == %d\n", opts.objType);
    printf("pushToDisplay: opts->value == %s\n", opts.value);
    if (opts.objType == strObject)
    {
        printf("writing string %d with '%s'\n", opts.objIndex,  val);
        result = genieWriteStr(opts.objIndex, val);
    } else {
        unsigned int valInt = 0;
        valInt = atoi(val);
        if (strcmp("0", val) == 0 ||
                valInt != 0)
        {
            printf("writing object %d type %d with '%s'\n", opts.objIndex, opts.objTypeConst,  val);
            result = genieWriteObj(opts.objTypeConst, opts.objIndex, valInt);
        }
        else
            result =  1;
    }
    return result;
}
static void updateTemp (int value)
{
	int v ;
 	v = value + 10 ;
 	genieWriteObj (GENIE_OBJ_THERMOMETER, 0, v) ;
}