aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/IFS/ifs.c
blob: fc50173fcbc92c6fd5056dd8ef9f787e8eca4324 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
 * @file ifs.h
 *
 *  @date   Jul 8, 2019
 *  @author Shlomo
 *
 *  @date   26.8.2019
 *  @author Shai
 */

#include "PMR/Diagnostics/CartridgeSlot.pb-c.h"
#include "PMR/Diagnostics/CartridgeTagContent.pb-c.h"
#include "common/utilities/RfidTagContent.h"
#include <Drivers/I2C_Communication/RFID_NFC/RFIDTagInfo.h>
#include <Utilities/RfidTagContent.h>

#include <DataDef.h>
#include "include.h"

#include "Modules/Waste/Waste.h"
#include "Modules/IFS/ifs.h"
#include "Modules/AlarmHandling/AlarmHandling.h"
#include "CartridgeValidationRequest.pb-c.h"
#include "CartridgeValidationResponse.pb-c.h"
#include  <PMR/Diagnostics/EventType.pb-c.h>
#include "drivers/Valves/Valve.h"
#include "Modules/Waste/Waste.h"

bool IFS_TimeOutAlarm(bool status);
bool ColorMatch();
bool MidTankValvesAction(bool action);


//RfidTagContent TagContent[MAX_CARTRIDGES];
NFC_Tag NFCTag[MAX_CARTRIDGES];
//void TagInitialize(RfidTagContent* TagContent);

void IFS_Init(void)
{
    int i;
    for (i=0;i<MAX_CARTRIDGES;i++)
    {
        //TagInitialize(&TagContent[i]);
        memset(NFCTag[i].Buf,0,sizeof(NFCTag[i]));
    }
}
/*void TagInitialize(RfidTagContent* TagContent)
{

}*/






/**
 *
 * @brief detect changes in cartridge INK presence
 *  this function detect changes in IFS presence cartridge
 *  when changes are happens it update the presence status
 *  and the correct event
 * @return IFS_INK_presence_sensor or WHS_no_event event
 */

WHS_sensor CartridgeInkCallBackFunction()
{
    uint32_t parameter = 6 ;

    WHS_sensor ret = WHS_no_event;

    if (IFS_info.IFS_Ink_precense_sensor != RdInkCartridgeSensor())
    {
        REPORT_MSG(parameter," ------------ find  : IFS_Ink_presence_sensor event   ----------------- ");
        WHS_info.event = IFS_INK_presence_sensor;
        IFS_info.IFS_Ink_precense_sensor = !IFS_info.IFS_Ink_precense_sensor;
        ret = IFS_INK_presence_sensor;
    }
    return ret;
}

/**
 *
 * @brief detect TimeOut
 * we have 2 way to stop filling:
 * 1. time out
 * 2. detect Midtank is full.
 *
 * 1. time out process is:
 *      1. detect timeout after 5 minutes
 *      2. close and open the valves
 *      3. count 5 minutes again
 *2. measure MidTank capacity
 *  if MidTank is full stop count
 *
 * @return IFS_INK_presence_sensor or WHS_no_event event
 */
WHS_sensor CartridgeInkTimeOutCallBackFunction()
{
    WHS_sensor ret = WHS_no_event;
    double MidTank_Pressure = 0;

    MidTank_Pressure = Read_MidTank_Pressure_Sensor(IFS_info.Ink.cart_color);// Get_MidTank_Pressure_Sensor(IFS_info.Ink.cart_color);
    ReportWithPackageFilter(WasteFilter,"------------ IFS_info.Ink.time_out and Pressure  -----------------", __FILE__,__LINE__,(IFS_info.Ink.time_out * SECONDS_5), RpMessage, (int)(MidTank_Pressure*1000), 0);

    if (IFS_info.Ink.time_out) IFS_info.Ink.time_out += 1; /*** counter  ***/


//#ifdef timeout_capacity
    /*** detect the MidTank capacity is full  ***/
    //Read_MidTank_Pressure_Sensor(IFS_info.Ink.cart_color);
    if (MidTank_Pressure >= IFS_info.MidTank_capacity  + CARTRIDGE_CAPATICY)
    {
        IFS_info.Ink.time_out = 0;
        WHS_info.event = IFS_MidTankFull;
        return IFS_MidTankFull;
    }
//#endif
    /*** first TimeOut  ***/
    if (IFS_info.Ink.time_out == CARTRIDGE_INK_TIMEOUT)
    {
        ReportWithPackageFilter(WasteFilter,"------------ first IFS_info.Ink.time_out   -----------------", __FILE__, __LINE__, (IFS_info.Ink.time_out * SECONDS_5), RpMessage, MidTank_Pressure, 0);
        //IFS_info.Ink.time_out = 0;
        //return IFS_info.event = IFS_Timeout;

       REPORT_MSG(WHS_emptying," ------------ IFS_Timeout close valves----------------- ");
        // try again :
        // start timeout
        // close and open the correct valves

        MidTankValvesAction(Cartridge_MidTank_OFF);
    }

    if (IFS_info.Ink.time_out == (CARTRIDGE_INK_TIMEOUT + 1)) //need to open the valve for second chance
        {
        ReportWithPackageFilter(WasteFilter,"------------ IFS_Timeout open valves for second timeout  -----------------", __FILE__, __LINE__, (IFS_info.Ink.time_out * SECONDS_5), RpMessage, MidTank_Pressure, 0);
        MidTankValvesAction(Cartridge_MidTank_ON);
        }

    if (IFS_info.Ink.time_out >= (CARTRIDGE_INK_TIMEOUTx2 + 1))
        {
            REPORT_MSG(WHS_full," ------------ IFS_Timeout_Second_Time ----------------- ");
            IFS_info.Ink.time_out = 0;
            WHS_info.event = IFS_Timeout_Second;
            return IFS_Timeout_Second;
        }
    return ret;
}

