#platform "Gen4-uLCD-35DT"





#inherit "4DGL_16bitColours.fnc"





func main()



    var Voffset0[2];

    var Voffset1[2];

    var Voffset2[2];

    var Voffset3[2];



    flt_VAL(Voffset0,"0.866");         //Where the tank low voltage goes  Left Aux

    flt_VAL(Voffset1,"0.008");         //Left Main

    flt_VAL(Voffset2,"0.005");         //Right Main

    flt_VAL(Voffset3,"0.701");         //Right Aux



    var Voffset0B[2];                  // Voffset in binary fp

    var Voffset0I;                     // Voffset in binary integer

    var Voffset1B[2];

    var Voffset1I;

    var Voffset2B[2];

    var Voffset2I;

    var Voffset3B[2];

    var Voffset3I;

    var Vsteps0B[2];

    var Vsteps0f[2];                   // fp version of  Vsteps

    var Vsteps1B[2];

    var Vsteps1f[2];

    var Vsteps2B[2];

    var Vsteps2f[2];

    var Vsteps3B[2];

    var Vsteps3f[2];

    var Voltsf[2] ;

    var FuelFlowf[2] ;

    var FuelFlow ;

    var FuelFlowDeg ;

    var Brightness;



    var Vsteps0;                       // variable to store conversion results

    var Vsteps1;                       // variable to store conversion results

    var Vsteps2;                       // variable to store conversion results

    var Vsteps3;                       // variable to store conversion results



    var n;                             // variable for counting

    var m;

    var Volts ;



    var x[2];

    var y[2];



    var AudioOn ;

    var State;

    var ChargeDischarge ;





    gfx_ScreenMode(PORTRAIT_R) ;       // orientation

    //gfx_Contrast(15) ;               // Backlight level    This is where brightness will be controled

    drawBackground();                  // draw labels onscreen. This function is defined below.



    //configure pins PA0, PA1, PA2, PA3

    pin_Set(PIN_AN, PA0);              // set pin PA0 to be used as an analogue input left aux, standard mode

    //pin_Set(PIN_ANAVG, PA0);         // set pin PA0 to be used as an analogue input, average  mode



    pin_Set(PIN_AN, PA1);              // set pin PA1 to be used as an analogue input left main, standard mode

    //pin_Set(PIN_ANAVG, PA1);         // set pin PA1 to be used as an analogue input, average  mode



    pin_Set(PIN_AN, PA2);              // set pin PA2 to be used as an analogue input right main, standard mode

    //pin_Set(PIN_ANAVG, PA2);         // set pin PA2 to be used as an analogue input, average  mode



    pin_Set(PIN_AN, PA3);              // set pin PA3 to be used as an analogue input right aux, standard mode

    //pin_Set(PIN_ANAVG, PA3);         // set pin PA3 to be used as an analogue input, average  mode



    pin_Set(PIN_OUT, PA4);

    pin_Set(PIN_OUT, PA5);

    pin_Set(PIN_OUT, PA6);

    pin_Set(PIN_INP, PA7);





    repeat



        //Set brightness

        pin_HI (PA5);

        pin_HI (PA6);

        pause (50);



        Brightness := pin_Read(PA0) ;       //4 steps 4095/1350 , +12 min offset

        Brightness /= 1350;

        Brightness += 12 ;

        gfx_Contrast(Brightness) ;          // Backlight level    This is where brightness will be controled

        ////NEED to emove later

        txt_Width(1);

        txt_Height(1);

        gfx_MoveTo(12,0);

        print(Brightness);





    //Get Fuel levels and display

        pin_LO (PA5);

        pin_LO (PA6);

        pause (50);

        Vsteps0 := pin_Read(PA0) ;          // 12 bit analogue 0 to 4095

        Vsteps1 := pin_Read(PA1) ;          // 12 bit analogue 0 to 4095

        Vsteps2 := pin_Read(PA2) ;          // 12 bit analogue 0 to 4095

        Vsteps3 := pin_Read(PA3) ;          // 12 bit analogue 0 to 4095



        flt_VAL(x,"819");

        flt_MUL(Voffset0B,Voffset0,x);      //The offset converted from voltage to fp bit in 0-4095 range

        Voffset0I := flt_FTOI(Voffset0B);   //Convert to integer

        Vsteps0 -= Voffset0I;               // ubtract Voffset0I integer version

    if(Vsteps0 <= 0)                        // If Vsteps offset is less than 0, set to 0

        Vsteps0 := 0;

    endif

        flt_ITOF(Vsteps0f,Vsteps0);

        flt_VAL(x,"5");

        flt_MUL(Vsteps0f,Vsteps0f,x);       //Multiply Voffset by 5

        flt_SUB(y,x,Voffset0);              //y=5-Voffset

        flt_DIV(x,Vsteps0f,y);              //x= 5*Vstepsf/(5- Voffset)

        Vsteps0 := flt_FTOI(x);             //Convert shifted and scaled Vsteps back to integer





        //scale down the range 0 to 4095 of Vsteps to a range of 0 to 187

        //so we can graph the results onscreen. Multiply by 8, divid by 179

        Vsteps0 *=   8 ;                    // 0 to 32760

        Vsteps0 /= 179 ;                    // 0 to 187



        flt_VAL(x,"819");

        flt_MUL(Voffset1B,Voffset1,x);      //The offset converted from voltage to fp bit in 0-4095 range

        Voffset1I := flt_FTOI(Voffset1B);   //Convert to integer

        Vsteps1 -= Voffset1I;               // subtract Voffset0I integer version

    if(Vsteps1 <= 0)                        // If Vsteps offset is less than 0, set to 0

        Vsteps1 := 0;

    endif

        flt_ITOF(Vsteps1f,Vsteps1);

        flt_VAL(x,"5");

        flt_MUL(Vsteps1f,Vsteps1f,x);       //Multiply Voffset by 5

        flt_SUB(y,x,Voffset1);              //y=5-Voffset

        flt_DIV(x,Vsteps1f,y);              //x= 5*Vstepsf/(5- Voffset)

        Vsteps1 := flt_FTOI(x);             //Convert shifted and scaled Vsteps back to integer



        Vsteps1 *=   8 ;                    // 0 to 32760

        Vsteps1 /= 179;                     // 0 to 187



        flt_VAL(x,"819");

        flt_MUL(Voffset2B,Voffset2,x);      //The offset converted from voltage to fp bit in 0-4095 range

        Voffset2I := flt_FTOI(Voffset2B);   //Convert to integer

        Vsteps2 -= Voffset2I;               //    subtract Voffset0I integer version

    if(Vsteps2 <= 0)                        // If Vsteps offset is less than 0, set to 0

        Vsteps2 := 0;

    endif

        flt_ITOF(Vsteps2f,Vsteps2);

        flt_VAL(x,"5");

        flt_MUL(Vsteps2f,Vsteps2f,x);       //Multiply Voffset by 5

        flt_SUB(y,x,Voffset2);              //y=5-Voffset

        flt_DIV(x,Vsteps2f,y);              //x= 5*Vstepsf/(5- Voffset)

        Vsteps2 := flt_FTOI(x);             //Convert shifted and scaled Vsteps back to integer



        Vsteps2 *=   8 ;                    // 0 to 32760

        Vsteps2 /= 179 ;                    // 0 to 187



        flt_VAL(x,"819");

        flt_MUL(Voffset3B,Voffset3,x);      //The offset converted from voltage to fp bit in 0-4095 range

        Voffset3I := flt_FTOI(Voffset3B);   //Convert to integer

        Vsteps3 -= Voffset3I;               //    subtract Voffset0I integer version

    if(Vsteps3 <= 0)                        // If Vsteps offset is less than 0, set to 0

        Vsteps3 := 0;

    endif

        flt_ITOF(Vsteps3f,Vsteps3);

        flt_VAL(x,"5");

        flt_MUL(Vsteps3f,Vsteps3f,x);       //Multiply Voffset by 5

        flt_SUB(y,x,Voffset3);              //y=5-Voffset

        flt_DIV(x,Vsteps3f,y);              //x= 5*Vstepsf/(5- Voffset)

        Vsteps3 := flt_FTOI(x);             //Convert shifted and scaled Vsteps back to integer



        Vsteps3 *=   8 ;                    // 0 to 32760

        Vsteps3 /= 179 ;                    // 0 to 187



        //This section creates the bar graph with top section black, bottom color

        //use the scaled value of Vsteps to draw rectangles

        gfx_RectangleFilled( 61, 291, 89, 475 - Vsteps0, BLACK) ;   // top rectangle (black)

        gfx_RectangleFilled( 131, 291, 159, 475 - Vsteps1, BLACK) ; // top rectangle (black)

        gfx_RectangleFilled( 201, 291, 229, 475 - Vsteps2, BLACK) ; // top rectangle (black)

        gfx_RectangleFilled( 270, 291, 299, 475 - Vsteps3, BLACK) ; // top rectangle (black)







        gfx_RectangleFilled( 61,  475 - Vsteps0, 89, 475, BLUE) ;   // bottom rectangle (LIME) (RM changed to 317)

        gfx_RectangleFilled( 131,  475 - Vsteps1, 159, 475, BLUE ) ;// bottom rectangle (LIME) (RM changed to 317)

        gfx_RectangleFilled( 201,  475 - Vsteps2, 229, 475, BLUE ) ;// bottom rectangle (LIME) (RM changed to 317)

        gfx_RectangleFilled( 271,  475 - Vsteps3, 299, 475, BLUE ) ;// bottom rectangle (LIME) (RM changed to 317)



