Subversion Repositories Code-Repo

Rev

Rev 192 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
152 Kevin 1
#include <xc.h>
2
#include <delays.h>
3
#include <stdio.h>
155 Kevin 4
#include <string.h>
152 Kevin 5
#include "defines.h"
158 Kevin 6
#include "base_INTERRUPTS.h"
7
#include "base_TIMERS.h"
8
#include "base_UART.h"
9
#include "base_I2C.h"
10
#include "base_SPI.h"
11
#include "base_ADC.h"
12
#include "sensor_nfc_PN532.h"
13
#include "sensor_lux_TSL2561.h"
14
#include "sensor_temp_BMP085.h"
160 Kevin 15
#include "sensor_gyro_L3G.h"
16
#include "sensor_accel_LSM303.h"
17
#include "sensor_rtc_DS3231.h"
158 Kevin 18
#include "display_led_HT16K33.h"
19
#include "display_oled_ssd1306.h"
20
#include "display_oled_ssd1331.h"
21
#include "display_oled_NHD-0216KZW-AB5.h"
22
#include "comm_xbee.h"
152 Kevin 23
 
24
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
25
/* --------------------------- Configuration Bits --------------------------- */
26
/* CONFIG1L @ 0x1FFF8 */
27
#pragma config CFGPLLEN = ON        // Enable PLL on startup
28
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
29
#pragma config WDTEN = OFF          // Turn off watchdog timer
30
#pragma config STVREN = OFF         // Stack overflow/underflow reset disabled
31
#pragma config XINST = OFF          // Turn off extended instruction set
32
 
33
/* CONFIG1H @ 0x1FFF9 */
34
#pragma config CP0 = OFF            // Program memory is not code-protected
35
 
36
/* CONFIG2L @ 0x1FFFA */
37
#pragma config CLKOEC = OFF         // CLKO output disabled on RA6 pin
38
#pragma config SOSCSEL = LOW        // Low Power T1OSC/SOSC circuit selected
39
#pragma config IESO = ON            // Internal external oscillator switch over disabled
40
#pragma config OSC = HSPLL          // Use external oscillator (101)
41
#pragma config FCMEN = OFF          // Fail-safe clock monitor disabled
42
 
43
/* CONFIG2H @ 0x1FFFB */
44
#pragma config WDTPS = 1            // Watchdog postscaler of 1:1
45
 
46
/* CONFIG3L @ 0x1FFFC */
47
#pragma config RTCOSC = T1OSCREF    // RTCC uses T1OSC/T1CKI
48
#pragma config DSBOREN = ON         // Deep sleep BOR enabled
49
#pragma config DSWDTPS = M2         // Deep sleep watchdog postscaler of 1:2 (36m)
50
#pragma config DSWDTEN = OFF        // Deep sleep watchdog timer disabled
51
#pragma config DSWDTOSC = INTOSCREF  // DSWDT clock select uses INTRC
52
 
53
/* CONFIG3H @ 0x1FFFD */
54
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
55
#pragma config ADCSEL = BIT12       // 12-bit ADC
56
#pragma config MSSP7B_EN = MSK7     // 7-bit address masking mode
57
#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed
58
 
59
/* CONFIG4L @ 0x1FFFE */
60
#pragma config WPCFG = ON           // Configuration words page protected
61
 
62
/* CONFIG4H @ 0x1FFFF */
63
#pragma config WPEND = PAGE_WPFP    // Pages WPFP<6:0> through Configuration Words erase/write protected
64
#pragma config WPDIS = OFF          // WPFP<6:0>/WPEND region ignored
65
/* -------------------------------------------------------------------------- */
66
// </editor-fold>
67
 
