upm  1.0.2
Sensor/Actuator repository for libmraa (v1.1.1)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bh1750.h
Go to the documentation of this file.
1 /*
2  * Authors: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include <stdint.h>
28 
29 #include "mraa/i2c.h"
30 
31 #include "upm.h"
32 #include "upm_types.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif // __cplusplus
37 
46 #define BH1750_DEFAULT_I2C_BUS 0
47 #define BH1750_DEFAULT_I2C_ADDR 0x23
48 
49 // BH1750 commands
50 
51 #define BH1750_CMD_POWER_DOWN 0x00
52 #define BH1750_CMD_POWER_UP 0x01
53 
54 #define BH1750_CMD_RESET 0x07
55 
56 // continuous modes
57 #define BH1750_CMD_CONT_H_RES_MODE1 0x10 // 1 lx resolution
58 #define BH1750_CMD_CONT_H_RES_MODE2 0x11 // .5 lx resolution
59 #define BH1750_CMD_CONT_L_RES_MODE 0x13 // 4 lx resolution
60 
61 // one-time modes
62 #define BH1750_CMD_ONETIME_H_RES_MODE1 0x20
63 #define BH1750_CMD_ONETIME_H_RES_MODE2 0x21
64 #define BH1750_CMD_ONETIME_L_RES_MODE 0x23
65 
66 // max measurement time in ms (for H modes)
67 #define BH1750_MAX_MEAS_TIME_H 180
68 
69 // max measurement time in ms (for L modes)
70 #define BH1750_MAX_MEAS_TIME_L 30
71 
72 // an enum for the operating mode to pass to init
73 typedef enum {
74  BH1750_OPMODE_H1_CONT, // continuous 1 lx high resolution
75  BH1750_OPMODE_H2_CONT, // continuous .5 lx high resolution
76  BH1750_OPMODE_L_CONT, // continuous 4 lx low resolution
77  BH1750_OPMODE_H1_ONCE, // onetime 1 lx high resolution
78  BH1750_OPMODE_H2_ONCE, // onetime .5 lx high resolution
79  BH1750_OPMODE_L_ONCE, // onetime 4 lx low resolution
80 } BH1750_OPMODES_T;
81 
85 typedef struct _bh1750_context
86 {
87  int bus;
88  mraa_i2c_context i2c;
89 
90  // these are set by bh1750_set_opmode()
91  uint8_t opmode;
92  bool is_continuous;
93  int delayms;
95 
104 bh1750_context bh1750_init(int bus, uint8_t addr, BH1750_OPMODES_T mode);
105 
111 void bh1750_close(const bh1750_context dev);
112 
120 upm_result_t bh1750_get_lux(const bh1750_context dev, float* lux);
121 
128 bool bh1750_power_up(const bh1750_context dev);
129 
136 bool bh1750_power_down(const bh1750_context dev);
137 
146 bool bh1750_reset(const bh1750_context dev);
147 
155 upm_result_t bh1750_send_command(const bh1750_context dev, uint8_t cmd);
156 
164 upm_result_t bh1750_read_data(const bh1750_context dev, uint16_t* data);
165 
174 upm_result_t bh1750_set_opmode(const bh1750_context dev,
175  BH1750_OPMODES_T mode);
176 
177 #ifdef __cplusplus
178 }
179 #endif // __cplusplus
upm_result_t bh1750_set_opmode(const bh1750_context dev, BH1750_OPMODES_T mode)
Definition: bh1750.c:222
bh1750_context bh1750_init(int bus, uint8_t addr, BH1750_OPMODES_T mode)
Definition: bh1750.c:30
upm_result_t bh1750_get_lux(const bh1750_context dev, float *lux)
Definition: bh1750.c:95
bool bh1750_power_down(const bh1750_context dev)
Definition: bh1750.c:132
upm_result_t bh1750_read_data(const bh1750_context dev, uint16_t *data)
Definition: bh1750.c:174
upm_result_t bh1750_send_command(const bh1750_context dev, uint8_t cmd)
Definition: bh1750.c:160
bool bh1750_power_up(const bh1750_context dev)
Definition: bh1750.c:118
struct _bh1750_context * bh1750_context
bool bh1750_reset(const bh1750_context dev)
Definition: bh1750.c:146
void bh1750_close(const bh1750_context dev)
Definition: bh1750.c:85
Definition: bh1750.h:85