//draw multiple horizontal lines  These are the spaced dividers

        for(n := 334; n < 473; n += 47)

            gfx_Line(61, n, 89, n, WHITE);  //(RM changed to 317)

        next



        for(n := 334; n < 473; n += 47)

            gfx_Line(131, n, 159, n, WHITE);  //(RM changed to 317)

        next



        for(n := 334; n < 473; n += 47)

            gfx_Line(201, n, 229, n, WHITE);

        next



        for(n := 334; n < 473; n += 47)

            gfx_Line(270, n, 299, n, WHITE);

        next



//Touch audio on/off detection and button set

    touch_Set(TOUCH_ENABLE);

    touch_Set(2) ;

    State := touch_Get(TOUCH_STATUS);



    if (State ==1)

        if (AudioOn == 1)

            AudioOn := 0;

            pin_LO(PA4);

            else

                if (AudioOn == 0)

                AudioOn := 1;

                pin_HI(PA4);

                endif

        endif

    endif



    if (AudioOn == 1 )

         gfx_CircleFilled(293,206,12,RED) ;

           else

             gfx_CircleFilled(293,206,12,LIME) ;

    endif



//Drive charge/discharge indicator

     ChargeDischarge := pin_Read(PA7);

     if (ChargeDischarge == 1)                               //High = charge = green

        gfx_TriangleFilled(228,49,212,49,219,33,LIME);

        gfx_TriangleFilled(228,62,212,62,219,77,BLACK);

        else

            gfx_TriangleFilled(228,62,212,62,219,77,RED);

            gfx_TriangleFilled(228,49,212,49,219,33,BLACK);

     endif



  //Fuel flow display

      gfx_RingSegment(95, 100, 58, 56, 135, 405, RED);       //Perimeter rings



    // Draw fuel flow arc





    pin_HI (PA5);

    pin_LO (PA6);

    pause (50);

    FuelFlow := pin_Read(PA0);      //Need to change later Read the fuel flow

    FuelFlow *=   6 ;               // 0 to 32760

    FuelFlow /= 91 ;                // 0 to 187

    FuelFlow += 135;



      gfx_RingSegment(95, 100, 70, 60, FuelFlow, 405,BLACK);

      gfx_RingSegment(95, 100, 70, 60, 135, FuelFlow, PURPLE);       //Graph







  //Fuel flow radial markers



    var Target1ix, Target1iy;                                //Radial line segments for fuel flow

    var Target1ox, Target1oy;



    gfx_OrbitInit(&Target1ix,&Target1iy);

    gfx_MoveTo(95, 100) ;

    gfx_Orbit(135,56);

    gfx_OrbitInit(&Target1ox,&Target1oy);

    gfx_MoveTo(95, 100) ;

    gfx_Orbit(135,81);

    gfx_Line(Target1ix,Target1iy,Target1ox,Target1oy,WHITE);



    for(n := 162; n < 379; n += 27)

        gfx_OrbitInit(&Target1ix,&Target1iy);

        gfx_MoveTo(95, 100) ;

        gfx_Orbit(n,60);

        gfx_OrbitInit(&Target1ox,&Target1oy);

        gfx_MoveTo(95, 100) ;

        gfx_Orbit(n,72);

        gfx_Line(Target1ix,Target1iy,Target1ox,Target1oy,WHITE);



    next



    for(n := 163; n < 380; n += 27)

        gfx_OrbitInit(&Target1ix,&Target1iy);

        gfx_MoveTo(95, 100) ;

        gfx_Orbit(n,60);

        gfx_OrbitInit(&Target1ox,&Target1oy);

        gfx_MoveTo(95, 100) ;

        gfx_Orbit(n,72);

        gfx_Line(Target1ix,Target1iy,Target1ox,Target1oy,WHITE);



    next



    gfx_OrbitInit(&Target1ix,&Target1iy);

    gfx_MoveTo(95, 100) ;

    gfx_Orbit(405,56);

    gfx_OrbitInit(&Target1ox,&Target1oy);

    gfx_MoveTo(95, 100) ;

    gfx_Orbit(405,81);

    gfx_Line(Target1ix,Target1iy,Target1ox,Target1oy,WHITE);





  //Display analog battery voltage



    pin_LO (PA5);

    pin_HI (PA6);

    pause (50);



    Volts := pin_Read(PA0) ;



    flt_ITOF(Voltsf,Volts);

        flt_VAL(x,"256");

        flt_DIV(Voltsf,Voltsf,x);

        txt_FGcolour(YELLOW);



        txt_Width(2);

        txt_Height(2);

        gfx_MoveTo(225, 152) ;

        flt_PRINT(Voltsf, "%.2f" );