159 Kevin 68
#if defined(_TEST_UART)
158 Kevin 69
// <editor-fold defaultstate="collapsed" desc="_TEST_UART">
152 Kevin 70
int main() {
71
    char buffer[100];
158 Kevin 72
 
155 Kevin 73
    // Set all ports as digial I/O
74
    ANCON0 = 0xFF;
75
    ANCON1 = 0x1F;
76
 
77
    UART_DATA uart_data;
78
    UART1_Init(&uart_data); // Initialize the UART handler code
79
 
80
    Interrupt_Init(); // Initialize the interrupt priorities
81
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
158 Kevin 82
 
155 Kevin 83
    char output[] = "\r\nBegin Program\r\n";
84
    DBG_PRINT_MAIN(output, strlen(output));
85
 
86
    while (1) {
87
        char length = UART1_Read_Buffer((char *) buffer);
88
        if (length != 0) {
89
            UART1_WriteS(buffer, length);
90
        }
91
 
92
        Delay10KTCYx(255);
93
        Delay10KTCYx(255);
94
    }
95
}
158 Kevin 96
// </editor-fold>
155 Kevin 97
#elif defined(_TEST_I2C_MASTER)
158 Kevin 98
// <editor-fold defaultstate="collapsed" desc="_TEST_I2C_MASTER">
155 Kevin 99
void main(void) {
100
    char length = 0;
101
    char result = 0;
102
    char buffer[100];
103
    char output[64];
104
 
105
    // Set all ports as digial I/O
106
    ANCON0 = 0xFF;
107
    ANCON1 = 0x1F;
108
 
109
    UART_DATA uart_data;
110
    UART1_Init(&uart_data); // Initialize the UART handler code
111
    I2C_DATA i2c_data;
112
    I2C_Init(&i2c_data); // Initialize the I2C handler code
113
 
114
    I2C_Configure_Master(I2C_100KHZ);
115
 
116
    Interrupt_Init(); // Initialize the interrupt priorities
117
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
118
 
119
    sprintf(output, "\r\nBegin Program\r\n");
120
    DBG_PRINT_MAIN(output, strlen(output));
121
 
122
    while (1) {
123
        buffer[0] = 0x8;
124
 
125
        I2C_Master_Send(0x24, 1, buffer);
126
        do {
127
            result = I2C_Get_Status();
128
        } while (!result);
129
        sprintf(output, "S: %X ", result);
130
        DBG_PRINT_MAIN(output, strlen(output));
131
 
132
        I2C_Master_Recv(0x24, 2);
133
        do {
134
            result = I2C_Get_Status();
135
        } while (!result);
136
        sprintf(output, "S: %X ", result);
137
        DBG_PRINT_MAIN(output, strlen(output));
138
        length = I2C_Read_Buffer(buffer);
139
        sprintf(output, "L: %d D: ", length);
140
        DBG_PRINT_MAIN(output, strlen(output));
141
        for (char i = 0; i < length; i++) {
142
            sprintf(output, "%c ", buffer[i]);
143
            DBG_PRINT_MAIN(output, strlen(output));
144
        }
145
        sprintf(output, "\r\n");
146
        DBG_PRINT_MAIN(output, strlen(output));
147
 
148
        I2C_Master_Restart(0x30, 0xBB, 2);
149
        result = I2C_Get_Status();
150
        while (!result) {
151
            result = I2C_Get_Status();
152
        }
153
        sprintf(output, "S: %X ", result);
154
        DBG_PRINT_MAIN(output, strlen(output));
155
        length = I2C_Read_Buffer(buffer);
156
        sprintf(output, "L: %d D: ", length);
157
        DBG_PRINT_MAIN(output, strlen(output));
158
        for (char i = 0; i < length; i++) {
159
            sprintf(output, "%c ", buffer[i]);
160
            DBG_PRINT_MAIN(output, strlen(output));
161
        }
162
        sprintf(output, "\r\n");
163
        DBG_PRINT_MAIN(output, strlen(output));
164
 
165
        Delay10KTCYx(255);
166
        Delay10KTCYx(255);
167
    }
168
}
158 Kevin 169
// </editor-fold>
155 Kevin 170
#elif defined(_TEST_I2C_SLAVE)
158 Kevin 171
// <editor-fold defaultstate="collapsed" desc="_TEST_I2C_SLAVE">
155 Kevin 172
void main(void) {
173
    char length = 0;
174
    char result = 0;
175
    char buffer[100];
176
    char output[64];
177
 
178
    // Set all ports as digial I/O
179
    ANCON0 = 0xFF;
180
    ANCON1 = 0x1F;
181
 
182
    UART_DATA uart_data;
183
    UART1_Init(&uart_data); // Initialize the UART handler code
184
    I2C_DATA i2c_data;
185
    I2C_Init(&i2c_data); // Initialize the I2C handler code
186
 
187
    I2C_Configure_Slave(0x24);
188
 
189
    Interrupt_Init(); // Initialize the interrupt priorities
190
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
191
 
192
    sprintf(output, "\r\nBegin Program\r\n");
193
    DBG_PRINT_MAIN(output, strlen(output));
194
 
195
    while (1) {
196
 
197
        result = I2C_Get_Status();
198
        while (!result) {
199
            result = I2C_Get_Status();
200
        }
201
        sprintf(output, "S: %X ", result);
202
        DBG_PRINT_MAIN(output, strlen(output));
203
        length = I2C_Read_Buffer(buffer);
204
        sprintf(output, "L: %d D: ", length);
205
        DBG_PRINT_MAIN(output, strlen(output));
206
        for (char i = 0; i < length; i++) {
207
            sprintf(output, "%X ", buffer[i]);
208
            DBG_PRINT_MAIN(output, strlen(output));
209
        }
210
        sprintf(output, "\r\n");
211
        DBG_PRINT_MAIN(output, strlen(output));
212
 
213
        Delay10KTCYx(255);
214
        Delay10KTCYx(255);
215
    }
216
}
158 Kevin 217
// </editor-fold>
155 Kevin 218
#elif defined(_TEST_SPI)
158 Kevin 219
// <editor-fold defaultstate="collapsed" desc="_TEST_SPI">
155 Kevin 220
void main(void) {
221
    char length = 0;
222
    char result = 0;
223
    char buffer[100];
224
    char output[64];
225
    char test[8] = "ASDF123";
226
 
227
    // Set all ports as digial I/O
228
    ANCON0 = 0xFF;
229
    ANCON1 = 0x1F;
230
 
231
    UART_DATA uart_data;
232
    UART1_Init(&uart_data); // Initialize the UART handler code
233
    SPI_DATA spi_data;
234
    SPI2_Init(&spi_data, SPI2_FOSC_8); // Initialize the SPI module
235
 
236
    Interrupt_Init(); // Initialize the interrupt priorities
237
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
238
 
239
    sprintf(output, "\r\nBegin Program\r\n");
240
    DBG_PRINT_MAIN(output, strlen(output));
241
 
242
    while (1) {
243
 
244
        SPI2_Write(test, 7);
245
        while (result != 7) {
246
            length = SPI2_Read_Buffer(buffer);
247
            if (length) {
248
                result += length;
249
            }
250
        }
251
        result = 0;
252
 
253
        for (char i = 0; i < result; i++) {
254
            sprintf(output, "%X ", buffer[i]);
255
            DBG_PRINT_MAIN(output, strlen(output));
256
        }
257
        sprintf(output, "\r\n");
258
        DBG_PRINT_MAIN(output, strlen(output));
259
 
260
        Delay10KTCYx(255);
261
        Delay10KTCYx(255);
262
    }
263
}
158 Kevin 264
// </editor-fold>
192 Kevin 265
#elif defined(_TEST_SPI_DMA)
266
// <editor-fold defaultstate="collapsed" desc="_TEST_SPI_DMA">
267
void main(void) {
268
    char buffer[42];
269
    char buffer2;
270
 
271
    // Set all ports as digial I/O
272
    ANCON0 = 0xFF;
273
    ANCON1 = 0x1F;
274
 
275
    SPI_DATA spi_data;
276
    SPI2_Init(&spi_data, SPI2_FOSC_4); // Initialize the SPI module
277
    SPI2_DMA_Init();
278
 
279
//    Interrupt_Init(); // Initialize the interrupt priorities
280
//    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
281
 
282
//    for (char i = 0; i < 42; i++) {
283
//        buffer[i] = i;
284
//    }
285
 
286
    while (1) {
287
        SPI2_DMA_Start(42, &buffer, &buffer2);
288
 
289
        __delay_ms(1);
290
    }
291
}
292
// </editor-fold>
159 Kevin 293
#elif defined(_TEST_ADC)
294
// <editor-fold defaultstate="collapsed" desc="_TEST_ADC">
295
void main(void) {
296
    unsigned int x, y, z;
297
    char buffer[60];
298
 
299
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
300
    ANCON0 = 0xF8;
301
    ANCON1 = 0x1F;
302
 
303
    UART_DATA uart_data;
304
    UART1_Init(&uart_data); // Initialize the UART handler code
305
    SPI_DATA spi_data;
306
    SPI2_Init(&spi_data, SPI2_FOSC_8); // Initialize the SPI module
307
    SSD1306_DATA ssd1306_data;
308
    SSD1306_Init(&ssd1306_data); // Initialize the SSD1331 OLED display (uses SPI2)
309
    ADC_DATA adc_data;
310
    ADC_Init(&adc_data, ADC_TAD_20, ADC_FOSC_64_);
311
 
312
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
313
 
314
    Interrupt_Init(); // Initialize the interrupt priorities
315
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
316
 
317
    sprintf(buffer, "\r\nBegin Program\r\n");
318
    SSD1306_Write_String(buffer, strlen(buffer));
319
 
320
    memset(buffer, 0, 60);
321
    SSD1306_Clear_Display();
322
    SSD1306_Display();
323
 
324
    while (1) {
325
        // ADC read from AN0-AN2 and prints to display
326
        ADC_Start(ADC_CHANNEL_AN2);
327
//        SSD1306_Fill_Rect(0, 0, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
328
        SSD1306_Set_Cursor(0, 0);
329
        while (!ADC_Get_Result(&x));
330
        sprintf(buffer, "X: %u", x);
331
        SSD1306_Write_String(buffer, strlen(buffer));
332
        SSD1306_Display();
333
 
334
        ADC_Start(ADC_CHANNEL_AN1);
335
//        SSD1306_Fill_Rect(0, 8, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
336
        SSD1306_Set_Cursor(0, 8);
337
        while (!ADC_Get_Result(&y));
338
        sprintf(buffer, "Y: %u", y);
339
        SSD1306_Write_String(buffer, strlen(buffer));
340
        SSD1306_Display();
341
 
342
        ADC_Start(ADC_CHANNEL_AN0);
343
//        SSD1306_Fill_Rect(0, 16, SSD1306_LCDWIDTH, 8, SSD1331_BLACK);
344
        SSD1306_Set_Cursor(0, 16);
345
        while (!ADC_Get_Result(&z));
346
        sprintf(buffer, "Z: %u", z);
347
        SSD1306_Write_String(buffer, strlen(buffer));
348
        SSD1306_Display();
349
    }
350
}
351
// </editor-fold>
352
#elif defined(_TEST_TIMER1_RTC)
353
// <editor-fold defaultstate="collapsed" desc="_TEST_TIMER1_RTC">
354
void main(void) {
355
 
356
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
357
    ANCON0 = 0xF8;
358
    ANCON1 = 0x1F;
359
 
360
    Timer1_Init();
361
 
362
    Interrupt_Init(); // Initialize the interrupt priorities
363
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
364
 
365
    LED_BLUE_TRIS = 0;
366
    LED_RED_TRIS = 0;
367
 
368
    Timer1_Enable();
369
 
370
    while (1) {
371
 
372
    }
373
}
374
// </editor-fold>
155 Kevin 375
#elif defined(_TEST_NFC)
158 Kevin 376
// <editor-fold defaultstate="collapsed" desc="_TEST_NFC">
155 Kevin 377
void main(void) {
378
    char length = 0;
379
    char output[64];
380
 
381
    // NFC stuff
382
    NFC_FIRMWARE_VERSION version;
383
    NFC_TargetDataMiFare cardData[2];
384
    NFC_TargetDataMiFare cardData_prev[2];
385
 
386
    // Set all ports as digial I/O
387
    ANCON0 = 0xFF;
388
    ANCON1 = 0x1F;
389
 
390
    UART_DATA uart_data;
391
    UART1_Init(&uart_data); // Initialize the UART handler code
392
    I2C_DATA i2c_data;
393
    I2C_Init(&i2c_data); // Initialize the I2C handler code
394
    NFC_DATA nfc_data;
395
    NFC_Init(&nfc_data); // Initialize the NFC chip (uses I2C)
396
 
397
    I2C_Configure_Master(I2C_400KHZ);
398
 
399
    Interrupt_Init(); // Initialize the interrupt priorities
400
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
401
 
402
    sprintf(output, "\r\nBegin Program\r\n");
403
    DBG_PRINT_MAIN(output, strlen(output));
404
 
405
    version = NFC_Get_Firmware_Version();
406
    while (!version.IC) {
407
        sprintf(output, "Waiting for NFC board..\r\n");
408
        DBG_PRINT_MAIN(output, strlen(output));
409
        Delay10KTCYx(3);
410
        version = NFC_Get_Firmware_Version();
411
    }
412
    sprintf(output, "Found chip PN5%X\r\n", version.IC);
413
    DBG_PRINT_MAIN(output, strlen(output));
414
    sprintf(output, "Firmware ver. %d.%d\r\n", version.Ver, version.Rev);
415
    DBG_PRINT_MAIN(output, strlen(output));
416
    NFC_SAMConfig();
417
 
418
    memset(cardData, 0, 24);
419
 
420
    while (1) {
421
 
422
        //        // This query will hang until the NFC chip replies (card detected)
423
        //        length = NFC_readPassiveTargetID(cardData);
424
        //        if (length) {
425
        //            DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
426
        //            DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
427
        //            DBG_PRINT_MAIN("UID: ");
428
        //            for (i = 0; i < cardData[0].NFCID_LEN; i++) {
429
        //                DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
430
        //            }
431
        //            DBG_PRINT_MAIN("\r\n");
432
        //            if (length == 2) {
433
        //                DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
434
        //                DBG_PRINT_MAIN("UID: ");
435
        //                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
436
        //                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
437
        //                }
438
        //                DBG_PRINT_MAIN("\r\n");
439
        //            }
440
        //        }
441
 
442
        //        // This query will hang until the NFC chip replies (card detected)
443
        //        length = NFC_readPassiveTargetID(cardData);
444
        //        if (length) {
445
        //            DBG_PRINT_MAIN("Cards Found: %u\r\n", length);
446
        //            DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[0].NFCID_LEN);
447
        //            DBG_PRINT_MAIN("UID: ");
448
        //            for (i = 0; i < cardData[0].NFCID_LEN; i++) {
449
        //                DBG_PRINT_MAIN("%02X ", cardData[0].NFCID[i]);
450
        //            }
451
        //            DBG_PRINT_MAIN("\r\n");
452
        //            if (length == 2) {
453
        //                DBG_PRINT_MAIN("UID Length: %d bytes\r\n", cardData[1].NFCID_LEN);
454
        //                DBG_PRINT_MAIN("UID: ");
455
        //                for (i = 0; i < cardData[1].NFCID_LEN; i++) {
456
        //                    DBG_PRINT_MAIN("%02X ", cardData[1].NFCID[i]);
457
        //                }
458
        //                DBG_PRINT_MAIN("\r\n");
459
        //            }
460
        //        }
461
 
462
        // This query will not wait for a detection before responding
463
        length = NFC_Poll_Targets(1, 1, cardData);
464
        if (!length) {
465
            memset(cardData_prev, 0, 24);
466
        } else if (length == 1) {
467
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
468
                // Do nothing
469
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
470
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
471
            } else {
472
                sprintf(output, "UID: ");
473
                DBG_PRINT_MAIN(output, strlen(output));
474
                for (char i = 0; i < cardData[0].NFCID_LEN; i++) {
475
                    sprintf(output, "%02X ", cardData[0].NFCID[i]);
476
                    DBG_PRINT_MAIN(output, strlen(output));
477
                }
478
                sprintf(output, "\r\n");
479
                DBG_PRINT_MAIN(output, strlen(output));
480
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
481
            }
482
            memset(&cardData_prev[1], 0, 12);
483
        } else if (length == 2) {
484
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
485
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
486
                // Do nothing
487
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
488
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
489
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
490
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
491
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
492
                // First card matched
493
                sprintf(output, "UID2: ");
494
                DBG_PRINT_MAIN(output, strlen(output));
495
                for (char i = 0; i < cardData[1].NFCID_LEN; i++) {
496
                    sprintf(output, "%02X ", cardData[1].NFCID[i]);
497
                    DBG_PRINT_MAIN(output, strlen(output));
498
                }
499
                sprintf(output, "\r\n");
500
                DBG_PRINT_MAIN(output, strlen(output));
501
                memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
502
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
503
                // Second card matched
504
                sprintf(output, "UID1: ");
505
                DBG_PRINT_MAIN(output, strlen(output));
506
                for (char i = 0; i < cardData[0].NFCID_LEN; i++) {
507
                    sprintf(output, "%02X ", cardData[0].NFCID[i]);
508
                    DBG_PRINT_MAIN(output, strlen(output));
509
                }
510
                sprintf(output, "\r\n");
511
                DBG_PRINT_MAIN(output, strlen(output));
512
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
513
            } else {
514
                // No match
515
                sprintf(output, "UID1: ");
516
                DBG_PRINT_MAIN(output, strlen(output));
517
                for (char i = 0; i < cardData[0].NFCID_LEN; i++) {
518
                    sprintf(output, "%02X ", cardData[0].NFCID[i]);
519
                    DBG_PRINT_MAIN(output, strlen(output));
520
                }
521
                sprintf(output, "\r\n");
522
                DBG_PRINT_MAIN(output, strlen(output));
523
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
524
                sprintf(output, "UID2: ");
525
                DBG_PRINT_MAIN(output, strlen(output));
526
                for (char i = 0; i < cardData[1].NFCID_LEN; i++) {
527
                    sprintf(output, "%02X ", cardData[1].NFCID[i]);
528
                    DBG_PRINT_MAIN(output, strlen(output));
529
                }
530
                sprintf(output, "\r\n");
531
                DBG_PRINT_MAIN(output, strlen(output));
532
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
533
            }
534
        }
