Project

General

Profile

Statistics
| Branch: | Revision:

pse-2015-16-grupo-3 / tpf / mainTest.c @ 87a830c5

History | View | Annotate | Download (13.5 KB)

1
#include "tempo.h"
2
#include "display.h"
3
#include "config.h"
4
#include "io.h"
5
#include"power.h"
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <wiringPi.h>
9

    
10
//#define testing
11
#define release
12

    
13
#ifdef testing
14

    
15
//#define testing_display
16
#include <lcd.h>
17
#include <wiringPi.h>
18

    
19
#endif
20

    
21
//switches / plugs / doors
22

    
23
//state of the on/off plug (switch) (1-> on; 0->off)
24
uint8_t onOffState = 0;
25

    
26
//state of the door (switch) (0-> closed; 1->open)
27
uint8_t doorState = 0;
28

    
29
//state of temperature detector (switch) (1-> over 80º; 0->under 80º)
30
uint8_t temperatureState = 0;
31

    
32

    
33
//buttons
34
//method:
35
//btnPressed -> current state of the button
36
//btnPressedLast -> previous state of the button
37

    
38
//power button
39
uint8_t powerBtnPressed=0;
40
uint8_t powerBtnPressedLast=0;
41
//time button
42
uint8_t timeBtnPressed=0;
43
uint8_t timeBtnPressedLast=0;
44
//plus button
45
uint8_t plusBtnPressed=0;
46
uint8_t plusBtnPressedLast=0;
47
//minus button
48
uint8_t minusBtnPressed=0;
49
uint8_t minusBtnPressedLast=0;
50
//start/stop button
51
uint8_t startBtnPressed=0;
52
uint8_t startBtnPressedLast=0;
53

    
54
//internal states
55

    
56
//internal state of the hoven (0-> disabled; 1->running)
57
uint8_t runState=0;
58
//internal state to define which value is being configured (0-> power; 1-> time)
59
uint8_t powerOrTime=0;
60

    
61

    
62
//Ciclo de vida do sistema
63
int main()
64
{
65
#ifndef testing
66
#ifdef release
67
    
68
    pse_time_t temp;
69
    uint8_t flag=0;
70
    
71
    initBoard();
72
    initDisplay();
73
    
74
    while(1)
75
    {
76
        (uint8_t) getONOFFSwitch(&onOffState);
77
        
78
        if(onOffState==1)
79
        {
80
            //check pressed buttons
81
            
82
            //if user is already defining power settings, program only listens to pressing of the time button
83
            if(powerOrTime==0)
84
            {
85
                (uint8_t) getTimeButton(&timeBtnPressed);
86

    
87
                if (timeBtnPressed == 1 && timeBtnPressedLast == 0) 
88
                {
89
                    timeBtnPressedLast = 1;
90
                    powerOrTime=1;
91
                } 
92
                else if (timeBtnPressed == 0 && timeBtnPressedLast == 1) 
93
                {
94
                    timeBtnPressedLast = 0;
95
                }
96
                
97
                
98
                //read plus button
99
                (uint8_t) getPlusButton(&plusBtnPressed);
100

    
101
                if (plusBtnPressed == 1 && plusBtnPressedLast == 0) 
102
                {
103
                    plusBtnPressedLast = 1;
104
                    
105
                    nextPowerSetting();
106
                    
107
                } 
108
                else if (plusBtnPressed == 0 && plusBtnPressedLast == 1) 
109
                {
110
                    plusBtnPressedLast = 0;
111
                }
112
                
113
                
114
                //read minus button
115
                (uint8_t) getMinusButton(&minusBtnPressed);
116

    
117
                if (minusBtnPressed == 1 && minusBtnPressedLast == 0) 
118
                {
119
                    minusBtnPressedLast = 1;
120
                    
121
                    previousPowerSetting();
122
                    
123
                } 
124
                else if (minusBtnPressed == 0 && minusBtnPressedLast == 1) 
125
                {
126
                    minusBtnPressedLast = 0;
127
                }
128
                
129
                
130
            }
131
            //if user is already defining time settings, program only listens to pressing of the power button
132
            else
133
            {
134
                (uint8_t) getPowerButton(&powerBtnPressed);
135

    
136
                if (powerBtnPressed == 1 && powerBtnPressedLast == 0) 
137
                {
138
                    timeBtnPressedLast = 1;
139
                    powerOrTime=0;
140
                } 
141
                else if (powerBtnPressed == 0 && powerBtnPressedLast == 1) 
142
                {
143
                    powerBtnPressedLast = 0;
144
                }
145
                
146
                
147
                //read plus button
148
                (uint8_t) getPlusButton(&plusBtnPressed);
149

    
150
                if (plusBtnPressed == 1 && plusBtnPressedLast == 0) 
151
                {
152
                    plusBtnPressedLast = 1;
153
                    
154
                    increase();
155
                    
156
                } 
157
                else if (plusBtnPressed == 0 && plusBtnPressedLast == 1) 
158
                {
159
                    plusBtnPressedLast = 0;
160
                }
161
                
162
                
163
                //read minus button
164
                (uint8_t) getMinusButton(&minusBtnPressed);
165

    
166
                if (minusBtnPressed == 1 && minusBtnPressedLast == 0) 
167
                {
168
                    minusBtnPressedLast = 1;
169
                    
170
                    decrease();
171
                    
172
                } 
173
                else if (minusBtnPressed == 0 && minusBtnPressedLast == 1) 
174
                {
175
                    minusBtnPressedLast = 0;
176
                }
177
                
178
                
179
            }
180
            
181
            
182
            
183
            (uint8_t) IsDoorOpen(&doorState);
184
            
185
            if(doorState==0)
186
            {
187
                //read start/stop button
188
                (uint8_t) getStartStop(&startBtnPressed);
189

    
190
                if (startBtnPressed == 1 && startBtnPressedLast == 0) 
191
                {
192
                    startBtnPressedLast = 1;
193
                    
194
                    if(runState==0)
195
                    {
196
                        runState=1;
197
                    }
198
                    else
199
                    {
200
                        runState=0;
201
                    }
202
                    
203
                } 
204
                else if (startBtnPressed == 0 && startBtnPressedLast == 1) 
205
                {
206
                    startBtnPressedLast = 0;
207
                }
208
                
209
                
210
                //machine is Running
211
                if(runState==1)
212
                {
213
                    while(flag==0)
214
                    {
215
                        delay(1000);
216
                        decrease();
217
                        getTime(&temp);
218
                        if(temp.segundos==0)
219
                        {
220
                            flag=1;
221
                            runState=0;
222
                        }
223
                        showDisplay();
224
                    }
225
                    flag=0;
226
                }
227
                    
228
                
229
                
230
            }
231
            
232
            showDisplay();
233
        }
234
        
235
    }
236
    
237
    
238
    
239
#endif
240
#endif
241
    
242
#ifndef release
243
#ifndef testing
244
        pse_time_t tempo;
245
        tempo.segundos = 0;
246
        tempo.minutos = 0;
247
        tempo.horas = 0;
248
        setTime(tempo);
249
        char c;
250
        
251

    
252
        while(1)
253
        {
254
            
255
                
256
                printf("t-Escolher tempo\n p-Escolher power\n s-Start/Stop\n");
257
                c = '0';
258
                do{c=getchar();}while(isspace(c));
259
                if(c == 't')
260
                {
261
                        c = '0';
262
                        printf("Escolha de tempo\n");
263
                        while(c != 't') //Enquanto não pressionar o botão Time novamente, fica na janela de regulação do tempo
264
                        {
265
                                //disp.setDisplay(time.getTime());        //Mostra o tempo atualmente escolhido no visor
266
                                printf("t-Sair de escolha de tempo\n +Aumentar tempo\n -Diminuir tempo\n");
267
                                pse_time_t atual;
268
                                getTime(&atual);
269
                                printf("Tempo atual = %d:%d:%d\n", atual.horas, atual.minutos, atual.segundos);
270
                                c = '0';
271
                                do{c=getchar();}while(isspace(c));
272
                                if(c == '+')
273
                                {
274
                                        //if(time.increase());        //Se o botão "+" for pressionado é chamada a função para incrementar o tempo
275
                                        increase();
276
                                        printf("Tempo Aumentado\n");
277
                                        pse_time_t atual;
278
                                        getTime(&atual);
279
                                }
280
                                if(c == '-')
281
                                {
282
                                        //if(time.decrease());        //Se o botão "-" for pressionado é chamada a função para decrementar o tempo
283
                                        decrease();
284
                                        printf("Tempo Decrementado\n");
285
                                        pse_time_t atual;
286
                                        getTime(&atual);
287
                                }
288
                                
289
                        }
290
                        timeSet = 1;        
291
                }
292
                else if(c == 'p')
293
                {
294
                        c='0';
295
                        printf("Modo de escolha de power\n");
296
                        
297
                        
298
                        //Caso o microondas esteja a funcionar, apenas mostra o valor do power durante 2 segundos
299
                        if(isRunning)
300
                                printf("Microondas em execucao\n");
301
                                //disp.setDisplay(powerSettings[currentPower]);        //mostrar isto durante 2 segundos
302
                        
303
                        //Caso não esteja a funcionar, permite ao utilizador escolher o power pretendido
304
                        else
305
                        {
306
                                printf("p-Sair de escolha de power\n +Aumentar power\n -Diminuir power\n");
307
                                while(c != 'p')
308
                                {
309
                                        
310
                                        printf("Power atual:%d \n",powerSettings[currentPower]);
311
                                        c = '0';
312
                                        do{c=getchar();}while(isspace(c));
313
                                        //disp.setDisplay(powerSettings[currentPower]);
314
                                        if(c == '+')
315
                                        {
316
                                                if(currentPower < 3)
317
                                                currentPower++;
318
                                        }
319
                                        if(c == '-')
320
                                        {
321
                                                if(currentPower > 0)
322
                                                currentPower--;
323
                                        }
324
                                }
325
                                powerSet = 1;        
326
                        }
327
                }
328
                else if(c == 's')
329
                {
330
                        if(!isRunning)
331
                        {
332
                                //Verifica se as condicoes estao definidas
333
                                if(powerSet && timeSet)
334
                                {        
335
                                        printf("MICROONDAS LIGADO\n");
336
                                        isRunning = 1;
337
                                        //Liga o microondas com o tempo e potencia configuradas
338
                                        /*powerOven(time.getTime(),powerSettings[currentPower];
339
                                        switchLight(ON);
340
                                        switchTurnMotor(ON);*/
341
                                }
342
                                else printf("Tempo ou Power não definido\n");
343
                        }
344
                        else printf("MICROONDAS DESLIGADO\n");                        
345
                        
346
                }
347
        }
348
        
349
#endif
350
#ifdef testing
351
        
352
        
353
        initBoard();
354
        /*
355
        //char c;
356
        uint8_t btnPressed;
357
        uint8_t lastStateBtnPressed=0;
358
        uint8_t state=0;
359
        uint16_t btnCounter = 0;
360
        uint8_t tim=0;
361
        
362
        
363
        while(1)
364
        {
365
            
366
            (uint8_t) getMinusButton(&btnPressed);
367
            
368
            if(btnPressed == 1 && lastStateBtnPressed==0)
369
            {
370
                
371
                if (state == 0) {
372
                    (u_int8_t) turnOnLight();
373
                    (u_int8_t) turnOnMegatron();
374
                    state = 1;
375
                } 
376
                else {
377
                    (u_int8_t) turnOffLight();
378
                    (u_int8_t) turnOffMegatron();
379
                    state = 0;
380
                }
381
                lastStateBtnPressed = 1;
382
                
383
                
384
               
385
            }
386
            else if(btnPressed == 0 && lastStateBtnPressed==1)
387
            {
388
                lastStateBtnPressed = 0;
389
                btnCounter=0;
390
            }
391
            
392
            
393
        }
394
        */
395
        
396
#ifndef testing_display
397
        
398
        (uint8_t) turnOffLight();
399
        (uint8_t) turnOffMegatron();
400
        
401
        (uint8_t) initDisplay();
402
        
403
        // start btn test
404
        displayShowMessage("Press start btn.");
405
        
406
        while (startBtnPressed == 0)
407
        {
408
            (uint8_t) getStartStop(&startBtnPressed);
409
        }
410
        
411
        displayShowMessage("   WELL DONE!   ");
412
        delay(3000);
413
        
414
        //Plus btn test
415
        displayShowMessage("Press plus  btn.");
416
        
417
        while (plusBtnPressed == 0)
418
        {
419
            (uint8_t) getPlusButton(&plusBtnPressed);
420
        }
421
        
422
        displayShowMessage("   WELL DONE!   ");
423
        delay(3000);
424
        
425
        
426
        //Minus btn test
427
        displayShowMessage("Press minus btn.");
428
        
429
        while (minusBtnPressed == 0)
430
        {
431
            (uint8_t) getMinusButton(&minusBtnPressed);
432
        }
433
        
434
        displayShowMessage("   WELL DONE!   ");
435
        delay(3000);
436
             
437
        
438
        //Power btn test
439
        displayShowMessage("Press power btn.");
440
        
441
        while (powerBtnPressed == 0)
442
        {
443
            (uint8_t) getPowerButton(&powerBtnPressed);
444
        }
445
        
446
        displayShowMessage("   WELL DONE!   ");
447
        delay(3000);
448
        
449
        
450
        
451
        
452
        
453
        //Time btn test
454
        displayShowMessage("Press time btn.");
455
        
456
        while (timeBtnPressed == 0)
457
        {
458
            (uint8_t) getTimeButton(&timeBtnPressed);
459
        }
460
        
461
        displayShowMessage("   WELL DONE!   ");
462
        delay(3000);
463
        
464

    
465
        //door sw test
466
        displayShowMessage(" open the door. ");
467
        
468
        while (doorState == 0)
469
        {
470
            (uint8_t) IsDoorOpen(&doorState);
471
        }
472
        
473
        displayShowMessage("   WELL DONE!   ");
474
        delay(3000);
475
                
476
        
477
        //on off test
478
        displayShowMessage(" turn power on. ");
479
        
480
        while (onOffState == 0)
481
        {
482
            (uint8_t) getONOFFSwitch(&onOffState);
483
        }
484
        
485
        displayShowMessage("   WELL DONE!   ");
486
        delay(3000);
487
        
488
        
489
        //temperature sw test
490
        displayShowMessage("    heat up.    ");
491
        
492
        while (temperatureState == 0)
493
        {
494
            (uint8_t) getTemperatureSwitch(&temperatureState);
495
        }
496
        
497
        displayShowMessage("   WELL DONE!   ");
498
        delay(3000);
499
        
500
        
501
        //inside light check
502
        displayShowMessage("check insid lamp");
503
        (uint8_t) turnOnLight();
504
        delay(5000);
505
        (uint8_t) turnOffLight();
506
        
507
        
508
        displayShowMessage("   WELL DONE!   ");
509
        delay(3000);
510
        
511
        //megatron light check
512
        displayShowMessage(" check MEGATRON ");
513
        (uint8_t) turnOnMegatron();
514
        delay(5000);
515
        (uint8_t) turnOffMegatron();
516
        
517
        
518
        displayShowMessage("   WELL DONE!   ");
519
        delay(3000);
520
        
521
        
522
        //tests finished
523
        displayShowMessage("   THANK YOU!   ");
524
        delay(3000);
525
        
526
#endif
527
        
528
#ifdef testing_display
529
        
530
        pse_time_t tempo;
531
        tempo.horas=0;
532
        tempo.minutos=20;
533
        tempo.segundos=20;
534
        uint16_t power = powerSettings[0];
535
        
536
        initDisplay();
537
        setDisplayPower(power);
538
        setDisplayTime(tempo);
539
        showDisplay();
540
        displayShowMessage("joguetes");
541
        
542
        while(1)
543
        {
544
            displayShowCurrentTime();
545
            delay(1000);
546
        }
547
        
548
        
549
        
550
#endif
551
        
552
#endif
553
        
554
#endif
555

    
556
}