added init function from NXP Rapid ioT SDK, read_proximity_sensors function now works well.
Dependents: rIoTwear-touch rIoTwear-snake
Diff: sx9500.cpp
- Revision:
- 0:d46a1b9267a3
- Child:
- 1:aa30dc96dc77
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sx9500.cpp Wed Mar 18 00:57:23 2015 +0000 @@ -0,0 +1,79 @@ +#include "sx9500.h" + +#define SX9500_I2C_ADDRESS 0x50 //0x28 + +SX9500::SX9500(I2C& r, PinName en_pin) : m_i2c(r), m_txen(en_pin) +{ +} + +SX9500::~SX9500() +{ +} + +void SX9500::reset() +{ + write(SX9500_REG_RESET, SX9500_RESET_CMD); +} + +uint16_t SX9500::get_sensor() +{ + //uint16_t offset; + uint8_t status; + uint8_t buf[2]; + + m_txen = 1; + + write(SX9500_REG_IRQMSK, 0x10); + write(SX9500_REG_IRQSRC, 0x10); + + do { + status = read_single(SX9500_REG_IRQSRC); + } while (!(status & 0x10)); + + read(SX9500_REG_OFFSETMSB, buf, 2); + /*printf("MSB:%x\n", read_single(SX9500_REG_OFFSETMSB)); + printf("LSB:%x\n", read_single(SX9500_REG_OFFSETLSB));*/ + return (buf[0] << 8) + buf[1]; +} + +void SX9500::write(uint8_t addr, uint8_t data) +{ + uint8_t cmd[2]; + + cmd[0] = addr; + cmd[1] = data; + + if (m_i2c.write(SX9500_I2C_ADDRESS, (char *)cmd, 2)) + printf("SX9500 write-fail\n"); +} + +void SX9500::read(uint8_t addr, uint8_t *dst_buf, int length) +{ + char cmd[2]; + + cmd[0] = addr; + if (m_i2c.write(SX9500_I2C_ADDRESS, cmd, 1, true)) + printf("SX9500 write-fail\n"); + if (m_i2c.read(SX9500_I2C_ADDRESS, (char *)dst_buf, length)) + printf("SX9500 read-fail\n"); +} + +uint8_t SX9500::read_single(uint8_t addr) +{ + char cmd[2]; + + cmd[0] = addr; + if (m_i2c.write(SX9500_I2C_ADDRESS, cmd, 1, true)) + printf("SX9500 write-fail\n"); + if (m_i2c.read(SX9500_I2C_ADDRESS, cmd, 1)) + printf("SX9500 read-fail\n"); + + return cmd[0]; +} + +void SX9500::standby(void) +{ + m_txen = 0; + write(SX9500_REG_PROXCTRL0, 0); // turn off all sensor pins +} +