535
    }
536
}
158 Kevin 537
// </editor-fold>
159 Kevin 538
#elif defined(_TEST_LUX)
539
// <editor-fold defaultstate="collapsed" desc="_TEST_LUX">
540
void main(void) {
541
    char output[64];
542
 
543
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
544
    ANCON0 = 0xF8;
545
    ANCON1 = 0x1F;
546
 
547
    UART_DATA uart_data;
548
    UART1_Init(&uart_data);
549
    I2C_DATA i2c_data;
550
    I2C_Init(&i2c_data);
551
    TSL2561_DATA lux_data;
552
    LUX_Init(&lux_data, TSL2561_ADDR_FLOAT);
553
 
554
    I2C_Configure_Master(I2C_100KHZ);
555
 
556
    Interrupt_Init(); // Initialize the interrupt priorities
557
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
558
 
559
    LUX_Begin();
560
 
561
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
562
//    LUX_Set_Gain(TSL2561_GAIN_0X);   // set no gain (for bright situtations)
563
    LUX_Set_Gain(TSL2561_GAIN_16X);  // set 16x gain (for dim situations)
564
 
565
    // Changing the integration time gives you a longer time over which to sense light
566
    // longer timelines are slower, but are good in very low light situtations!
567
//    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_13MS);  // shortest integration time (bright light)
568
    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium light)
569
//    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim light)
570
 
571
    sprintf(output, "\r\nBegin Program\r\n");
572
    DBG_PRINT_MAIN(output, strlen(output));
573
 
574
    while (1) {
575
        unsigned long lum = LUX_Get_Full_Luminosity();
576
        unsigned int ir = lum >> 16;
577
        unsigned int full = lum & 0xFFFF;
578
        sprintf(output, "IR: %d\r\n", ir);
579
        DBG_PRINT_MAIN(output, strlen(output));
580
        sprintf(output, "Visible: %d\r\n", full - ir);
581
        DBG_PRINT_MAIN(output, strlen(output));
582
        sprintf(output, "Full: %d\r\n", full);
583
        DBG_PRINT_MAIN(output, strlen(output));
584
        sprintf(output, "Lux: %ld\r\n\r\n", LUX_Calculate_Lux(full, ir));
585
        DBG_PRINT_MAIN(output, strlen(output));
586
 
587
        Delay10KTCYx(255);
588
        Delay10KTCYx(255);
589
        Delay10KTCYx(255);
590
        Delay10KTCYx(255);
591
    }
