diff --git a/developpment/interfacer/bh1750.cpp b/developpment/interfacer/bh1750.cpp new file mode 100644 index 0000000..02dacfc --- /dev/null +++ b/developpment/interfacer/bh1750.cpp @@ -0,0 +1,61 @@ +#include "bh1750.h" + + +I2C_Driver i2c(1,1); + +Bh1750::Bh1750() +{ + currentMode = 0; // no mode selected +} + +uint8_t Bh1750::sleep() +{ + i2c.writeByte(BH1750_ADDR,BH1750_POWER_DOWN); + return 0; +} + +uint8_t Bh1750::wake() +{ + i2c.writeByte(BH1750_ADDR,BH1750_POWER_ON); + return 0; +} + +uint8_t Bh1750::reset() +{ + i2c.writeByte(BH1750_ADDR,BH1750_RESET); + return 0; +} + + +float Bh1750::oneShot(uint8_t mode) +{ + if(mode > 0) + { + if( mode == BH1750_ONE_TIME_HIGH_RES_MODE_1 || + mode == BH1750_ONE_TIME_HIGH_RES_MODE_2 || + mode == BH1750_ONE_TIME_LOW_RES_MODE) + { + return i2c.readWord(BH1750_ADDR,mode) / 1.2 ; + + } + } + std::cout<< "please seelct a one shot mode "<< std::endl; + exit(1); + return 0; +} + +float Bh1750::continious(uint8_t mode, uint8_t delayMs) +{ + if(mode > 0) + { + if( mode == BH1750_CONTINUOUS_HIGH_RES_MODE_1 || + mode == BH1750_CONTINUOUS_HIGH_RES_MODE_2 || + mode == BH1750_CONTINUOUS_LOW_RES_MODE) + { + return i2c.readWord(BH1750_ADDR,mode) / 1.2; + } + } + std::cout<< "please seelct a continious mode "<< std::endl; + exit(1); + return 0; +} diff --git a/developpment/interfacer/bh1750.h b/developpment/interfacer/bh1750.h new file mode 100644 index 0000000..65f4921 --- /dev/null +++ b/developpment/interfacer/bh1750.h @@ -0,0 +1,49 @@ + +#ifndef _BH1750_H_ +#define _BH1750_H_ + +#include +#include +#include +#include "i2c_driver.h" + + +//Start measurement at 4lx resolution. Time typically 16ms. +#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13 +//Start measurement at 1lx resolution. Time typically 120ms +#define BH1750_CONTINUOUS_HIGH_RES_MODE_1 0x10 +//Start measurement at 0.5lx resolution. Time typically 120ms +#define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11 +//Start measurement at 1lx resolution. Time typically 120ms +//Device is automatically set to Power Down after measurement. +#define BH1750_ONE_TIME_HIGH_RES_MODE_1 0x20 +//Start measurement at 0.5lx resolution. Time typically 120ms +//Device is automatically set to Power Down after measurement. +#define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21 +//Start measurement at 1lx resolution. Time typically 120ms +//Device is automatically set to Power Down after measurement. +#define BH1750_ONE_TIME_LOW_RES_MODE 0x23 + +#define BH1750_POWER_DOWN 0x00 // No active state +#define BH1750_POWER_ON 0x01 // Power on +#define BH1750_RESET 0x07 // Reset data register value + +#define BH1750_ADDR 0x23 // Device Adress + +class Bh1750 +{ + public: + Bh1750(); + uint8_t sleep(); // To be testes + uint8_t wake(); // To be tested + uint8_t reset(); // To be tested + float oneShot(uint8_t mode); // ok + float continious(uint8_t mode, uint8_t delayMs); // IMplment delay or make a delay class ??? + private: + uint8_t high; + uint8_t low; + uint8_t currentMode; + uint8_t currentState; +}; + +#endif // _BH1750_H_ diff --git a/developpment/interfacer/bh1750.py b/developpment/interfacer/bh1750.py new file mode 100644 index 0000000..9b85f24 --- /dev/null +++ b/developpment/interfacer/bh1750.py @@ -0,0 +1,69 @@ +#!/usr/bin/python +#--------------------------------------------------------------------- +# ___ ___ _ ____ +# / _ \/ _ \(_) __/__ __ __ +# / , _/ ___/ /\ \/ _ \/ // / +# /_/|_/_/ /_/___/ .__/\_, / +# /_/ /___/ +# +# bh1750.py +# Read data from a BH1750 digital light sensor. +# +# Author : Matt Hawkins +# Date : 26/06/2018 +# +# For more information please visit : +# https://www.raspberrypi-spy.co.uk/?s=bh1750 +# +#--------------------------------------------------------------------- +import smbus +import time + +# Define some constants from the datasheet + +DEVICE = 0x23 # Default device I2C address + +POWER_DOWN = 0x00 # No active state +POWER_ON = 0x01 # Power on +RESET = 0x07 # Reset data register value + +# Start measurement at 4lx resolution. Time typically 16ms. +CONTINUOUS_LOW_RES_MODE = 0x13 +# Start measurement at 1lx resolution. Time typically 120ms +CONTINUOUS_HIGH_RES_MODE_1 = 0x10 +# Start measurement at 0.5lx resolution. Time typically 120ms +CONTINUOUS_HIGH_RES_MODE_2 = 0x11 +# Start measurement at 1lx resolution. Time typically 120ms +# Device is automatically set to Power Down after measurement. +ONE_TIME_HIGH_RES_MODE_1 = 0x20 +# Start measurement at 0.5lx resolution. Time typically 120ms +# Device is automatically set to Power Down after measurement. +ONE_TIME_HIGH_RES_MODE_2 = 0x21 +# Start measurement at 1lx resolution. Time typically 120ms +# Device is automatically set to Power Down after measurement. +ONE_TIME_LOW_RES_MODE = 0x23 + +#bus = smbus.SMBus(0) # Rev 1 Pi uses 0 +bus = smbus.SMBus(1) # Rev 2 Pi uses 1 + +def convertToNumber(data): + # Simple function to convert 2 bytes of data + # into a decimal number. Optional parameter 'decimals' + # will round to specified number of decimal places. + result=(data[1] + (256 * data[0])) / 1.2 + return (result) + +def readLight(addr=DEVICE): + # Read data from I2C interface + data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1) + return convertToNumber(data) + +def main(): + + while True: + lightLevel=readLight() + print("Light Level : " + format(lightLevel,'.2f') + " lx") + time.sleep(0.5) + +if __name__=="__main__": + main() diff --git a/developpment/interfacer/commandManager.cpp b/developpment/interfacer/commandManager.cpp index 4c89a55..1df86cf 100644 --- a/developpment/interfacer/commandManager.cpp +++ b/developpment/interfacer/commandManager.cpp @@ -12,7 +12,7 @@ #include "commandManager.h" -CommandManager:: CommandManager():emptyIndex(0) +CommandManager::CommandManager():emptyIndex(0) { } diff --git a/developpment/interfacer/commandManager.o b/developpment/interfacer/commandManager.o deleted file mode 100644 index c9eefcf..0000000 Binary files a/developpment/interfacer/commandManager.o and /dev/null differ diff --git a/developpment/interfacer/errorHandling.o b/developpment/interfacer/errorHandling.o deleted file mode 100644 index 5826368..0000000 Binary files a/developpment/interfacer/errorHandling.o and /dev/null differ diff --git a/developpment/interfacer/i2c_driver.cpp b/developpment/interfacer/i2c_driver.cpp index e4b0a67..7b81d3d 100644 --- a/developpment/interfacer/i2c_driver.cpp +++ b/developpment/interfacer/i2c_driver.cpp @@ -14,6 +14,8 @@ #define PORT_I2C "/dev/i2c-1" +// some curious things https://www.john.geek.nz/2012/12/update-reading-data-from-a-bosch-bmp085-with-a-raspberry-pi/ + I2C_Driver::I2C_Driver(const uint8_t& channel, const uint8_t& mode) { @@ -25,7 +27,6 @@ I2C_Driver::I2C_Driver(const uint8_t& channel, const uint8_t& mode) std::cout << "unable to open : "<< deviceDescriptor << " ! quiting" << std::endl; exit(1); } - std::cout << "I2C initialized sucsessfully" << std::endl; } uint8_t I2C_Driver::writeByte(const uint8_t& address, const uint8_t& data) // retuns 0 when a sucsessful transation ocures @@ -43,7 +44,7 @@ uint8_t I2C_Driver::writeByte(const uint8_t& address, const uint8_t& data) // std::cout << "Unable to write quiting" << std::endl; exit(0); } - return 1; + return 0; } uint8_t I2C_Driver::readByte(const uint8_t& address, const uint8_t& reg) @@ -54,13 +55,53 @@ uint8_t I2C_Driver::readByte(const uint8_t& address, const uint8_t& reg) exit(1); } - if(writeByte(address,reg)) + writeByte(address,reg); //Initiate a write to indicate the desired register to read + + if (read(deviceDescriptor, recieve_buffer, 1) != 1) // An then initare a read request of 1 byte { - if (read(deviceDescriptor, recieve_buffer, 1) != 1) - { - std::cout << "Unable to read quiting" << std::endl; - exit(1); // ERROR HANDLING: i2c transaction failed - } - } + std::cout << "Unable to read quiting" << std::endl; + exit(1); + } return recieve_buffer[0] ; } + + +uint8_t I2C_Driver::writeWord(const uint8_t& address, const uint8_t& reg, const uint16_t& data) // retuns 0 when a sucsessful transation ocures +{ + + if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) + { + std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl; + exit(1); + } + + send_buffer[0] = reg; + send_buffer[1] = data; + + if ((write(deviceDescriptor, send_buffer, 2)) != 2) + { + std::cout << "Unable to write quiting" << std::endl; + exit(0); + } + return 0; +} + +uint16_t I2C_Driver::readWord(const uint8_t& address, const uint8_t& reg) +{ + uint16_t result = 0 ; + if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) + { + std::cout << "Unable to reach device : "<< address << " ! quiting" << std::endl; + exit(1); + } + + writeByte(address,reg); //Initiate a write to indicate the desired register to read + + if (read(deviceDescriptor, recieve_buffer, 2) != 2) // An then initare a read request of 2 bytes + { + std::cout << "Unable to read quiting" << std::endl; + exit(1); + } + result = (recieve_buffer[0] << 8) + recieve_buffer[1] ; + return result ; +} diff --git a/developpment/interfacer/i2c_driver.o b/developpment/interfacer/i2c_driver.o deleted file mode 100644 index 1bb39e8..0000000 Binary files a/developpment/interfacer/i2c_driver.o and /dev/null differ diff --git a/developpment/interfacer/interfacer.cpp b/developpment/interfacer/interfacer.cpp index 57474da..3807fe6 100644 --- a/developpment/interfacer/interfacer.cpp +++ b/developpment/interfacer/interfacer.cpp @@ -12,30 +12,38 @@ #include "errorHandling.h" #include "commandManager.h" #include "systemCall.h" -#include "i2c_driver.h" +#include "bh1750.h" + +#include + +unsigned int miliSecond = 1000; ErrorHandler errorHandle; CommandManager commander; -I2C_Driver i2cDriver(1,1); - +//I2C_Driver i2cDriver(1,1); +Bh1750 lightSens; int initPlatform() { // Dev. Module initialisation "Ex i2c_init() ...." - return 0; } void dummy() { - std::cout<< unsigned(i2cDriver.readByte(0x40,0x00)) <