aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Waste/Waste_init.c
blob: c8101c031c2bb97f9fcd43e281ea61dd66a4aaee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#include <Container.h>
#include <PMR/Common/MessageContainer.pb-c.h>
#include <PMR/Stubs/ProgressRequest.pb-c.h>
#include <PMR/Stubs/ProgressResponse.pb-c.h>
#include "include.h"
#include "driverlib/gpio.h"

#include "Drivers/USB_Communication/USBCDCD.h"

//#include "graphics_adapter.h"

void Stub_ProgressRequest(MessageContainer* requestContainer)
{
    MessageContainer responseContainer;

    ProgressRequest* request = progress_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    //writeLine("Progress Request...");

    ProgressResponse response = PROGRESS_RESPONSE__INIT;
    response.has_progress = true;

    int i = 0;
    for (i = 0; i < request->amount; i++)
    {
        response.progress = i;
        responseContainer = createContainer(MESSAGE_TYPE__ProgressResponse, requestContainer->token, false, &response, &progress_response__pack, &progress_response__get_packed_size);
        uint8_t* container_buffer = malloc(message_container__get_packed_size(&responseContainer));
        size_t container_size = message_container__pack(&responseContainer, container_buffer);
        free(responseContainer.data.data);
        //writeLine("Sending Progress: ");
        //writeFloat(response.progress);
        SendChars((char*)container_buffer, container_size);
        //free(container_buffer);


        int co = 0;
        for (co = 0; co < request->delay; co++)
        {
            __delay_cycles(1000000);
        }
    }

    responseContainer = createContainer(MESSAGE_TYPE__ProgressResponse, requestContainer->token, true, &response, &progress_response__pack, &progress_response__get_packed_size);
    uint8_t* container_buffer = malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    free(responseContainer.data.data);
    //writeLine("Progress Completed!");
    SendChars((char*)container_buffer, container_size);
    //free(container_buffer);

    return;

}
a> 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
/*   ----  WHS      */
#include <Container.h>
#include <DataDef.h>
#include "include.h"
#include "Modules/Control/control.h" //  use for FPGA IO
#include "drivers/FPGA/FPGA_GPIO/FPGA_GPIO.h"//#include "FPGA_GPIO.h"  //  use for FPGA IO
#include "drivers/Valves/Valve.h"
#include "Common/report/report.h"
#include "Modules/Waste/Waste.h"
#include "common/utilities/RfidTagContent.h"

#include  <PMR/Diagnostics/EventType.pb-c.h>
#include "Modules/AlarmHandling/AlarmHandling.h"
#include "StateMachines/Initialization/PowerOffSequence.h"
#include "drivers/I2C_Communication/ADC_MUX/ADC_MUX.h"
#include "CartridgeValidationRequest.pb-c.h"
#include "CartridgeValidationResponse.pb-c.h"
#include "Modules/General/buttons.h"
#include "Modules/IFS/ifs.h"
#include <Drivers/I2C_Communication/I2C_Task.h>
//
//#define CARTRIDGE_INK_TIMEOUT           60
//#define CARTRIDGE_INK_TIMEOUTx2         120
//#define CartridgeInkTimeOutCallBackTime eOneSecond
//#define MidTank_Pressure_EMPTY 1000  //todo get from Moti the correct number
//#define CARTRIDGE_CAPATICY 1500 //liters
//#define FULL 1000

#define WasteTankCallBackTime           eOneSecond
#define CartridgeCoverCallBackTime      eOneSecond
#define CartridgeCoverCallBackFastTime  eTenMillisecond
#define CartridgeWaste1CallBackTime     eOneSecond
#define CartridgeWaste2CallBackTime     eOneSecond
//#define CartridgeInkCallBackTime        eOneSecond
#define STARTCOUNT  1
#define STOPCOUNT   0
#define PUMPTIMEOUT 1200 //  seconds; 20 minutes, per Moty,  15/4/19


/*------------Waste Tank function-----------------------*/
bool initWHS_WasteTank();
bool InitCartStatus();

//bool CartridgeCoverCBFunction();
U8 CartridgeCoverCallBackFunction();
// WHS_sensor CartridgeInkCallBackFunction();
WHS_sensor CartridgeWaste1CallBackFunction();
WHS_sensor CartridgeWaste2CallBackFunction();
WHS_sensor WasteTankCallBackFunction();
U8 CartridgeWasteFilling(bool status);
//bool WasteTankCBFunction();
//WHS_sensor CartridgeInkTimeOutCallBackFunction();


U8 CartridgePrecenceCBFunction();
//U8 SetCartridgeLED(cartridge_name name,led_status led);

bool SetWastePump( bool power);
bool SetValveDirection();
bool RdCartridgeCoverSensor();

bool RdWasteCartridge1Sensor();
bool RdWasteCartridge2Sensor();
bool RdWasteTankFilterSensor();
bool RdWasteTankOverFlowSensor();
bool RdWasteTankFullSensor();
bool RdWasteTankEmptySensor();
bool RdCartridgeParam(cartridge_name cart_name);
bool CartridgeAuthentication(cartridge_name cart_name);
bool SetActiveWastCartridge();



RfidTagContent cartridge_info;

// function for RFID:
bool WrRFIDStatus(cartridge_name active_cartridge, cartridge_status status);
cartridge_status RdRFIDStatus (cartridge_name active_cartridge);
bool RD_MID_TANK_Level();

//bool PowerOffInProcessGetState() {return OK;}; //todo
bool WrRFIDStatus(cartridge_name active_cartridge, cartridge_status status)  { return OK;} //todo
cartridge_status RdRFIDStatus (cartridge_name active_cartridge)  { return WasteEmpty; }  //todo

bool WHS_IsContainerFull(){return WHS_info.WHS_sensors.waste_tank_full_sensor;}
bool WHS_IsContainerEmpty(){return WHS_info.WHS_sensors.waste_tank_empty_sensor;}

bool WHS_IsContainerOverflow(){return WHS_info.WHS_sensors.waste_tank_over_flow_sensor;}
bool WHS_WasteCartridgeLowerPresent(){return WHS_info.WHS_sensors.waste_cartridge1_precense_sensor;}
bool WHS_WasteCartridgeMiddlePresent(){return WHS_info.WHS_sensors.waste_cartridge2_precense_sensor;}
bool WHS_IsPumpActive(){return WHS_info.WHS_pump.status;}
bool WHS_IsValveOpen(){return WHS_info.WHS_valve;}
bool RD_MID_TANK_Level(){return 0 /* FULL*/ ;}
//bool ColorMatch();
//bool MidTankValvesAction(bool action);



//bool MidTankValvesAction(bool action) //Cartridge_MidTank_ON of Cartridge_MidTank_OFF
//{
//    bool ret =  false;
//
//    Valve_Set(IDS_Id_to_AirValve[WHS_info.Ink.cart_color], action );   //Atm_MidTank_OFF/ON
//    Valve_Set(IDS_Id_to_CartrideValve[WHS_info.Ink.cart_color], action );   //Atm_MidTank_OFF/ON
//
//    ret = true;
//    return ret;
//}
//
//bool ColorMatch()
//{
//    bool ret =  false;
//
//    ret = true;
//    return ret;
//}

bool WHS_IsEmptying()
{
    bool ret =  false;
    if ( WHS_info.sttMachine == WHS_emptying )
    {
        ret = true;
    }
    return ret;
}