592
}
593
// </editor-fold>
594
#elif defined(_TEST_BMP)
595
// <editor-fold defaultstate="collapsed" desc="_TEST_BMP">
596
void main(void) {
597
    char output[64];
598
 
599
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
600
    ANCON0 = 0xF8;
601
    ANCON1 = 0x1F;
602
 
603
    UART_DATA uart_data;
604
    UART1_Init(&uart_data);
605
    I2C_DATA i2c_data;
606
    I2C_Init(&i2c_data);
607
    BMP085_DATA bmp_data;
608
    BMP_Init(&bmp_data);
609
 
610
    I2C_Configure_Master(I2C_400KHZ);
611
 
612
    Interrupt_Init(); // Initialize the interrupt priorities
613
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
614
 
615
    BMP_Begin(BMP085_ULTRAHIGHRES);
616
 
617
    BMP_Read_Temperature();
618
    BMP_Read_Pressure();
619
    BMP_Read_Altitude(101592);
620
 
621
    while (1) {
622
        sprintf(output, "Temp: %f *C\r\n", BMP_Read_Temperature());
623
        DBG_PRINT_MAIN(output, strlen(output));
624
        sprintf(output, "Pressure: %ld Pa\r\n", BMP_Read_Pressure());
625
        DBG_PRINT_MAIN(output, strlen(output));
626
        sprintf(output, "Altitude: %f meters\r\n", BMP_Read_Altitude(101592));
627
        DBG_PRINT_MAIN(output, strlen(output));
628
 
629
        Delay10KTCYx(255);
630
        Delay10KTCYx(255);
631
        Delay10KTCYx(255);
632
        Delay10KTCYx(255);
633
    }
634
}
635
// </editor-fold>
636
#elif defined(_TEST_GYRO)
637
// <editor-fold defaultstate="collapsed" desc="_TEST_GYRO">
638
void main(void) {
639
    char output[64];
640
 
641
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
642
    ANCON0 = 0xF8;
643
    ANCON1 = 0x1F;
644
 
645
    UART_DATA uart_data;
646
    UART1_Init(&uart_data);
647
    I2C_DATA i2c_data;
648
    I2C_Init(&i2c_data);
649
    L3G_DATA gyro_data;
650
    L3G_Init(&gyro_data, L3GD20_DEVICE, L3G_SA0_HIGH);
651
 
652
    I2C_Configure_Master(I2C_100KHZ);
653
 
654
    Interrupt_Init(); // Initialize the interrupt priorities
655
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
656
 
657
    sprintf(output, "\r\nBegin Program\r\n");
658
    DBG_PRINT_MAIN(output, strlen(output));
659
 
660
    L3G_Begin();
661
    int x,y,z;
662
    while (1) {
663
        L3G_Read(&x, &y, &z);
664
        sprintf(output, "X: %d  Y: %d  Z: %d\r\n", x, y, z);
665
        DBG_PRINT_MAIN(output, strlen(output));
666
 
667
        Delay10KTCYx(100);
668
    }
669
}
670
// </editor-fold>
671
#elif defined(_TEST_ACCEL)
672
// <editor-fold defaultstate="collapsed" desc="_TEST_ACCEL">
673
void main(void) {
674
    char output[64];
675
 
676
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
677
    ANCON0 = 0xF8;
678
    ANCON1 = 0x1F;
679
 
680
    UART_DATA uart_data;
681
    UART1_Init(&uart_data);
682
    I2C_DATA i2c_data;
683
    I2C_Init(&i2c_data);
684
    LSM303_DATA acc_data;
685
    LSM303_Init(&acc_data, LSM303DLHC_DEVICE, ACC_ADDRESS_SA0_A_LOW);
686
 
687
    I2C_Configure_Master(I2C_100KHZ);
688
 
689
    Interrupt_Init(); // Initialize the interrupt priorities
690
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
691
 
692
    sprintf(output, "\r\nBegin Program\r\n");
693
    DBG_PRINT_MAIN(output, strlen(output));
694
 
695
    LSM303_Begin();
696
    int a_x, a_y, a_z, m_x, m_y, m_z;
697
    while (1) {
698
        LSM303_Read_Acc(&a_x, &a_y, &a_z);
699
        LSM303_Read_Mag(&m_x, &m_y, &m_z);
700
        sprintf(output, "A - X: %d  Y: %d  Z: %d\r\n", a_x, a_y, a_z);
701
        DBG_PRINT_MAIN(output, strlen(output));
702
        sprintf(output, "M - X: %d  Y: %d  Z: %d\r\n", m_x, m_y, m_z);
703
        DBG_PRINT_MAIN(output, strlen(output));
704
 
705
        Delay10KTCYx(100);
706
    }
707
}
708
// </editor-fold>
160 Kevin 709
#elif defined(_TEST_RTC)
710
// <editor-fold defaultstate="collapsed" desc="_TEST_RTC">
711
void main(void) {
712
    char output[64];
713
 
714
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
715
    ANCON0 = 0xF8;
716
    ANCON1 = 0x1F;
717
 
718
    UART_DATA uart_data;
719
    UART1_Init(&uart_data);
720
    I2C_DATA i2c_data;
721
    I2C_Init(&i2c_data);
722
 
723
    I2C_Configure_Master(I2C_100KHZ);
724
 
725
    Interrupt_Init(); // Initialize the interrupt priorities
726
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
727
 
728
    sprintf(output, "\r\nBegin Program\r\n");
729
    DBG_PRINT_MAIN(output, strlen(output));
730
 
731
    DS3231_Begin();
732
    // Sec, Min, Hour, DOW, Day, Month, Year, Mil Time, AM/PM
161 Kevin 733
    DS3231_Set_Time(00, 59, 7, 5, 18, 1, 13, 0, 0);
160 Kevin 734
 
735
    char sec, min, hour, day, date, month, year, h_mil, h_am_pm;
736
    while (1) {
737
        DS3231_Get_Time(&sec, &min, &hour, &day, &date, &month, &year, &h_mil, &h_am_pm);
738
        sprintf(output, "%02d:%02d:%02d %s %s - %d/%d/%d (%d)\r\n", hour, min, sec, (h_am_pm) ? "PM" : "AM",
739
                (h_mil) ? "24H" : "12H", month, date, year, day);
740
        DBG_PRINT_MAIN(output, strlen(output));
741
        Delay10KTCYx(255);
742
        Delay10KTCYx(255);
743
        Delay10KTCYx(255);
744
        Delay10KTCYx(255);
745
        Delay10KTCYx(255);
746
        Delay10KTCYx(255);
747
    }
748
}
749
// </editor-fold>
155 Kevin 750
#elif defined(_TEST_LED_BACKPACK)
158 Kevin 751
// <editor-fold defaultstate="collapsed" desc="_TEST_LED_BACKPACK">
155 Kevin 752
void main(void) {
753
    unsigned int counter = 0;
754
    char output[64];
755
 
756
    // Set all ports as digial I/O
757
    ANCON0 = 0xFF;
758
    ANCON1 = 0x1F;
759
 
760
    UART_DATA uart_data;
761
    UART1_Init(&uart_data); // Initialize the UART handler code
762
    I2C_DATA i2c_data;
763
    I2C_Init(&i2c_data); // Initialize the I2C handler code
764
    LED_DATA led_data;
765
    LED_Init(&led_data); // Initialize the LED backpack (uses I2C);
766
 
767
    I2C_Configure_Master(I2C_400KHZ);
768
 
769
    Interrupt_Init(); // Initialize the interrupt priorities
770
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
771
 
772
    sprintf(output, "\r\nBegin Program\r\n");
773
    DBG_PRINT_MAIN(output, strlen(output));
774
 
775
    LED_Start();
776
    LED_Write_Digit_Num(0, 1, 1);
777
    LED_Write_Digit_Num(1, 2, 0);
778
    LED_Write_Digit_Num(2, 3, 0);
779
    LED_Write_Digit_Num(3, 4, 0);
780
    LED_Write_Display();
781
    for (char i = 0; i < 15; i++) {
782
        LED_Set_Brightness(15 - i);
783
        Delay10KTCYx(100);
784
    }
785
    for (char i = 0; i < 15; i++) {
786
        LED_Set_Brightness(i);
787
        Delay10KTCYx(100);
788
    }
789
    LED_Blink_Rate(HT16K33_BLINK_OFF);
790
 
791
    while (1) {
792
        LED_Write_Num(counter);
793
        counter++;
794
        if (counter > 9999)
795
            counter = 0;
796
 
797
        //        Delay10KTCYx(255);
798
    }
799
}
158 Kevin 800
// </editor-fold>
155 Kevin 801
#elif defined(_TEST_SSD1306_OLED)
158 Kevin 802
// <editor-fold defaultstate="collapsed" desc="_TEST_SDS1306_OLED">
155 Kevin 803
void main(void) {
804
    char output[64];
805
 
806
    // Set all ports as digial I/O
807
    ANCON0 = 0xFF;
808
    ANCON1 = 0x1F;
809
 
810
    UART_DATA uart_data;
811
    UART1_Init(&uart_data); // Initialize the UART handler code
812
    SPI_DATA spi_data;
813
    SPI2_Init(&spi_data, SPI2_FOSC_4); // Initialize the SPI module
814
    SSD1306_DATA ssd1306_data;
815
    SSD1306_Init(&ssd1306_data); // Initialize the OLED code
816
 
817
    Interrupt_Init(); // Initialize the interrupt priorities
818
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
819
 
820
    sprintf(output, "\r\nBegin Program\r\n");
821
    DBG_PRINT_MAIN(output, strlen(output));
822
 
823
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
824
 
825
    SSD1306_Display(); // Show splashscreen
826
 
827
    while (1) {
828
        Delay10KTCYx(255);
829
        Delay10KTCYx(255);
830
        SSD1306_Clear_Display();
831
        SSD1306_Test_DrawLine();
832
        SSD1306_Display();
833
 
834
        Delay10KTCYx(255);
835
        Delay10KTCYx(255);
836
        SSD1306_Clear_Display();
837
        SSD1306_Test_DrawRect();
838
        SSD1306_Display();
839
 
840
        Delay10KTCYx(255);
841
        Delay10KTCYx(255);
842
        SSD1306_Clear_Display();
843
        SSD1306_Test_FillRect();
844
        SSD1306_Display();
845
 
846
        Delay10KTCYx(255);
847
        Delay10KTCYx(255);
848
        SSD1306_Clear_Display();
849
        SSD1306_Test_DrawCircle();
850
        SSD1306_Display();
851
 
852
        Delay10KTCYx(255);
853
        Delay10KTCYx(255);
854
        SSD1306_Clear_Display();
855
        SSD1306_Fill_Circle(SSD1306_LCDWIDTH / 2, SSD1306_LCDHEIGHT / 2, 10, SSD1306_WHITE);
856
        SSD1306_Display();
857
 
858
        Delay10KTCYx(255);
859
        Delay10KTCYx(255);
860
        SSD1306_Clear_Display();
861
        SSD1306_Test_DrawRoundRect();
862
        SSD1306_Display();
863
 
864
        Delay10KTCYx(255);
865
        Delay10KTCYx(255);
866
        SSD1306_Clear_Display();
867
        SSD1306_Test_FillRoundRect();
868
        SSD1306_Display();
869
 
870
        Delay10KTCYx(255);
871
        Delay10KTCYx(255);
872
        SSD1306_Clear_Display();
873
        SSD1306_Test_DrawTriangle();
874
        SSD1306_Display();
875
 
876
        Delay10KTCYx(255);
877
        Delay10KTCYx(255);
878
        SSD1306_Clear_Display();
879
        SSD1306_Test_FillTriangle();
880
        SSD1306_Display();
881
 
882
        Delay10KTCYx(255);
883
        Delay10KTCYx(255);
884
        SSD1306_Clear_Display();
885
        SSD1306_Test_DrawChar();
886
        SSD1306_Display();
887
 
888
        Delay10KTCYx(255);
889
        Delay10KTCYx(255);
890
        SSD1306_Clear_Display();
891
        SSD1306_Set_Text_Size(1);
892
        SSD1306_Set_Text_Color(SSD1306_WHITE);
893
        SSD1306_Set_Cursor(0, 0);
894
        sprintf(output, "Hello World!\n");
895
        SSD1306_Write_String(output, strlen(output));
896
        //        SSD1306_Set_Text_Color_BG(BLACK, WHITE);
897
        unsigned int i = 65535;
898
        sprintf(output, "%u %d\n", i, i);
899
        SSD1306_Write_String(output, strlen(output));
900
        //        SSD1306_Set_Text_Size(2);
901
        //        SSD1306_Set_Text_Color(WHITE);
902
        unsigned long l = 0xDEADBEEF;
903
        sprintf(output, "0x%lX", (long) l);
904
        SSD1306_Write_String(output, strlen(output));
905
        SSD1306_Display();
906
 
907
        //        SSD1306_Clear_Display();
908
        //        SSD1306_Set_Rotation(0);
909
        //        SSD1306_Set_Text_Size(1);
910
        //        SSD1306_Set_Text_Color(SSD1306_WHITE);
911
        //        SSD1306_Set_Cursor(0, 0);
912
        //        SSD1306_Write_String("%u", i);
913
        //        i++;
914
        //        SSD1306_Display();
915
 
916
    }
917
}
158 Kevin 918
// </editor-fold>
155 Kevin 919
#elif defined(_TEST_SSD1331_OLED)
158 Kevin 920
// <editor-fold defaultstate="collapsed" desc="_TEST_SSD1331_OLED">
155 Kevin 921
void main(void) {
922
    char output[128];
923
 
924
    // Set all ports as digial I/O
925
    ANCON0 = 0xFF;
926
    ANCON1 = 0x1F;
927
 
928
    UART_DATA uart_data;
929
    UART1_Init(&uart_data); // Initialize the UART handler code
930
    SPI_DATA spi_data;
931
    SPI2_Init(&spi_data, SPI2_FOSC_64); // Initialize the SPI module
932
    SSD1331_DATA ssd1331_data;
933
    SSD1331_Init(&ssd1331_data); // Initialize the OLED code
934
 
935
    Interrupt_Init(); // Initialize the interrupt priorities
936
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
937
 
938
    sprintf(output, "\r\nBegin Program\r\n");
939
    DBG_PRINT_MAIN(output, strlen(output));
940
 
941
    SSD1331_Begin();
942
 
943
    while (1) {
944
 
945
        Delay10KTCYx(255);
946
        Delay10KTCYx(255);
947
        SSD1331_Set_Rotation(0);
948
        SSD1331_Test_Pattern();
949
 
950
        Delay10KTCYx(255);
951
        Delay10KTCYx(255);
952
        SSD1331_Clear_Display();
953
        SSD1331_Set_Rotation(0);
954
        SSD1331_Set_Cursor(0, 0);
955
        sprintf(output, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabit adipiscing ante sed nibh tincidunt feugiat.");
157 Kevin 956
        SSD1331_Write_String(output, strlen(output));
155 Kevin 957
 
958
//        Delay10KTCYx(255);
959
//        Delay10KTCYx(255);
960
//        SSD1331_Clear_Display();
961
//        SSD1331_Set_Rotation(3);
962
//        SSD1331_Set_Cursor(0, 0);
963
//        SSD1331_Write_String("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa");
964
 
965
        Delay10KTCYx(255);
966
        Delay10KTCYx(255);
967
        SSD1331_Set_Rotation(0);
968
        SSD1331_Test_DrawLines(SSD1331_YELLOW);
969
 
970
        Delay10KTCYx(255);
971
        Delay10KTCYx(255);
972
        SSD1331_Set_Rotation(3);
973
        SSD1331_Test_DrawLines(SSD1331_BLUE);
974
 
975
                Delay10KTCYx(255);
976
                Delay10KTCYx(255);
977
                SSD1331_Set_Rotation(0);
978
                SSD1331_Test_DrawRect(SSD1331_GREEN);
979
 
980
                Delay10KTCYx(255);
981
                Delay10KTCYx(255);
982
                SSD1331_Set_Rotation(1);
983
                SSD1331_Test_DrawRect(SSD1331_RED);
984
 
985
                Delay10KTCYx(255);
986
                Delay10KTCYx(255);
987
                SSD1331_Set_Rotation(2);
988
                SSD1331_Test_DrawRect(SSD1331_BLUE);
989
 
990
                Delay10KTCYx(255);
991
                Delay10KTCYx(255);
992
                SSD1331_Set_Rotation(3);
993
                SSD1331_Test_DrawRect(SSD1331_YELLOW);
994
 
995
                Delay10KTCYx(255);
996
                Delay10KTCYx(255);
997
                SSD1331_Set_Rotation(0);
998
                SSD1331_Test_FillRect(SSD1331_YELLOW, SSD1331_MAGENTA);
999
 
1000
                Delay10KTCYx(255);
1001
                Delay10KTCYx(255);
1002
                SSD1331_Set_Rotation(3);
1003
                SSD1331_Test_FillRect(SSD1331_BLUE, SSD1331_GREEN);
1004
 
1005
                Delay10KTCYx(255);
1006
                Delay10KTCYx(255);
1007
                SSD1331_Set_Rotation(0);
1008
                SSD1331_Clear_Display();
1009
                SSD1331_Test_FillCircle(10, SSD1331_BLUE);
1010
                SSD1331_Test_DrawCircle(10, SSD1331_WHITE);
1011
 
1012
                Delay10KTCYx(255);
1013
                Delay10KTCYx(255);
1014
                SSD1331_Set_Rotation(3);
1015
                SSD1331_Clear_Display();
1016
                SSD1331_Test_FillCircle(10, SSD1331_MAGENTA);
1017
                SSD1331_Test_DrawCircle(10, SSD1331_YELLOW);
1018
 
1019
                Delay10KTCYx(255);
1020
                Delay10KTCYx(255);
1021
                SSD1331_Set_Rotation(0);
1022
                SSD1331_Test_DrawTria();
1023
 
1024
                Delay10KTCYx(255);
1025
                Delay10KTCYx(255);
1026
                SSD1331_Set_Rotation(3);
1027
                SSD1331_Test_DrawTria();
1028
 
1029
                Delay10KTCYx(255);
1030
                Delay10KTCYx(255);
1031
                SSD1331_Set_Rotation(0);
1032
                SSD1331_Test_DrawRoundRect();
1033
 
1034
                Delay10KTCYx(255);
1035
                Delay10KTCYx(255);
1036
                SSD1331_Set_Rotation(3);
1037
                SSD1331_Test_DrawRoundRect();
1038
 
1039
        //        SSD1331_Clear_Display();
1040
        //        SSD1331_Set_Rotation(3);
1041
        //        SSD1331_Set_Cursor(0,0);
1042
        //        SSD1331_Set_Text_Color_BG(SSD1331_WHITE, SSD1331_BLACK);
1043
        //        SSD1331_Write_String("%u", i);
1044
        //        i++;
1045
    }
1046
}
158 Kevin 1047
// </editor-fold>
155 Kevin 1048
#elif defined(_TEST_OLED_CHAR)
158 Kevin 1049
// <editor-fold defaultstate="collapsed" desc="_TEST_OLED_CHAR">
155 Kevin 1050
void main(void) {
1051
    char output[64];
1052
 
1053
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
1054
    ANCON0 = 0xFF;
1055
    ANCON1 = 0x1F;
1056
 
1057
//    UART1_Init();
1058
    OLED_CHAR_DATA oled_data;
1059
    NHD_Init(&oled_data);
1060
 
1061
    Interrupt_Init(); // Initialize the interrupt priorities
1062
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
1063
 
1064
    NHD_Begin(16, 2);
1065
 
1066
    sprintf(output, "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do");
1067
    NHD_Write_String(output, strlen(output));
1068
    NHD_Set_Cursor(0,1);
1069
    sprintf(output, "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e");
1070
    NHD_Write_String(output, strlen(output));
1071
 
1072
    while (1) {
1073
        Delay10KTCYx(150);
1074
        NHD_Scroll_Display_Left();
1075
    }
1076
}
158 Kevin 1077
// </editor-fold>
155 Kevin 1078
#elif defined(_TEST_NFC_TO_SSD1306_OLED)
158 Kevin 1079
// <editor-fold defaultstate="collapsed" desc="_TEST_NFC_TO_SSD1306_OLED">
155 Kevin 1080
void main(void) {
1081
    char output[64];
1082
 
1083
    // NFC stuff
1084
    NFC_FIRMWARE_VERSION version;
1085
    NFC_TargetDataMiFare cardData[2];
1086
    NFC_TargetDataMiFare cardData_prev[2];
1087
 
1088
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
1089
    ANCON0 = 0xF8;
1090
    ANCON1 = 0x1F;
1091
 
1092
    UART_DATA uart_data;
1093
    UART1_Init(&uart_data);
1094
    I2C_DATA i2c_data;
1095
    I2C_Init(&i2c_data);
1096
    NFC_DATA nfc_data;
1097
    NFC_Init(&nfc_data);
1098
    SPI_DATA spi_data;
1099
    SPI2_Init(&spi_data, SPI2_FOSC_4);
1100
    SSD1306_DATA ssd1306_data;
1101
    SSD1306_Init(&ssd1306_data);
1102
 
1103
    I2C_Configure_Master(I2C_400KHZ);
1104
 
1105
    Interrupt_Init(); // Initialize the interrupt priorities
1106
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
1107
 
1108
    sprintf(output, "\r\nBegin Program\r\n");
1109
    DBG_PRINT_MAIN(output, strlen(output));
1110
 
1111
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
1112
    memset(cardData, 0, 24);
1113
    memset(cardData_prev, 0, 24);
1114
    SSD1306_Clear_Display();
1115
    SSD1306_Set_Rotation(0);
1116
    SSD1306_Set_Cursor(0, 0);
1117
 
1118
    version = NFC_Get_Firmware_Version();
1119
    while (!version.IC) {
1120
        sprintf(output, "Waiting for NFC board..\n");
1121
        SSD1306_Write_String(output, strlen(output));
1122
        SSD1306_Display();
1123
        Delay10KTCYx(255);
1124
        version = NFC_Get_Firmware_Version();
1125
    }
1126
    sprintf(output, "PN5%X Ver. %d.%d\n", version.IC, version.Ver, version.Rev);
1127
    SSD1306_Write_String(output, strlen(output));
1128
    SSD1306_Display();
1129
    NFC_SAMConfig();
1130
 
1131
    while (1) {
1132
 
1133
        // This query will not wait for a detection before responding
1134
        char length = NFC_Poll_Targets(1, 1, cardData);
1135
        if (!length) {
1136
            memset(cardData_prev, 0, 24);
1137
        } else if (length == 1) {
1138
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
1139
                // Do nothing
1140
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
1141
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
1142
            } else {
1143
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
1144
                SSD1306_Write_String(output, strlen(output));
1145
                SSD1306_Display();
1146
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
1147
            }
1148
            memset(&cardData_prev[1], 0, 12);
1149
        } else if (length == 2) {
1150
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
1151
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
1152
                // Do nothing
1153
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
1154
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
1155
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
1156
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
1157
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
1158
                // First card matched
1159
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
1160
                SSD1306_Write_String(output, strlen(output));
1161
                SSD1306_Display();
1162
                memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
1163
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
1164
                // Second card matched
1165
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
1166
                SSD1306_Write_String(output, strlen(output));
1167
                SSD1306_Display();
1168
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
1169
            } else {
1170
                // No match
1171
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
1172
                SSD1306_Write_String(output, strlen(output));
1173
                SSD1306_Display();
1174
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
1175
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
1176
                SSD1306_Write_String(output, strlen(output));
1177
                SSD1306_Display();
1178
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
1179
            }
1180
        }
1181
    }
