// BigNum (Arduino)
//
// Illustrates displaying quantities using the Big Number (3-wide) format.
//
// Continually calls a stub program which returns temperatures in the range
// of -99.9 to 999.9 and display on the LCD in the 3-wide big number format.
//
// 9600 baud.
//
// Arduino                                 LCD #117
//
// TX  -----------------------------------> RX 
//
// Sets geometry of LCD as 4X20, sets backlight intensity to 40 hex and configures
// for no cursor.
//
// copyright, Peter H. Anderson, Baltimore, MD, May, '07

int MeasTemp(int sample_num);

void setup()
{

  Serial.begin(9600);
  delay(100);              // wait for things to settle
  Serial.print("?G420");  // must be 4 X 20 for big number format
  delay(100);             // wait for LCD #117 EEPROM to program
  Serial.print("?c0");   // turn cursor off
  delay(100);
  Serial.print("?B40"); // set backlight intensity
  delay(100);
}    

void loop()
{
  int n, T_10, Whole, Fract, sign_bit, Digit;
  for (n= 0; n<7; n++) // seven different temperatures
  {
      Serial.print("?f"); // clear the LCD
      delay(25);  // give it some time
      Serial.print("?>3");
      delay(200); // allow time for the special characters to be downloaded
      
      T_10 = MeasTemp(n);
      sign_bit = T_10 & 0x8000;
      if (sign_bit) // its negative
      {
         T_10 = T_10 ^ 0xffff + 1;  // or T_10 = -T_10
         Whole = T_10 / 10;       
         Fract = T_10 % 10;

         if (Whole > 9) // -10 to -99
         {
             Serial.print("-");
             Digit = Whole / 10;
             Serial.print(Digit);
             Digit = Whole % 10;
             Serial.print(Digit);
         }
         else         // -0.0 to - 9.9
         {
            Serial.print("?y0?x04");
            Serial.print("-");
            Digit = Whole;
            Serial.print(Digit);
         }
      }

      else        // its positive
      {
         Whole = T_10 / 10;
         Fract = T_10 % 10;
         if(Whole > 99) //  100.0 - 999.0
         {
            Digit = Whole / 100;
            Serial.print(Digit);
            Whole = Whole % 100;
            Digit = Whole / 10;
            Serial.print(Digit);
            Digit = Whole % 10;
            Serial.print(Digit);
         }
         else if(Whole > 9) // 10.0 to 99.0
         {
            Serial.print("?y0?x02");
            Digit = Whole / 10;
            Serial.print(Digit);
            Digit = Whole % 10;
            Serial.print(Digit);
         }
         else // 0.0 - 9.9
         {
            Serial.print("/?y0?x06");
            Digit = Whole;
            Serial.print(Digit);
         }
      }
         
      // now the fractional part
      Serial.print(".");
      Serial.print(Fract);

      Serial.print("?<");  // Display "Deg F" in normal text
      Serial.print("?y1?x17");
      Serial.print("Deg");
      Serial.print("?y2?x17");
      Serial.print(" F ");

      delay(5000);
  }
}

int MeasTemp(int sample_num)
{
// This is a stub which returns various values of a temerature T_10
   const int T_10[7] = {-123, -42, -8, 1234, 723, 72, 2};
                 // -12.3, -4.2, -0.8, 123.4, 72.3, 7.2, 0.2
   return(T_10[sample_num]);
}