Code to log MAX30102 sensor data to phone application (Based on Nexpaq)
Dependencies: ds3231 maxim_dev nexpaq_mdk MAX30102
Fork of IO_Demo by
main.cpp@0:088641a122ff, 2016-11-08 (annotated)
- Committer:
- switches
- Date:
- Tue Nov 08 18:38:59 2016 +0000
- Revision:
- 0:088641a122ff
- Child:
- 1:5fb525e83780
Committing demo with updated library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:088641a122ff | 1 | #include "mbed.h" |
switches | 0:088641a122ff | 2 | #include "nexpaq_mdk.h" |
switches | 0:088641a122ff | 3 | |
switches | 0:088641a122ff | 4 | PwmOut ledR(P2_4); |
switches | 0:088641a122ff | 5 | PwmOut ledG(P2_5); |
switches | 0:088641a122ff | 6 | PwmOut ledB(P2_6); |
switches | 0:088641a122ff | 7 | AnalogIn aIn0(AIN_0); |
switches | 0:088641a122ff | 8 | AnalogIn aIn1(AIN_1); |
switches | 0:088641a122ff | 9 | AnalogIn aIn2(AIN_2); |
switches | 0:088641a122ff | 10 | AnalogIn aIn3(AIN_3); |
switches | 0:088641a122ff | 11 | AnalogIn aIn4(AIN_4); |
switches | 0:088641a122ff | 12 | AnalogIn aIn5(AIN_5); |
switches | 0:088641a122ff | 13 | DigitalInOut digPin[] = {DigitalInOut(P0_1),DigitalInOut(P0_0),DigitalInOut(P1_2),DigitalInOut(P1_1), |
switches | 0:088641a122ff | 14 | DigitalInOut(P1_0),DigitalInOut(P1_3),DigitalInOut(P1_7),DigitalInOut(P1_6), |
switches | 0:088641a122ff | 15 | DigitalInOut(P3_0),DigitalInOut(P3_1),DigitalInOut(P3_4),DigitalInOut(P3_5), |
switches | 0:088641a122ff | 16 | DigitalInOut(P0_2),DigitalInOut(P0_3),DigitalInOut(P4_0),DigitalInOut(P4_1) |
switches | 0:088641a122ff | 17 | }; |
switches | 0:088641a122ff | 18 | |
switches | 0:088641a122ff | 19 | /***** Definitions *****/ |
switches | 0:088641a122ff | 20 | #define FUNCTION_TABLE_NUM 2 |
switches | 0:088641a122ff | 21 | #define UUID_NUM 16 // UUID number is 16, don't change it |
switches | 0:088641a122ff | 22 | #define LOOP_DELAY 1000 |
switches | 0:088641a122ff | 23 | |
switches | 0:088641a122ff | 24 | /***** Globals *****/ |
switches | 0:088641a122ff | 25 | void my_function_CMD_2700(unsigned char *pData, unsigned char len); |
switches | 0:088641a122ff | 26 | void my_function_CMD_2702(unsigned char *pData, unsigned char len); |
switches | 0:088641a122ff | 27 | const MDK_REGISTER_CMD my_cmd_func_table[FUNCTION_TABLE_NUM] = { |
switches | 0:088641a122ff | 28 | {0x2700, my_function_CMD_2700}, // Command -> function |
switches | 0:088641a122ff | 29 | {0x2702, my_function_CMD_2702}, // Command -> function |
switches | 0:088641a122ff | 30 | }; |
switches | 0:088641a122ff | 31 | |
switches | 0:088641a122ff | 32 | unsigned char dataOut[10]; |
switches | 0:088641a122ff | 33 | |
switches | 0:088641a122ff | 34 | /***** Functions *****/ |
switches | 0:088641a122ff | 35 | void my_function_CMD_2700(unsigned char *pData, unsigned char len) |
switches | 0:088641a122ff | 36 | { |
switches | 0:088641a122ff | 37 | unsigned char response = 0x00; |
switches | 0:088641a122ff | 38 | ledR = 1.0f - (pData[0] / 255.0f); |
switches | 0:088641a122ff | 39 | ledG = 1.0f - (pData[1] / 255.0f); |
switches | 0:088641a122ff | 40 | ledB = 1.0f - (pData[2] / 255.0f); |
switches | 0:088641a122ff | 41 | np_api_upload(0x2701, &response, 1); |
switches | 0:088641a122ff | 42 | } |
switches | 0:088641a122ff | 43 | |
switches | 0:088641a122ff | 44 | void my_function_CMD_2702(unsigned char *pData, unsigned char len) |
switches | 0:088641a122ff | 45 | { |
switches | 0:088641a122ff | 46 | unsigned char response = 0x00; |
switches | 0:088641a122ff | 47 | int pinIndex = pData[0]; |
switches | 0:088641a122ff | 48 | int pinMode = pData[1]; |
switches | 0:088641a122ff | 49 | switch(pinMode) { |
switches | 0:088641a122ff | 50 | case 0: |
switches | 0:088641a122ff | 51 | case 1: |
switches | 0:088641a122ff | 52 | digPin[pinIndex].mode(PullNone); |
switches | 0:088641a122ff | 53 | digPin[pinIndex].input(); |
switches | 0:088641a122ff | 54 | break; |
switches | 0:088641a122ff | 55 | case 2: |
switches | 0:088641a122ff | 56 | digPin[pinIndex].mode(PullDown); |
switches | 0:088641a122ff | 57 | digPin[pinIndex].input(); |
switches | 0:088641a122ff | 58 | break; |
switches | 0:088641a122ff | 59 | case 3: |
switches | 0:088641a122ff | 60 | digPin[pinIndex].mode(PullUp); |
switches | 0:088641a122ff | 61 | digPin[pinIndex].input(); |
switches | 0:088641a122ff | 62 | break; |
switches | 0:088641a122ff | 63 | case 4: |
switches | 0:088641a122ff | 64 | digPin[pinIndex]=0; |
switches | 0:088641a122ff | 65 | digPin[pinIndex].output(); |
switches | 0:088641a122ff | 66 | break; |
switches | 0:088641a122ff | 67 | case 5: |
switches | 0:088641a122ff | 68 | digPin[pinIndex]=1; |
switches | 0:088641a122ff | 69 | digPin[pinIndex].output(); |
switches | 0:088641a122ff | 70 | break; |
switches | 0:088641a122ff | 71 | } |
switches | 0:088641a122ff | 72 | np_api_upload(0x2703, &response, 1); |
switches | 0:088641a122ff | 73 | } |
switches | 0:088641a122ff | 74 | |
switches | 0:088641a122ff | 75 | void sendStatus() |
switches | 0:088641a122ff | 76 | { |
switches | 0:088641a122ff | 77 | np_api_upload(0x2800, dataOut, 10); |
switches | 0:088641a122ff | 78 | } |
switches | 0:088641a122ff | 79 | |
switches | 0:088641a122ff | 80 | /******************************************************************************/ |
switches | 0:088641a122ff | 81 | void app_setup() |
switches | 0:088641a122ff | 82 | { |
switches | 0:088641a122ff | 83 | int i = 0; |
switches | 0:088641a122ff | 84 | // np_api_set_app_version(0, 0, 3); |
switches | 0:088641a122ff | 85 | if ( np_api_register((MDK_REGISTER_CMD*)my_cmd_func_table, FUNCTION_TABLE_NUM) == MDK_REGISTER_FAILD ) { |
switches | 0:088641a122ff | 86 | // Register failed handle code |
switches | 0:088641a122ff | 87 | } |
switches | 0:088641a122ff | 88 | ledR = 1.0f; |
switches | 0:088641a122ff | 89 | ledG = 1.0f; |
switches | 0:088641a122ff | 90 | ledB = 1.0f; |
switches | 0:088641a122ff | 91 | for (i = 0; i < 16; i++) { |
switches | 0:088641a122ff | 92 | digPin[i].mode(PullUp); |
switches | 0:088641a122ff | 93 | digPin[i].input(); |
switches | 0:088641a122ff | 94 | } |
switches | 0:088641a122ff | 95 | for (i = 0; i < 10; i++) { |
switches | 0:088641a122ff | 96 | dataOut[i] = 0xFF; |
switches | 0:088641a122ff | 97 | } |
switches | 0:088641a122ff | 98 | } |
switches | 0:088641a122ff | 99 | |
switches | 0:088641a122ff | 100 | void app_loop() |
switches | 0:088641a122ff | 101 | { |
switches | 0:088641a122ff | 102 | int anaData; |
switches | 0:088641a122ff | 103 | anaData = aIn0.read_u16(); |
switches | 0:088641a122ff | 104 | if (anaData >= 0xFFFF) { |
switches | 0:088641a122ff | 105 | anaData = anaData/5; |
switches | 0:088641a122ff | 106 | dataOut[0] = anaData; |
switches | 0:088641a122ff | 107 | dataOut[1] = anaData>>8; |
switches | 0:088641a122ff | 108 | } else { |
switches | 0:088641a122ff | 109 | anaData = aIn4.read_u16(); |
switches | 0:088641a122ff | 110 | dataOut[0] = anaData; |
switches | 0:088641a122ff | 111 | dataOut[1] = anaData>>8; |
switches | 0:088641a122ff | 112 | } |
switches | 0:088641a122ff | 113 | anaData = aIn1.read_u16(); |
switches | 0:088641a122ff | 114 | if (anaData >= 0xFFFF) { |
switches | 0:088641a122ff | 115 | anaData = anaData/5; |
switches | 0:088641a122ff | 116 | dataOut[2] = anaData; |
switches | 0:088641a122ff | 117 | dataOut[3] = anaData>>8; |
switches | 0:088641a122ff | 118 | } else { |
switches | 0:088641a122ff | 119 | anaData = aIn5.read_u16(); |
switches | 0:088641a122ff | 120 | dataOut[2] = anaData; |
switches | 0:088641a122ff | 121 | dataOut[3] = anaData>>8; |
switches | 0:088641a122ff | 122 | } |
switches | 0:088641a122ff | 123 | anaData = aIn2.read_u16(); |
switches | 0:088641a122ff | 124 | anaData = anaData/5; |
switches | 0:088641a122ff | 125 | dataOut[4] = anaData; |
switches | 0:088641a122ff | 126 | dataOut[5] = anaData>>8; |
switches | 0:088641a122ff | 127 | anaData = aIn3.read_u16(); |
switches | 0:088641a122ff | 128 | anaData = anaData/5; |
switches | 0:088641a122ff | 129 | dataOut[6] = anaData; |
switches | 0:088641a122ff | 130 | dataOut[7] = anaData>>8; |
switches | 0:088641a122ff | 131 | |
switches | 0:088641a122ff | 132 | dataOut[8] = (digPin[7].read()<<7) + |
switches | 0:088641a122ff | 133 | (digPin[6].read()<<6) + |
switches | 0:088641a122ff | 134 | (digPin[5].read()<<5) + |
switches | 0:088641a122ff | 135 | (digPin[4].read()<<4) + |
switches | 0:088641a122ff | 136 | (digPin[3].read()<<3) + |
switches | 0:088641a122ff | 137 | (digPin[2].read()<<2) + |
switches | 0:088641a122ff | 138 | (digPin[1].read()<<1) + |
switches | 0:088641a122ff | 139 | (digPin[0].read()); |
switches | 0:088641a122ff | 140 | dataOut[9] = (digPin[15].read()<<7) + |
switches | 0:088641a122ff | 141 | (digPin[14].read()<<6) + |
switches | 0:088641a122ff | 142 | (digPin[13].read()<<5) + |
switches | 0:088641a122ff | 143 | (digPin[12].read()<<4) + |
switches | 0:088641a122ff | 144 | (digPin[11].read()<<3) + |
switches | 0:088641a122ff | 145 | (digPin[10].read()<<2) + |
switches | 0:088641a122ff | 146 | (digPin[9].read()<<1) + |
switches | 0:088641a122ff | 147 | (digPin[8].read()); |
switches | 0:088641a122ff | 148 | sendStatus(); |
switches | 0:088641a122ff | 149 | } |
switches | 0:088641a122ff | 150 | |
switches | 0:088641a122ff | 151 | int main(void) |
switches | 0:088641a122ff | 152 | { |
switches | 0:088641a122ff | 153 | np_api_init(); |
switches | 0:088641a122ff | 154 | app_setup(); |
switches | 0:088641a122ff | 155 | np_api_start(); |
switches | 0:088641a122ff | 156 | while(1) { |
switches | 0:088641a122ff | 157 | app_loop(); |
switches | 0:088641a122ff | 158 | np_api_bsl_chk(); |
switches | 0:088641a122ff | 159 | Thread::wait(LOOP_DELAY); |
switches | 0:088641a122ff | 160 | } |
switches | 0:088641a122ff | 161 | return 0; |
switches | 0:088641a122ff | 162 | } |