1182
}
158 Kevin 1183
// </editor-fold>
155 Kevin 1184
#elif defined(_TEST_LUX_TO_CHAR_OLED)
158 Kevin 1185
// <editor-fold defaultstate="collapsed" desc="_TEST_LUX_TO_CHAR_OLED">
155 Kevin 1186
void main(void) {
1187
    char output[64];
1188
 
1189
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
1190
    ANCON0 = 0xF8;
1191
    ANCON1 = 0x1F;
1192
 
1193
    I2C_DATA i2c_data;
1194
    I2C_Init(&i2c_data);
1195
    OLED_CHAR_DATA oled_data;
1196
    NHD_Init(&oled_data);
1197
    TSL2561_DATA lux_data;
1198
    LUX_Init(&lux_data, TSL2561_ADDR_FLOAT);
1199
 
1200
    I2C_Configure_Master(I2C_400KHZ);
1201
 
1202
    Interrupt_Init(); // Initialize the interrupt priorities
1203
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
1204
 
1205
    NHD_Begin(16, 2);
1206
 
1207
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
158 Kevin 1208
    LUX_Set_Gain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
1209
//    LUX_Set_Gain(TSL2561_GAIN_16X);  // set 16x gain (for dim situations)
155 Kevin 1210
 
1211
    // Changing the integration time gives you a longer time over which to sense light
1212
    // longer timelines are slower, but are good in very low light situtations!
159 Kevin 1213
//    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
1214
    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium light)
