心拍・酸素飽和度モニタモジュール MAXREFDES117から取得した心拍の値でmicro:bit上のLEDに表示したハートを鼓動させるプログラムです。
Fork of microbit-Heartbeat-serial by
MAX30102/MAX30102.cpp@2:d04db7a8d012, 2018-04-17 (annotated)
- Committer:
- JKsoft_main
- Date:
- Tue Apr 17 12:11:39 2018 +0000
- Revision:
- 2:d04db7a8d012
- Parent:
- 1:83ace7df2c63
??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JKsoft_main | 1:83ace7df2c63 | 1 | #include "mbed.h" |
JKsoft_main | 1:83ace7df2c63 | 2 | #include "MAX30102.h" |
JKsoft_main | 1:83ace7df2c63 | 3 | |
JKsoft_main | 1:83ace7df2c63 | 4 | bool MAX30102::write_reg(uint8_t uch_addr, uint8_t uch_data) |
JKsoft_main | 1:83ace7df2c63 | 5 | { |
JKsoft_main | 1:83ace7df2c63 | 6 | char ach_i2c_data[2]; |
JKsoft_main | 1:83ace7df2c63 | 7 | ach_i2c_data[0]=uch_addr; |
JKsoft_main | 1:83ace7df2c63 | 8 | ach_i2c_data[1]=uch_data; |
JKsoft_main | 1:83ace7df2c63 | 9 | |
JKsoft_main | 1:83ace7df2c63 | 10 | if(_i2c.write(I2C_ADDR, ach_i2c_data, 2, false)==0) return true; |
JKsoft_main | 1:83ace7df2c63 | 11 | else return false; |
JKsoft_main | 1:83ace7df2c63 | 12 | } |
JKsoft_main | 1:83ace7df2c63 | 13 | |
JKsoft_main | 1:83ace7df2c63 | 14 | bool MAX30102::read_reg(uint8_t uch_addr, uint8_t *puch_data) |
JKsoft_main | 1:83ace7df2c63 | 15 | { |
JKsoft_main | 1:83ace7df2c63 | 16 | char ch_i2c_data; |
JKsoft_main | 1:83ace7df2c63 | 17 | ch_i2c_data=uch_addr; |
JKsoft_main | 1:83ace7df2c63 | 18 | |
JKsoft_main | 1:83ace7df2c63 | 19 | if(_i2c.write(I2C_ADDR, &ch_i2c_data, 1, true)!=0) return false; |
JKsoft_main | 1:83ace7df2c63 | 20 | |
JKsoft_main | 1:83ace7df2c63 | 21 | if(_i2c.read(I2C_ADDR | 0x01, &ch_i2c_data, 1, false)==0) |
JKsoft_main | 1:83ace7df2c63 | 22 | { |
JKsoft_main | 1:83ace7df2c63 | 23 | *puch_data=(uint8_t) ch_i2c_data; |
JKsoft_main | 1:83ace7df2c63 | 24 | return true; |
JKsoft_main | 1:83ace7df2c63 | 25 | } |
JKsoft_main | 1:83ace7df2c63 | 26 | else |
JKsoft_main | 1:83ace7df2c63 | 27 | return false; |
JKsoft_main | 1:83ace7df2c63 | 28 | } |
JKsoft_main | 1:83ace7df2c63 | 29 | |
JKsoft_main | 1:83ace7df2c63 | 30 | bool MAX30102::init() |
JKsoft_main | 1:83ace7df2c63 | 31 | { |
JKsoft_main | 1:83ace7df2c63 | 32 | if(!write_reg(REG_INTR_ENABLE_1,0xc0)) return false; |
JKsoft_main | 1:83ace7df2c63 | 33 | if(!write_reg(REG_INTR_ENABLE_2,0x00)) return false; |
JKsoft_main | 1:83ace7df2c63 | 34 | if(!write_reg(REG_FIFO_WR_PTR,0x00)) return false; |
JKsoft_main | 1:83ace7df2c63 | 35 | if(!write_reg(REG_OVF_COUNTER,0x00)) return false; |
JKsoft_main | 1:83ace7df2c63 | 36 | if(!write_reg(REG_FIFO_RD_PTR,0x00)) return false; |
JKsoft_main | 1:83ace7df2c63 | 37 | if(!write_reg(REG_FIFO_CONFIG,0x0f)) return false; |
JKsoft_main | 1:83ace7df2c63 | 38 | if(!write_reg(REG_MODE_CONFIG,0x03)) return false; |
JKsoft_main | 1:83ace7df2c63 | 39 | if(!write_reg(REG_SPO2_CONFIG,0x27)) return false; |
JKsoft_main | 1:83ace7df2c63 | 40 | if(!write_reg(REG_LED1_PA,0x24)) return false; |
JKsoft_main | 1:83ace7df2c63 | 41 | if(!write_reg(REG_LED2_PA,0x24)) return false; |
JKsoft_main | 1:83ace7df2c63 | 42 | if(!write_reg(REG_PILOT_PA,0x7f)) return false; |
JKsoft_main | 1:83ace7df2c63 | 43 | return true; |
JKsoft_main | 1:83ace7df2c63 | 44 | } |
JKsoft_main | 1:83ace7df2c63 | 45 | |
JKsoft_main | 1:83ace7df2c63 | 46 | bool MAX30102::read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led) |
JKsoft_main | 1:83ace7df2c63 | 47 | { |
JKsoft_main | 1:83ace7df2c63 | 48 | uint32_t un_temp; |
JKsoft_main | 1:83ace7df2c63 | 49 | unsigned char uch_temp; |
JKsoft_main | 1:83ace7df2c63 | 50 | *pun_red_led=0; |
JKsoft_main | 1:83ace7df2c63 | 51 | *pun_ir_led=0; |
JKsoft_main | 1:83ace7df2c63 | 52 | char ach_i2c_data[6]; |
JKsoft_main | 1:83ace7df2c63 | 53 | |
JKsoft_main | 1:83ace7df2c63 | 54 | read_reg(REG_INTR_STATUS_1, &uch_temp); |
JKsoft_main | 1:83ace7df2c63 | 55 | read_reg(REG_INTR_STATUS_2, &uch_temp); |
JKsoft_main | 1:83ace7df2c63 | 56 | |
JKsoft_main | 1:83ace7df2c63 | 57 | ach_i2c_data[0]=REG_FIFO_DATA; |
JKsoft_main | 1:83ace7df2c63 | 58 | if(_i2c.write(I2C_ADDR, ach_i2c_data, 1, true)!=0) return false; |
JKsoft_main | 1:83ace7df2c63 | 59 | if(_i2c.read(I2C_ADDR | 0x01, ach_i2c_data, 6, false)!=0) return false; |
JKsoft_main | 1:83ace7df2c63 | 60 | |
JKsoft_main | 1:83ace7df2c63 | 61 | un_temp=(unsigned char) ach_i2c_data[0]; |
JKsoft_main | 1:83ace7df2c63 | 62 | un_temp<<=16; |
JKsoft_main | 1:83ace7df2c63 | 63 | *pun_red_led+=un_temp; |
JKsoft_main | 1:83ace7df2c63 | 64 | un_temp=(unsigned char) ach_i2c_data[1]; |
JKsoft_main | 1:83ace7df2c63 | 65 | un_temp<<=8; |
JKsoft_main | 1:83ace7df2c63 | 66 | *pun_red_led+=un_temp; |
JKsoft_main | 1:83ace7df2c63 | 67 | un_temp=(unsigned char) ach_i2c_data[2]; |
JKsoft_main | 1:83ace7df2c63 | 68 | *pun_red_led+=un_temp; |
JKsoft_main | 1:83ace7df2c63 | 69 | |
JKsoft_main | 1:83ace7df2c63 | 70 | un_temp=(unsigned char) ach_i2c_data[3]; |
JKsoft_main | 1:83ace7df2c63 | 71 | un_temp<<=16; |
JKsoft_main | 1:83ace7df2c63 | 72 | *pun_ir_led+=un_temp; |
JKsoft_main | 1:83ace7df2c63 | 73 | un_temp=(unsigned char) ach_i2c_data[4]; |
JKsoft_main | 1:83ace7df2c63 | 74 | un_temp<<=8; |
JKsoft_main | 1:83ace7df2c63 | 75 | *pun_ir_led+=un_temp; |
JKsoft_main | 1:83ace7df2c63 | 76 | un_temp=(unsigned char) ach_i2c_data[5]; |
JKsoft_main | 1:83ace7df2c63 | 77 | *pun_ir_led+=un_temp; |
JKsoft_main | 1:83ace7df2c63 | 78 | *pun_red_led&=0x03FFFF; //Mask MSB [23:18] |
JKsoft_main | 1:83ace7df2c63 | 79 | *pun_ir_led&=0x03FFFF; //Mask MSB [23:18] |
JKsoft_main | 1:83ace7df2c63 | 80 | |
JKsoft_main | 1:83ace7df2c63 | 81 | |
JKsoft_main | 1:83ace7df2c63 | 82 | return true; |
JKsoft_main | 1:83ace7df2c63 | 83 | } |
JKsoft_main | 1:83ace7df2c63 | 84 | |
JKsoft_main | 1:83ace7df2c63 | 85 | bool MAX30102::reset() |
JKsoft_main | 1:83ace7df2c63 | 86 | { |
JKsoft_main | 1:83ace7df2c63 | 87 | if(!write_reg(REG_MODE_CONFIG,0x40)) return false; |
JKsoft_main | 1:83ace7df2c63 | 88 | else return true; |
JKsoft_main | 1:83ace7df2c63 | 89 | } |