//Display Fuel flow digital numbers



    pin_HI (PA5);

    pin_LO (PA6);

    pause (50);



    FuelFlow := pin_Read(PA0) ;



    flt_ITOF(FuelFlowf,FuelFlow);

        flt_VAL(x,"204.75");

        flt_DIV(FuelFlowf,FuelFlowf,x);

        txt_FGcolour(YELLOW);



    txt_Width(2);

    txt_Height(2);

    gfx_MoveTo(70, 120) ;

    flt_PRINT(FuelFlowf, "%.1f" );



    pause(400); //to slow down sampling rate (changed 200 to 500 RM)



    forever



endfunc





func drawBackground()



    txt_FontID(FONT_3);



    gfx_Rectangle(60, 290, 91, 477, RED);      //(****Added surround rectangle right main RM)

    gfx_Rectangle(59, 289, 90, 476, RED);      //(****Added surround rectangle RM)



    gfx_Rectangle(130, 290, 161, 477, RED);    //(****Added surround rectangle left main RM)

    gfx_Rectangle(129, 289, 160, 476, RED);    //(****Added surround rectangle RM)



    gfx_Rectangle(200, 290, 231, 477, RED);    //(****Added surround rectangle left aux RM)

    gfx_Rectangle(199, 289, 230, 476, RED);    //(****Added surround rectangle RM)



    gfx_Rectangle(270, 290, 301, 477, RED);    //(****Added surround rectangle right aux RM)

    gfx_Rectangle(269, 289, 300, 476, RED);    //(****Added surround rectangle RM)





    txt_FontID(FONT_5);

    txt_Width(2);                 // character width multiplier set to 3

    txt_Height(2);                // character height multiplier set to 3



    txt_FGcolour(GHOSTWHITE);                   //change text foreground color to yellow

    gfx_MoveTo(99, 235) ;                    //move origin to point 0, 108, printing will start from this point

    print("FUEL LEVEL") ;                   //print this text onscreen



    txt_FontID(FONT_3);

    txt_Width(1);                 // character width multiplier set to 3

    txt_Height(1);                // character height multiplier set to 3



    txt_FGcolour(YELLOW);                   //change text foreground color to yellow

    gfx_MoveTo(60, 265) ;                    //move origin to point 0, 108, printing will start from this point

    print("Left \nAux") ;                   //print this text onscreen



    txt_FGcolour(YELLOW);                   //change text foreground color to yellow

    gfx_MoveTo(128, 265) ;                    //move origin to point 0, 108, printing will start from this point

    print("Left\nMain") ;                   //print this text onscreen



    txt_FGcolour(YELLOW);                   //change text foreground color to yellow

    gfx_MoveTo(194, 265) ;                    //move origin to point 0, 108, printing will start from this point

    print("Right\n Main") ;                   //print this text onscreen



    txt_FGcolour(YELLOW);                   //change text foreground color to yellow

    gfx_MoveTo(264, 265) ;                    //move origin to point 0, 108, printing will start from this point

    print("Right\n Aux") ;                   //print this text onscreen