158 Kevin 1215
//    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim light)
155 Kevin 1216
 
1217
    while (1) {
1218
        unsigned long lum = LUX_Get_Full_Luminosity();
1219
        unsigned int ir = lum >> 16;
1220
        unsigned int full = lum & 0xFFFF;
1221
        NHD_Set_Cursor(0, 0);
1222
        sprintf(output, "I: %d ", ir);
1223
        NHD_Write_String(output, strlen(output));
1224
        sprintf(output, "V: %d        ", full - ir);
1225
        NHD_Write_String(output, strlen(output));
1226
        NHD_Set_Cursor(0, 1);
1227
        sprintf(output, "Lux: %ld        ", LUX_Calculate_Lux(full, ir));
1228
        NHD_Write_String(output, strlen(output));
1229
 
159 Kevin 1230
//        Delay10KTCYx(100);
155 Kevin 1231
    }
1232
}
158 Kevin 1233
// </editor-fold>
160 Kevin 1234
#elif defined(_TEST_RTC_TO_LED_BACKPACK_CHAR_OLED)
1235
// <editor-fold defaultstate="collapsed" desc="_TEST_RTC_TO_LED_BACKPACK_CHAR_OLED">
1236
void main(void) {
1237
    char output[64];
1238
 
1239
    // Set all ports as digial I/O
1240
    ANCON0 = 0xFF;
1241
    ANCON1 = 0x1F;
1242
 
1243
    I2C_DATA i2c_data;
1244
    I2C_Init(&i2c_data);
1245
    LED_DATA led_data;
1246
    LED_Init(&led_data);
1247
    OLED_CHAR_DATA oled_data;
1248
    NHD_Init(&oled_data);
1249
 
1250
    I2C_Configure_Master(I2C_400KHZ);
1251
 
1252
    Interrupt_Init(); // Initialize the interrupt priorities
1253
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
1254
 
1255
    LED_Start();
1256
    LED_Draw_Colon(1);
1257
    NHD_Begin(16, 2);
1258
    DS3231_Begin();
1259
 
1260
    char sec, min, hour, day, date, month, year, h_mil, h_am_pm;
1261
    int time;
1262
    while (1) {
1263
        DS3231_Get_Time(&sec, &min, &hour, &day, &date, &month, &year, &h_mil, &h_am_pm);
1264
        time = hour * 100 + min;
1265
        LED_Write_Num(time);
1266
 
1267
        NHD_Set_Cursor(0, 0);
1268
        sprintf(output, "%02d:%02d:%02d %s", hour, min, sec, h_am_pm ? "PM" : "AM");
1269
        NHD_Write_String(output, strlen(output));
1270
 
1271
        NHD_Set_Cursor(12, 0);
1272
        switch (day) {
1273
            case 1:
1274
                sprintf(output, "*MON");
1275
                break;
1276
            case 2:
1277
                sprintf(output, "*TUE");
1278
                break;
1279
            case 3:
1280
                sprintf(output, "*WED");
1281
                break;
1282
            case 4:
1283
                sprintf(output, "*THU");
1284
                break;
1285
            case 5:
1286
                sprintf(output, "*FRI");
1287
                break;
1288
            case 6:
1289
                sprintf(output, "*SAT");
1290
                break;
1291
            case 7:
1292
                sprintf(output, "*SUN");
1293
                break;
1294
        }
1295
        NHD_Write_String(output, strlen(output));
1296
 
1297
        NHD_Set_Cursor(0, 1);
1298
        switch (month) {
1299
            case 1:
1300
                sprintf(output, "January");
1301
                break;
1302
            case 2:
1303
                sprintf(output, "February");
1304
                break;
1305
            case 3:
1306
                sprintf(output, "March");
1307
                break;
1308
            case 4:
1309
                sprintf(output, "April");
1310
                break;
1311
            case 5:
1312
                sprintf(output, "May");
1313
                break;
1314
            case 6:
1315
                sprintf(output, "June");
1316
                break;
1317
            case 7:
1318
                sprintf(output, "July");
1319
                break;
1320
            case 8:
1321
                sprintf(output, "August");
1322
                break;
1323
            case 9:
1324
                sprintf(output, "September");
1325
                break;
1326
            case 10:
1327
                sprintf(output, "October");
1328
                break;
1329
            case 11:
1330
                sprintf(output, "November");
1331
                break;
1332
            case 12:
1333
                sprintf(output, "December");
1334
                break;
1335
        }
1336
        NHD_Write_String(output, strlen(output));
1337
 
1338
        sprintf(output, " %d 20%d", date, year);
1339
        NHD_Write_String(output, strlen(output));
1340
 
1341
        Delay10KTCYx(100);
1342
    }
1343
}
1344
// </editor-fold>
161 Kevin 1345
#elif defined(_TEST_AHRS)
1346
// <editor-fold defaultstate="collapsed" desc="_TEST_AHRS">
1347
void main(void) {
1348
    char output[64];
1349
 
1350
    // Set all ports as digial I/O except for AN0-AN2 (pins 2-4)
1351
    ANCON0 = 0xF8;
1352
    ANCON1 = 0x1F;
1353
 
1354
    UART_DATA uart_data;
1355
    UART1_Init(&uart_data);
1356
    I2C_DATA i2c_data;
1357
    I2C_Init(&i2c_data);
1358
    L3G_DATA gyro_data;
1359
    L3G_Init(&gyro_data, L3GD20_DEVICE, L3G_SA0_HIGH);
1360
    LSM303_DATA acc_data;
1361
    LSM303_Init(&acc_data, LSM303DLHC_DEVICE, ACC_ADDRESS_SA0_A_LOW);
1362
 
1363
    I2C_Configure_Master(I2C_100KHZ);
1364
 
1365
    Interrupt_Init(); // Initialize the interrupt priorities
1366
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
1367
 
1368
    sprintf(output, "\r\nBegin Program\r\n");
1369
    DBG_PRINT_MAIN(output, strlen(output));
1370
 
1371
    L3G_Begin();
1372
    LSM303_Begin();
1373
    int a_x, a_y, a_z, m_x, m_y, m_z, g_x, g_y, g_z;
1374
    while (1) {
1375
        L3G_Read(&g_x, &g_y, &g_z);
1376
        LSM303_Read_Acc(&a_x, &a_y, &a_z);
1377
        LSM303_Read_Mag(&m_x, &m_y, &m_z);
1378
        sprintf(output, "GAM:%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",
1379
                g_x,g_y,g_z,a_x,a_y,a_z,m_x,m_y,m_z);
1380
        DBG_PRINT_MAIN(output, strlen(output));
1381
 
1382
        Delay10KTCYx(255);
1383
    }
1384
}
1385
// </editor-fold>
155 Kevin 1386
#elif defined(_TEST_XBEE)
158 Kevin 1387
// <editor-fold defaultstate="collapsed" desc="_TEST_XBEE">
155 Kevin 1388
void main(void) {
157 Kevin 1389
    char buffer[100];
152 Kevin 1390
 
155 Kevin 1391
    XBEE_RX_AT_COMMAND_RESPONSE_FRAME *rx_at_cmd_response_frame;
1392
    XBEE_RX_DATA_PACKET_FRAME *rx_data_frame;
1393
    XBEE_RX_DATA_TX_STATUS_FRAME *rx_tx_status_frame;
1394
    XBEE_RX_REMOTE_AT_COMMAND_FRAME *rx_remote_at_cmd_frame;
1395
    XBEE_RX_NODE_IDENTIFICATION_INDICATOR_FRAME *rx_node_ident_frame;
1396
    XBEE_RX_MODEM_STATUS_FRAME *rx_modem_status_frame;
1397
 
152 Kevin 1398
    /* --------------------- Oscillator Configuration --------------------- */
1399
    //    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
1400
    OSCCONbits.IRCF = 0b111; // Set INTOSC postscaler to 8MHz
1401
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
1402
    /* -------------------------------------------------------------------- */
1403
 
1404
    // Set all ports as digial I/O
1405
    ANCON0 = 0xFF;
1406
    ANCON1 = 0x1F;
1407
 
157 Kevin 1408
    UART_DATA uart_data;
1409
    UART1_Init(&uart_data); // Initialize the UART handler code
1410
    XBEE_DATA xbee_data;
1411
    XBee_Init(&xbee_data);
152 Kevin 1412
 
157 Kevin 1413
    Interrupt_Init(); // Initialize the interrupt priorities
155 Kevin 1414
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
152 Kevin 1415
 
1416
 
157 Kevin 1417
    sprintf(buffer, "\r\nBegin Program\r\n");
1418
    DBG_PRINT_MAIN(buffer, strlen(buffer));
1419
 
152 Kevin 1420
    while (1) {
1421
 
157 Kevin 1422
//#define _ROUTER
155 Kevin 1423
#define _COORDINATOR
1424
 
1425
#ifdef _ROUTER
1426
        XBEE_TX_DATA_PACKET_FRAME *tx_data_frame;
1427
        tx_data_frame = (void *) buffer;
1428
        tx_data_frame->frame_type = XBEE_TX_DATA_PACKET;
1429
        tx_data_frame->frame_id = 1;
1430
        tx_data_frame->destination_64.UPPER_32.long_value = 0x00000000;
1431
        tx_data_frame->destination_64.LOWER_32.long_value = 0x00000000;
1432
        tx_data_frame->destination_16.INT_16.int_value = 0xFEFF;
1433
        tx_data_frame->broadcast_radius = 0;
1434
        tx_data_frame->options = 0;
1435
        tx_data_frame->data[0] = 0x54;
1436
        tx_data_frame->data[1] = 0x78;
1437
        tx_data_frame->data[2] = 0x32;
1438
        tx_data_frame->data[3] = 0x43;
1439
        tx_data_frame->data[4] = 0x6F;
1440
        tx_data_frame->data[5] = 0x6F;
1441
        tx_data_frame->data[6] = 0x72;
1442
        tx_data_frame->data[7] = 0x11;
1443
        XBee_Process_Transmit_Frame(buffer, XBEE_TX_DATA_PACKET_FRAME_SIZE + 8);
1444
 
1445
        Delay10KTCYx(255);
1446
        Delay10KTCYx(255);
1447
        Delay10KTCYx(255);
1448
        Delay10KTCYx(255);
1449
        Delay10KTCYx(255);
1450
        Delay10KTCYx(255);
1451
        Delay10KTCYx(255);
1452
        Delay10KTCYx(255);
1453
#endif
1454
 
1455
#ifdef _COORDINATOR
157 Kevin 1456
        int length = XBee_Get_Received_Frame(buffer);
155 Kevin 1457
        if (length != 0) {
157 Kevin 1458
            switch (*(char *) buffer) {
155 Kevin 1459
                case XBEE_RX_AT_COMMAND_RESPONSE:
157 Kevin 1460
                    sprintf(buffer, "XBEE: parsing recieved AT command response frame\r\n");
1461
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1462
                    rx_at_cmd_response_frame = (void *) buffer;
157 Kevin 1463
//                    DBG_PRINT_MAIN("Frame ID: %u\r\n", rx_at_cmd_response_frame->frame_id);
1464
//                    DBG_PRINT_MAIN("AT Command: %c%c  Status: %02X\r\n", rx_at_cmd_response_frame->command[0], \\
1465
//                            rx_at_cmd_response_frame->command[1], rx_at_cmd_response_frame->command_status);
155 Kevin 1466
                    if (length > XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE) {
157 Kevin 1467
//                        DBG_PRINT_MAIN("Command Data: ");
1468
                        for (int i = 0; i < length - XBEE_RX_AT_COMMAND_RESPONSE_FRAME_SIZE; i++) {
1469
//                            DBG_PRINT_MAIN("%02X ", rx_at_cmd_response_frame->data[i]);
155 Kevin 1470
                        }
157 Kevin 1471
//                        DBG_PRINT_MAIN("\r\n");
155 Kevin 1472
                    }
1473
                    break;
1474
                case XBEE_RX_DATA_PACKET:
157 Kevin 1475
                    sprintf(buffer, "XBEE: parsing recieved data recieved frame\r\n");
1476
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1477
                    rx_data_frame = (void *) buffer;
1478
                    XBee_Convert_Endian_64(&(rx_data_frame->source_64));
1479
                    XBee_Convert_Endian_16(&(rx_data_frame->source_16));
157 Kevin 1480
//                    DBG_PRINT_MAIN("Source 64: %08lX %08lX  Source 16: %04X  Options: %02X\r\n", \\
1481
//                            rx_data_frame->source_64.UPPER_32.long_value, \\
1482
//                            rx_data_frame->source_64.LOWER_32.long_value, \\
1483
//                            rx_data_frame->source_16.INT_16.int_value, \\
1484
//                            rx_data_frame->recieve_options);
1485
//                    DBG_PRINT_MAIN("Data: ");
1486
                    for (int i = 0; i < length - XBEE_RX_DATA_PACKET_FRAME_SIZE; i++) {
1487
//                        DBG_PRINT_MAIN("%02X ", rx_data_frame->data[i]);
155 Kevin 1488
                    }
157 Kevin 1489
//                    DBG_PRINT_MAIN("\r\n");
155 Kevin 1490
                    break;
1491
                case XBEE_RX_DATA_TX_STATUS:
157 Kevin 1492
                    sprintf(buffer, "XBEE: parsing recieved TX status frame\r\n");
1493
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1494
                    rx_tx_status_frame = (void *) buffer;
1495
                    XBee_Convert_Endian_16(&(rx_tx_status_frame->destination_16));
157 Kevin 1496
//                    DBG_PRINT_MAIN("Frame ID: %u  Destination 16: %04X\r\n", \\
1497
//                            rx_tx_status_frame->frame_id, rx_tx_status_frame->destination_16.INT_16.int_value);
1498
//                    DBG_PRINT_MAIN("Transmit Retry Count: %02X  Delivery Status: %02X  Discovery Status: %02X\r\n", \\
1499
//                            rx_tx_status_frame->transmit_retry_count, rx_tx_status_frame->delivery_status, \\
1500
//                            rx_tx_status_frame->discovery_status);
155 Kevin 1501
                    break;
1502
                case XBEE_RX_IO_DATA_SAMPLE:
157 Kevin 1503
                    sprintf(buffer, "XBEE: parsing recieved IO data sample frame\r\n");
1504
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1505
                    break;
1506
                case XBEE_RX_EXPLICIT_COMMAND:
157 Kevin 1507
                    sprintf(buffer, "XBEE: parsing recieved explicit command frame\r\n");
1508
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1509
                    break;
1510
                case XBEE_RX_REMOTE_AT_COMMAND_RESPONSE:
157 Kevin 1511
                    sprintf(buffer, "XBEE: parsing recieved remote AT command frame\r\n");
1512
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1513
                    rx_remote_at_cmd_frame = (void *) buffer;
1514
                    break;
1515
                case XBEE_RX_ROUTE_RECORD:
157 Kevin 1516
                    sprintf(buffer, "XBEE: parsing recieved route record frame\r\n");
1517
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1518
                    break;
1519
                case XBEE_RX_NODE_IDENTIFICATION:
157 Kevin 1520
                    sprintf(buffer, "XBEE: parsing recieved node identification frame\r\n");
1521
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1522
                    rx_node_ident_frame = (void *) buffer;
1523
                    XBee_Convert_Endian_64(&(rx_node_ident_frame->source_64));
1524
                    XBee_Convert_Endian_16(&(rx_node_ident_frame->source_16));
1525
                    XBee_Convert_Endian_64(&(rx_node_ident_frame->remote_64));
1526
                    XBee_Convert_Endian_16(&(rx_node_ident_frame->remote_16));
1527
                    XBee_Convert_Endian_16(&(rx_node_ident_frame->parent_16));
157 Kevin 1528
//                    DBG_PRINT_MAIN("Source 64: %08lX %08lX  Source 16: %04X  Options: %02X\r\n", \\
1529
//                            rx_node_ident_frame->source_64.UPPER_32.long_value, \\
1530
//                            rx_node_ident_frame->source_64.LOWER_32.long_value, \\
1531
//                            rx_node_ident_frame->source_16.INT_16.int_value, \\
1532
//                            rx_node_ident_frame->recieve_options);
1533
//                    DBG_PRINT_MAIN("Remote 64: %08lX %08lX  Remote 16: %04X  Parent 16: %04X\r\n", \\
1534
//                            rx_node_ident_frame->remote_64.UPPER_32.long_value, \\
1535
//                            rx_node_ident_frame->remote_64.LOWER_32.long_value, \\
1536
//                            rx_node_ident_frame->remote_16.INT_16.int_value, \\
1537
//                            rx_node_ident_frame->parent_16.INT_16.int_value);
1538
//                    DBG_PRINT_MAIN("Device Type: %02X  Source Event: %02X\r\n", \\
1539
//                            rx_node_ident_frame->device_type, rx_node_ident_frame->source_event);
155 Kevin 1540
                    break;
1541
                case XBEE_RX_FRAME_MODEM_STATUS:
157 Kevin 1542
                    sprintf(buffer, "XBEE: parsing recieved modem status frame\r\n");
1543
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1544
                    rx_modem_status_frame = (void *) buffer;
157 Kevin 1545
//                    DBG_PRINT_MAIN("Status: %02X\r\n", rx_modem_status_frame->status);
155 Kevin 1546
                    break;
1547
                default:
157 Kevin 1548
                    sprintf(buffer, "??\r\n");
1549
                    DBG_PRINT_MAIN(buffer, strlen(buffer));
155 Kevin 1550
                    break;
1551
            }
1552
        }
1553
#endif
1554
 
152 Kevin 1555
    }
1556
}
158 Kevin 1557
// </editor-fold>
152 Kevin 1558
#else
1559
int main() {
161 Kevin 1560
    char output[64];
1561
 
1562
    // Set all ports as digial I/O
1563
    ANCON0 = 0xFF;
1564
    ANCON1 = 0x1F;
1565
 
1566
    // NFC stuff
1567
    NFC_FIRMWARE_VERSION version;
1568
    NFC_TargetDataMiFare cardData[2];
1569
    NFC_TargetDataMiFare cardData_prev[2];
1570
 
1571
    I2C_DATA i2c_data;
1572
    I2C_Init(&i2c_data);
1573
    LED_DATA led_data;
1574
    LED_Init(&led_data);
1575
    OLED_CHAR_DATA oled_data;
1576
    NHD_Init(&oled_data);
1577
    TSL2561_DATA lux_data;
1578
    LUX_Init(&lux_data, TSL2561_ADDR_FLOAT);
1579
    NFC_DATA nfc_data;
1580
    NFC_Init(&nfc_data);
1581
    SPI_DATA spi_data;
1582
    SPI2_Init(&spi_data, SPI2_FOSC_4);
1583
    SSD1306_DATA ssd1306_data;
1584
    SSD1306_Init(&ssd1306_data);
1585
 
1586
    I2C_Configure_Master(I2C_400KHZ);
1587
 
1588
    Interrupt_Init(); // Initialize the interrupt priorities
1589
    Interrupt_Enable(); // Enable high-priority interrupts and low-priority interrupts
1590
 
1591
    LED_Start();
1592
    LED_Draw_Colon(1);
1593
    NHD_Begin(16, 2);
1594
    DS3231_Begin();
1595
 
1596
    // You can change the gain on the fly, to adapt to brighter/dimmer light situations
1597
//    LUX_Set_Gain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
1598
    LUX_Set_Gain(TSL2561_GAIN_16X);  // set 16x gain (for dim situations)
1599
 
1600
    // Changing the integration time gives you a longer time over which to sense light
1601
    // longer timelines are slower, but are good in very low light situtations!
1602
    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright light)
