Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MMA8451Q.h 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef MMA8451Q_H
  19. #define MMA8451Q_H
  20. #include "mbed.h"
  21. /**
  22. * MMA8451Q accelerometer example
  23. * #include "mbed.h"
  24. * #include "MMA8451Q.h"
  25. *
  26. * #define MMA8451_I2C_ADDRESS (0x1d<<1)
  27. *
  28. * int main(void) {
  29. * DigitalOut led(LED_GREEN);
  30. * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS);
  31. * printf("WHO AM I: 0x%2X\r\n", acc.getWhoAmI());
  32. *
  33. * while (true) {
  34. * printf("-----------\r\n");
  35. * printf("acc_x: %d\r\n", acc.getAccX());
  36. * printf("acc_y: %d\r\n", acc.getAccY());
  37. * printf("acc_z: %d\r\n", acc.getAccZ());
  38. *
  39. * wait(1);
  40. * led = !led;
  41. * }
  42. * }
  43. */
  44. class MMA8451Q
  45. {
  46. public:
  47. /**
  48. * MMA8451Q constructor
  49. *
  50. * @param sda SDA pin
  51. * @param sdl SCL pin
  52. * @param addr addr of the I2C peripheral
  53. */
  54. MMA8451Q(PinName sda, PinName scl, int addr);
  55. /**
  56. * MMA8451Q destructor
  57. */
  58. ~MMA8451Q();
  59. /**
  60. * Get the value of the WHO_AM_I register
  61. *
  62. * @returns WHO_AM_I value
  63. */
  64. uint8_t getWhoAmI();
  65. /**
  66. * Get X axis acceleration
  67. *
  68. * @returns X axis acceleration
  69. */
  70. int16_t getAccX();
  71. /**
  72. * Get Y axis acceleration
  73. *
  74. * @returns Y axis acceleration
  75. */
  76. int16_t getAccY();
  77. /**
  78. * Get Z axis acceleration
  79. *
  80. * @returns Z axis acceleration
  81. */
  82. int16_t getAccZ();
  83. /**
  84. * Get XYZ axis acceleration
  85. *
  86. * @param res array where acceleration data will be stored
  87. */
  88. void getAccAllAxis(int16_t * res);
  89. private:
  90. I2C m_i2c;
  91. int m_addr;
  92. void readRegs(int addr, uint8_t * data, int len);
  93. void writeRegs(uint8_t * data, int len);
  94. int16_t getAccAxis(uint8_t addr);
  95. };
  96. #endif