Version FC
Dependencies: DmTftLibrary eeprom SX1280Lib filesystem mbed
Fork of MSNV2-Terminal_V1-5 by
Diff: COMPONENTS/lsm303d_component.hpp
- Revision:
- 31:2b8b98f3feed
- Parent:
- 26:271d2d510f6c
- Child:
- 32:3bef9b81f639
diff -r 20f58f30113d -r 2b8b98f3feed COMPONENTS/lsm303d_component.hpp --- a/COMPONENTS/lsm303d_component.hpp Tue Sep 18 20:18:44 2018 +0000 +++ b/COMPONENTS/lsm303d_component.hpp Wed Oct 03 17:22:58 2018 +0000 @@ -1,206 +1,214 @@ -/* - * lsm303d_component.hpp - * - * Created on: 18 sept. 2018 - * Author: hoel - */ - -#ifndef COMPONENTS_LSM303D_COMPONENT_HPP_ -#define COMPONENTS_LSM303D_COMPONENT_HPP_ - -#include <iostream> -#include <cstdlib> -#include "mbed.h" -#include "Context.h" -#include "Service.hpp" -#include "Component.hpp" - -using namespace std; -using namespace misnet; - -#define MAG_ADDRESS 0x3C -#define ACC_ADDRESS 0x32 - -#define CTRL_REG1_A 0x20 -#define CTRL_REG2_A 0x21 -#define CTRL_REG3_A 0x22 -#define CTRL_REG4_A 0x23 -#define CTRL_REG5_A 0x24 -#define CTRL_REG6_A 0x25 // DLHC only -#define REFERENCE_A 0x26 -#define STATUS_REG_A 0x27 - -#define OUT_X_L_A 0x28 -#define OUT_X_H_A 0x29 -#define OUT_Y_L_A 0x2A -#define OUT_Y_H_A 0x2B -#define OUT_Z_L_A 0x2C -#define OUT_Z_H_A 0x2D - -#define INT1_CFG_A 0x30 -#define INT1_SRC_A 0x31 -#define INT1_THS_A 0x32 -#define INT1_DURATION_A 0x33 -#define INT2_CFG_A 0x34 -#define INT2_SRC_A 0x35 -#define INT2_THS_A 0x36 -#define INT2_DURATION_A 0x37 - -#define CRA_REG_M 0x00 -#define CRB_REG_M 0x01 -#define MR_REG_M 0x02 - -#define OUT_X_H_M 0x03 -#define OUT_X_L_M 0x04 -#define OUT_Y_H_M 0x07 -#define OUT_Y_L_M 0x08 -#define OUT_Z_H_M 0x05 -#define OUT_Z_L_M 0x06 - -#define SR_REG_M 0x09 -#define IRA_REG_M 0x0A -#define IRB_REG_M 0x0B -#define IRC_REG_M 0x0C - -class LSM303DLHC_component : public Component{ - - public: - LSM303DLHC_component(COMPONENT_ID id, vector<Service*>& services, PinName sda, PinName scl): - _device(sda, scl) - { - this->setServices(services); - this->setId(id); - _device.frequency(400000); - } - ~LSM303DLHC_component(); - void init(){ - // init mag - // continuous conversion mode - _data[0] = MR_REG_M; - _data[1] = 0x00; - _device.write(MAG_ADDRESS, _data, 2); - // data rate 75hz - _data[0] = CRA_REG_M; - _data[1] = 0x18; // 0b00011000 - _device.write(MAG_ADDRESS, _data, 2); - // init acc - // data rate 100hz - _data[0] = CTRL_REG1_A; - _data[1] = 0x2F; // 0b00101111 - _device.write(ACC_ADDRESS, _data, 2); - } - - private: - void setScale(float x, float y, float z){ - scale[0] = x; - scale[1] = y; - scale[2] = z; - } - void setOffset(float x, float y, float z){ - offset[0] = x; - offset[1] = y; - offset[2] = z; - } - - I2C _device; - char _data[6]; - int offset[3], scale[3]; - - class LSM303DLHC_accelerometer : public Service{ - - public: - LSM303DLHC_accelerometer( - DEVICE_TYPE type, - MISNET_CODE misnet_code, - STATE state, - ACCESS_TYPE access_type, - REQUEST_MODE request_mode, - UP_MODE up_mode, - ACCESS_PIN access_pins[6], - uint32_t subsample_rate, - ACTION action, - OUTPUT_MODE output_mode, - string comment, - LSM303DLHC_component* parent - ) - { - this->parent = parent; - this->setDeviceType(type); - this->setMisnetCode(misnet_code); - this->setState(state); - this->setAccessType(access_type); - this->setRequestMode(request_mode); - this->setUpMode(up_mode); - this->setAction(action); - this->setOutputMode(output_mode); - this->setComment(comment); - this->setSubsampleRate(subsample_rate); - } - virtual ~LSM303DLHC_accelerometer() {} - int* readValue(void) { - int a[3]; - parent->_data[0] = OUT_X_L_A | (1<<7); - parent->_device.write(ACC_ADDRESS, _data, 1); - parent->_device.read(ACC_ADDRESS, _data, 6); - - // 12-bit values - a[0] = (short)(parent->_data[1]<<8 | parent->_data[0]) >> 4; - a[1] = (short)(parent->_data[3]<<8 | parent->_data[2]) >> 4; - a[2] = (short)(parent->_data[5]<<8 | parent->_data[4]) >> 4; - return a; - } - - private: - LSM303DLHC_component *parent; - }; - - class LSM303DLHC_megnetometer : public Service{ - - public: - LSM303DLHC_megnetometer( - DEVICE_TYPE type, - MISNET_CODE misnet_code, - STATE state, - ACCESS_TYPE access_type, - REQUEST_MODE request_mode, - UP_MODE up_mode, - ACCESS_PIN access_pins[6], - uint32_t subsample_rate, - ACTION action, - OUTPUT_MODE output_mode, - string comment, - LSM303DLHC_component* parent - ) - { - this->parent = parent; - this->setDeviceType(type); - this->setMisnetCode(misnet_code); - this->setState(state); - this->setAccessType(access_type); - this->setRequestMode(request_mode); - this->setUpMode(up_mode); - this->setAction(action); - this->setOutputMode(output_mode); - this->setComment(comment); - this->setSubsampleRate(subsample_rate); - } - virtual ~LSM303DLHC_megnetometer() {} - int* readValue(void) { - int a[3]; - parent->_data[0] = OUT_X_H_M; - parent->_device.write(MAG_ADDRESS, parent->_data, 1); - parent->_device.read(MAG_ADDRESS, parent->_data, 6); - - a[0] = (short) (parent->_data[0]<<8 | parent->_data[1]); // X - a[1] = (short) (parent->_data[4]<<8 | parent->_data[5]); // Y - a[2] = (short) (parent->_data[2]<<8 | parent->_data[3]); // Z - return a; - } - - private: - LSM303DLHC_component *parent; - }; -}; - -#endif /* COMPONENTS_LSM303D_COMPONENT_HPP_ */ +/* + * lsm303d_component.hpp + * + * Created on: 18 sept. 2018 + * Author: hoel + */ + +#ifndef COMPONENTS_LSM303D_COMPONENT_HPP_ +#define COMPONENTS_LSM303D_COMPONENT_HPP_ + +#include <iostream> +#include <cstdlib> +#include "mbed.h" +#include "Context.h" +#include "Service.hpp" +#include "Component.hpp" + +using namespace std; +using namespace misnet; + +#define MAG_ADDRESS 0x3C +#define ACC_ADDRESS 0x32 + +#define CTRL_REG1_A 0x20 +#define CTRL_REG2_A 0x21 +#define CTRL_REG3_A 0x22 +#define CTRL_REG4_A 0x23 +#define CTRL_REG5_A 0x24 +#define CTRL_REG6_A 0x25 // DLHC only +#define REFERENCE_A 0x26 +#define STATUS_REG_A 0x27 + +#define OUT_X_L_A 0x28 +#define OUT_X_H_A 0x29 +#define OUT_Y_L_A 0x2A +#define OUT_Y_H_A 0x2B +#define OUT_Z_L_A 0x2C +#define OUT_Z_H_A 0x2D + +#define INT1_CFG_A 0x30 +#define INT1_SRC_A 0x31 +#define INT1_THS_A 0x32 +#define INT1_DURATION_A 0x33 +#define INT2_CFG_A 0x34 +#define INT2_SRC_A 0x35 +#define INT2_THS_A 0x36 +#define INT2_DURATION_A 0x37 + +#define CRA_REG_M 0x00 +#define CRB_REG_M 0x01 +#define MR_REG_M 0x02 + +#define OUT_X_H_M 0x03 +#define OUT_X_L_M 0x04 +#define OUT_Y_H_M 0x07 +#define OUT_Y_L_M 0x08 +#define OUT_Z_H_M 0x05 +#define OUT_Z_L_M 0x06 + +#define SR_REG_M 0x09 +#define IRA_REG_M 0x0A +#define IRB_REG_M 0x0B +#define IRC_REG_M 0x0C + +class LSM303DLHC_component : public Component{ + public: + LSM303DLHC_component(COMPONENT_ID id, vector<Service*>& services, PinName sda, PinName scl): + _device(sda, scl) + { + this->setServices(services); + this->setId(id); + _device.frequency(400000); + } + ~LSM303DLHC_component(); + void init(){ + // init mag + // continuous conversion mode + _data[0] = MR_REG_M; + _data[1] = 0x00; + _device.write(MAG_ADDRESS, _data, 2); + // data rate 75hz + _data[0] = CRA_REG_M; + _data[1] = 0x18; // 0b00011000 + _device.write(MAG_ADDRESS, _data, 2); + // init acc + // data rate 100hz + _data[0] = CTRL_REG1_A; + _data[1] = 0x2F; // 0b00101111 + _device.write(ACC_ADDRESS, _data, 2); + } + void readValues(void){ + for (std::vector<misnet::Service*>::iterator + srvIt = this->getServices().begin(); + srvIt != this->getServices().end(); + srvIt++) { + (*srvIt)->readValue(); + } + } + + private: + void setScale(float x, float y, float z){ + scale[0] = x; + scale[1] = y; + scale[2] = z; + } + void setOffset(float x, float y, float z){ + offset[0] = x; + offset[1] = y; + offset[2] = z; + } + + I2C _device; + char _data[6]; + int offset[3], scale[3]; + + class LSM303DLHC_accelerometer : public Service{ + + public: + LSM303DLHC_accelerometer( + DEVICE_TYPE type, + MISNET_CODE misnet_code, + STATE state, + ACCESS_TYPE access_type, + REQUEST_MODE request_mode, + UP_MODE up_mode, + ACCESS_PIN access_pins[6], + uint32_t subsample_rate, + ACTION action, + OUTPUT_MODE output_mode, + string comment, + LSM303DLHC_component* parent + ) + { + this->parent = parent; + this->setDeviceType(type); + this->setMisnetCode(misnet_code); + this->setState(state); + this->setAccessType(access_type); + this->setRequestMode(request_mode); + this->setUpMode(up_mode); + this->setAction(action); + this->setOutputMode(output_mode); + this->setComment(comment); + this->setSubsampleRate(subsample_rate); + } + virtual ~LSM303DLHC_accelerometer() {} + int* readValue(void) { + int a[3]; + parent->_data[0] = OUT_X_L_A | (1<<7); + parent->_device.write(ACC_ADDRESS, _data, 1); + parent->_device.read(ACC_ADDRESS, _data, 6); + + // 12-bit values + a[0] = (short)(parent->_data[1]<<8 | parent->_data[0]) >> 4; + a[1] = (short)(parent->_data[3]<<8 | parent->_data[2]) >> 4; + a[2] = (short)(parent->_data[5]<<8 | parent->_data[4]) >> 4; + return a; + } + + private: + LSM303DLHC_component *parent; + }; + + class LSM303DLHC_megnetometer : public Service{ + + public: + LSM303DLHC_megnetometer( + DEVICE_TYPE type, + MISNET_CODE misnet_code, + STATE state, + ACCESS_TYPE access_type, + REQUEST_MODE request_mode, + UP_MODE up_mode, + ACCESS_PIN access_pins[6], + uint32_t subsample_rate, + ACTION action, + OUTPUT_MODE output_mode, + string comment, + LSM303DLHC_component* parent + ) + { + this->parent = parent; + this->setDeviceType(type); + this->setMisnetCode(misnet_code); + this->setState(state); + this->setAccessType(access_type); + this->setRequestMode(request_mode); + this->setUpMode(up_mode); + this->setAction(action); + this->setOutputMode(output_mode); + this->setComment(comment); + this->setSubsampleRate(subsample_rate); + } + virtual ~LSM303DLHC_megnetometer() {} + int* readValue(void) { + int a[3]; + parent->_data[0] = OUT_X_H_M; + parent->_device.write(MAG_ADDRESS, parent->_data, 1); + parent->_device.read(MAG_ADDRESS, parent->_data, 6); + + a[0] = (short) (parent->_data[0]<<8 | parent->_data[1]); // X + a[1] = (short) (parent->_data[4]<<8 | parent->_data[5]); // Y + a[2] = (short) (parent->_data[2]<<8 | parent->_data[3]); // Z + return a; + } + + private: + LSM303DLHC_component *parent; + }; +}; + +#endif /* COMPONENTS_LSM303D_COMPONENT_HPP_ */ +