1603
//    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium light)
1604
//    LUX_Set_Timing(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim light)
1605
 
1606
    SSD1306_Begin(SSD1306_SWITCHCAPVCC);
1607
    memset(cardData, 0, 24);
1608
    memset(cardData_prev, 0, 24);
1609
    SSD1306_Clear_Display();
1610
    SSD1306_Set_Rotation(0);
1611
    SSD1306_Set_Cursor(0, 0);
157 Kevin 1612
 
161 Kevin 1613
    char sec, min, hour, day, date, month, year, h_mil, h_am_pm;
1614
    int time;
1615
 
1616
    version = NFC_Get_Firmware_Version();
1617
    while (!version.IC) {
1618
        sprintf(output, "Waiting for NFC board..\n");
1619
        SSD1306_Write_String(output, strlen(output));
1620
        SSD1306_Display();
1621
        Delay10KTCYx(255);
1622
        version = NFC_Get_Firmware_Version();
1623
    }
1624
    sprintf(output, "PN5%X Ver. %d.%d\n", version.IC, version.Ver, version.Rev);
1625
    SSD1306_Write_String(output, strlen(output));
1626
    SSD1306_Display();
1627
    NFC_SAMConfig();
1628
 
1629
    while (1) {
1630
        // Time to LED backpack
1631
        DS3231_Get_Time(&sec, &min, &hour, &day, &date, &month, &year, &h_mil, &h_am_pm);
1632
        time = hour * 100 + min;
1633
        LED_Write_Num(time);
1634
 
1635
        // Lux to Character OLED
1636
        unsigned long lum = LUX_Get_Full_Luminosity();
1637
        unsigned int ir = lum >> 16;
1638
        unsigned int full = lum & 0xFFFF;
1639
        NHD_Set_Cursor(0, 0);
1640
        sprintf(output, "I: %d ", ir);
1641
        NHD_Write_String(output, strlen(output));
1642
        sprintf(output, "V: %d        ", full - ir);
1643
        NHD_Write_String(output, strlen(output));
1644
        NHD_Set_Cursor(0, 1);
1645
        sprintf(output, "Lux: %ld        ", LUX_Calculate_Lux(full, ir));
1646
        NHD_Write_String(output, strlen(output));
1647
 
1648
        // NFC Query
1649
        char length = NFC_Poll_Targets(1, 1, cardData);
1650
        if (!length) {
1651
            memset(cardData_prev, 0, 24);
1652
        } else if (length == 1) {
1653
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
1654
                // Do nothing
1655
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0) {
1656
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
1657
            } else {
1658
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
1659
                SSD1306_Write_String(output, strlen(output));
1660
                SSD1306_Display();
1661
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
1662
            }
1663
            memset(&cardData_prev[1], 0, 12);
1664
        } else if (length == 2) {
1665
            if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0 &&
1666
                    memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
1667
                // Do nothing
1668
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[1].NFCID, cardData[0].NFCID_LEN) == 0 &&
1669
                    memcmp(&cardData[1].NFCID, &cardData_prev[0].NFCID, cardData[1].NFCID_LEN) == 0) {
1670
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
1671
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
1672
            } else if (memcmp(&cardData[0].NFCID, &cardData_prev[0].NFCID, cardData[0].NFCID_LEN) == 0) {
1673
                // First card matched
1674
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
1675
                SSD1306_Write_String(output, strlen(output));
1676
                SSD1306_Display();
1677
                memcpy(&cardData_prev[1], (const char *) &cardData[1], 12);
1678
            } else if (memcmp(&cardData[1].NFCID, &cardData_prev[1].NFCID, cardData[1].NFCID_LEN) == 0) {
1679
                // Second card matched
1680
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
1681
                SSD1306_Write_String(output, strlen(output));
1682
                SSD1306_Display();
1683
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
1684
            } else {
1685
                // No match
1686
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[0].NFCID[0], cardData[0].NFCID[1], cardData[0].NFCID[2], cardData[0].NFCID[3]);
1687
                SSD1306_Write_String(output, strlen(output));
1688
                SSD1306_Display();
1689
                memcpy((char *) &cardData_prev[0], (const char *) &cardData[0], 12);
1690
                sprintf(output, "UID: %02X %02X %02X %02X\n", cardData[1].NFCID[0], cardData[1].NFCID[1], cardData[1].NFCID[2], cardData[1].NFCID[3]);
1691
                SSD1306_Write_String(output, strlen(output));
1692
                SSD1306_Display();
1693
                memcpy((char *) &cardData_prev[1], (const char *) &cardData[1], 12);
1694
            }
1695
        }
1696
 
1697
 
1698
        Delay10KTCYx(100);
1699
    }
152 Kevin 1700
}
157 Kevin 1701
#endif