mma8451q driver with a few minor modifications
Fork of lib_mma8451q by
Diff: mma8451q.cpp
- Revision:
- 3:96faac0d688e
- Parent:
- 2:4bc96749141e
- Child:
- 4:b06535efa239
- Child:
- 5:7bcda574c0fa
--- a/mma8451q.cpp Tue Sep 01 00:27:13 2015 +0000 +++ b/mma8451q.cpp Mon Mar 07 15:21:11 2016 -0500 @@ -10,7 +10,6 @@ */ #define MMA8451_I2C_ADDRESS 0x38 //0x1C - MMA8451Q::MMA8451Q(I2C& r, DigitalIn& int_pin) : m_i2c(r), m_int_pin(int_pin) { /* INT pins on this chip default to push-pull output */ @@ -49,17 +48,17 @@ void MMA8451Q::print_regs() { - printf("ID: %02x\n", read_single(MMA8451_ID)); - printf("sysmod:%02x\n", read_single(MMA8451_SYSMOD)); + printf("ID: %02x\r\n", read_single(MMA8451_ID)); + printf("sysmod:%02x\r\n", read_single(MMA8451_SYSMOD)); ctrl_reg1.octet = read_single(MMA8451_CTRL_REG1); - printf("ctrl_reg1:%02x\n", ctrl_reg1.octet); - printf("ctrl_reg2:%02x\n", read_single(MMA8451_CTRL_REG2)); - printf("ctrl_reg3:%02x\n", read_single(MMA8451_CTRL_REG3)); /* TODO: PP_OD is bit 0 (1=open drain) */ - printf("(int en) ctrl_reg4:%02x\n", read_single(MMA8451_CTRL_REG4)); - printf("(int cfg) ctrl_reg5:%02x\n", read_single(MMA8451_CTRL_REG5)); - printf("status:%02x\n", read_single(MMA8451_STATUS)); + printf("ctrl_reg1:%02x\r\n", ctrl_reg1.octet); + printf("ctrl_reg2:%02x\r\n", read_single(MMA8451_CTRL_REG2)); + printf("ctrl_reg3:%02x\r\n", read_single(MMA8451_CTRL_REG3)); /* TODO: PP_OD is bit 0 (1=open drain) */ + printf("(int en) ctrl_reg4:%02x\r\n", read_single(MMA8451_CTRL_REG4)); + printf("(int cfg) ctrl_reg5:%02x\r\n", read_single(MMA8451_CTRL_REG5)); + printf("status:%02x\r\n", read_single(MMA8451_STATUS)); /* (interrupt status) int src at 0x0c (MMA8451_INT_SOURCE): data ready, motion/freefall, pulse, orientation, transient, auto sleep */ - printf("INT_SOURCE:%02x\n", read_single(MMA8451_INT_SOURCE)); + printf("INT_SOURCE:%02x\r\n", read_single(MMA8451_INT_SOURCE)); } void MMA8451Q::write(uint8_t addr, uint8_t data) @@ -212,6 +211,7 @@ uint8_t MMA8451Q::service() { mma_int_source_t int_src; + if (m_int_pin) return 0; // no interrupt @@ -230,28 +230,63 @@ mma_pl_status_t pl_status; /*AN4068 Step 12: Write a Service Routine to Service the Interrupt */ pl_status.octet = read_single(MMA8451_PL_STATUS); - if (verbose) { + + if(verbose) printf("PL_STATUS: "); - if (pl_status.bits.NEWLP) { - if (pl_status.bits.LO) + + if (pl_status.bits.NEWLP) + { + orientation.reset(); + + if (pl_status.bits.LO) + { + orientation.low = true; + if(verbose) printf("Z-tilt-LO "); - - if (pl_status.bits.LAPO == 0) + } + + + if (pl_status.bits.LAPO == 0) + { + orientation.up = true; + if(verbose) printf("up "); - else if (pl_status.bits.LAPO == 1) + } + else if (pl_status.bits.LAPO == 1) + { + orientation.down = true; + if(verbose) printf("down "); - else if (pl_status.bits.LAPO == 2) + } + else if (pl_status.bits.LAPO == 2) + { + orientation.left = true; + if(verbose) printf("left "); - else if (pl_status.bits.LAPO == 3) + } + else if (pl_status.bits.LAPO == 3) + { + orientation.right = true; + if(verbose) printf("right "); - - if (pl_status.bits.BAFRO) + } + + if (pl_status.bits.BAFRO) + { + orientation.back = true; + if(verbose) printf("back "); - else + } + else + { + orientation.front = true; + if(verbose) printf("front "); } + } + + if(verbose) printf("\r\n"); - } } // ...int_src.bits.SRC_LNDPRT if (int_src.bits.SRC_TRANS) { @@ -266,7 +301,7 @@ if (t_src.bits.ZTRANSE) printf("Z_Pol:%d ", t_src.bits.Z_Trans_Pol); printf("\r\n"); - } + } } // ...int_src.bits.SRC_TRANS if (int_src.bits.SRC_ASLP) {