lora sensnode

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_LoRa_Sensornode by Adrian Mitevski

Committer:
socie123
Date:
Wed Aug 10 12:54:10 2016 +0000
Revision:
1:e67174cc4953
Parent:
0:f2815503561f
lora sensnode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mitea1 0:f2815503561f 1 /*
mitea1 0:f2815503561f 2 * I2C_RT.cpp
mitea1 0:f2815503561f 3 *
mitea1 0:f2815503561f 4 * Created on: 19.05.2016
mitea1 0:f2815503561f 5 * Author: Adrian
mitea1 0:f2815503561f 6 */
mitea1 0:f2815503561f 7
mitea1 0:f2815503561f 8 #include "I2C_RT.h"
mitea1 0:f2815503561f 9
mitea1 0:f2815503561f 10 I2C_RT::I2C_RT(){
mitea1 0:f2815503561f 11
mitea1 0:f2815503561f 12 }
mitea1 0:f2815503561f 13
mitea1 0:f2815503561f 14 I2C_RT::~I2C_RT() {
mitea1 0:f2815503561f 15
mitea1 0:f2815503561f 16 }
mitea1 0:f2815503561f 17
mitea1 0:f2815503561f 18
mitea1 0:f2815503561f 19 void I2C_RT::read_RT(uint8_t address,uint16_t reg,bool isTwoBytes,uint8_t*data,uint8_t dataLenght){
mitea1 0:f2815503561f 20
mitea1 0:f2815503561f 21 mbed::I2C i2c(I2C_SDA,I2C_SCL);
mitea1 0:f2815503561f 22 i2c.frequency(100000);
mitea1 0:f2815503561f 23
mitea1 0:f2815503561f 24 // Some Cast
mitea1 0:f2815503561f 25 //TODO find a more proper solution
mitea1 0:f2815503561f 26 int sensorAddress = (int)address;
mitea1 0:f2815503561f 27 char dataToWrite[2] = {(char)(reg>>8),(char)reg};
mitea1 0:f2815503561f 28 char* readData = (char*) data;
mitea1 0:f2815503561f 29
mitea1 0:f2815503561f 30 if(isTwoBytes){
mitea1 0:f2815503561f 31 i2c.write(sensorAddress,dataToWrite,dataLenght);
mitea1 0:f2815503561f 32 }
mitea1 0:f2815503561f 33 else{
mitea1 0:f2815503561f 34 i2c.write(sensorAddress,&dataToWrite[1],dataLenght);
mitea1 0:f2815503561f 35 }
mitea1 0:f2815503561f 36
mitea1 0:f2815503561f 37 i2c.read(address,readData,dataLenght);
mitea1 0:f2815503561f 38
mitea1 0:f2815503561f 39 }
mitea1 0:f2815503561f 40 void I2C_RT::write_RT(uint8_t address,uint16_t reg,bool isTwoBytes,uint8_t*data,uint8_t dataLenght){
mitea1 0:f2815503561f 41 mbed::I2C i2c(I2C_SDA,I2C_SCL);
mitea1 0:f2815503561f 42 i2c.frequency(100000);
mitea1 0:f2815503561f 43
mitea1 0:f2815503561f 44 // Some Cast
mitea1 0:f2815503561f 45 //TODO find a more proper solution
mitea1 0:f2815503561f 46 int sensorAddress = (int) address;
mitea1 0:f2815503561f 47 char dataToWrite[3] = {(char)(reg>>8),(char)reg,(char)*data};
mitea1 0:f2815503561f 48
mitea1 0:f2815503561f 49 if(isTwoBytes){
mitea1 0:f2815503561f 50 i2c.write(sensorAddress,&dataToWrite[0],3);
mitea1 0:f2815503561f 51 }
mitea1 0:f2815503561f 52 else{
mitea1 0:f2815503561f 53 i2c.write(sensorAddress,&dataToWrite[1],2);
mitea1 0:f2815503561f 54 }
mitea1 0:f2815503561f 55
mitea1 0:f2815503561f 56 }
mitea1 0:f2815503561f 57