//char CartridgeRequestToken[36+1];
//int  CartridgeRequestId = 0x01010101;
///* --------    cartridge function  ----*/
//uint32_t ReadCartridgeData(cartridge_name cart_name)
//{
//    //this function triggers reading and validation of the cartridge information
//    // for now it sends a request to the tablet to inquire for the ink color/midtank id
//    MessageContainer responseContainer;
//    CartridgeValidationRequest ValidationReq = CARTRIDGE_VALIDATION_REQUEST__INIT;
//    Cartridge CartridgeData;
//
//    strcpy(CartridgeRequestToken,"Happy days are coming to us, soon");
//    memcpy(&CartridgeRequestToken[32],&CartridgeRequestId,sizeof(CartridgeRequestId));
//    CartridgeRequestId++;
//
//    cartridge__init(&CartridgeData);
//    CartridgeData.has_slot = true;
//    CartridgeData.slot = CARTRIDGE_SLOT__Ink;
//    CartridgeData.tag = 0;
//    CartridgeData.has_index = false;
//
//    ValidationReq.has_action = true;
//    ValidationReq.action = CARTRIDGE_ACTION__Inserted;
//
//    ValidationReq.cartridge = &CartridgeData;
//
//    responseContainer = createContainer(MESSAGE_TYPE__CartridgeValidationRequest, CartridgeRequestToken, true, &ValidationReq, &cartridge_validation_request__pack, &cartridge_validation_request__get_packed_size);
//    //-------------------------------------------------------------------------------------------
//    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
//    size_t container_size = message_container__pack(&responseContainer, container_buffer);
//    my_free(responseContainer.data.data);
//    //SendChars((char*)container_buffer, container_size);
//
//    return OK;
//}
//void ResponseDemo(int MidtankId)
//{
//    WHS_info.event = IFS_INK_authentication_pass;
//    //global mid tank id = MidtankId;
//    WHS_info.Ink.cart_color = MidtankId;
//    WasteTankCBFunction();//call cart is ok
//}
//bool CartridgeValidationResponseFunc(MessageContainer* requestContainer)
//{
//    bool ret = notOK;
//    CartridgeValidationResponse* response = cartridge_validation_response__unpack(NULL, requestContainer->data.len, requestContainer->data.data);
//
//    Cartridge *cartridge;
//    uint32_t midtankId;
//    cartridge_name cart_name;
//
//    // chack the massege:
//    // 1. the cartridge is valid ( autantication)
//    // 2. the pointer is not 0 !!!
//    if (response->cartridge == 0 )
//    {
//        REPORT_MSG(WHS_filling," ------------ IFS_filling message from PPC is null!!! ----------------- "); // write to log error
//        return ret;
//    }
//
//    if (response->isvalid == true)
//    {
//        REPORT_MSG(WHS_filling," ------------ IFS_filling INK cartridge is valid ----------------- "); // write to log error
//        cartridge = response->cartridge;
//        midtankId = cartridge->index; // midtank 1-8
//        cart_name = cartridge->slot;    //cart1(INK),cart2(WASTE1),cart3(WASTE1)
//        if (cartridge->slot == INK_cartridge)
//        {
//            WHS_info.Ink.cart_color = cartridge->index; // midtank 1-8
//            WHS_info.event = IFS_INK_authentication_pass;
//        }
//        WasteTankCBFunction();//call cart is ok
//        ret = OK;
//    }
//    else
//    {
//        REPORT_MSG(WHS_filling," ------------ IFS_filling INK cartridge is not valid !!!----------------- "); // write to log error
////        cartridge = response->cartridge;
////        mimidtankId = cartridge->index;
////        cart_name = cartridge->slot;
//        WHS_info.event = IFS_INK_authentication_fail;
//        WasteTankCBFunction();// call cart is INVALID
//        ret = OK;
//    }
//return ret;
//}



bool RdCartridgeParam(cartridge_name cart_name)
{
    /*
    read all parameters from RFid cartridge:
    serial number, cartridge_status...
    waste_cartridge 1 or 1
    update waste_cartridge struct
    */
    bool ret = notOK;
    switch (cart_name)
    {
    case waste_cartridge1:
        WHS_info.cartridge_1.serial_number = temp_funcion; //to do
        WHS_info.cartridge_1.status = temp_funcion; // to do
        break;
    case waste_cartridge2:
        WHS_info.cartridge_2.serial_number = temp_funcion;
        WHS_info.cartridge_2.status = temp_funcion;
        break;
    case INK_cartridge:
        ReadCartridgeData(cart_name);
        //memcpy(cartridge_info.ColorName,RED);
//        add call back!!  WasteTankCallBackTime
//        state - wait for cartridge validation
//        //WHS_info.Ink.serial_number = temp_funcion;
        //WHS_info.Ink.status = temp_funcion;
        break;
    default:
        break;
    }
    ret = OK;
    return ret;
}

bool  CartridgeAuthentication(cartridge_name cart_name)
{
    bool ret = notOK;
    // TBD;
    switch (cart_name)
    {
//        case INK_cartridge:
//            WHS_info.Ink.autheticate = PASSED;  //todo
//            //Pannel_Leds(CART_1, MODE_ON);
//            break;
        case waste_cartridge1:
            WHS_info.cartridge_1.autheticate = PASSED;  //todo
            //Pannel_Leds(CART_2, MODE_ON);
            break;
        case waste_cartridge2:
            WHS_info.cartridge_2.autheticate = PASSED; //todo
            //Pannel_Leds(CART_3, MODE_ON);
            break;
        default:
            WHS_info.cartridge_1.autheticate = FAILED; //todo
            WHS_info.cartridge_2.autheticate = FAILED; //todo
            //Pannel_Leds(CART_2, MODE_OFF);
            //Pannel_Leds(CART_3, MODE_OFF);
            break;
    }
    ret = OK;
    return ret;
}

/*
bool check_RFID_authentication()
{
    bool ret = notOK;
 //   rd_waste_cartridge_param();
    if ( 1 )
        ret = OK;
    return ret;
}


U8 Wr_cartridge_RFid(cartridge_name cartridge, cartridge_status status)
{
    bool ret = notOK;
    // TBD;
    ret = OK;
    return ret;
}

U8 set_cartridge_2(U8 cartridge_status)
{
    bool ret = notOK;
    WHS_info.cartridge_2.status = temp_funcion;
    return ret;
}
*/

/* ------------------------------*/


bool SetWastePump( bool power)
{
    /*
    set the waste pump on/off
    off - :
        1. when waste tank is empty
        2. when empting is time out
        3. cartrigde cover is open
        4. when waste1 is not precense AND valve3way==1
        5. when waste2 is not precense AND valve3way==2
        6. when autentication with waste cartige is disable
    on :
        1. when waste_tank_full

        void Pumps_Control(PUMPS_ENUM Pump_Id, bool Direction) //1 - OPEN, 0 - CLOSE ?? WHS_WTANKPUMP2 or WASTECH_PUMP2
    */
    bool ret = notOK;
//    if (( power == OPEN ) && (WHS_info.WHS_sensors.cartridge_cover == CLOSE))
    if ( power == OPEN )
    {
        if (WHS_Type == WHS_TYPE_UNKNOWN)
        {
		    Pumps_Control(WHS_WTANKPUMP2, OPEN); //waste_pump_power_on();
        }
		else //new WHS
        {
		    Trigger_SetWHSPump(OPEN);
		}
        WHS_info.WHS_pump.status = OPEN;
        ReportWithPackageFilter(WasteFilter,"------------ WHS WHS_empty start PUMP -----------------", __FILE__, __LINE__, WHS_info.WHS_pump.status, RpMessage, 0, 0);
        WHS_info.WHS_pump.time = STARTCOUNT;
        ret = OPEN;
    }
    else
    {
        if (WHS_Type == WHS_TYPE_UNKNOWN)
        {
		    Pumps_Control(WHS_WTANKPUMP2, CLOSE); //waste_pump_power_on();
        }
		else //new WHS
        {
		    Trigger_SetWHSPump(CLOSE);
        }
		WHS_info.WHS_pump.status = CLOSE;
        ReportWithPackageFilter(WasteFilter,"------------ WHS WHS_empty stop PUMP -----------------", __FILE__, __LINE__, WHS_info.WHS_pump.status, RpMessage, 0, 0);
        WHS_info.WHS_pump.time = STOPCOUNT;
        ret = CLOSE;
    }
    return ret;
}