/**
 *
 * @brief IFS Time Out Alarm
 *
 * @return OK
 */
bool IFS_TimeOutAlarm(bool status)
{
    bool ret =  OK;
        switch (IFS_info.Ink.cart_color) //todo enter the correct color from RFID
        {
            case 1 :
                AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_1_FILL_TIMEOUT,  status);
                break;
            case 2 :
                AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_2_FILL_TIMEOUT,  status);
               break;
            case 3 :
                AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_3_FILL_TIMEOUT,  status);
                break;
            case 4 :
                AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_4_FILL_TIMEOUT,  status);
               break;
            case 5 :
                AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_5_FILL_TIMEOUT,  status);
               break;
            case 6 :
                AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_6_FILL_TIMEOUT,  status);
               break;
            case 7 :
                AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_7_FILL_TIMEOUT,  status);
                break;
            case 8 :
                AlarmHandlingSetAlarm( EVENT_TYPE__MID_TANK_8_FILL_TIMEOUT,  status);
               break;
            default: //wrong color
                ret = notOK;
                break;
        }

        return ret;
}

/**
 *
 * @brief MidTank Valves Action
 * the MidTank have 2 Valves:
 * 1. Ink Color Valve to get INK from the INK Cartridge to Midtank
 * 2. Air valve to get INK in the Midtank we have to open the Air valve so the air can go out
 *
 * @return action to open the Valve use: Cartridge_MidTank_ON/Atm_MidTank_ON
 *                to close the Valve use : Cartridge_MidTank_OFF/Atm_MidTank_OFF
 */
bool MidTankValvesAction(bool action) //Cartridge_MidTank_ON of Cartridge_MidTank_OFF
{
    bool ret =  false;
    if (action == Cartridge_MidTank_ON)
    {
        Enable_MidTank_Pressure_Reading(IFS_info.Ink.cart_color);
    }
    else
    {
        Disable_MidTank_Pressure_Reading(IFS_info.Ink.cart_color);
    }
    Valve_Set(IDS_Id_to_AirValve[IFS_info.Ink.cart_color], action );   //Atm_MidTank_OFF/ON
    Valve_Set(IDS_Id_to_CartrideValve[IFS_info.Ink.cart_color], action );   //Atm_MidTank_OFF/ON

    ret = true;
    return ret;
}

bool ColorMatch()
{
    bool ret =  false;

    ret = true;
    return ret;
}

char CartridgeRequestToken[36+1];
int  CartridgeRequestId = 0x01010101;


/**
 *
 * @brief create message to PPC
 *  temporary message RFID will replace that message.
 *  send a request to PPC to know the MidTank number
 * @return OK
 */
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);
    REPORT_MSG(WHS_full," ------------ IFS: send message_container__pack ----------------- ");
    my_free(responseContainer.data.data);
    SendChars((char*)container_buffer, container_size);

    return OK;
}

/**
 *
 * @brief PPC answers the MidTank number and call WasteTankCBFunction() function.
 *  temporary message RFID will replace that message.
 *
 * @return OK
 */
bool CartridgeValidationResponseFunc(MessageContainer* requestContainer)
{
    bool ret = notOK;

    CartridgeValidationResponse* response = cartridge_validation_response__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    // check the message:
    // 1. the pointer is not NULL !!!
    // 2. the index is out of rang
    if (response == NULL)
    {
        REPORT_MSG(WHS_filling," ------------ IFS_filling message from PPC is null!!! ----------------- "); // write to log error
        return ret;
    }

    if (response->index < NUM_OF_MIDTANKS)
    {
        IFS_info.Ink.cart_color = response->index; // midtank 1-8
        ReportWithPackageFilter(WasteFilter,"------------ IFS get message response->index is  -----------------", __FILE__, __LINE__, response->index, RpMessage, 0, 0);
        WHS_info.event = IFS_INK_authentication_pass;
    }
    else
    {
        ReportWithPackageFilter(WasteFilter,"------------ IFS get message response->index is out of rang -----------------", __FILE__, __LINE__, response->index, RpMessage, 0, 0);
        WHS_info.event = IFS_INK_authentication_fail;
    }

    WasteTankCBFunction();//call cart is ok
    ret = OK;

return ret;
}


/**
 *
 * @brief that response is from STUB. replace the PPC
 *  use stub : Poweroff_progress.cs from P:\Users - Public\Shai_Frieder
 *  ProgressRequest progressRequest = new ProgressRequest();
 *  progressRequest.Amount = 0xCC; //porgress id is 0xCC
 *  progressRequest.Delay = 1; //midtank number from 0-7
 *
 *
 * @return OK
 */
void ResponseDemo(int MidtankId)
{
    WHS_info.event = IFS_INK_authentication_pass;
    //global mid tank id = MidtankId;
    IFS_info.Ink.cart_color = MidtankId;
    WasteTankCBFunction();//call cart is ok
}

bool  INKCartridgeAuthentication(cartridge_name cart_name)
{
    bool ret = notOK;
    // TBD;
    switch (cart_name)
    {
        case INK_cartridge:
            IFS_info.Ink.autheticate = PASSED;  //todo
            //Pannel_Leds(CART_1, 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;
}