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.

MMA7660.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* Copyright (c) <year> <copyright holders>, 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 restriction,
  5. * including without limitation the rights to use, copy, modify, merge, publish, distribute,
  6. * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  7. * 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. #include "mbed.h"
  19. #ifndef MMA7660_H
  20. #define MMA7660_H
  21. #define MMA7660_ADDRESS 0x98
  22. #define MMA7660_SENSITIVITY 21.33
  23. #define MMA7660_XOUT_R 0x00
  24. #define MMA7660_YOUT_R 0x01
  25. #define MMA7660_ZOUT_R 0x02
  26. #define MMA7660_TILT_R 0x03
  27. #define MMA7660_INT_R 0x06
  28. #define MMA7660_MODE_R 0x07
  29. #define MMA7660_SR_R 0x08
  30. /** An interface for the MMA7660 triple axis accelerometer
  31. *
  32. * @code
  33. * //Uses the measured z-acceleration to drive leds 2 and 3 of the mbed
  34. *
  35. * #include "mbed.h"
  36. * #include "MMA7660.h"
  37. *
  38. * MMA7660 MMA(p28, p27);
  39. *
  40. * DigitalOut connectionLed(LED1);
  41. * PwmOut Zaxis_p(LED2);
  42. * PwmOut Zaxis_n(LED3);
  43. *
  44. * int main() {
  45. * if (MMA.testConnection())
  46. * connectionLed = 1;
  47. *
  48. * while(1) {
  49. * Zaxis_p = MMA.z();
  50. * Zaxis_n = -MMA.z();
  51. * }
  52. *
  53. * }
  54. * @endcode
  55. */
  56. class MMA7660
  57. {
  58. public:
  59. /**
  60. * The 6 different orientations and unknown
  61. *
  62. * Up & Down = X-axis
  63. * Right & Left = Y-axis
  64. * Back & Front = Z-axis
  65. *
  66. */
  67. enum Orientation {Up, Down,
  68. Right, Left,
  69. Back, Front,
  70. Unknown
  71. };
  72. /**
  73. * Creates a new MMA7660 object
  74. *
  75. * @param sda - I2C data pin
  76. * @param scl - I2C clock pin
  77. * @param active - true (default) to enable the device, false to keep it standby
  78. */
  79. MMA7660(PinName sda, PinName scl, bool active = true);
  80. /**
  81. * Tests if communication is possible with the MMA7660
  82. *
  83. * Because the MMA7660 lacks a WHO_AM_I register, this function can only check
  84. * if there is an I2C device that responds to the MMA7660 address
  85. *
  86. * @param return - true for successfull connection, false for no connection
  87. */
  88. bool testConnection( void );
  89. /**
  90. * Sets the active state of the MMA7660
  91. *
  92. * Note: This is unrelated to awake/sleep mode
  93. *
  94. * @param state - true for active, false for standby
  95. */
  96. void setActive( bool state);
  97. /**
  98. * Reads acceleration data from the sensor
  99. *
  100. * When the parameter is a pointer to an integer array it will be the raw data.
  101. * When it is a pointer to a float array it will be the acceleration in g's
  102. *
  103. * @param data - pointer to array with length 3 where the acceleration data will be stored, X-Y-Z
  104. */
  105. void readData( int *data);
  106. void readData( float *data);
  107. /**
  108. * Get X-data
  109. *
  110. * @param return - X-acceleration in g's
  111. */
  112. float x( void );
  113. /**
  114. * Get Y-data
  115. *
  116. * @param return - Y-acceleration in g's
  117. */
  118. float y( void );
  119. /**
  120. * Get Z-data
  121. *
  122. * @param return - Z-acceleration in g's
  123. */
  124. float z( void );
  125. /**
  126. * Sets the active samplerate
  127. *
  128. * The entered samplerate will be rounded to nearest supported samplerate.
  129. * Supported samplerates are: 120 - 64 - 32 - 16 - 8 - 4 - 2 - 1 samples/second.
  130. *
  131. * @param samplerate - the samplerate that will be set
  132. */
  133. void setSampleRate(int samplerate);
  134. /**
  135. * Returns if it is on its front, back, or unknown side
  136. *
  137. * This is read from MMA7760s registers, page 12 of datasheet
  138. *
  139. * @param return - Front, Back or Unknown orientation
  140. */
  141. Orientation getSide( void );
  142. /**
  143. * Returns if it is on it left, right, down or up side
  144. *
  145. * This is read from MMA7760s registers, page 12 of datasheet
  146. *
  147. * @param return - Left, Right, Down, Up or Unknown orientation
  148. */
  149. Orientation getOrientation ( void );
  150. private:
  151. /**
  152. * Writes data to the device
  153. *
  154. * @param adress - register address to write to
  155. * @param data - data to write
  156. */
  157. void write( char address, char data);
  158. /**
  159. * Read data from the device
  160. *
  161. * @param adress - register address to write to
  162. * @return - data from the register specified by RA
  163. */
  164. char read( char adress);
  165. /**
  166. * Read multiple regigsters from the device, more efficient than using multiple normal reads.
  167. *
  168. * @param adress - register address to write to
  169. * @param length - number of bytes to read
  170. * @param data - pointer where the data needs to be written to
  171. */
  172. void read( char adress, char *data, int length);
  173. /**
  174. * Reads single axis
  175. */
  176. float getSingle(int number);
  177. I2C _i2c;
  178. bool active;
  179. float samplerate;
  180. };
  181. #endif