bool SetValveDirection()
{
    /*
     * waste_cartridge = 1- OPEN, 0 - CLOSE
     *   VALVE_WASTE_TANK
     *
     * */
    bool ret = OK;
    //if (WHS_info.WHS_valve != no_waste_cartridge)
    if (WHS_info.active_cartridge == waste_cartridge1)
    {
        WHS_info.WHS_valve = VALVE_DIR_WASTE_CARTRIDGE1;
        ReportWithPackageFilter(WasteFilter,"------------WHS_info.WHS_valve VALVE_DIR_WASTE_CARTRIDGE1  -----------------", __FILE__, __LINE__, WHS_info.WHS_valve, RpMessage, 0 ,0);
}
    else
    {
        WHS_info.WHS_valve = VALVE_DIR_WASTE_CARTRIDGE2;
        ReportWithPackageFilter(WasteFilter,"------------WHS_info.WHS_valve VALVE_DIR_WASTE_CARTRIDGE2   -----------------", __FILE__, __LINE__, WHS_info.WHS_valve, RpMessage, 0 ,0);
    }


    if (WHS_Type == WHS_TYPE_UNKNOWN)
        Valve_Set(VALVE_WASTE_TANK, WHS_info.WHS_valve);
    else //new WHS
        Trigger_SetWHSValveWatseCartridge(WHS_info.WHS_valve);

    //ret = OK;

    return ret;
}


/*--------------------------    function for  WHS  -------------------------------   */

U8 CartridgeWasteFilling(bool status)
{
    bool ret = notOK;
    //SetValveDirection();
    if ((status == ON) && (WHS_info.active_cartridge != no_waste_cartridge))
    {
        SetValveDirection();//Valve_Set(VALVE_WASTE_TANK, WHS_info.active_cartridge); //set the valve direction
        //Pannel_Leds(PANEL_BUTTON_OR_CRAT_ID Pannel_Led_Id, OPERATION_MODE LED_Mode); //set cartridge led color slow blink
        SetWastePump(OPEN);
        //WHS_info.WHS_pump.time = 0;
        AlarmHandlingSetAlarm( EVENT_TYPE__NO_WASTE_CARTRIDGE_AVAILABLE,  false);
        ret=OK;
    }
    else // stop Waste cartridge filling
    {
        SetWastePump(CLOSE);
        WHS_info.WHS_valve = VALVE_DIR_WASTE_CARTRIDGE1;
        if (WHS_Type == WHS_TYPE_UNKNOWN)
            Valve_Set(VALVE_WASTE_TANK, WHS_info.WHS_valve);
        else //new WHS
            Trigger_SetWHSValveWatseCartridge(WHS_info.WHS_valve);

        AlarmHandlingSetAlarm( EVENT_TYPE__ALL_WASTE_CARTRIDGES_FULL,  true);

        switch (WHS_info.active_cartridge)
           {
              case waste_cartridge1:
                  Pannel_Leds( CART_2,  MODE_OFF);                //set led color
                  cart2.color = colorOFF;
                  ReportWithPackageFilter(WasteFilter,"------------ WHS  waste_cartridge1 is used please replace it -----------------", __FILE__, __LINE__, 0, RpMessage, 0, 0);
                  break;
              case waste_cartridge2:
                  Pannel_Leds( CART_3,  MODE_OFF);                //set led color
                  cart3.color = colorOFF;
                  ReportWithPackageFilter(WasteFilter,"------------ WHS  waste_cartridge2 is used please replace it -----------------", __FILE__, __LINE__, 0, RpMessage, 0, 0);
                  break;
              case no_waste_cartridge:
                  ReportWithPackageFilter(WasteFilter,"------------ !!!!!WHS  No waste cartridge !!!!! -----------------", __FILE__, __LINE__, 0, RpMessage, 0, 0);
                  ReportWithPackageFilter(WasteFilter,"------------ !!!! WHS  insert cartridge!!!! -----------------", __FILE__, __LINE__, 0, RpMessage, 0, 0);
                  ret = ERROR;
                  break;
              default:
                  break;
           }


        //write RFID status
    }
    return ret;
}

/* ------read waste tank sensors -----------  */
bool RdWasteTankEmptySensor()
{
    bool ret = notOK;
    WHS_Read_GPI_Registers();
    //WHS_info.WHS_sensors.waste_tank_empty_sensor = WHS_GPI_WCONTAINER_WARN();
    //ret = WHS_info.WHS_sensors.waste_tank_empty_sensor;
     ret = WHS_GPI_WCONTAINER_WARN();
     return ret;
}

bool RdWasteTankFullSensor()
{
    bool ret = notOK;
    WHS_Read_GPI_Registers();
    //WHS_info.WHS_sensors.waste_tank_full_sensor =  WHS_GPI_WCONTAINER_FULL();
    //ret = WHS_info.WHS_sensors.waste_tank_full_sensor;
    ret = WHS_GPI_WCONTAINER_FULL();
    return ret;
}

bool RdWasteTankOverFlowSensor()
{
    bool ret = notOK;
    WHS_Read_GPI_Registers();
    //WHS_info.WHS_sensors.waste_tank_over_flow_sensor =  WHS_GPI_WASTE_OVERFULL();
    //ret = WHS_info.WHS_sensors.waste_tank_over_flow_sensor;
    ret = WHS_GPI_WASTE_OVERFULL();
    return ret;
}

/*-------------*/


WHS_sttMachin UpdateStateMachine()
{
    //WHS_sttMachin ret = WHS_sttError  ;

    if ( WHS_info.WHS_sensors.waste_tank_full_sensor == SENSORnotFULL)
    {
        WHS_info.sttMachine = WHS_empty  ;
        ReportWithPackageFilter(WasteFilter,"------------ WHS WHS_empty -----------------", __FILE__, __LINE__, WHS_info.WHS_sensors.waste_tank_full_sensor, RpMessage, WHS_info.WHS_sensors.waste_tank_empty_sensor, 0);
    }
    else if ((WHS_info.WHS_sensors.waste_tank_full_sensor   == SENSORFULL) &&
             (WHS_info.WHS_sensors.waste_tank_empty_sensor  == SENSORnotEMPTY))
    {
        WHS_info.sttMachine =  WHS_full  ;
        ReportWithPackageFilter(WasteFilter,"------------ WHS WHS_full -----------------", __FILE__, __LINE__, WHS_info.WHS_sensors.waste_tank_full_sensor, RpMessage, WHS_info.WHS_sensors.waste_tank_empty_sensor, 0);
    }
    else
    {
        WHS_info.sttMachine =  WHS_sttError  ;
        ReportWithPackageFilter(WasteFilter,"------------ WHS sensor Error -----------------", __FILE__, __LINE__, WHS_info.WHS_sensors.waste_tank_full_sensor, RpMessage, WHS_info.WHS_sensors.waste_tank_empty_sensor, 0);
    }
    return WHS_info.sttMachine;
}


bool initWHS_WasteTank()
{
    WHS_info.WHS_sensors.waste_tank_empty_sensor = RdWasteTankEmptySensor();
    WHS_info.WHS_sensors.waste_tank_full_sensor =  SENSORnotFULL;
    Task_sleep(15);
    UpdateStateMachine();
    AddControlCallback("Waste tank",WasteTankCBFunction, WasteTankCallBackTime , WasteTankCallBackFunction, 0,0,0);//eOneMinute
    return OK;
}


WHS_sensor EmptySensorEvent()
{
    WHS_sensor ret = WHS_no_event;
    if (RdWasteTankEmptySensor() != WHS_info.WHS_sensors.waste_tank_empty_sensor)
    {
        WHS_info.event =  WHS_empty_sensor;
        ret = WHS_empty_sensor;
        WHS_info.WHS_sensors.waste_tank_empty_sensor = !WHS_info.WHS_sensors.waste_tank_empty_sensor;
        ReportWithPackageFilter(WasteFilter,"------------ WHS EmptySensorEvent (status) -----------------", __FILE__, __LINE__, WHS_info.WHS_sensors.waste_tank_full_sensor, RpMessage, WHS_info.WHS_sensors.waste_tank_empty_sensor, 0);
    }
    return ret;
}

