Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)

Dependencies:   UniGraphic mbed vt100

18-Jun-2018 外部センサの電源オン・オフ機能は下位互換の為に無効になっていました。 この版で再度有効にしました。

Committer:
Rhyme
Date:
Mon Jun 18 01:56:00 2018 +0000
Revision:
1:8d65cfc3a2e2
Parent:
0:846e2321c637
External sensor power on/off function enabled. (Previously disabled)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:846e2321c637 1 /**
Rhyme 0:846e2321c637 2 * MMA8451Q 3-Axis, 14-bit/8-bit Digital Accelerometer
Rhyme 0:846e2321c637 3 */
Rhyme 0:846e2321c637 4
Rhyme 0:846e2321c637 5 #include "mbed.h"
Rhyme 0:846e2321c637 6 #include "MMA8451Q.h"
Rhyme 0:846e2321c637 7 #include "af_mgr.h"
Rhyme 0:846e2321c637 8
Rhyme 0:846e2321c637 9 #define REG_STATUS 0x00 // when F_MODE = 00
Rhyme 0:846e2321c637 10 #define REG_FIFO_STATUS 0x00 // when F_MODE > 0
Rhyme 0:846e2321c637 11 #define REG_XYZ_FIFO 0x01 // Root pointer to XYZ FIFO data
Rhyme 0:846e2321c637 12 #define REG_OUT_X_MSB 0x01 // 8 MSBs of 14-bit sample
Rhyme 0:846e2321c637 13 #define REG_OUT_X_LSB 0x02 // 6 LSBs of 14-bit sample
Rhyme 0:846e2321c637 14 #define REG_OUT_Y_MSB 0x03
Rhyme 0:846e2321c637 15 #define REG_OUT_Y_LSB 0x04
Rhyme 0:846e2321c637 16 #define REG_OUT_Z_MSB 0x05
Rhyme 0:846e2321c637 17 #define REG_OUT_Z_LSB 0x06
Rhyme 0:846e2321c637 18 #define REG_F_SETUP 0x09 // FIFO setup
Rhyme 0:846e2321c637 19 #define REG_TRIG_CFG 0x0A // Map of FIFO daa capture events
Rhyme 0:846e2321c637 20 #define REG_SYSMOD 0x0B // Current System Mode
Rhyme 0:846e2321c637 21 #define REG_INT_SOURCE 0x0C // Interrupt status
Rhyme 0:846e2321c637 22 #define REG_WHO_AM_I 0x0D // Device ID (0x1A)
Rhyme 0:846e2321c637 23 #define REG_XYZ_DATA_CFG 0x0E // Dynamic Range Settings
Rhyme 0:846e2321c637 24 #define REG_HP_FILTER_CUTOFF 0x0F // Cutoff freq is set to 16Hz@800Hz
Rhyme 0:846e2321c637 25 #define REG_PL_STATUS 0x10 // Landscape/Portrait orientation status
Rhyme 0:846e2321c637 26 #define REG_PL_CFG 0x11 // Landscape/Portrait configuration
Rhyme 0:846e2321c637 27 #define REG_PL_COUNT 0x12 // Landscape/Portrait debounce counter
Rhyme 0:846e2321c637 28 #define REG_PL_BF_ZCOMP 0x13 // Back/Front, Z-Lock Trip threshold
Rhyme 0:846e2321c637 29 #define REG_P_L_THS_REG 0x14 // Portrait to Landscape Trip Angle is 29 degree
Rhyme 0:846e2321c637 30 #define REG_FF_MT_CFG 0x15 // Freefall/Motion function block configuration
Rhyme 0:846e2321c637 31 #define REG_FF_MT_SRC 0x16 // Freefall/Motion event source register
Rhyme 0:846e2321c637 32 #define REG_FF_MT_THS 0x17 // Freefall/Motion threshold register
Rhyme 0:846e2321c637 33 #define REG_FF_MT_COUNT 0x18 // Freefall/Motion debounce counter
Rhyme 0:846e2321c637 34 // TRANSIENT
Rhyme 0:846e2321c637 35 #define REG_TRANSIENT_CFG 0x1D // Transient functional block configuration
Rhyme 0:846e2321c637 36 #define REG_TRANSIENT_SRC 0x1E // Transient event status register
Rhyme 0:846e2321c637 37 #define REG_TRANSIENT_THS 0x1F // Transient event threshold
Rhyme 0:846e2321c637 38 #define REG_TRANSIENT_COUNT 0x20 // Transient debounce counter
Rhyme 0:846e2321c637 39 // PULSE
Rhyme 0:846e2321c637 40 #define REG_PULSE_CFG 0x21 // ELE, Double_XYZ or Single_XYZ
Rhyme 0:846e2321c637 41 #define REG_PULSE_SRC 0x22 // EA, Double_XYZ or Single_XYZ
Rhyme 0:846e2321c637 42 #define REG_PULSE_THSX 0x23 // X pulse threshold
Rhyme 0:846e2321c637 43 #define REG_PULSE_THSY 0x24 // Y pulse threshold
Rhyme 0:846e2321c637 44 #define REG_PULSE_THSZ 0x25 // Z pulse threshold
Rhyme 0:846e2321c637 45 #define REG_PULSE_TMLT 0x26 // Time limit for pulse
Rhyme 0:846e2321c637 46 #define REG_PULSE_LTCY 0x27 // Latency time for 2nd pulse
Rhyme 0:846e2321c637 47 #define REG_PULSE_WIND 0x28 // Window time for 2nd pulse
Rhyme 0:846e2321c637 48 #define REG_ASLP_COUNT 0x29 // Counter setting for Auto-SLEEP
Rhyme 0:846e2321c637 49 // Control Registers
Rhyme 0:846e2321c637 50 #define REG_CTRL_REG1 0x2A // ODR = 800Hz, STANDBY Mode
Rhyme 0:846e2321c637 51 #define REG_CTRL_REG2 0x2B // Sleep Enable, OS Modes, RST, ST
Rhyme 0:846e2321c637 52 #define REG_CTRL_REG3 0x2C // Wake from Sleep, IPOL, PP_OD
Rhyme 0:846e2321c637 53 #define REG_CTRL_REG4 0x2D // Interrupt enable register
Rhyme 0:846e2321c637 54 #define REG_CTRL_REG5 0x2E // Interrupt pin (INT1/INT2) map
Rhyme 0:846e2321c637 55 // User Offset
Rhyme 0:846e2321c637 56 #define REG_OFF_X 0x2F // X-axis offset adjust
Rhyme 0:846e2321c637 57 #define REG_OFF_Y 0x30 // Y-axis offset adjust
Rhyme 0:846e2321c637 58 #define REG_OFF_Z 0x31 // Z-axis offset adjust
Rhyme 0:846e2321c637 59
Rhyme 0:846e2321c637 60 // Value definitions
Rhyme 0:846e2321c637 61 #define BIT_TRIG_TRANS 0x20 // Transient interrupt trigger bit
Rhyme 0:846e2321c637 62 #define BIT_TRIG_LNDPRT 0x10 // Landscape/Portrati Orientation
Rhyme 0:846e2321c637 63 #define BIT_TRIG_PULSE 0x08 // Pulse interrupt trigger bit
Rhyme 0:846e2321c637 64 #define BIT_TRIG_FF_MT 0x04 // Freefall/Motion trigger bit
Rhyme 0:846e2321c637 65
Rhyme 0:846e2321c637 66 MMA8451Q::MMA8451Q(I2C *i2c, int addr) : m_addr(addr<<1) {
Rhyme 0:846e2321c637 67 // activate the peripheral
Rhyme 0:846e2321c637 68 p_i2c = i2c ;
Rhyme 0:846e2321c637 69 uint8_t data[2] = {REG_CTRL_REG1, 0x01};
Rhyme 0:846e2321c637 70 writeRegs(data, 2);
Rhyme 0:846e2321c637 71 }
Rhyme 0:846e2321c637 72
Rhyme 0:846e2321c637 73 MMA8451Q::~MMA8451Q() { }
Rhyme 0:846e2321c637 74
Rhyme 0:846e2321c637 75 int MMA8451Q::readRegs(int addr, uint8_t * data, int len)
Rhyme 0:846e2321c637 76 {
Rhyme 0:846e2321c637 77 char t[1] = {addr};
Rhyme 0:846e2321c637 78 int result ;
Rhyme 0:846e2321c637 79 __disable_irq() ; // Disable Interrupts
Rhyme 0:846e2321c637 80 result = p_i2c->write(m_addr, t, 1, true);
Rhyme 0:846e2321c637 81 if (result == 0) {
Rhyme 0:846e2321c637 82 result = p_i2c->read(m_addr, (char *)data, len);
Rhyme 0:846e2321c637 83 }
Rhyme 0:846e2321c637 84 __enable_irq() ; // Enable Interrupts
Rhyme 0:846e2321c637 85 return( result ) ;
Rhyme 0:846e2321c637 86 }
Rhyme 0:846e2321c637 87
Rhyme 0:846e2321c637 88 int MMA8451Q::writeRegs(uint8_t * data, int len)
Rhyme 0:846e2321c637 89 {
Rhyme 0:846e2321c637 90 int result ;
Rhyme 0:846e2321c637 91 __disable_irq() ; // Disable Interrupts
Rhyme 0:846e2321c637 92 result = p_i2c->write(m_addr, (char *)data, len);
Rhyme 0:846e2321c637 93 __enable_irq() ; // Enable Interrupts
Rhyme 0:846e2321c637 94 return( result ) ;
Rhyme 0:846e2321c637 95 }
Rhyme 0:846e2321c637 96
Rhyme 0:846e2321c637 97 int MMA8451Q::getAllRawData(int16_t value[])
Rhyme 0:846e2321c637 98 {
Rhyme 0:846e2321c637 99 int result ;
Rhyme 0:846e2321c637 100 uint8_t data[6] ;
Rhyme 0:846e2321c637 101 result = readRegs(REG_OUT_X_MSB, data, 6) ;
Rhyme 0:846e2321c637 102 if (result == 0) {
Rhyme 0:846e2321c637 103 value[0] = ((int16_t)((data[0] << 8) | data[1])) >> 2 ;
Rhyme 0:846e2321c637 104 value[1] = ((int16_t)((data[2] << 8) | data[3])) >> 2 ;
Rhyme 0:846e2321c637 105 value[2] = ((int16_t)((data[4] << 8) | data[5])) >> 2 ;
Rhyme 0:846e2321c637 106 }
Rhyme 0:846e2321c637 107 return( result ) ;
Rhyme 0:846e2321c637 108 }
Rhyme 0:846e2321c637 109
Rhyme 0:846e2321c637 110 int MMA8451Q::getAllData(float fvalue[])
Rhyme 0:846e2321c637 111 {
Rhyme 0:846e2321c637 112 int result ;
Rhyme 0:846e2321c637 113 uint8_t data[6] ;
Rhyme 0:846e2321c637 114 result = readRegs(REG_OUT_X_MSB, data, 6) ;
Rhyme 0:846e2321c637 115 if (result == 0) {
Rhyme 0:846e2321c637 116 fvalue[0] = (float)((int16_t)((data[0] << 8) | data[1])) / 16384.0 ;
Rhyme 0:846e2321c637 117 fvalue[1] = (float)((int16_t)((data[2] << 8) | data[3])) / 16384.0 ;
Rhyme 0:846e2321c637 118 fvalue[2] = (float)((int16_t)((data[4] << 8) | data[5])) / 16384.0 ;
Rhyme 0:846e2321c637 119 }
Rhyme 0:846e2321c637 120 return( result ) ;
Rhyme 0:846e2321c637 121 }
Rhyme 0:846e2321c637 122
Rhyme 0:846e2321c637 123 int16_t MMA8451Q::getRawData(uint8_t addr)
Rhyme 0:846e2321c637 124 {
Rhyme 0:846e2321c637 125 int16_t value ;
Rhyme 0:846e2321c637 126 uint8_t data[2] ;
Rhyme 0:846e2321c637 127 readRegs(addr, data, 2) ;
Rhyme 0:846e2321c637 128 value = ((int16_t)((data[0] << 8) | data[1])) >> 2 ;
Rhyme 0:846e2321c637 129 return( value ) ;
Rhyme 0:846e2321c637 130 }
Rhyme 0:846e2321c637 131
Rhyme 0:846e2321c637 132 int16_t MMA8451Q::getRawX(void)
Rhyme 0:846e2321c637 133 {
Rhyme 0:846e2321c637 134 int16_t value ;
Rhyme 0:846e2321c637 135 value = getRawData(REG_OUT_X_MSB) ;
Rhyme 0:846e2321c637 136 return( value ) ;
Rhyme 0:846e2321c637 137 }
Rhyme 0:846e2321c637 138
Rhyme 0:846e2321c637 139 int16_t MMA8451Q::getRawY(void)
Rhyme 0:846e2321c637 140 {
Rhyme 0:846e2321c637 141 int16_t value ;
Rhyme 0:846e2321c637 142 value = getRawData(REG_OUT_Y_MSB) ;
Rhyme 0:846e2321c637 143 return( value ) ;
Rhyme 0:846e2321c637 144 }
Rhyme 0:846e2321c637 145
Rhyme 0:846e2321c637 146 int16_t MMA8451Q::getRawZ(void)
Rhyme 0:846e2321c637 147 {
Rhyme 0:846e2321c637 148 int16_t value ;
Rhyme 0:846e2321c637 149 value = getRawData(REG_OUT_Z_MSB) ;
Rhyme 0:846e2321c637 150 return( value ) ;
Rhyme 0:846e2321c637 151 }
Rhyme 0:846e2321c637 152
Rhyme 0:846e2321c637 153 float MMA8451Q::getAccX(void)
Rhyme 0:846e2321c637 154 {
Rhyme 0:846e2321c637 155 return(((float)getRawX())/4096.0) ;
Rhyme 0:846e2321c637 156 }
Rhyme 0:846e2321c637 157
Rhyme 0:846e2321c637 158 float MMA8451Q::getAccY(void)
Rhyme 0:846e2321c637 159 {
Rhyme 0:846e2321c637 160 return(((float)getRawY())/4096.0) ;
Rhyme 0:846e2321c637 161 }
Rhyme 0:846e2321c637 162
Rhyme 0:846e2321c637 163 float MMA8451Q::getAccZ(void)
Rhyme 0:846e2321c637 164 {
Rhyme 0:846e2321c637 165 return(((float)getRawZ())/4096.0) ;
Rhyme 0:846e2321c637 166 }