-
Notifications
You must be signed in to change notification settings - Fork 0
/
APDS_Gesture.c
677 lines (623 loc) · 25.9 KB
/
APDS_Gesture.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
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
/*
* Apds9960.c
*
* Created on: Sep 26, 2019
* Author: GLNUG
*/
#include "C_Extensions.h"
#include "I2c.h"
#include "Micro.h"
#include "I2cMgr.h"
#include <string.h>
#include "Apds_Gesture.h"
#define APDS_I2C_CHANNEL I2C1
#define ENABLE_REGISTER 0x80u
//#define APDS_I2C_SPEED I2C_50KHZ
BOOL_TYPE Write_stat_enable_1 = FALSE;
BOOL_TYPE Write_stat_enable = FALSE;
BOOL_TYPE Write_stat_gmode = FALSE;
BOOL_TYPE Write_stat_gmode_1 = FALSE;
BOOL_TYPE Write_stat_gstat = FALSE;
BOOL_TYPE check_gstat = FALSE;
BOOL_TYPE check_gvalid = FALSE;
BOOL_TYPE stat_read = FALSE;
BOOL_TYPE GFLVL_read = FALSE;
BOOL_TYPE data_valid = FALSE;
BOOL_TYPE check_gvalid_fn = FALSE;
BOOL_TYPE GFIFO_U_STAT = FALSE;
BOOL_TYPE GFIFO_U_read = FALSE;
BOOL_TYPE Write_Enter_Threshold_enable_addr = FALSE;
BOOL_TYPE Write_Enter_Threshold_enable_value = FALSE;
BOOL_TYPE Read_threshold_value = FALSE;
BOOL_TYPE Write_Exit_Threshold_enable_addr = FALSE;
BOOL_TYPE Write_Exit_Threshold_enable_value = FALSE;
BOOL_TYPE Read_Exit_threshold_value = FALSE;
#define APDS_REG_CONFIG1 0x8Du
#define APDS_REG_ID 0x92u
#define APDS_I2C_ADDRESS (0x39 << 1)
#define APDS_BUF_SIZE 0x08u
//Data exclusively for Gesture
uint8 fifo_level = 0;
uint8 bytes_read = 0;
uint8 fifo_data[128];
uint8 gstatus;
int motion;
int i;
int val = 0;
static uint8 ApdsHandle;
static APDS_I2C_STATE_TYPE ApdsI2cState;
APDS_I2C_DIR_TYPE ApdsDir;
static uint8 ApdsBuf[APDS_BUF_SIZE];
static uint8 ApdsBuf_1[1];
static uint8 GmodeBuf[1];
static uint8 GstatBuf[1];
static uint8 GFLVLBuf[1];
static uint8 Threshold_Enter_Buf[1];
static uint8 Threshold_Exit_Buf[1];
//static uint8 GFIFO_U_Buf[1];
static uint8 ApdsBusyCounter;
static uint8 Enable_Value[1] = {0x41};
static uint8 GMODE_VAL[1] = {0x01};
static uint8 DEFAULT_GENTER_THRESHOLD[1] = {0x40}; // Threshold for entering gesture mode
static uint8 DEFAULT_GEXIT_THRESHOLD[1] = {0x30}; // Threshold for exiting gesture mode
void Apds__Initialize(void){
ApdsI2cState = APDS_I2C_STATE_INIT;
memset(ApdsBuf, 0, 8);
ApdsBusyCounter = 0;
ApdsHandle = I2cMgr__GetDeviceHandle(APDS_I2C_CHANNEL);
}
/*
BOOL_TYPE CheckValidData(){
}
*/
void Apds__Handler(void){
switch (ApdsI2cState){
//setting value for ENABLE_REGISTER and reading it
case APDS_I2C_STATE_INIT:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
//I2c__Write(APDS_I2C_CHANNEL, APDS_REG_ID, ApdsBuf, 0);
Write_stat_enable = I2c__Write(APDS_I2C_CHANNEL, ENABLE_REGISTER, Enable_Value, 1);
//Write_stat_1 = I2c__Write(APDS_I2C_CHANNEL, ENABLE_REGISTER, ApdsBuf, 0);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE;
}
break;
case APDS_I2C_STATE_WAIT_END_WRITE:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else
{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_STATE_WRITE_VALUE;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
case APDS_I2C_STATE_WRITE_VALUE:
Write_stat_enable_1 = I2c__Write(APDS_I2C_CHANNEL, ENABLE_REGISTER, ApdsBuf, 0);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_1;
break;
case APDS_I2C_STATE_WAIT_END_WRITE_1:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_STATE_REQUEST;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
case APDS_I2C_STATE_REQUEST:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
I2c__RequestRead(APDS_I2C_CHANNEL, 1);
ApdsI2cState = APDS_I2C_STATE_CHECK_READ;
}
break;
case APDS_I2C_STATE_CHECK_READ:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if (I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE) == I2C_STATE_IDLE){
//I2c__Read(APDS_I2C_CHANNEL, ApdsBuf, 1);
I2c__Read(APDS_I2C_CHANNEL, ApdsBuf_1, 1);
I2cMgr__ReleaseBus(ApdsHandle);
ApdsI2cState = APDS_I2C_STATE_WRITE_ENTER_THRESHOLD;
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
}
}
break;
//-------------------------------------------------------------------------------------------------//
//CURRENTLY AT THE STATE OF SETTING THE ENTER AND EXIT THRESHOLD USING I2C COMMUNICATION
case APDS_I2C_STATE_WRITE_ENTER_THRESHOLD:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
Write_Enter_Threshold_enable_addr = I2c__Write(APDS_I2C_CHANNEL, APDS9960_GPENTH, DEFAULT_GENTER_THRESHOLD, 1);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_7;
}
break;
case APDS_I2C_STATE_WAIT_END_WRITE_7:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_STATE_WRITE_ENTER_THRESHOLD_VALUE;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
case APDS_I2C_STATE_WRITE_ENTER_THRESHOLD_VALUE:
Write_Enter_Threshold_enable_value = I2c__Write(APDS_I2C_CHANNEL, APDS9960_GPENTH, ApdsBuf, 0);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_8;
break;
case APDS_I2C_STATE_WAIT_END_WRITE_8:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_STATE_REQUEST_THRESHOLD_VALUE;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
case APDS_I2C_STATE_REQUEST_THRESHOLD_VALUE:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
I2c__RequestRead(APDS_I2C_CHANNEL, 1);
ApdsI2cState = APDS_I2C_STATE_CHECK_THRESHOLD_VALUE;
}
break;
case APDS_I2C_STATE_CHECK_THRESHOLD_VALUE:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if (I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE) == I2C_STATE_IDLE){
//I2c__Read(APDS_I2C_CHANNEL, ApdsBuf, 1);
Read_threshold_value = I2c__Read(APDS_I2C_CHANNEL, Threshold_Enter_Buf, 1);
I2cMgr__ReleaseBus(ApdsHandle);
ApdsI2cState = APDS_I2C_STATE_WRITE_EXIT_THRESHOLD;
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
}
}
break;
//--------------------------------------------------------------------------------------------------//
//CURRENTLY AT THE STATE OF SETTING THE ENTER AND EXIT THRESHOLD USING I2C COMMUNICATION
case APDS_I2C_STATE_WRITE_EXIT_THRESHOLD:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
Write_Exit_Threshold_enable_addr = I2c__Write(APDS_I2C_CHANNEL, APDS9960_GEXTH, DEFAULT_GEXIT_THRESHOLD, 1);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_9;
}
break;
case APDS_I2C_STATE_WAIT_END_WRITE_9:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_STATE_WRITE_EXIT_THRESHOLD_VALUE;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
case APDS_I2C_STATE_WRITE_EXIT_THRESHOLD_VALUE:
Write_Exit_Threshold_enable_value = I2c__Write(APDS_I2C_CHANNEL, APDS9960_GEXTH, ApdsBuf, 0);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_10;
break;
case APDS_I2C_STATE_WAIT_END_WRITE_10:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_STATE_REQUEST_EXIT_THRESHOLD_VALUE;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
case APDS_I2C_STATE_REQUEST_EXIT_THRESHOLD_VALUE:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
I2c__RequestRead(APDS_I2C_CHANNEL, 1);
ApdsI2cState = APDS_I2C_STATE_CHECK_EXIT_THRESHOLD_VALUE;
}
break;
case APDS_I2C_STATE_CHECK_EXIT_THRESHOLD_VALUE:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if (I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE) == I2C_STATE_IDLE){
//I2c__Read(APDS_I2C_CHANNEL, ApdsBuf, 1);
Read_Exit_threshold_value = I2c__Read(APDS_I2C_CHANNEL, Threshold_Exit_Buf, 1);
I2cMgr__ReleaseBus(ApdsHandle);
ApdsI2cState = APDS_I2C_GMODE_ENABLE;
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
}
}
break;
//--------------------------------------------------------------------------------------------------//
//GMODE_ENABLE_LINK
//setting value for GMODE REGISTER and reading it
case APDS_I2C_GMODE_ENABLE:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
Write_stat_gmode = I2c__Write(APDS_I2C_CHANNEL, APDS9960_GCONF4, GMODE_VAL, 1);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_2;
}
break;
case APDS_I2C_STATE_WAIT_END_WRITE_2:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_GMODE_ENABLE_CHECK;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
case APDS_I2C_GMODE_ENABLE_CHECK:
Write_stat_gmode_1 = I2c__Write(APDS_I2C_CHANNEL, APDS9960_GCONF4, ApdsBuf, 0);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_3;
break;
case APDS_I2C_STATE_WAIT_END_WRITE_3:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_STATE_REQUEST_GMODE;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
case APDS_I2C_STATE_REQUEST_GMODE:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
I2c__RequestRead(APDS_I2C_CHANNEL, 1);
ApdsI2cState = APDS_I2C_STATE_CHECK_READ_GMODE;
}
break;
case APDS_I2C_STATE_CHECK_READ_GMODE:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if (I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE) == I2C_STATE_IDLE){
//I2c__Read(APDS_I2C_CHANNEL, ApdsBuf, 1);
I2c__Read(APDS_I2C_CHANNEL, GmodeBuf, 1);
I2cMgr__ReleaseBus(ApdsHandle);
ApdsI2cState = APDS_I2C_STATE_GSTAT;
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
}
}
break;
//-------------------------------------------------------------------------------------------------//
//Reading the value from GSTATUS register
case APDS_I2C_STATE_GSTAT:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
Write_stat_gstat = I2c__Write(APDS_I2C_CHANNEL, APDS9960_GSTATUS, ApdsBuf, 0);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_4;
}
break;
case APDS_I2C_STATE_WAIT_END_WRITE_4:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_STATE_REQUEST_GSTAT;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
case APDS_I2C_STATE_REQUEST_GSTAT:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
I2c__RequestRead(APDS_I2C_CHANNEL, 1);
ApdsI2cState = APDS_I2C_STATE_CHECK_READ_GSTAT;
}
break;
case APDS_I2C_STATE_CHECK_READ_GSTAT:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if (I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE) == I2C_STATE_IDLE){
//I2c__Read(APDS_I2C_CHANNEL, ApdsBuf, 1);
stat_read = I2c__Read(APDS_I2C_CHANNEL, GstatBuf, 1);
I2cMgr__ReleaseBus(ApdsHandle);
val = GstatBuf[0];
val &= APDS9960_GVALID_1;
if(val == 1){
check_gstat = TRUE;
}
ApdsI2cState = APDS_I2C_STATE_READ_GESTURE;
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
}
}
break;
case APDS_I2C_STATE_READ_GESTURE:
/* Make sure that power and gesture is on and data is valid */
if( check_gstat == TRUE && (ApdsBuf_1[0] & APDS9960_VALID_CHECK) ) {
ApdsDir = DIR_NONE;
}
if(check_gstat == TRUE){
check_gvalid = TRUE;
}
if(stat_read == TRUE){
data_valid = TRUE;
}else{
data_valid = FALSE;
}
// If we have valid data, read in FIFO
if( (GstatBuf[0] & APDS9960_GVALID_1) == APDS9960_GVALID_1 ){
// Read the current FIFO level (i.e., read the contents of APDS9960_GFLVL)
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
Write_stat_gstat = I2c__Write(APDS_I2C_CHANNEL, APDS9960_GFLVL, ApdsBuf, 0);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_5;
}
}
// If we have valid data, read in FIFO
// ApdsI2cState = APDS_I2C_STATE_IDLE;
/* Keep looping as long as gesture data is valid */
/*while(1){
// Wait some time to collect next batch of FIFO data
Micro__DelayNumNops(1000);
if(check_gstat == TRUE){
check_gvalid = TRUE;
}
// Get the contents of the STATUS register. Is data still valid?
if(stat_read == TRUE){
data_valid = TRUE;
}else{
data_valid = FALSE;
}
}*/
break;
// If we have valid data, read in FIFO
//CURRENT UPDATE...WAS TRYING TO CREATE THE FUNCTION WITH PARAMETERS
// check_gvalid_fn = CheckValidData();
/* if( (GstatBuf[0] & APDS9960_GVALID_1) == APDS9960_GVALID_1 ){
// Read the current FIFO level (i.e., read the contents of APDS9960_GFLVL)
if(I2cMgr__RequestBus(ApdsHandle) == TRUE)
{
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
Write_stat_gstat = I2c__Write(APDS_I2C_CHANNEL, APDS9960_GFLVL, ApdsBuf, 0);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_5;
}
}
}
break;*/
case APDS_I2C_STATE_WAIT_END_WRITE_5:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_STATE_REQUEST_GFLVL;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
//-------------------------------------------------------------------------------------------------//
case APDS_I2C_STATE_REQUEST_GFLVL:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
I2c__RequestRead(APDS_I2C_CHANNEL, 1);
ApdsI2cState = APDS_I2C_STATE_CHECK_READ_GFLVL;
}
break;
case APDS_I2C_STATE_CHECK_READ_GFLVL:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if (I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE) == I2C_STATE_IDLE){
GFLVL_read = I2c__Read(APDS_I2C_CHANNEL, GFLVLBuf, 1);
I2cMgr__ReleaseBus(ApdsHandle);
if(GFLVLBuf[0] > 0){
//read the contents of APDS9960_GFIFO_U into fifo_data that's declared above
ApdsI2cState = APDS_I2C_STATE_GFIFO_U;
}
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
}
}
break;
/* If there's stuff in the FIFO, read it into our data block */
/* if(GFLVLBuf[0] > 0){
//read the contents of APDS9960_GFIFO_U into fifo_data that's declared above
}*/
case APDS_I2C_STATE_GFIFO_U:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
GFIFO_U_STAT = I2c__Write(APDS_I2C_CHANNEL, APDS9960_GFIFO_U, ApdsBuf, 0);
//Write_stat_1 = I2c__Write(APDS_I2C_CHANNEL, ENABLE_REGISTER, ApdsBuf, 0);
ApdsI2cState = APDS_I2C_STATE_WAIT_END_WRITE_6;
}
break;
case APDS_I2C_STATE_WAIT_END_WRITE_6:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE)) == I2C_STATE_IDLE){
ApdsI2cState = APDS_I2C_STATE_REQUEST_GFIFO_U;
I2cMgr__ReleaseBus(ApdsHandle);
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
if (ApdsBusyCounter == 255){
ApdsBusyCounter = 0;
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
}
}
break;
case APDS_I2C_STATE_REQUEST_GFIFO_U:
if(I2cMgr__RequestBus(ApdsHandle) == TRUE){
I2c__Initialize(APDS_I2C_CHANNEL, APDS_I2C_SPEED, I2C_ADDR_7BITS, APDS_I2C_ADDRESS);
I2c__RequestRead(APDS_I2C_CHANNEL, 1);
ApdsI2cState = APDS_I2C_STATE_CHECK_READ_GFIFO_U;
}
break;
case APDS_I2C_STATE_CHECK_READ_GFIFO_U:
if ((I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_ERROR)) == I2C_ERROR_ACK){
ApdsI2cState = APDS_I2C_STATE_INIT;
I2cMgr__ReleaseBus(ApdsHandle);
}
else{
if (I2c__GetStatus(APDS_I2C_CHANNEL, I2C_STATUS_STATE) == I2C_STATE_IDLE){
GFIFO_U_read = I2c__Read(APDS_I2C_CHANNEL, fifo_data, 128);
I2cMgr__ReleaseBus(ApdsHandle);
ApdsI2cState = APDS_I2C_STATE_IDLE;
ApdsBusyCounter = 0;
}
else{
ApdsBusyCounter++;
}
}
break;
//-------------------------------------------------------------------------------------------------//
case APDS_I2C_STATE_IDLE:
break;
//-------------------------------------------------------------------------------------------------//
default: break;
}
}