//Labels on fuel level rectangles



    txt_FGcolour(LIME);                     //Labels on fuel guage

    gfx_MoveTo(22, 291) ;

    print("FULL") ;

    txt_FGcolour(RED);

    gfx_MoveTo(15, 468);

    print("EMPTY") ;



//Charge/Discharge indicator



    gfx_Triangle(230,50,210,50,220,30,WHITE);

    gfx_Triangle(230,60,210,60,220,80,WHITE);



    txt_FGcolour(GHOSTWHITE);



    gfx_MoveTo(235, 35) ;

    print("CHARGE") ;



    gfx_MoveTo(235, 65) ;

    print("DISCHARGE") ;



    gfx_MoveTo(237, 130) ;

    print("VOLTS") ;



    txt_FGcolour(GREEN);



    txt_FontID(FONT_5);           //Change font

    txt_Width(2);                 // character width multiplier set to 3

    txt_Height(2);

    gfx_MoveTo(202, 95) ;

    print("BATTERY") ;



    txt_FGcolour(GHOSTWHITE);

    txt_Width(1);

    txt_Height(1);

    gfx_MoveTo(30, 149) ;                    //Numbers on scale

    print("0") ;

    gfx_MoveTo(17, 116) ;

    print("2") ;

    gfx_MoveTo(15, 82) ;

    print("4") ;

    gfx_MoveTo(29, 45) ;

    print("6") ;

    gfx_MoveTo(57, 22) ;                    //Numbers on scale

    print("8") ;

    gfx_MoveTo(88, 15) ;

    print("10") ;

    gfx_MoveTo(125, 22) ;

    print("12") ;

    gfx_MoveTo(155, 46) ;

    print("14") ;

    gfx_MoveTo(169, 83) ;

    print("16") ;

    gfx_MoveTo(167, 116) ;

    print("18") ;

    gfx_MoveTo(155, 142) ;

    print("20") ;



    txt_Width(2);                 // character width multiplier set to 2

    txt_Height(2);



    gfx_MoveTo(55, 80) ;

    print("GAL/Hr") ;



    txt_FGcolour(GHOSTWHITE);

    gfx_MoveTo(32, 170) ;

    print("FUEL FLOW") ;



    txt_Width(1);

    txt_Height(1);

    gfx_MoveTo(185, 200) ;

    txt_FGcolour(GHOSTWHITE);

    print("AUDIO ON/OFF") ;



    gfx_Circle(293,206,15,BLUE) ;

    gfx_Circle(293,206,16,BLUE) ;





endfunc