test

Dependents:   gather_sensor_data

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BH1750.cpp Source File

BH1750.cpp

00001 /*
00002 
00003 This is a library for the BH1750FVI (GY-30) Digital Light Sensor
00004 breakout board.
00005 
00006 The board uses I2C for communication. 2 pins are required to
00007 interface to the device and in most cases also pull up resistors.
00008 
00009 Datasheet:
00010 http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf
00011 
00012 Inspired by Christopher Laws, March, 2013.
00013 Written by Michal Stehlik, August, 2014.
00014 
00015 */
00016 
00017 #include "BH1750.h"
00018 
00019 BH1750::BH1750 (PinName sda, PinName scl) : i2c(sda,scl)
00020 {
00021     rawDataBuffer[0] = 0;
00022     rawDataBuffer[1] = 0;
00023     status = -1;
00024 }
00025 
00026 void BH1750::init(char mode)
00027 {
00028     configure(mode);
00029 }
00030 
00031 
00032 void BH1750::configure(char mode)
00033 {
00034     char data[1];
00035     switch (mode) {
00036         case BH1750_CONTINUOUS_HIGH_RES_MODE:
00037         case BH1750_CONTINUOUS_HIGH_RES_MODE_2:
00038         case BH1750_CONTINUOUS_LOW_RES_MODE:
00039         case BH1750_ONE_TIME_HIGH_RES_MODE:
00040         case BH1750_ONE_TIME_HIGH_RES_MODE_2:
00041         case BH1750_ONE_TIME_LOW_RES_MODE:
00042             data[0] = mode;
00043             status = i2c.write(BH1750_I2CADDR, data, sizeof(data), false);
00044             wait_ms(10);
00045             break;
00046         default:
00047             break;
00048     }
00049 }
00050 
00051 
00052 float BH1750::readIntensity(void)
00053 {
00054     status = i2c.read(BH1750_I2CADDR, rawDataBuffer, 2, false);
00055     if(status != 0) {
00056         return -1;
00057     }
00058     //success
00059     float result = 0;
00060     result = ((rawDataBuffer[0]<<8)|rawDataBuffer[1])/1.2;
00061     return result;
00062 }
00063 
00064 int BH1750::getStatus (void)
00065 {
00066     return status;
00067 }
00068 
00069 char* BH1750::getRawData(void)
00070 {
00071     return rawDataBuffer;
00072 }