#ifndef L3GD20_H #define L3GD20_H #include #include #include class L3GD20 { private: Pin& cs; SPI_t& spi; public: float x, y, z; L3GD20(Pin& cs_pin, SPI_t& spi_bus) : cs(cs_pin), spi(spi_bus) {} void init() { spi.reg.CR1 = (1 << 9) | (1 << 8) | (1 << 6) | (3 << 3) | (1 << 2) | (1 << 1) | (1 << 0); // SSM, SSI, SPE, 84/16MHz, MSTR, CPOL, CPHA cs.off(); spi.transfer_byte(0x40 | 0x20); spi.transfer_byte(0xff); cs.on(); } void update() { int16_t raw_x, raw_y, raw_z; spi.reg.CR1 = 0x37f; cs.off(); spi.transfer_byte(0xc0 | 0x28); raw_x = spi.transfer_byte(); raw_x |= spi.transfer_byte() << 8; raw_y = spi.transfer_byte(); raw_y |= spi.transfer_byte() << 8; raw_z = spi.transfer_byte(); raw_z |= spi.transfer_byte() << 8; cs.on(); x = float(raw_x) * (2.0 * M_PI * 250.0 / 360.0 / 32768.0); y = float(raw_y) * (2.0 * M_PI * 250.0 / 360.0 / 32768.0); z = float(raw_z) * (2.0 * M_PI * 250.0 / 360.0 / 32768.0); } }; #endif