WHS_sensor FullSensorEvent()
{
    WHS_sensor ret = WHS_no_event;
    if ( RdWasteTankFullSensor() != WHS_info.WHS_sensors.waste_tank_full_sensor )
    {
        WHS_info.event =  WHS_full_sensor;
        ret = WHS_full_sensor;
        WHS_info.WHS_sensors.waste_tank_full_sensor = 1-WHS_info.WHS_sensors.waste_tank_full_sensor;
        ReportWithPackageFilter(WasteFilter,"------------ WHS FullSensorEvent (status) -----------------", __FILE__, __LINE__, WHS_info.WHS_sensors.waste_tank_full_sensor, RpMessage, WHS_info.WHS_sensors.waste_tank_empty_sensor, 0);
    }
    return ret;
}

WHS_sensor WasteTankCallBackFunction()
{
    /* detect waste tank sensor event */
    WHS_sensor ret = WHS_no_event;
/*    if ( OverflowSensorEvent())
        {
         return WHS_info.event;
        }
    else */
    if (WHS_info.WHS_pump.time) WHS_info.WHS_pump.time += 1;
    if (WHS_info.WHS_pump.time == PUMPTIMEOUT)
    {
        WHS_info.WHS_pump.time = 0;
        AlarmHandlingSetAlarm( EVENT_TYPE__WASTE_CONTAINER_EMPTYING_TIMEOUT,  false);
        ReportWithPackageFilter(WasteFilter,"------------ WHS PUMPTIMEOUT -----------------", __FILE__, __LINE__, PUMPTIMEOUT, RpMessage, 0, 0);
        return WHS_info.event = WHS_Timeout;
    }
    if ( FullSensorEvent())
         {
             return WHS_info.event;
         }
    else if ( EmptySensorEvent())
         {
            return WHS_info.event;
         }

    return ret;
}

