Subversion Repositories Code-Repo

Rev

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