fish

Dependencies:   mbed

Committer:
tzxl10000
Date:
Wed Jun 14 20:22:45 2017 +0000
Revision:
0:561f7672eaad
fish;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tzxl10000 0:561f7672eaad 1 /**
tzxl10000 0:561f7672eaad 2 * Copyright (c) 2011 Pololu Corporation. For more information, see
tzxl10000 0:561f7672eaad 3 *
tzxl10000 0:561f7672eaad 4 * http://www.pololu.com/
tzxl10000 0:561f7672eaad 5 * http://forum.pololu.com/
tzxl10000 0:561f7672eaad 6 *
tzxl10000 0:561f7672eaad 7 * Permission is hereby granted, free of charge, to any person
tzxl10000 0:561f7672eaad 8 * obtaining a copy of this software and associated documentation
tzxl10000 0:561f7672eaad 9 * files (the "Software"), to deal in the Software without
tzxl10000 0:561f7672eaad 10 * restriction, including without limitation the rights to use,
tzxl10000 0:561f7672eaad 11 * copy, modify, merge, publish, distribute, sublicense, and/or sell
tzxl10000 0:561f7672eaad 12 * copies of the Software, and to permit persons to whom the
tzxl10000 0:561f7672eaad 13 * Software is furnished to do so, subject to the following
tzxl10000 0:561f7672eaad 14 * conditions:
tzxl10000 0:561f7672eaad 15 *
tzxl10000 0:561f7672eaad 16 * The above copyright notice and this permission notice shall be
tzxl10000 0:561f7672eaad 17 * included in all copies or substantial portions of the Software.
tzxl10000 0:561f7672eaad 18 *
tzxl10000 0:561f7672eaad 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
tzxl10000 0:561f7672eaad 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
tzxl10000 0:561f7672eaad 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
tzxl10000 0:561f7672eaad 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
tzxl10000 0:561f7672eaad 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
tzxl10000 0:561f7672eaad 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
tzxl10000 0:561f7672eaad 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
tzxl10000 0:561f7672eaad 26 * OTHER DEALINGS IN THE SOFTWARE.
tzxl10000 0:561f7672eaad 27 */
tzxl10000 0:561f7672eaad 28
tzxl10000 0:561f7672eaad 29 #include "mbed.h"
tzxl10000 0:561f7672eaad 30 #include "L3GD20.h"
tzxl10000 0:561f7672eaad 31
tzxl10000 0:561f7672eaad 32
tzxl10000 0:561f7672eaad 33 // Defines ////////////////////////////////////////////////////////////////
tzxl10000 0:561f7672eaad 34
tzxl10000 0:561f7672eaad 35 // The Arduino two-wire interface uses a 7-bit number for the address,
tzxl10000 0:561f7672eaad 36 // and sets the last bit correctly based on reads and writes
tzxl10000 0:561f7672eaad 37 // mbed I2C libraries take the 7-bit address shifted left 1 bit
tzxl10000 0:561f7672eaad 38 // #define GYR_ADDRESS (0xD2 >> 1)
tzxl10000 0:561f7672eaad 39 #define GYR_ADDRESS 0xD6
tzxl10000 0:561f7672eaad 40
tzxl10000 0:561f7672eaad 41
tzxl10000 0:561f7672eaad 42 // Public Methods //////////////////////////////////////////////////////////////
tzxl10000 0:561f7672eaad 43
tzxl10000 0:561f7672eaad 44 // Constructor
tzxl10000 0:561f7672eaad 45 L3GD20::L3GD20(PinName sda, PinName scl):
tzxl10000 0:561f7672eaad 46 _L3GD20(sda, scl)
tzxl10000 0:561f7672eaad 47 {
tzxl10000 0:561f7672eaad 48 char reg_v;
tzxl10000 0:561f7672eaad 49 _L3GD20.frequency(100000);
tzxl10000 0:561f7672eaad 50
tzxl10000 0:561f7672eaad 51 // 0x0F = 0b00001111
tzxl10000 0:561f7672eaad 52 // Normal power mode, all axes enabled
tzxl10000 0:561f7672eaad 53 reg_v = 0;
tzxl10000 0:561f7672eaad 54 reg_v |= 0x0F;
tzxl10000 0:561f7672eaad 55 write_reg(GYR_ADDRESS,L3GD20_CTRL_REG1,reg_v);
tzxl10000 0:561f7672eaad 56
tzxl10000 0:561f7672eaad 57
tzxl10000 0:561f7672eaad 58
tzxl10000 0:561f7672eaad 59 }
tzxl10000 0:561f7672eaad 60
tzxl10000 0:561f7672eaad 61
tzxl10000 0:561f7672eaad 62
tzxl10000 0:561f7672eaad 63 bool L3GD20::read(float *gx, float *gy, float *gz) {
tzxl10000 0:561f7672eaad 64 char gyr[6];
tzxl10000 0:561f7672eaad 65
tzxl10000 0:561f7672eaad 66 if (recv(GYR_ADDRESS, L3GD20_OUT_X_L, gyr, 6)) {
tzxl10000 0:561f7672eaad 67 //scale is 8.75 mdps/digit
tzxl10000 0:561f7672eaad 68 *gx = float(short(gyr[1] << 8 | gyr[0]))*0.00875;
tzxl10000 0:561f7672eaad 69 *gy = float(short(gyr[3] << 8 | gyr[2]))*0.00875;
tzxl10000 0:561f7672eaad 70 *gz = float(short(gyr[5] << 8 | gyr[4]))*0.00875;
tzxl10000 0:561f7672eaad 71
tzxl10000 0:561f7672eaad 72
tzxl10000 0:561f7672eaad 73 return true;
tzxl10000 0:561f7672eaad 74 }
tzxl10000 0:561f7672eaad 75
tzxl10000 0:561f7672eaad 76 return false;
tzxl10000 0:561f7672eaad 77 }
tzxl10000 0:561f7672eaad 78
tzxl10000 0:561f7672eaad 79
tzxl10000 0:561f7672eaad 80
tzxl10000 0:561f7672eaad 81
tzxl10000 0:561f7672eaad 82 bool L3GD20::write_reg(int addr_i2c,int addr_reg, char v)
tzxl10000 0:561f7672eaad 83 {
tzxl10000 0:561f7672eaad 84 char data[2] = {addr_reg, v};
tzxl10000 0:561f7672eaad 85 return L3GD20::_L3GD20.write(addr_i2c, data, 2) == 0;
tzxl10000 0:561f7672eaad 86 }
tzxl10000 0:561f7672eaad 87
tzxl10000 0:561f7672eaad 88 bool L3GD20::read_reg(int addr_i2c,int addr_reg, char *v)
tzxl10000 0:561f7672eaad 89 {
tzxl10000 0:561f7672eaad 90 char data = addr_reg;
tzxl10000 0:561f7672eaad 91 bool result = false;
tzxl10000 0:561f7672eaad 92
tzxl10000 0:561f7672eaad 93 __disable_irq();
tzxl10000 0:561f7672eaad 94 if ((_L3GD20.write(addr_i2c, &data, 1) == 0) && (_L3GD20.read(addr_i2c, &data, 1) == 0)){
tzxl10000 0:561f7672eaad 95 *v = data;
tzxl10000 0:561f7672eaad 96 result = true;
tzxl10000 0:561f7672eaad 97 }
tzxl10000 0:561f7672eaad 98 __enable_irq();
tzxl10000 0:561f7672eaad 99 return result;
tzxl10000 0:561f7672eaad 100 }
tzxl10000 0:561f7672eaad 101
tzxl10000 0:561f7672eaad 102
tzxl10000 0:561f7672eaad 103 bool L3GD20::recv(char sad, char sub, char *buf, int length) {
tzxl10000 0:561f7672eaad 104 if (length > 1) sub |= 0x80;
tzxl10000 0:561f7672eaad 105
tzxl10000 0:561f7672eaad 106 return _L3GD20.write(sad, &sub, 1, true) == 0 && _L3GD20.read(sad, buf, length) == 0;
tzxl10000 0:561f7672eaad 107 }