bool WasteTankCBFunction()
{

    bool ret = notOK;
    static bool pump_on_flag = CLOSE;
    //uint32_t Cartridge_Cover_device_Id = 0;
    double MidTank_Pressure = 0;

    switch (WHS_info.event)
    {
        case WHS_empty_sensor:
            if (WHS_info.WHS_sensors.waste_tank_empty_sensor == SENSORnotEMPTY)
            {
                if (WHS_info.sttMachine == WHS_empty)
                {
                    WHS_info.sttMachine = WHS_filling;
                    ReportWithPackageFilter(WasteFilter,"------------ WHS WHS_filling ------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, 0, 0);
                }
                else
                {
                    ReportWithPackageFilter(WasteFilter,"------------ WHS WHS EMPTY sensor failed !!!!!!! ------------", __FILE__, __LINE__, 0, RpMessage, 0, 0);
                }
            }
            else //SENSOREMPTY
            {
                if (WHS_info.sttMachine == WHS_emptying)
                {
                    WHS_info.sttMachine = WHS_empty;
                    ReportWithPackageFilter(WasteFilter,"------------ WHS WHS_empty -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, 0, 0);
                    ret = CartridgeWasteFilling(OFF);
                    WHS_info.Cartridge_Cover_device_Id = RemoveControlCallback(WHS_info.Cartridge_Cover_device_Id, WasteTankCBFunction );
                    WHS_info.Cartridge_Cover_device_Id = AddControlCallback("Cartridge Cover Door", WasteTankCBFunction, CartridgeCoverCallBackTime, CartridgeCoverCallBackFunction, 0,0,0 );//eOneSecond
                    WrRFIDStatus(WHS_info.active_cartridge, WasteFull);//write RFID status todo
                }
                else
                {
                    ReportWithPackageFilter(WasteFilter,"------------ WHS WHS EMPTY sensor failed !!!!!!! -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, 0, 0);
                }
            }
            break;
        case WHS_full_sensor:
            /* if the machine status is power down : don't start the waste emptying sequence !!!*/
            if ( PowerOffInProcessGetState() )
                break;
            if ((WHS_info.WHS_sensors.cartridge_cover == OPEN) && (WHS_info.WHS_sensors.waste_tank_full_sensor == SENSORFULL))
            {
                ReportWithPackageFilter(WasteFilter,"------------ WHS_full , cartridge cover is open cant start the pump!!!! -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, WHS_info.WHS_sensors.cartridge_cover, 0);
                ReportWithPackageFilter(WasteFilter,"------------ WHS, close the cartridge cover to start the pump!!!! -------------"      , __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, WHS_info.WHS_sensors.cartridge_cover, 0);

                AlarmHandlingSetAlarm( EVENT_TYPE__NO_WASTE_CARTRIDGE_AVAILABLE,  true);

                WHS_info.WHS_sensors.waste_tank_full_sensor = SENSORnotFULL;
                break;
            }
            if (WHS_info.WHS_sensors.waste_tank_full_sensor == SENSORFULL)
            {
                WHS_info.sttMachine = WHS_full;
                ReportWithPackageFilter(WasteFilter,"------------ WHS, WHS_full -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, WHS_info.WHS_sensors.waste_tank_full_sensor, 0);
                WHS_info.Cartridge_Cover_device_Id = RemoveControlCallback(WHS_info.Cartridge_Cover_device_Id, WasteTankCBFunction );
                WHS_info.Cartridge_Cover_device_Id = AddControlCallback("Cartridge Cover Door", WasteTankCBFunction, CartridgeCoverCallBackFastTime , CartridgeCoverCallBackFunction, 0,0,0 );//eTenMillisecond
                if (RdRFIDStatus (WHS_info.active_cartridge) == WasteEmpty)
                {
                    ret = WrRFIDStatus(WHS_info.active_cartridge, WasteFilling);//write RFID status todo
                    if (ret == OK)
                    {
                        ret = CartridgeWasteFilling(ON);
                    }
                    else
                    {
                        WrRFIDStatus(WHS_info.active_cartridge,  WasteFail);//write RFID status todo
                    }
                }

                if (ret != OK)
                {
                    //recreate the full event until cartridge is inserted or authentication
                    WHS_info.WHS_sensors.waste_tank_full_sensor = 1-WHS_info.WHS_sensors.waste_tank_full_sensor;
                }

                // cant start a new Job
            }
            else
            {
                if (WHS_info.sttMachine == WHS_full)
                {
                    WHS_info.sttMachine = WHS_emptying;
                    ReportWithPackageFilter(WasteFilter,"------------ WHS, WHS_emptying -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, WHS_info.WHS_sensors.waste_tank_full_sensor, 0);
                    // can start a new JOB
                }
                else
                {
                    ReportWithPackageFilter(WasteFilter,"------------ WHS, WHS FULL sensor failed !!!!!!! -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, WHS_info.WHS_sensors.waste_tank_full_sensor, 0);
                }
            }
            break;

        case WHS_cartridge_cover_sensor:

            if ( WHS_info.WHS_sensors.cartridge_cover == OPEN )
            {
                ReportWithPackageFilter(WasteFilter,"------------ WHS, Cartridge cover open -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, WHS_info.WHS_sensors.cartridge_cover, 0);//popup message :"Cartridge cover open"  //todo
                if ( WHS_info.WHS_pump.status == OPEN)
                {
                    CartridgeWasteFilling(CLOSE);
                    pump_on_flag = OPEN;
                }

//                if (cart1.color != colorOFF)
//                {
//
//                }

                if ( !PowerOffInProcessGetState() )
                {
                    IFS_info.Cartridge_Ink_device_Id    = AddControlCallback("Ink_Cartridge", WasteTankCBFunction, CartridgeInkCallBackTime   , CartridgeInkCallBackFunction   , 0,0,0 ); //eOneSecond
                    WHS_info.Cartridge_Waste1_device_Id = AddControlCallback("Waste1 Cartridge", WasteTankCBFunction, CartridgeWaste1CallBackTime, CartridgeWaste1CallBackFunction, 0,0,0 ); //eOneSecond
                    WHS_info.Cartridge_Waste2_device_Id = AddControlCallback("Waste2 Cartridge", WasteTankCBFunction, CartridgeWaste2CallBackTime, CartridgeWaste2CallBackFunction, 0,0,0 ); //eOneSecond
                }
                else
                {
                    ReportWithPackageFilter(WasteFilter,"------------ WHS, Power down in process -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, WHS_info.WHS_sensors.cartridge_cover, 0);//popup message :"Cartridge cover open"  //todo
                }



                //if (WHS_info.WHS_pump == ON)
                //{
                //    SetWastePump(OFF); //pump OFF
                //}
                // prevent NEW Job
                // Display "Close cartridge cover"
                // pop up message in GUI
            }
            else // CLOSE
            {
                //if (WHS_info.sttMachine == WHS_full) CartridgeWasteFilling(OPEN);
                SetActiveWastCartridge();
                if (pump_on_flag == OPEN )
                {
                    WHS_info.WHS_sensors.waste_tank_full_sensor =  SENSORnotFULL;  // todo
                    //                    CartridgeWasteFilling(OPEN);
                    pump_on_flag = CLOSE;
                }
                IFS_info.Cartridge_Ink_device_Id    = RemoveControlCallback(IFS_info.Cartridge_Ink_device_Id   , WasteTankCBFunction );
                WHS_info.Cartridge_Waste1_device_Id = RemoveControlCallback(WHS_info.Cartridge_Waste1_device_Id, WasteTankCBFunction );
                WHS_info.Cartridge_Waste2_device_Id = RemoveControlCallback(WHS_info.Cartridge_Waste2_device_Id, WasteTankCBFunction );
                if ( IFS_info.INK_isOK_flag )
                {
                    IFS_info.INK_isOK_flag = false;//0
                    ReportWithPackageFilter(WasteFilter,"------------ WHS, change INK cartridge status to color Used -------------", __FILE__, __LINE__, IFS_info.INK_isOK_flag, RpMessage, 0, 0);
                    // test the Mid-tank capacity is not at Nadav sequence
                    MidTank_Pressure = Get_MidTank_Pressure_Sensor(IFS_info.Ink.cart_color);
                    if ( MidTank_Pressure > MidTank_Pressure_EMPTY )
                    {
                        ReportWithPackageFilter(WasteFilter,"------------ close door  MidTank_Pressure > MidTank_Pressure_EMPTY -----------------", __FILE__,__LINE__,(int)(MidTank_Pressure_EMPTY*1000), RpMessage, (int)(MidTank_Pressure*1000), 0);
                        MidTankValvesAction(Cartridge_MidTank_OFF);
                        //Disable_MidTank_Pressure_Reading(IFS_info.Ink.cart_color);
                        ReportWithPackageFilter(WasteFilter,"------------ WHS, Mid-tank not empty -------------", __FILE__, __LINE__, 0, RpMessage, 0, 0);
                        break;
                    }
                    IFS_info.MidTank_capacity = MidTank_Pressure;
                    ReportWithPackageFilter(WasteFilter,"------------ Mid-tank filling in process -----------------", __FILE__,__LINE__,(int)(MidTank_Pressure_EMPTY*1000), RpMessage, (int)(MidTank_Pressure*1000), 0);
                    Pannel_Leds(CART_1, MODE_ON);
                    cart1.color  = BLINK;
                    MidTankValvesAction(Cartridge_MidTank_ON);
                    //add timeout
                    IFS_TimeOutAlarm(false);
                    IFS_info.Ink.time_out = 1;
                    IFS_info.Cartridge_Ink_TimeOut_device_Id = AddControlCallback("Ink Cartridge TimeOut", WasteTankCBFunction, CartridgeInkTimeOutCallBackTime, CartridgeInkTimeOutCallBackFunction, 0,0,0 ); //eOneSecond


//                    //add callback for midtank status is full
//                    IFS_info.Cartridge_Ink_TimeOut_device_Id = AddControlCallback(NULL, WasteTankCBFunction, CartridgeInkTimeOutCallBackTime, CartridgeInkTimeOutCallBackFunction, 0,0,0 ); //eOneSecond
//                    Enable_MidTank_Pressure_Reading(IFS_info.Ink.cart_color);
//                    Read_MidTank_Pressure_Sensor(IFS_info.Ink.cart_color);
//                    if ( Get_MidTank_Pressure_Sensor(IFS_info.Ink.cart_color) > MidTank_Pressure_FULL )
//                    {
//                        Disable_MidTank_Pressure_Reading(IFS_info.Ink.cart_color);
//                        REPORT_MSG(WHS_full," ------------ Mid-tank not empty ----------------- ");
//                        break;
//                    }

                }
            }
            break;

        case IFS_INK_presence_sensor:
            if (IFS_info.IFS_Ink_precense_sensor == CarteidgePrecense)
            {
                // is power down in process??  //todo
                if ( !PowerOffInProcessGetState() ) // not in power down process
                {
                    Pannel_Leds(CART_1, MODE_ON);
                    cart1.color = colorON;
                    RdCartridgeParam(INK_cartridge); //todo
                }
                else
                {
                    //  print: "power down in process";
                    ReportWithPackageFilter(WasteFilter,"------------ WHS, Power down in process -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, IFS_info.IFS_Ink_precense_sensor, 0);
                }
            }
            else // Cartridge is out
            {
                IFS_info.INK_isOK_flag = false;//0
                Pannel_Leds(CART_1, MODE_OFF);
                cart1.color = colorOFF;
                if (IFS_info.Ink.time_out != 0)
                {
                    MidTankValvesAction(Cartridge_MidTank_OFF);
                    IFS_info.Ink.time_out = 0;
                    IFS_info.Cartridge_Ink_TimeOut_device_Id = RemoveControlCallback(IFS_info.Cartridge_Ink_TimeOut_device_Id, WasteTankCBFunction );
                }
            }
            break;

        case IFS_INK_authentication_pass:
            if (!ColorMatch())
            {
                    ReportWithPackageFilter(WasteFilter,"------------ WHS, cartridge color does not match -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, 0, 0);
                    Pannel_Leds(CART_1, MODE_ON);
                    cart1.color  = BLINK;
                    break;
            }
#warning The messege removed (close cartridge cover)
            //REPORT_MSG(WHS_full," ------------ close cartridge cover ----------------- ");
            IFS_info.INK_isOK_flag = true; // 1

            break;

        case IFS_INK_authentication_fail:
            break;


        case WHS_waste1_presence_sensor :
                    if (WHS_info.WHS_sensors.waste_cartridge1_precense_sensor == CarteidgePrecense ) // waste cartridge 1 is inserted .
                    {
                        //SetCartridgeLED(waste_cartridge1, led_on);
                        Pannel_Leds(CART_2, MODE_ON);
                        cart2.color = colorON;
                        RdCartridgeParam(waste_cartridge1);
//                        SetActiveWastCartridge();
//                        SetValveDirection();
                        if ( CartridgeAuthentication(waste_cartridge1) == PASSED) // to define authentication
                        {
                            //WHS_info.cartridge_1.autheticate = PASSED; //='0'
                            //???WHS_info.WHS_valve = waste_cartridge1; // or we should do it only before pumping????
                            AlarmHandlingSetAlarm( EVENT_TYPE__ALL_WASTE_CARTRIDGES_FULL,  false);
                        }
                        else
                        {
                            //WHS_info.cartridge_1.autheticate = FAILED; //='1'
                            //SetCartridgeLED(waste_cartridge1, led_off);
                            Pannel_Leds(CART_2, MODE_OFF);
                            cart2.color = colorOFF;
                        }
                    }
                    else    // waste cartridge 1 take out
                    {
                        Pannel_Leds(CART_2, MODE_OFF);
                        cart2.color = colorOFF;
                        WHS_info.cartridge_1.autheticate = FAILED;
                    }
                    //SetActiveWastCartridge();
                    break;
        case WHS_waste2_presence_sensor :
                    if (WHS_info.WHS_sensors.waste_cartridge2_precense_sensor == CarteidgePrecense ) // waste cartridge  is inserted .
                    {
                       Pannel_Leds(CART_3, MODE_ON);//SetCartridgeLED(waste_cartridge2, led_on);
                       cart3.color = colorON;
                       RdCartridgeParam(waste_cartridge2);
                       if ( CartridgeAuthentication(waste_cartridge2) == PASSED) // to define authentication
                       {
                           //WHS_info.cartridge_2.autheticate = PASSED; //='0'
                           //???WHS_info.WHS_valve = waste_cartridge2; // or we should do it only before pumping????
                           //???SetValveDirection();    // or we should do it only before pumping????
                           AlarmHandlingSetAlarm( EVENT_TYPE__ALL_WASTE_CARTRIDGES_FULL,  false);
                       }
                       else
                       {
                           //WHS_info.cartridge_2.autheticate = FAILED; //='1'
                           Pannel_Leds(CART_3, MODE_OFF);//SetCartridgeLED(waste_cartridge2, led_off);
                           cart3.color = colorON;
                       }
                    }
                    else    // waste cartridge 2 take out
                    {
                        Pannel_Leds(CART_3, MODE_OFF);
                        cart3.color = colorOFF;
                        WHS_info.cartridge_2.autheticate = FAILED;
                        /*
                          if (WHS_info.cartridge_2.autheticate == PASSED) // or we should do it only before pumping????
                          {
                            WHS_info.WHS_valve = waste_cartridge;
                            SetValveDirection();
                            }
                       else
                        {

                        }
                        RdCartridgeParam(waste_cartridge2);
                       WHS_info.WHS_valve = waste_cartridge1;
                       */
                    }
                    //SetActiveWastCartridge();
                    break;

        case WHS_Timeout:
            ReportWithPackageFilter(WasteFilter,"------------ WHS, WHS_Timeout -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, 0, 0);
            AlarmHandlingSetAlarm( EVENT_TYPE__WASTE_CONTAINER_EMPTYING_TIMEOUT,  true);
            CartridgeWasteFilling(OFF);
            break;

//        case IFS_Timeout:
//            REPORT_MSG(WHS_emptying," ------------ IFS_Timeout ----------------- ");
//            // try again :
//            // start timeout
//            // close and open the correct valves
//            REPORT_MSG(WHS_full," ------------ IFS_Timeout_Second_Time ----------------- ");
//            MidTankValvesAction(Cartridge_MidTank_OFF);
//            MidTankValvesAction(Cartridge_MidTank_ON);
//
//            IFS_TimeOutAlarm();
//
//            break;
        case IFS_Timeout_Second:
//            Disable_MidTank_Pressure_Reading(IFS_info.Ink.cart_color);
            IFS_info.Cartridge_Ink_TimeOut_device_Id = RemoveControlCallback(IFS_info.Cartridge_Ink_TimeOut_device_Id, WasteTankCBFunction );
            ReportWithPackageFilter(WasteFilter,"------------ WHS, Change cartridge status to Fail -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, 0, 0);

            MidTankValvesAction(Cartridge_MidTank_OFF);
            cart1.color = fastBILNK;
            ReportWithPackageFilter(WasteFilter,"------------ Display Mid-tank # filling fail -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, 0, 0);
            ReportWithPackageFilter(WasteFilter,"------------ call customer support -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, 0, 0);
            Pannel_Leds(CART_1, MODE_ON);
            cart1.color = fastBILNK;
            IFS_TimeOutAlarm(true);
            break;

        case IFS_MidTankFull:
//            Disable_MidTank_Pressure_Reading(IFS_info.Ink.cart_color);
            IFS_info.Cartridge_Ink_TimeOut_device_Id = RemoveControlCallback(IFS_info.Cartridge_Ink_TimeOut_device_Id, WasteTankCBFunction );
            ReportWithPackageFilter(WasteFilter,"------------ Change cartridge status to Waste Empty -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, 0, 0);
            Pannel_Leds(CART_1, MODE_OFF);
            cart1.color = colorOFF;
            MidTankValvesAction(Cartridge_MidTank_OFF);
            ReportWithPackageFilter(WasteFilter,"------------ Display Mid-tank # filling done -------------", __FILE__, __LINE__, WHS_info.sttMachine, RpMessage, 0, 0);
            break;



        case WHS_no_event:
            break;
        default: //error state
            break;
    }
    WHS_info.event = WHS_no_event;
    return ret;
}




/*------------------------------- Cartridge  Cover       -----------------------------------------*/

bool RdCartridgeCoverSensor()
{

    //REPORT_MSG(parameter," ------------ RD Cartridge Cover Sensor    ----------------- ");
    bool ret = notOK;
    WHS_Read_GPI_Registers();
   // WHS_info.WHS_sensors.cartridge_cover = Get_COVER_1_State(CartridgesDoor);
   // ret = WHS_info.WHS_sensors.cartridge_cover;
    ret = Get_COVER_1_State(CartridgesDoor);
    if ((ret == OPEN ) && (ret != WHS_info.WHS_sensors.cartridge_cover))
    {
        ReportWithPackageFilter(WasteFilter,"------------ RD Cartridge Cover Sensor is = OPEN(1) -------------", __FILE__, __LINE__, ret, RpMessage, 0, 0);
    }
    if ((ret == CLOSE ) && (ret != WHS_info.WHS_sensors.cartridge_cover))
    {
        ReportWithPackageFilter(WasteFilter,"------------ RD Cartridge Cover Sensor is = CLOSE(0) -------------", __FILE__, __LINE__, ret, RpMessage, 0, 0);
    }
    return ret;
}




WHS_sensor CartridgeCoverCallBackFunction()
{
    WHS_sensor ret = WHS_no_event;
   //ret = RdCartridgeCoverSensor();
    if ( WHS_info.WHS_sensors.cartridge_cover != RdCartridgeCoverSensor() )
    {
        WHS_info.event = WHS_cartridge_cover_sensor;
        WHS_info.WHS_sensors.cartridge_cover = !WHS_info.WHS_sensors.cartridge_cover;
        ReportWithPackageFilter(WasteFilter,"------------ find  :  WHS_cartridge_cover_sensor event -------------", __FILE__, __LINE__, WHS_info.WHS_sensors.cartridge_cover, RpMessage, 0, 0);
        ret = WHS_cartridge_cover_sensor; //todo
    }
    else if (( IFS_info.INK_isOK_flag ) && ( WHS_info.WHS_sensors.cartridge_cover == WHS_CartridgeCoverOPEN))// for Mid-tank filling
    {
        ReportWithPackageFilter(WasteFilter,"------------ Mid-tank Filling  :  Close cartridge cover -------------", __FILE__, __LINE__, WHS_info.WHS_sensors.cartridge_cover, RpMessage, 0, 0);
    }
    /*else if(WHS_info.WHS_sensors.cartridge_cover == WHS_CartridgeCoverOPEN)
    {
        REPORT_MSG(parameter," ------------ Mid-tank Filling  :  Cartridge door open    ----------------- ");
    }*/
    return ret;
}



//WHS_sensor CartridgeInkTimeOutCallBackFunction()
//{
//    WHS_sensor ret = WHS_no_event;
////    uint32_t time_out_second = 0 ;
//
//    if (WHS_info.Ink.time_out) WHS_info.Ink.time_out += 1;
//    Read_MidTank_Pressure_Sensor(WHS_info.Ink.cart_color);
//    if (Get_MidTank_Pressure_Sensor(WHS_info.Ink.cart_color) >= WHS_info.MidTank_capacity  + CARTRIDGE_CAPATICY)
//    {
//        WHS_info.Ink.time_out = 0;
//        WHS_info.event = IFS_MidTankFull;
//        return IFS_MidTankFull;
//    }
//    if (WHS_info.Ink.time_out == CARTRIDGE_INK_TIMEOUT)
//    {
//        //WHS_info.Ink.time_out = 0;
//        //return WHS_info.event = IFS_Timeout;
//
//        REPORT_MSG(WHS_emptying," ------------ IFS_Timeout ----------------- ");
//        // try again :
//        // start timeout
//        // close and open the correct valves
//        REPORT_MSG(WHS_full," ------------ IFS_Timeout_Second_Time ----------------- ");
//        MidTankValvesAction(Cartridge_MidTank_OFF);
//    }
//
//    if (WHS_info.Ink.time_out == (CARTRIDGE_INK_TIMEOUT+1)) //need to open the valve for second chance
//        {
//            MidTankValvesAction(Cartridge_MidTank_ON);
//        }
//
//    if (WHS_info.Ink.time_out == CARTRIDGE_INK_TIMEOUTx2)
//        {
//            WHS_info.Ink.time_out = 0;
//            WHS_info.event = IFS_Timeout_Second;
//            return IFS_Timeout_Second;
//        }
//    return ret;
//}



//WHS_sensor CartridgeInkCallBackFunction()
//{
//    uint32_t parameter = 6 ;
//
//    WHS_sensor ret = WHS_no_event;
//    if (WHS_info.WHS_sensors.IFS_Ink_precense_sensor != RdInkCartridgeSensor())
//    {
//        REPORT_MSG(parameter," ------------ find  : IFS_Ink_presence_sensor event   ----------------- ");
//        WHS_info.event = IFS_INK_presence_sensor;
//        WHS_info.WHS_sensors.IFS_Ink_precense_sensor = !WHS_info.WHS_sensors.IFS_Ink_precense_sensor;
//        ret = IFS_INK_presence_sensor;
//    }
//    return ret;
//}



WHS_sensor CartridgeWaste1CallBackFunction()
{
    WHS_sensor ret = WHS_no_event;
    //ret = RdCartridgeCoverSensor();
    if (WHS_info.WHS_sensors.waste_cartridge1_precense_sensor != RdWasteCartridge1Sensor())
    {
        WHS_info.event = WHS_waste1_presence_sensor;
        WHS_info.WHS_sensors.waste_cartridge1_precense_sensor = !WHS_info.WHS_sensors.waste_cartridge1_precense_sensor;
        ReportWithPackageFilter(WasteFilter,"------------ find  : WHS_waste1_presence_sensor event -------------", __FILE__, __LINE__, WHS_info.WHS_sensors.waste_cartridge1_precense_sensor, RpMessage, 0, 0);
        ret = WHS_waste1_presence_sensor;
    }
    return ret;
}

WHS_sensor CartridgeWaste2CallBackFunction()
{
    WHS_sensor ret = WHS_no_event;

    if (WHS_info.WHS_sensors.waste_cartridge2_precense_sensor != RdWasteCartridge2Sensor())
    {
        WHS_info.event = WHS_waste2_presence_sensor;
        WHS_info.WHS_sensors.waste_cartridge2_precense_sensor = !WHS_info.WHS_sensors.waste_cartridge2_precense_sensor;
        ReportWithPackageFilter(WasteFilter,"------------ find  : WHS_waste2_presence_sensor event -------------", __FILE__, __LINE__, WHS_info.WHS_sensors.waste_cartridge2_precense_sensor, RpMessage, 0, 0);
        ret = WHS_waste2_presence_sensor;
    }
    return ret;
}

/*------------------------------- Waste Cartridge + IFS Cartridge    -----------------------------------------*/
bool InitCartStatus()
{
    WHS_info.WHS_sensors.cartridge_cover = RdCartridgeCoverSensor();

    /* INK =>  CART_1 */
    ReportWithPackageFilter(WasteFilter,"------------------------------------------------------------------------------------------", __FILE__,__LINE__,0, RpMessage, 0, 0);
    ReportWithPackageFilter(WasteFilter,"------------ !!!!!!!  at INIT what to do if IFS_INK is presence  !!!!!!! -----------------", __FILE__,__LINE__,0, RpMessage, 0, 0);
    ReportWithPackageFilter(WasteFilter,"------------------------------------------------------------------------------------------", __FILE__,__LINE__,0, RpMessage, 0, 0);

    if (RdInkCartridgeSensor())
    {
        IFS_info.IFS_Ink_precense_sensor = CarteidgePrecense;
        ReportWithPackageFilter(WasteFilter,"------------ INIT : IFS_INK is presence -----------------", __FILE__,__LINE__, IFS_info.IFS_Ink_precense_sensor, RpMessage, 0, 0);
        Pannel_Leds( CART_1,  MODE_ON);
        cart1.color = colorON;
    }
    else
    {
        IFS_info.IFS_Ink_precense_sensor = CarteidgeNotPrecense;
        ReportWithPackageFilter(WasteFilter,"------------ INIT : IFS_INK is NOT presence -----------------", __FILE__,__LINE__, IFS_info.IFS_Ink_precense_sensor, RpMessage, 0, 0);
        Pannel_Leds( CART_1,  MODE_OFF);
        cart1.color = colorOFF;
    }

    /* waste_cartridge1 => CART_2  */
    if (RdWasteCartridge1Sensor())
    {
        WHS_info.WHS_sensors.waste_cartridge1_precense_sensor = CarteidgePrecense;
        ReportWithPackageFilter(WasteFilter,"------------ INIT : WHS_waste1 cart is presence -----------------", __FILE__,__LINE__, WHS_info.WHS_sensors.waste_cartridge1_precense_sensor, RpMessage, 0, 0);
        Pannel_Leds( CART_2,  MODE_ON);
        cart2.color = colorON;
    }
    else
    {
        WHS_info.WHS_sensors.waste_cartridge1_precense_sensor = CarteidgeNotPrecense;
        ReportWithPackageFilter(WasteFilter,"------------ INIT : WHS_waste1 cart is NOT presence !!! -----------------", __FILE__,__LINE__,WHS_info.WHS_sensors.waste_cartridge1_precense_sensor, RpMessage, 0, 0);
        Pannel_Leds( CART_2,  MODE_OFF);
        cart2.color = colorOFF;
    }

    /* waste_cartridge2 => CART_3  */
    if (RdWasteCartridge2Sensor())
    {
        WHS_info.WHS_sensors.waste_cartridge2_precense_sensor = CarteidgePrecense;
        ReportWithPackageFilter(WasteFilter,"------------ INIT : WHS_waste2 cart is presence -----------------", __FILE__,__LINE__, WHS_info.WHS_sensors.waste_cartridge2_precense_sensor, RpMessage, 0, 0);
        Pannel_Leds( CART_3,  MODE_ON);
        cart3.color = colorON;
    }
    else
    {
        WHS_info.WHS_sensors.waste_cartridge2_precense_sensor = CarteidgeNotPrecense;
        ReportWithPackageFilter(WasteFilter,"------------ INIT : WHS_waste2 cart is NOT presence_sensor !!! -----------------", __FILE__,__LINE__, WHS_info.WHS_sensors.waste_cartridge2_precense_sensor, RpMessage, 0, 0);
        Pannel_Leds( CART_3,  MODE_OFF);
        cart3.color = colorOFF;
    }

    if (IFS_info.IFS_Ink_precense_sensor == CarteidgePrecense)          CartridgeAuthentication(INK_cartridge);
    if (WHS_info.WHS_sensors.waste_cartridge1_precense_sensor == CarteidgePrecense) CartridgeAuthentication(waste_cartridge1);
    if (WHS_info.WHS_sensors.waste_cartridge2_precense_sensor == CarteidgePrecense) CartridgeAuthentication(waste_cartridge2);
    SetActiveWastCartridge();

    if (WHS_info.WHS_sensors.cartridge_cover == WHS_CartridgeCoverOPEN)
    {
        ReportWithPackageFilter(WasteFilter,"------------ WHS_CartridgeCoverOPEN  : add callback for cart1 and cart 2 -----------------", __FILE__,__LINE__, WHS_info.WHS_sensors.cartridge_cover, RpMessage, 0, 0);
        IFS_info.Cartridge_Ink_device_Id    = AddControlCallback("Ink Cartridge CB"   , WasteTankCBFunction, eOneSecond, CartridgeInkCallBackFunction   , 0,0,0 );
        WHS_info.Cartridge_Waste1_device_Id = AddControlCallback("Waste1 Cartridge CB", WasteTankCBFunction, eOneSecond, CartridgeWaste1CallBackFunction, 0,0,0 );
        WHS_info.Cartridge_Waste2_device_Id = AddControlCallback("Waste2 Cartridge CB", WasteTankCBFunction, eOneSecond, CartridgeWaste2CallBackFunction, 0,0,0 );
    }
    WHS_info.Cartridge_Cover_device_Id = AddControlCallback("Cartridge Cover Door", WasteTankCBFunction, eOneSecond, CartridgeCoverCallBackFunction, 0,0,0 );

    return OK;
}

bool RdInkCartridgeSensor()
{
    bool ret = notOK;
    WHS_Read_GPI_Registers();
    ret = Is_Cartridge_Present(CART_1);
    return ret;
}

bool RdWasteCartridge1Sensor()
{
    bool ret = notOK;
    WHS_Read_GPI_Registers();
    ret = Is_Cartridge_Present(CART_2);
    return ret;
}

bool RdWasteCartridge2Sensor()
{
    bool ret = notOK;
    WHS_Read_GPI_Registers();
    ret = Is_Cartridge_Present(CART_3);
    return ret;
}

bool SetActiveWastCartridge()
{
    bool ret = notOK;

    if (( WHS_info.WHS_sensors.waste_cartridge1_precense_sensor == PRECENSE )
         &&   (WHS_info.cartridge_1.autheticate == PASSED)
         &&   (WHS_info.cartridge_1.status == WasteEmpty))
        {
            WHS_info.active_cartridge = waste_cartridge1;
            ReportWithPackageFilter(WasteFilter,"------------ WHS : active_cartridge = waste_cartridge  1 -----------------", __FILE__,__LINE__, WHS_info.active_cartridge, RpMessage, 0, 0);
            AlarmHandlingSetAlarm( EVENT_TYPE__NO_WASTE_CARTRIDGE_AVAILABLE,  false);
        }
    else if (( WHS_info.WHS_sensors.waste_cartridge2_precense_sensor == PRECENSE)
            && (WHS_info.cartridge_2.autheticate == PASSED)
            && (WHS_info.cartridge_2.status == WasteEmpty))
        {
            WHS_info.active_cartridge = waste_cartridge2;
            ReportWithPackageFilter(WasteFilter,"------------ WHS : active_cartridge = waste_cartridge  2 -----------------", __FILE__,__LINE__, WHS_info.active_cartridge, RpMessage, 0, 0);
            AlarmHandlingSetAlarm( EVENT_TYPE__NO_WASTE_CARTRIDGE_AVAILABLE,  false);
        }
    else
        {
            WHS_info.active_cartridge = no_waste_cartridge;
            ReportWithPackageFilter(WasteFilter,"------------ WHS : NO active_cartridge -----------------", __FILE__,__LINE__, WHS_info.active_cartridge, RpMessage, 0, 0);
            AlarmHandlingSetAlarm( EVENT_TYPE__NO_WASTE_CARTRIDGE_AVAILABLE,  true);
        }
    return ret;

}



/*------------------------------------------------------------------------------------------------*/



U8 WHS_init(void)
{
    SetWastePump(CLOSE);
    WHS_info.WHS_valve = VALVE_DIR_WASTE_CARTRIDGE1;
    if (WHS_Type == WHS_TYPE_UNKNOWN)
        Valve_Set(VALVE_WASTE_TANK, WHS_info.WHS_valve);
    else //new WHS
        Trigger_SetWHSValveWatseCartridge(WHS_info.WHS_valve);
        //Valve_Set(VALVE_WASTE_TANK, VALVE_DIR_WASTE_CARTRIDGE1);
    InitCartStatus();
    initWHS_WasteTank();

    return 0;
}


//void midtanktest6_ON(void)
//{
//    Valve_Set( VALVE_2W_CART_MID_1, Cartridge_MidTank_ON);
//    Valve_Set( VALVE_2W_MID_AIR_1 , Cartridge_MidTank_ON);
//}
//
//void midtanktest6_OFF(void)
//{
//    Valve_Set( VALVE_2W_CART_MID_1, Cartridge_MidTank_OFF);
//    Valve_Set( VALVE_2W_MID_AIR_1 , Cartridge_MidTank_OFF);
//}











/*----------------------------------    HW Test   --------------------------------------------------------------*/



bool RdWasteTankFilterSensor()
{
    bool ret = notOK;
    WHS_Read_GPI_Registers();
    WHS_info.WHS_sensors.waste_tank_filter_sensor = WHS_GPI_SW_FILTER_PRES();
    ret = WHS_info.WHS_sensors.waste_tank_filter_sensor;
    return ret;
}


U8 RdCartridgeDoor()
{
    bool ret = notOK;
    ret = RdCartridgeCoverSensor();
    return ret;
}
U8 RdWaste1()
{
    bool ret = notOK;
    ret = RdWasteCartridge1Sensor();
    return ret;
}
U8 RdWaste2()
{
    bool ret = notOK;
    ret = RdWasteCartridge2Sensor();
    return ret;
}
U8 SetValve(bool value)
{
    bool ret = OK;
    WHS_info.WHS_valve = value;
    if (WHS_Type == WHS_TYPE_UNKNOWN)
        Valve_Set(VALVE_WASTE_TANK, WHS_info.WHS_valve);
    else //new WHS
        Trigger_SetWHSValveWatseCartridge(WHS_info.WHS_valve);
    return ret;
}

U8 SetPump(bool value)
{
    bool ret = notOK;
    ret = SetWastePump(value);
    return ret;
}

U8 RdFilter()
{
    bool ret = notOK;
    ret = RdWasteTankFilterSensor();
    return ret;
}
U8 RdEmpty()
{
    bool ret = notOK;
    ret = RdWasteTankEmptySensor();
    return ret;
}
U8 RdFull()
{
    bool ret = notOK;
    ret = RdWasteTankFullSensor();
    return ret;
}
U8 RdOverFlow()
{
    bool ret = notOK;
    RdWasteTankOverFlowSensor();
    return ret;
}



U8 WHS_HW_test()
{
    bool ret = notOK;
/*    ret = RdCartridgeDoor();
    while (ret == RdCartridgeDoor());
*/
/* ---  connect JO271 cable ---*/
    ret = RdWaste1();
    while (ret == RdWaste1());// blue = waste1
    ret = RdWaste2();
    while (ret == RdWaste2());// yellow = waste2

    /* ---  connect JO71 JO72 JO121  cable ---*/

    ret = SetValve(1); //red LED On
    ret = SetValve(0); //red LED Off


    ret = SetPump(1);  // led green (backplan) On
    ret = SetPump(0);  // led green (backplan) Off

    ret = RdFilter();
    while (ret == RdFilter());
    ret = RdEmpty();
    while (ret == RdEmpty());
    ret = RdFull();
    while (ret == RdFull());
//    ret = RdOverFlow();
//    while (ret == RdOverFlow());
    return ret;
}


/*------------------------------------------------------------------------------------------------*/