![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Code to be run for the project Group Fitness
Dependencies: FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351 MAX30101
Fork of final_project_ee119 by
main.cpp@2:9dfb7ebaaa6e, 2018-06-01 (annotated)
- Committer:
- xihan94
- Date:
- Fri Jun 01 19:09:47 2018 +0000
- Revision:
- 2:9dfb7ebaaa6e
- Parent:
- 1:ad1b075585bc
Demo start;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xihan94 | 0:33686dd26bf9 | 1 | #include "mbed.h" |
xihan94 | 0:33686dd26bf9 | 2 | #include "mbed_events.h" |
xihan94 | 0:33686dd26bf9 | 3 | #include "MAX30101.h" |
xihan94 | 0:33686dd26bf9 | 4 | |
xihan94 | 0:33686dd26bf9 | 5 | #define FIFO_DATA_MAX 288 |
xihan94 | 0:33686dd26bf9 | 6 | |
xihan94 | 0:33686dd26bf9 | 7 | DigitalOut pwr1v8(PTA29); |
xihan94 | 1:ad1b075585bc | 8 | DigitalOut pwr3v3b(PTC13); |
xihan94 | 1:ad1b075585bc | 9 | DigitalOut pwr15v(PTB12); |
xihan94 | 0:33686dd26bf9 | 10 | I2C i2c0(PTB1, PTB0); |
xihan94 | 0:33686dd26bf9 | 11 | InterruptIn maximInterrupt(PTB18); |
xihan94 | 0:33686dd26bf9 | 12 | Serial pc(USBTX, USBRX); |
xihan94 | 0:33686dd26bf9 | 13 | |
xihan94 | 0:33686dd26bf9 | 14 | EventQueue evqueue(32 * EVENTS_EVENT_SIZE); |
xihan94 | 0:33686dd26bf9 | 15 | Thread t; |
xihan94 | 0:33686dd26bf9 | 16 | |
xihan94 | 0:33686dd26bf9 | 17 | MAX30101 hr(i2c0); |
xihan94 | 0:33686dd26bf9 | 18 | |
xihan94 | 1:ad1b075585bc | 19 | int mask_ppg = 0; |
xihan94 | 1:ad1b075585bc | 20 | uint32_t count = 0; |
xihan94 | 0:33686dd26bf9 | 21 | void interruptHandlerQueued() { |
xihan94 | 0:33686dd26bf9 | 22 | |
xihan94 | 0:33686dd26bf9 | 23 | MAX30101::InterruptBitField_u interruptStatus; |
xihan94 | 0:33686dd26bf9 | 24 | hr.getInterruptStatus(interruptStatus); |
xihan94 | 0:33686dd26bf9 | 25 | printf("Interrupt Status: 0x%02x\r\n", interruptStatus.all); |
xihan94 | 0:33686dd26bf9 | 26 | |
xihan94 | 0:33686dd26bf9 | 27 | if (interruptStatus.bits.pwr_rdy == 0x1) { |
xihan94 | 0:33686dd26bf9 | 28 | printf("Powered on\r\n"); |
xihan94 | 0:33686dd26bf9 | 29 | |
xihan94 | 2:9dfb7ebaaa6e | 30 | // // Soft reset |
xihan94 | 2:9dfb7ebaaa6e | 31 | // MAX30101::ModeConfiguration_u modeConf; |
xihan94 | 2:9dfb7ebaaa6e | 32 | // modeConf.all = 0; |
xihan94 | 2:9dfb7ebaaa6e | 33 | // modeConf.bits.reset = 1; |
xihan94 | 2:9dfb7ebaaa6e | 34 | // hr.setModeConfiguration(modeConf); |
xihan94 | 2:9dfb7ebaaa6e | 35 | // wait(0.01); |
xihan94 | 2:9dfb7ebaaa6e | 36 | // |
xihan94 | 2:9dfb7ebaaa6e | 37 | // // Configure FIFO |
xihan94 | 2:9dfb7ebaaa6e | 38 | // MAX30101::FIFO_Configuration_u fifoConf; |
xihan94 | 2:9dfb7ebaaa6e | 39 | // hr.getFIFOConfiguration(fifoConf); |
xihan94 | 2:9dfb7ebaaa6e | 40 | // pc.printf("FIFO Configuration: 0x%02x\r\n", fifoConf.all); |
xihan94 | 2:9dfb7ebaaa6e | 41 | // |
xihan94 | 2:9dfb7ebaaa6e | 42 | // // Set LED power |
xihan94 | 2:9dfb7ebaaa6e | 43 | // hr.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x0C); |
xihan94 | 2:9dfb7ebaaa6e | 44 | // hr.setLEDPulseAmplitude(MAX30101::ProxModeLED_PA, 0x19); |
xihan94 | 2:9dfb7ebaaa6e | 45 | // pc.printf("LED set\r\n"); |
xihan94 | 2:9dfb7ebaaa6e | 46 | // |
xihan94 | 2:9dfb7ebaaa6e | 47 | // MAX30101::SpO2Configuration_u spo2Conf; |
xihan94 | 2:9dfb7ebaaa6e | 48 | // hr.getSpO2Configuration(spo2Conf); |
xihan94 | 2:9dfb7ebaaa6e | 49 | // spo2Conf.bits.led_pw = MAX30101::PW_1; |
xihan94 | 2:9dfb7ebaaa6e | 50 | // spo2Conf.bits.spo2_sr = MAX30101::SR_100_Hz; |
xihan94 | 2:9dfb7ebaaa6e | 51 | // hr.setSpO2Configuration(spo2Conf); |
xihan94 | 2:9dfb7ebaaa6e | 52 | // hr.getSpO2Configuration(spo2Conf); |
xihan94 | 2:9dfb7ebaaa6e | 53 | // pc.printf("SpO2 Configuration: 0x%02x\r\n", spo2Conf.all); |
xihan94 | 2:9dfb7ebaaa6e | 54 | // |
xihan94 | 2:9dfb7ebaaa6e | 55 | // // Proximity settings |
xihan94 | 2:9dfb7ebaaa6e | 56 | // hr.setProxIntThreshold(0x14); |
xihan94 | 2:9dfb7ebaaa6e | 57 | // |
xihan94 | 2:9dfb7ebaaa6e | 58 | // // Enable HR mode |
xihan94 | 2:9dfb7ebaaa6e | 59 | // modeConf.all = 0; |
xihan94 | 2:9dfb7ebaaa6e | 60 | // modeConf.bits.mode = MAX30101::HeartRateMode; |
xihan94 | 2:9dfb7ebaaa6e | 61 | // hr.setModeConfiguration(modeConf); |
xihan94 | 2:9dfb7ebaaa6e | 62 | // printf("Mode set\r\n"); |
xihan94 | 0:33686dd26bf9 | 63 | } |
xihan94 | 0:33686dd26bf9 | 64 | |
xihan94 | 2:9dfb7ebaaa6e | 65 | // if (interruptStatus.bits.prox_int == 0x1) { |
xihan94 | 2:9dfb7ebaaa6e | 66 | // printf("Proximity Triggered, entered HR Mode."); |
xihan94 | 2:9dfb7ebaaa6e | 67 | // } |
xihan94 | 2:9dfb7ebaaa6e | 68 | // |
xihan94 | 2:9dfb7ebaaa6e | 69 | // if (interruptStatus.bits.ppg_rdy == 0x1) { |
xihan94 | 2:9dfb7ebaaa6e | 70 | // printf("PPG Ready.\r\n"); |
xihan94 | 2:9dfb7ebaaa6e | 71 | // mask_ppg = 1; |
xihan94 | 2:9dfb7ebaaa6e | 72 | // } |
xihan94 | 2:9dfb7ebaaa6e | 73 | // |
xihan94 | 2:9dfb7ebaaa6e | 74 | // if (interruptStatus.bits.a_full == 0x1) { |
xihan94 | 2:9dfb7ebaaa6e | 75 | // printf("FIFO Almost Full.\r\n"); |
xihan94 | 2:9dfb7ebaaa6e | 76 | // uint8_t data[FIFO_DATA_MAX]; |
xihan94 | 2:9dfb7ebaaa6e | 77 | // uint16_t readBytes = 0; |
xihan94 | 2:9dfb7ebaaa6e | 78 | // hr.readFIFO(MAX30101::OneLedChannel, data, readBytes); |
xihan94 | 2:9dfb7ebaaa6e | 79 | // |
xihan94 | 2:9dfb7ebaaa6e | 80 | // for (uint16_t i = 0; i < readBytes; i += 3) { |
xihan94 | 2:9dfb7ebaaa6e | 81 | // uint8_t sample[4] = {0}; |
xihan94 | 2:9dfb7ebaaa6e | 82 | // sample[0] = data[i + 2]; |
xihan94 | 2:9dfb7ebaaa6e | 83 | // sample[1] = data[i + 1]; |
xihan94 | 2:9dfb7ebaaa6e | 84 | // sample[2] = data[i]; |
xihan94 | 2:9dfb7ebaaa6e | 85 | // |
xihan94 | 2:9dfb7ebaaa6e | 86 | // printf("%u\r\n", *(uint32_t *) sample); |
xihan94 | 2:9dfb7ebaaa6e | 87 | // } |
xihan94 | 2:9dfb7ebaaa6e | 88 | // } |
xihan94 | 2:9dfb7ebaaa6e | 89 | // |
xihan94 | 2:9dfb7ebaaa6e | 90 | // interruptStatus.all = 0xFF; |
xihan94 | 2:9dfb7ebaaa6e | 91 | // if (mask_ppg == 1) { |
xihan94 | 2:9dfb7ebaaa6e | 92 | // interruptStatus.bits.ppg_rdy = 0; |
xihan94 | 2:9dfb7ebaaa6e | 93 | // } |
xihan94 | 2:9dfb7ebaaa6e | 94 | // hr.enableInterrupts(interruptStatus); |
xihan94 | 0:33686dd26bf9 | 95 | } |
xihan94 | 0:33686dd26bf9 | 96 | |
xihan94 | 0:33686dd26bf9 | 97 | void interruptHandler() { |
xihan94 | 0:33686dd26bf9 | 98 | evqueue.call(interruptHandlerQueued); |
xihan94 | 0:33686dd26bf9 | 99 | } |
xihan94 | 0:33686dd26bf9 | 100 | |
xihan94 | 0:33686dd26bf9 | 101 | // main() runs in its own thread in the OS |
xihan94 | 0:33686dd26bf9 | 102 | int main() { |
xihan94 | 0:33686dd26bf9 | 103 | printf("Hello world.\r\n"); |
xihan94 | 0:33686dd26bf9 | 104 | |
xihan94 | 0:33686dd26bf9 | 105 | t.start(callback(&evqueue, &EventQueue::dispatch_forever)); |
xihan94 | 0:33686dd26bf9 | 106 | |
xihan94 | 0:33686dd26bf9 | 107 | pwr1v8 = 1; |
xihan94 | 0:33686dd26bf9 | 108 | pwr3v3b = 1; |
xihan94 | 1:ad1b075585bc | 109 | pwr15v = 0; |
xihan94 | 0:33686dd26bf9 | 110 | |
xihan94 | 0:33686dd26bf9 | 111 | maximInterrupt.fall(interruptHandler); |
xihan94 | 0:33686dd26bf9 | 112 | maximInterrupt.enable_irq(); |
xihan94 | 0:33686dd26bf9 | 113 | |
xihan94 | 0:33686dd26bf9 | 114 | MAX30101::InterruptBitField_u interruptStatus; |
xihan94 | 0:33686dd26bf9 | 115 | interruptStatus.all = 0xFF; |
xihan94 | 0:33686dd26bf9 | 116 | hr.enableInterrupts(interruptStatus); |
xihan94 | 0:33686dd26bf9 | 117 | |
xihan94 | 0:33686dd26bf9 | 118 | return 0; |
xihan94 | 0:33686dd26bf9 | 119 | } |
xihan94 | 0:33686dd26bf9 | 120 |